Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD525x/ad525x.c

raw symbols SCIP
1 /******************************************************************************
2 * @file AD525x.c
3 * @brief Implementation of AD525X Driver for Microblaze processor.
4 * @author Istvan Csomortani ([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 /****************************** Include Files ********************************/
44 /*****************************************************************************/
45 #include "ad525x.h"
46 #include "Communication.h"
47 #include "TIME.h"
48
49 #define MSB_BYTE_MASK 0xFF00
50 #define LSB_BYTE_MASK 0x00FF
51 #define ONEBYTE_OFFSET 0x8
52 /*****************************************************************************/
53 /**************************** Private variables ******************************/
54 /*****************************************************************************/
55 struct ad525x_chip_info {
56 unsigned char num_channels;
57 CommType_t comm_type;
58 unsigned short num_position;
59 };
60
61 static const struct ad525x_chip_info AD525X_CHIP_INFO[] = {
62 [ID_AD5232] = {
63 .num_channels = 2,
64 .comm_type = SPI,
65 .num_position = 256,
66 },
67 [ID_AD5235] = {
68 .num_channels = 2,
69 .comm_type = SPI,
70 .num_position = 1024,
71 },
72 [ID_AD5252] = {
73 .num_channels = 2,
74 .comm_type = I2C,
75 .num_position = 256,
76 },
77 [ID_AD5251] = {
78 .num_channels = 2,
79 .comm_type = I2C,
80 .num_position = 64,
81 },
82 [ID_AD5254] = {
83 .num_channels = 4,
84 .comm_type = I2C,
85 .num_position = 256,
86 },
87 [ID_AD5253] = {
88 .num_channels = 4,
89 .comm_type = I2C,
90 .num_position = 64,
91 },
92 [ID_ADN2850] = {
93 .num_channels = 2,
94 .comm_type = SPI,
95 .num_position = 1024,
96 }
97 };
98
99 /* Actual device type */
100 AD525X_type_t this_device;
101
102 /*****************************************************************************/
103 /*************************** Functions definitions ***************************/
104 /*****************************************************************************/
105
106 /**************************************************************************//**
107 * @brief Initialize SPI and Initial Values for AD5172 Board.
108 *
109 * @param The current device type.
110 *
111 * @return success
112 ******************************************************************************/
113 char AD525X_Init(AD525X_type_t device)
114 {
115 char status = -1;
116
117 this_device = device;
118
119 if(AD525X_CHIP_INFO[this_device].comm_type == SPI)
120 {
121 /* CPHA = 0; CPOL = 0; */
122 status = SPI_Init(0, 100000, 0, 0);
123 }
124 else
125 {
126 status = I2C_Init(100000);
127 }
128
129 /* Deactivate Hardware Reset */
130 AD525X_RESET_OUT;
131 AD525X_RESET_HIGH;
132 /* Deactivate Shutdown Pin */
133 AD525X_SHUTDOWN_OUT;
134 AD525X_SHUTDOWN_HIGH;
135 /* Deactivate Write Protect Pin */
136 AD525X_WP_BF_OUT;
137 AD525X_WP_BF_HIGH;
138 /* Setup the RDY input */
139 AD525X_READY_IN;
140
141 return status;
142 }
143
144 /**************************************************************************//**
145 * @brief Read data from the EEMEM
146 *
147 * @param + slaveId - slave device ID in case of SPI interface, and
148 * programmable device address in case of the I2C
149 * + address - desired address of the EEMEM memory
150 *
151 *
152 * @return success
153 ******************************************************************************/
154 unsigned short AD525X_ReadMem(unsigned char slaveId,
155 unsigned char address)
156 {
157 unsigned char dataBuffer[3] = {0,};
158 unsigned char i2cSlaveAddr = 0;
159 unsigned short data = 0;
160
161 if(AD525X_CHIP_INFO[this_device].comm_type == SPI)
162 {
163 /* Sending the command, reading the result on the next frame */
164 dataBuffer[0] |= AD525X_CMD_SPI_MEM2SREG << AD525X_CMD_SPI_OFFSET;
165 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK;
166 /* 3 byte data word */
167 if((this_device == ID_AD5235) || (this_device == ID_ADN2850))
168 {
169 SPI_Write(slaveId,
170 dataBuffer,
171 3);
172 dataBuffer[0] &= AD525X_CMD_NOP << AD525X_CMD_SPI_OFFSET;
173 SPI_Read(slaveId,
174 dataBuffer,
175 3);
176 data = (dataBuffer[1] << ONEBYTE_OFFSET) | dataBuffer[2];
177 return data;
178 }
179 else /* 2 byte data word */
180 {
181 SPI_Read(slaveId,
182 dataBuffer,
183 2);
184 return (unsigned short)dataBuffer[1];
185 }
186 }
187 else /* Communication interface is I2C */
188 {
189 /* Setup I2C address */
190 i2cSlaveAddr |= (AD525X_I2C_HARD_ADDR | \
191 (slaveId & AD525X_I2C_PIN_ADDR_MASK));
192 dataBuffer[0] &= ~AD525X_I2C_CMD_OR_REG; // reset CMD/REG_n
193 dataBuffer[0] |= AD525X_I2C_EE_OR_RDAC; // set EE/RDAC_n
194 dataBuffer[0] |= address & AD525X_I2C_MEM_ADDR_MASK; // set address
195 /* Dummy write to select the desired register */
196 I2C_Write(i2cSlaveAddr,
197 dataBuffer,
198 1,
199 1);
200 dataBuffer[0] &= AD525X_CMD_NOP;
201 I2C_Read(i2cSlaveAddr,
202 dataBuffer,
203 1,
204 1);
205 data = (unsigned short)dataBuffer[0];
206 return data;
207 }
208 }
209
210 /*****SPI_*********************************************************************//**
211 * @brief Write data to EEMEM.
212 *
213 * @param + slaveId - slave device ID in case of SPI interface, and
214 * programmable device address in case of the I2C
215 * + address - desired address of the EEMEM memory
216 * + data - the data which will be written to the memory
217 *
218 * @return success
219 ******************************************************************************/
220 void AD525X_WriteMem(unsigned char slaveId,
221 unsigned char address,
222 unsigned short data)
223 {
224 unsigned char dataBuffer[3] = {0,};
225 unsigned char i2cSlaveAddr = 0;
226
227 if(AD525X_CHIP_INFO[this_device].comm_type == SPI)
228 {
229 /* Sending the command, reading the result on the next frame */
230 dataBuffer[0] |= AD525X_CMD_SPI_SREG2MEM << AD525X_CMD_SPI_OFFSET;
231 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK;
232 if((this_device == ID_AD5235) || (this_device == ID_ADN2850)) /* 3 byte data word */
233 {
234 dataBuffer[1] = (data & MSB_BYTE_MASK) >> ONEBYTE_OFFSET;
235 dataBuffer[2] = data & LSB_BYTE_MASK;
236 SPI_Write(slaveId,
237 dataBuffer,
238 3);
239 }
240 else /* 2 byte data word */
241 {
242 dataBuffer[1] = data & LSB_BYTE_MASK;
243 SPI_Write(slaveId,
244 dataBuffer,
245 2);
246 }
247 }
248 else /* Communication interface is I2C */
249 {
250 /* Setup I2C address */
251 i2cSlaveAddr |= (AD525X_I2C_HARD_ADDR | \
252 (slaveId & AD525X_I2C_PIN_ADDR_MASK));
253 dataBuffer[0] &= ~AD525X_I2C_CMD_OR_REG; // reset CMD/REG_n
254 dataBuffer[0] |= AD525X_I2C_EE_OR_RDAC; // set EE/RDAC_n
255 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK; // set address
256
257 dataBuffer[1] = (data & LSB_BYTE_MASK);
258 I2C_Write(i2cSlaveAddr,
259 dataBuffer,
260 2,
261 1);
262 }
263 }
264
265 /**************************************************************************//**
266 * @brief Read data from the RDAC register
267 *
268 * @param + slaveId - slave device ID in case of SPI interface, and
269 * programmable device address in case of the I2C
270 * + address - desired address of the RDAC registers
271 *
272 *
273 * @return success
274 ******************************************************************************/
275 unsigned short AD525X_ReadRdac(unsigned char slaveId,
276 unsigned char address)
277 {
278 unsigned char dataBuffer[3] = {0,};
279 unsigned char i2cSlaveAddr = 0;
280 unsigned short data = 0;
281
282 if(AD525X_CHIP_INFO[this_device].comm_type == SPI)
283 {
284 /* Sending the command, reading the result on the next frame */
285 dataBuffer[0] |= AD525X_CMD_SPI_RDAC2SREG << AD525X_CMD_SPI_OFFSET;
286 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK;
287 /* 3 byte data word */
288 if((this_device == ID_AD5235) || (this_device == ID_ADN2850))
289 {
290 SPI_Write(slaveId,
291 dataBuffer,
292 3);
293 TIME_DelayUs(50);
294 dataBuffer[0] &= AD525X_CMD_NOP << AD525X_CMD_SPI_OFFSET;
295 SPI_Read(slaveId,
296 dataBuffer,
297 3);
298 data = (dataBuffer[1] << ONEBYTE_OFFSET) | dataBuffer[2];
299 return (data & AD525X_DATA10_MASK);
300 }
301 else /* 2 byte data word */
302 {
303 SPI_Read(slaveId,
304 dataBuffer,
305 2);
306 TIME_DelayUs(50);
307 /* Sending a dummy frame to read the result */
308 dataBuffer[0] &= AD525X_CMD_NOP << AD525X_CMD_SPI_OFFSET;
309 SPI_Read(slaveId,
310 dataBuffer,
311 2);
312 data = (unsigned short)dataBuffer[1];
313 return data;
314 }
315 }
316 else /* Communication interface is I2C */
317 {
318 /* Setup I2C address */
319 i2cSlaveAddr |= (AD525X_I2C_HARD_ADDR | \
320 (slaveId & AD525X_I2C_PIN_ADDR_MASK));
321 dataBuffer[0] &= ~AD525X_I2C_CMD_OR_REG; // reset CMD/REG_n
322 dataBuffer[0] &= ~AD525X_I2C_EE_OR_RDAC; // reset EE/RDAC_n
323 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK; // set address
324 /* Dummy write to select the desired register */
325 I2C_Write(i2cSlaveAddr,
326 dataBuffer,
327 1,
328 1);
329 dataBuffer[0] &= AD525X_CMD_NOP;
330 I2C_Read(i2cSlaveAddr,
331 dataBuffer,
332 1,
333 1);
334 data = (unsigned short)dataBuffer[0];
335 return data;
336 }
337 }
338
339 /**************************************************************************//**
340 * @brief Write data to RDAC register.
341 *
342 * @param + slaveId - slave device ID in case of SPI interface, and
343 * programmable device address in case of the I2C
344 * + address - desired address of the RDAC register
345 * + data - the data which will be written to the RDAC register
346 *
347 * @return success
348 ******************************************************************************/
349 void AD525X_WriteRdac(unsigned char slaveId,
350 unsigned char address,
351 unsigned short data)
352 {
353 unsigned char dataBuffer[3] = {0,};
354 unsigned char i2cSlaveAddr = 0;
355
356 if(AD525X_CHIP_INFO[this_device].comm_type == SPI)
357 {
358 /* Sending the command, reading the result on the next frame */
359 dataBuffer[0] |= AD525X_CMD_SPI_SREG2RDAC << AD525X_CMD_SPI_OFFSET;
360 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK;
361 if((this_device == ID_AD5235) || (this_device == ID_ADN2850)) /* 3 byte data word */
362 {
363 dataBuffer[1] = (data & MSB_BYTE_MASK) >> ONEBYTE_OFFSET;
364 dataBuffer[2] = data & LSB_BYTE_MASK;
365 SPI_Write(slaveId,
366 dataBuffer,
367 3);
368 }
369 else /* 2 byte data word */
370 {
371 dataBuffer[1] = data & LSB_BYTE_MASK;
372 SPI_Write(slaveId,
373 dataBuffer,
374 2);
375 }
376 }
377 else /* Communication interface is I2C */
378 {
379 /* Setup I2C address */
380 i2cSlaveAddr |= (AD525X_I2C_HARD_ADDR | \
381 (slaveId & AD525X_I2C_PIN_ADDR_MASK));
382 dataBuffer[0] &= ~AD525X_I2C_CMD_OR_REG; // reset CMD/REG_n
383 dataBuffer[0] &= ~AD525X_I2C_EE_OR_RDAC; // reset EE/RDAC_n
384 dataBuffer[0] |= address & AD525X_MEM_ADDR_MASK; // set address
385
386 dataBuffer[1] = (data & LSB_BYTE_MASK);
387 I2C_Write(i2cSlaveAddr,
388 dataBuffer,
389 2,
390 1);
391 }
392 TIME_DelayMs(25);
393 }
394
395 /**************************************************************************//**
396 * @brief Write quick commands to the device.
397 *
398 * @param + slaveId - slave device ID in case of SPI interface, and
399 * programmable device address in case of the I2C
400 * + command - desired command, the functionality of a command it may
401 * vary at diffrent devices
402 *
403 * @return success
404 ******************************************************************************/
405 void AD525X_WriteCommand(unsigned char slaveId,
406 unsigned char command,
407 unsigned char address)
408 {
409 unsigned char dataBuffer[3] = {0,};
410 unsigned char i2cSlaveAddr = 0;
411
412 if(AD525X_CHIP_INFO[this_device].comm_type == SPI)
413 {
414 /* Sending the command, reading the result on the next frame */
415 command &= AD525X_CMD_MASK;
416 /* Command adjustment (because of the diffrence between command
417 representations) */
418 if((command >= AD525X_CMD_DECRDAC_6DB) && (command <= AD525X_CMD_RESET))
419 {
420 command += 1;
421 }
422 else if(command >= AD525X_CMD_INCRDAC_6DB)
423 {
424 command += 4;
425 }
426 dataBuffer[0] |= command << AD525X_CMD_SPI_OFFSET;
427 if(command == AD525X_CMD_RESET)
428 {
429 dataBuffer[0] &= 0xF0;
430 }
431 else
432 {
433 dataBuffer[0] |= (address & AD525X_RDAC_ADDR_MASK_1BIT);
434 }
435
436 if((this_device == ID_AD5235) || (this_device == ID_ADN2850)) /* 3 byte data word */
437 {
438 SPI_Write(slaveId,
439 dataBuffer,
440 3);
441 if(command == AD525X_CMD_MEM2RDAC)
442 {
443 dataBuffer[0] &= AD525X_CMD_NOP;
444 SPI_Write(slaveId,
445 dataBuffer,
446 3);
447 }
448 }
449 else /* 2 byte data word */
450 {
451 SPI_Write(slaveId,
452 dataBuffer,
453 2);
454 if(command == AD525X_CMD_MEM2RDAC)
455 {
456 dataBuffer[0] &= AD525X_CMD_NOP;
457 SPI_Write(slaveId,
458 dataBuffer,
459 2);
460 }
461 }
462 }
463 else /* Communication interface is I2C */
464 {
465 /* Setup I2C address */
466 i2cSlaveAddr |= (AD525X_I2C_HARD_ADDR | \
467 (slaveId & AD525X_I2C_PIN_ADDR_MASK));
468
469 dataBuffer[0] |= AD525X_I2C_CMD_OR_REG; // set CMD/REG_n
470 dataBuffer[0] |= (command & AD525X_CMD_MASK) << AD525X_CMD_I2C_OFFSET;
471 dataBuffer[0] |= address & AD525X_RDAC_ADDR_MASK_3BIT; // set address
472
473 I2C_Write(i2cSlaveAddr,
474 dataBuffer,
475 2,
476 1);
477 if(command == AD525X_CMD_MEM2RDAC)
478 {
479 dataBuffer[0] &= AD525X_CMD_NOP;
480 I2C_Write(i2cSlaveAddr,
481 dataBuffer,
482 1,
483 1);
484 }
485 }
486 TIME_DelayMs(25);
487 }