Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Pmods/PmodAD2/AD7991.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD7991.c
3 * @brief AD7991 Driver for Microblaze processor.
4 * @author ATofan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 ********************************************************************************
40 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /*****************************************************************************/
44 /***************************** Include Files *********************************/
45 /*****************************************************************************/
46 #include "i2c_axi.h"
47 #include "AD7991.h"
48 #include "xil_io.h"
49
50 /*****************************************************************************/
51 /************************** Macros Definitions *******************************/
52 /*****************************************************************************/
53
54 /******************************************************************************
55 * @brief Initialize the AD7911 device.
56 *
57 * @return None
58 ******************************************************************************/
59 void AD7991_Init(void)
60 {
61 int cfgValue = 0;
62 unsigned char txBuffer[1] = {0x00};
63
64 // Set default Configuration Register Values
65 /*
66 Channel 3 - Selected for conversion
67 Channel 2 - Selected for conversion
68 Channel 1 - Selected for conversion
69 Channel 0 - Selected for conversion
70 reference Voltage - Supply Voltage
71 filtering on SDA and SCL - Enabled
72 bit Trial Delay - Enabled
73 sample Interval Delay - Enabled
74 */
75 cfgValue = (1 << CH3) |
76 (1 << CH2) |
77 (1 << CH1) |
78 (1 << CH0) |
79 (0 << REF_SEL) |
80 (0 << FLTR) |
81 (0 << bitTrialDelay) |
82 (0 << sampleDelay);
83
84 txBuffer[0] = cfgValue;
85 // Write to the Configuration Register
86 I2C_Write_axi(IIC_BASEADDR, AD7991IICAddr, -1, 1, txBuffer);
87 }
88
89 /**************************************************************************//**
90 * @brief Configure the AD7911 device.
91 *
92 * @param chan3 - CHAN3 bit in control register.
93 *
94 * @param chan2 - CHAN2 bit in control register.
95 *
96 * @param chan1 - CHAN1 bit in control register.
97 *
98 * @param chan0 - CHAN0 bit in control register.
99 *
100 * @param ref - REF bit in control register.
101 *
102 * @param filter - FILTER bit in control register.
103 *
104 * @param bit - BIT bit in control register.
105 *
106 * @param sample - SAMPLE bit in control register.
107 *
108 * @return None
109 ******************************************************************************/
110 void AD7991_Config(char chan3, char chan2, char chan1, char chan0, char ref, char filter, char bit, char sample)
111 {
112 int cfgValue = 0;
113 unsigned char txBuffer[1] = {0x00};
114
115 // Set Configuration Register Values
116 cfgValue = (chan3 << CH3) | // Read Channel 3
117 (chan2 << CH2) | // Read Channel 2
118 (chan1 << CH1) | // Read Channel 1
119 (chan0 << CH0) | // Read Channel 0
120 (ref << REF_SEL) | // Select External reference / Vcc as reference
121 (filter << FLTR) | // filter IIC Bus
122 (bit << bitTrialDelay) | // Delay IIC Commands
123 (sample << sampleDelay); // Delay IIC Messages
124
125 txBuffer[0] = cfgValue;
126
127 // Write to the Configuration Register
128 I2C_Write_axi(IIC_BASEADDR, AD7991IICAddr, -1, 1, txBuffer);
129 }
130
131 /**************************************************************************//**
132 * @brief Display read data from AD7911 device to the UART.
133 *
134 * @param nr_cnv - Number of conversions to be made.
135 *
136 * @param vref - Voltage Reference value in mV.
137 *
138 * @return None
139 ******************************************************************************/
140 void AD7991_Print(u32 nr_cnv, u32 vref)
141 {
142 int rxData;
143 unsigned char rxBuffer[2] = {0x00, 0x00};
144 char c[7] = {'0','.','0','0','0',0};
145 u32 nr;
146 int i;
147 int j;
148
149 for(j=0;j<nr_cnv;j++)
150 {
151 i = 5;
152 c[7]= 0;
153 c[6] = '0';
154 c[5] = '0';
155 c[4] = '0';
156 c[3] = '0';
157 c[2] = '0';
158 c[1] = '.';
159 c[0] = '0';
160
161 // Read data from AD7991
162 I2C_Read_axi(IIC_BASEADDR, AD7991IICAddr, -1, 2, rxBuffer);
163 rxData = (rxBuffer[0] << 8) | rxBuffer[1];
164
165 // Process read voltage value
166 nr = (rxData & ADCValue) * vref / 4096;
167 while(nr>0)
168 {
169 // Transform hex value into char
170 // for display purposes
171 c[i] = (nr % 10) + 48;
172 nr = nr / 10;
173 i = i - 1;
174
175 // Skip the 2nd position (it is pre-loaded with '.')
176 if(i == 1)
177 {
178 i = i-1;
179 }
180 }
181
182 i = 5;
183
184 // Determine if received data from AD7991 is correct
185 // by checking the first 2 Leading zeros
186 if((rxData & LeadingZeros) == 0)
187 {
188 xil_printf("\n\r");
189 xil_printf("Channel %x = ",((rxData & CHID) >> 12));
190 xil_printf("%s V\n\r",c);
191 }
192 else
193 {
194 xil_printf("\n\rRead Error occurred!\n\r");
195 }
196 }
197 }