Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD5270/ad527x.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ad527x.c
3 * @brief Implementation of ad527x Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2013(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
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "ad527x.h"
47 #include "Console.h"
48 #include "Time.h"
49 /******************************************************************************/
50 /************************ Variables Definitions *******************************/
51 /******************************************************************************/
52 typedef enum
53 {
54 com_spi,
55 com_i2c
56 } comm_type_t;
57
58 struct ad527x_chip_info {
59 unsigned int bitsNumber;
60 comm_type_t communication;
61 };
62
63 static const struct ad527x_chip_info ad527x_chip_info[] = {
64 [ID_AD5270] = {
65 .bitsNumber = 10,
66 .communication = com_spi,
67 },
68 [ID_AD5271] = {
69 .bitsNumber = 8,
70 .communication = com_spi,
71 },
72 [ID_AD5272] = {
73 .bitsNumber = 10,
74 .communication = com_i2c,
75 },
76 [ID_AD5274] = {
77 .bitsNumber = 8,
78 .communication = com_i2c,
79 }
80 };
81
82 AD527x_type act_device;
83 unsigned char ad527xDevAddr = 0;
84
85 /******************************************************************************/
86 /************************** Functions Definitions *****************************/
87 /******************************************************************************/
88
89 /***************************************************************************//**
90 * @brief Writes data to AD527x.
91 *
92 * @param command - Desired command.
93 * @param data - Data to be loaded.
94 *
95 * @return None.
96 *******************************************************************************/
97 void AD527x_SetRegister(unsigned char command, unsigned short data)
98 {
99 unsigned char dataBuf[2] = {0, 0};
100
101
102 data <<= (MAX_BITS_NUMBER - ad527x_chip_info[act_device].bitsNumber);
103 data &= 0x3FF;
104 command &= 0x0F;
105 dataBuf[0] = (command << 2) + ((data & 0x0300) >> 8);
106 dataBuf[1] = (data & 0x00FF);
107
108 if (ad527x_chip_info[act_device].communication == com_i2c)
109 {
110 I2C_Write(AD527x_I2C_ADDR_0, dataBuf, 2, 1);
111 }
112 else
113 {
114 if (ad527x_chip_info[act_device].communication == com_spi)
115 {
116 SPI_Write(AD527x_SLAVE_ID, dataBuf, 2);
117 }
118 }
119 }
120
121 /***************************************************************************//**
122 * @brief Reads data from AD527x.
123 *
124 * @param command - Desired command.
125 * @param memReg50TP - one of the 50-TP memory registers (param needed only for
126 * command 5).
127 *
128 * @return None.
129 *******************************************************************************/
130 unsigned short AD527x_GetRegister(unsigned char command, unsigned char memReg50TP)
131 {
132 long returnVal = 0;
133 unsigned char rxBuf[2] = {0, 0};
134 unsigned char txBuf[2] = {0, 0};
135 char status = 0;
136
137 command &= 0x0F;
138 txBuf[0] = command << 2;
139 memReg50TP &= 0x3F;
140 txBuf[1] = memReg50TP;
141
142 if (ad527x_chip_info[act_device].communication == com_spi)
143 {
144 status = SPI_Write(AD527x_SLAVE_ID, txBuf, 2);
145 if(status != 2)
146 {
147 return -1;
148 }
149 status = SPI_Read(AD527x_SLAVE_ID, rxBuf, 2);
150 if (status != 2)
151 {
152 return -1;
153 }
154 }
155 else
156 {
157 if (ad527x_chip_info[act_device].communication == com_i2c)
158 {
159 status = I2C_Write(AD527x_I2C_ADDR_0, txBuf, 2, 1);
160 if(status != 2)
161 {
162 return -1;
163 }
164 status = I2C_Read(AD527x_I2C_ADDR_0, rxBuf, 2, 1);
165 if(status != 2)
166 {
167 return -1;
168 }
169 }
170 }
171
172 returnVal |= (short) ((rxBuf[0] & 0x03) << 8);
173 returnVal |= (short) (rxBuf[1] << 0);
174
175 return(returnVal);
176 }
177
178 /***************************************************************************//**
179 * @brief Initializes the communication with the device.
180 *
181 * @param devAddr - AD5272/AD5274 device address.
182 * Example: AD5272_I2C_ADDR_0 - ADDR_Pin=GND;
183 * AD5272_I2C_ADDR_1 - ADDR_Pin=VDD;
184 * AD5272_I2C_ADDR_2 - ADDR_Pin=NC.
185 * device - supported devices:
186 * Example: AD5270, AD5271, AD5272, AD5274.
187 * @return status - Result of the initialization procedure.
188 * Example: 0 - if initialization was successful;
189 * -1 - if initialization was unsuccessful.
190 *******************************************************************************/
191 char AD527x_Init(unsigned char devAddr, AD527x_type device)
192 {
193 char status = -1;
194
195 ad527xDevAddr = devAddr;
196 act_device = device;
197
198 if (ad527x_chip_info[act_device].communication == com_spi)
199 {
200 status = SPI_Init(0, 1000000, 0, 0);
201 }
202 else
203 {
204 if (ad527x_chip_info[act_device].communication == com_i2c)
205 {
206 status = I2C_Init(100000);
207 }
208 }
209
210 AD527x_RESET_OUT;
211 AD527x_RESET_HIGH;
212
213 /* setup AD527x */
214 /* enable 50TP, allow update of RDAC register */
215 AD527x_SetRegister(AD527x_WRITE_CONTROL,(AD527x_C1 | AD527x_C0));
216
217 return status;
218 }