| 1 | /**************************************************************************//** |
| 2 | * @file main.c |
| 3 | * @brief main file |
| 4 | * @author ATofan ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2011(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * - Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * - Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * - The use of this software may or may not infringe the patent rights |
| 23 | * of one or more patent holders. This license does not release you |
| 24 | * from the requirement that you obtain separate licenses from these |
| 25 | * patent holders to use this software. |
| 26 | * - Use of the software either in source or binary form, must be run |
| 27 | * on or directly connected to an Analog Devices Inc. component. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY |
| 31 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 34 | * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | ******************************************************************************* |
| 41 | * SVN Revision: $WCREV$ |
| 42 | ******************************************************************************/ |
| 43 | |
| 44 | #include <stdio.h> |
| 45 | #include "xparameters.h" |
| 46 | #include "xil_io.h" |
| 47 | #include "admp441.h" |
| 48 | |
| 49 | #define DMA_BASEADDR XPAR_AXIDMA_0_BASEADDR |
| 50 | #define CF_BASEADDR XPAR_AXI_ADMP441_0_BASEADDR |
| 51 | |
| 52 | unsigned int nrSamples = 488000; |
| 53 | unsigned int nrSeconds = 10; |
| 54 | volatile int admp441Data[1000044] = {0,}; |
| 55 | volatile int* admp441DataPointer = &admp441Data[0]; |
| 56 | volatile int admp441DataLitte; |
| 57 | volatile int ampFactor = 1; |
| 58 | volatile unsigned int i = 0; |
| 59 | |
| 60 | volatile u32* point = XPAR_PS7_DDR_0_S_AXI_BASEADDR + 1024*1024 ; // pointer to data memory location |
| 61 | |
| 62 | void Xil_DCacheFlush(void); |
| 63 | |
| 64 | void delay_ms(u32 ms_count) { |
| 65 | u32 count; |
| 66 | for (count = 0; count < ((ms_count * 10000000) + 1); count++) { |
| 67 | asm("nop"); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /****************************************************************************** |
| 72 | * @brief Reads data from an ADC usign AXI_DMA core |
| 73 | * |
| 74 | * @param buf - Start address of the buffer where to store the read data |
| 75 | * @param size - Size in bytes of the data to read |
| 76 | * @return None |
| 77 | ******************************************************************************/ |
| 78 | void ReadAdcData(u32 buf, u32 size) |
| 79 | { |
| 80 | // program DMA engine |
| 81 | Xil_Out32((DMA_BASEADDR + 0x030), 0); // Clear dma operations |
| 82 | Xil_Out32((DMA_BASEADDR + 0x030), 1); // Enable Run bit |
| 83 | Xil_Out32((DMA_BASEADDR + 0x048), buf); // Configure DMA with the destination address |
| 84 | Xil_Out32((DMA_BASEADDR + 0x058), (size * 4 )); // Number of bites to be written |
| 85 | |
| 86 | // program the DMA module in the ADC core |
| 87 | Xil_Out32((CF_BASEADDR + 0x040), 0x1); // Power up the core |
| 88 | Xil_Out32((CF_BASEADDR + 0x088), 0x7); // Reset overflows |
| 89 | Xil_Out32((CF_BASEADDR + 0x080), 0x0); // DMA stop |
| 90 | |
| 91 | Xil_Out32((CF_BASEADDR + 0x084), size * 4 ); // Program the number of bytes to be captured |
| 92 | Xil_Out32((CF_BASEADDR + 0x080), 0x1); // Start capturing data |
| 93 | |
| 94 | // Wait for data transfer to finish |
| 95 | do { |
| 96 | delay_ms(1); |
| 97 | } |
| 98 | while ((Xil_In32(CF_BASEADDR + 0x088) & 0x1) == 1); |
| 99 | if (Xil_In32(CF_BASEADDR + 0x088) != 0x00) { |
| 100 | xil_printf("overflow occured, capture may be corrupted\n\r"); |
| 101 | xil_printf ("%x\n", Xil_In32(CF_BASEADDR + 0x088) ); |
| 102 | } |
| 103 | Xil_DCacheFlush(); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /****************************************************************************** |
| 108 | * @brief Main function. |
| 109 | * |
| 110 | * @return Always returns 0. |
| 111 | ******************************************************************************/ |
| 112 | int main() |
| 113 | { |
| 114 | unsigned long dataMask = 0x7FFFFF80; |
| 115 | int dmaRepeat = 0; |
| 116 | unsigned long rxData = 0; |
| 117 | |
| 118 | Xil_ICacheEnable(); |
| 119 | Xil_DCacheEnable(); |
| 120 | |
| 121 | xil_printf("PmodMIC2 Demo Program\n\r"); |
| 122 | |
| 123 | // Prepare WAV Header |
| 124 | xil_printf("Preparing WAV File Header..."); |
| 125 | admp441Data[0] = CHUNK_RIFF_ID; |
| 126 | admp441Data[1] = CHUNK_SIZE; |
| 127 | admp441Data[2] = WAVE_FORMAT; |
| 128 | admp441Data[3] = SUBCHUNK1_ID; |
| 129 | admp441Data[4] = SUBCHUNK1_SIZE; |
| 130 | admp441Data[5] = (AUDIO_FORMAT << 16) | (NR_CHANNELS); |
| 131 | admp441Data[6] = SAMPLE_RATE; |
| 132 | admp441Data[7] = BYTE_RATE; |
| 133 | admp441Data[8] = (BLOCK_ALIGN << 16) | (NR_BITS); |
| 134 | admp441Data[9] = SUBCHUNK2_ID; |
| 135 | admp441Data[10] = SUBCHUNK2_SIZE; |
| 136 | xil_printf(" Done!\n\r"); |
| 137 | |
| 138 | // Set number of samples |
| 139 | nrSamples = nrSeconds * 48800; |
| 140 | |
| 141 | // Start from admp441Data[11] |
| 142 | xil_printf("Receiving Data... "); |
| 143 | |
| 144 | dmaRepeat = nrSamples / 8096; |
| 145 | i = 0; |
| 146 | |
| 147 | while(i < dmaRepeat) |
| 148 | { |
| 149 | ReadAdcData( point + i*8096 , (8096) ); |
| 150 | i += 1; |
| 151 | } |
| 152 | |
| 153 | xil_printf("Done!\n\r"); |
| 154 | xil_printf("Samples Acquired: %d\n\r", nrSamples); |
| 155 | |
| 156 | // Convert data and save in RAM |
| 157 | for (i = 11; i < 8096 * dmaRepeat + 11; i++) |
| 158 | { |
| 159 | rxData = (point[i - 11] & dataMask) << 1; |
| 160 | admp441DataLitte = bigToLittle(rxData * ampFactor); |
| 161 | admp441Data[i] = admp441DataLitte; |
| 162 | } |
| 163 | |
| 164 | while(1) |
| 165 | { |
| 166 | |
| 167 | } |
| 168 | |
| 169 | Xil_DCacheDisable(); |
| 170 | Xil_ICacheDisable(); |
| 171 | |
| 172 | return 0; |
| 173 | } |