| 1 | /**************************************************************************//** |
| 2 | * @file main.c |
| 3 | * @brief Implementation of main program for AD7780. |
| 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: $WCWREV$ |
| 42 | ******************************************************************************/ |
| 43 | |
| 44 | /*****************************************************************************/ |
| 45 | /***************************** Include Files *********************************/ |
| 46 | /*****************************************************************************/ |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include "xil_io.h" |
| 50 | #include "xparameters.h" |
| 51 | |
| 52 | /*****************************************************************************/ |
| 53 | /************************ Constants Definitions ******************************/ |
| 54 | /*****************************************************************************/ |
| 55 | #define SAMPLE_NO 8*1024 |
| 56 | |
| 57 | #define DMA_BASEADDR XPAR_AXIDMA_0_BASEADDR |
| 58 | #define CF_BASEADDR XPAR_AXI_AD7780_0_BASEADDR |
| 59 | |
| 60 | /*****************************************************************************/ |
| 61 | /************************ Variables Definitions ******************************/ |
| 62 | /*****************************************************************************/ |
| 63 | volatile u32 startAcquisition; //starts one acquisition cycle |
| 64 | volatile u32* pStartAcquisition = &startAcquisition; |
| 65 | volatile u32* point = XPAR_PS7_DDR_0_S_AXI_BASEADDR + 1024*1024 ; // pointer to data memory location |
| 66 | |
| 67 | void Xil_DCacheFlush(void); |
| 68 | |
| 69 | void delay_ms(u32 ms_count) { |
| 70 | u32 count; |
| 71 | for (count = 0; count < ((ms_count * 10000000) + 1); count++) { |
| 72 | asm("nop"); |
| 73 | } |
| 74 | } |
| 75 | /****************************************************************************** |
| 76 | * @brief Reads data from an ADC usign AXI_DMA core |
| 77 | * |
| 78 | * @param buf - Start address of the buffer where to store the read data |
| 79 | * @param size - Size in bytes of the data to read |
| 80 | * @return None |
| 81 | ******************************************************************************/ |
| 82 | void ReadAdcData(u32 buf, u32 size) |
| 83 | { |
| 84 | // program DMA engine |
| 85 | Xil_Out32((DMA_BASEADDR + 0x030), 0); // Clear dma operations |
| 86 | Xil_Out32((DMA_BASEADDR + 0x030), 1); // Enable Run bit |
| 87 | Xil_Out32((DMA_BASEADDR + 0x048), buf); // Configure DMA with the destination address |
| 88 | Xil_Out32((DMA_BASEADDR + 0x058), (size * 4 )); // Number of bites to be written |
| 89 | |
| 90 | // program the DMA module in the ADC core |
| 91 | Xil_Out32((CF_BASEADDR + 0x040), 0x1); // Power up the core |
| 92 | Xil_Out32((CF_BASEADDR + 0x088), 0x7); // Reset overflows |
| 93 | Xil_Out32((CF_BASEADDR + 0x080), 0x0); // DMA stop |
| 94 | |
| 95 | Xil_Out32((CF_BASEADDR + 0x084), size * 4 ); // Program the number of bytes to be captured |
| 96 | Xil_Out32((CF_BASEADDR + 0x080), 0x1); // Start capturing data |
| 97 | |
| 98 | // Wait for data transfer to finish |
| 99 | do { |
| 100 | delay_ms(1); |
| 101 | } |
| 102 | while ((Xil_In32(CF_BASEADDR + 0x088) & 0x1) == 1); |
| 103 | if (Xil_In32(CF_BASEADDR + 0x088) != 0x00) { |
| 104 | xil_printf("overflow occured, capture may be corrupted\n\r"); |
| 105 | xil_printf ("%x\n", Xil_In32(CF_BASEADDR + 0x088) ); |
| 106 | } |
| 107 | Xil_DCacheFlush(); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | void AD7780_PrintVin(u32 value); |
| 112 | |
| 113 | /**************************************************************************//** |
| 114 | * @brief Main function implementation. |
| 115 | * |
| 116 | * @return None |
| 117 | ******************************************************************************/ |
| 118 | int main() |
| 119 | { |
| 120 | int i; |
| 121 | |
| 122 | xil_printf("\n-------------------------------------\n\r"); |
| 123 | xil_printf("Waiting for command from the PC...\n\r"); |
| 124 | |
| 125 | xil_printf("Starting a new data acquisition...\n\r"); |
| 126 | |
| 127 | xil_printf("Reading data from the ADC...\n\r"); |
| 128 | ReadAdcData( point , (256) ); |
| 129 | |
| 130 | for (i = 0 ; i < 256; i++) |
| 131 | { |
| 132 | AD7780_PrintVin(point[i]); |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /****************************************************************************** |
| 139 | * @brief Display Vin. |
| 140 | * |
| 141 | * @param None. |
| 142 | * |
| 143 | * @return None. |
| 144 | ******************************************************************************/ |
| 145 | void AD7780_PrintVin(u32 data) |
| 146 | { |
| 147 | unsigned char calcGain = 0x00; |
| 148 | unsigned int inputV = 0x00; |
| 149 | float value = 0x00; |
| 150 | float vref = 3300; |
| 151 | int whole = 0x00; |
| 152 | int thousands = 0x00; |
| 153 | int fullScale = 8388608; |
| 154 | |
| 155 | calcGain = 1; |
| 156 | |
| 157 | // Process Voltage data from 32 bit stream |
| 158 | inputV = (data & 0xFFFFFF00) >> 8; |
| 159 | |
| 160 | // Calculate float value of Vin based on datasheet formula |
| 161 | value = (((float)(vref/1000) * ((float)inputV - fullScale)) / (calcGain * fullScale)); |
| 162 | |
| 163 | // If voltage is positive |
| 164 | if(value >= 0) |
| 165 | { |
| 166 | xil_printf("Vin = +"); |
| 167 | } |
| 168 | // If voltage is negative |
| 169 | else |
| 170 | { |
| 171 | xil_printf("Vin = -"); |
| 172 | value = value * (-1); |
| 173 | } |
| 174 | // Process whole and thousands |
| 175 | whole = (int) value; |
| 176 | thousands = (value - whole) * 1000; |
| 177 | // Properly display whole and thousands |
| 178 | if(thousands > 99) |
| 179 | { |
| 180 | xil_printf("%d.%d V\n\r", whole, thousands); |
| 181 | } |
| 182 | else if(thousands > 9) |
| 183 | { |
| 184 | xil_printf("%d.0%d V\n\r", whole, thousands); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | xil_printf("%d.00%d V\n\r", whole, thousands); |
| 189 | } |
| 190 | } |