Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/AD7280A/ad7280a.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ad7280a.c
3 * @brief Driver for the AD7280A Lithium Ion Battery Monitoring System
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2014(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 "ad7280a.h"
46 #include "Communication.h"
47 #include "Console.h"
48 #include "TIME.h"
49
50 /******************************************************************************/
51 /************************** Variables Definitions *****************************/
52 /******************************************************************************/
53 unsigned long readData[24];
54 float cellVoltage[12];
55 float auxADC[12];
56
57 /*****************************************************************************/
58 /************************ Functions Definitions ******************************/
59 /*****************************************************************************/
60
61 /******************************************************************************
62 * @brief Initializes the communication with the device.
63 *
64 * @param None.
65 *
66 * @return status - Result of the initialization procedure.
67 * Example: 0 - if initialization was successful;
68 * -1 - if initialization was unsuccessful.
69 ******************************************************************************/
70 char AD7280A_Init(void)
71 {
72 char status = -1;
73 unsigned long value;
74
75 AD7280A_PD_OUT;
76 AD7280A_PD_HIGH;
77 AD7280A_CNVST_OUT;
78 AD7280A_CNVST_HIGH;
79 AD7280A_ALERT_IN;
80
81 /* Wait 250us */
82 TIME_DelayUs(250);
83
84 status = SPI_Init(0, 1000000, 0, 0);
85 /* Example 1 from the datasheet */
86 /* Configure the Control LB register for all devices */
87 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CONTROL_LB << 21) |
88 ((AD7280A_CTRL_LB_MUST_SET |
89 AD7280A_CTRL_LB_LOCK_DEV_ADDR |
90 AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN) << 13) |
91 (1 << 12));
92 AD7280A_Transfer_32bits(value);
93 /* Configure the Read Register for all devices */
94 value = AD7280A_CRC_Write((unsigned long) (AD7280A_READ << 21) |
95 (AD7280A_CONTROL_LB << 15) |
96 (1 << 12));
97 AD7280A_Transfer_32bits(value);
98 /* Read master address */
99 value = AD7280A_Transfer_32bits(AD7280A_READ_TXVAL);
100 //CONSOLE_Print("Master address=0x%x\r\n",(value >> 27));
101 /* Read slave address */
102 value = AD7280A_Transfer_32bits(AD7280A_READ_TXVAL);
103 //CONSOLE_Print("Slave address=0x%x\r\n",(value >> 27));
104
105 return status;
106 }
107 /******************************************************************************
108 * @brief Reads/transmits 32 data bits from/to AD7280A.
109 *
110 * @param data - Data to be transmitted (MOSI pin).
111 *
112 * @return receivedData - Received data (MISO pin).
113 ******************************************************************************/
114 unsigned long AD7280A_Transfer_32bits(unsigned long data)
115 {
116 unsigned char dataBuf[4];
117 unsigned long receivedData;
118
119 dataBuf[0] = (data >> 24) & 0xff;
120 dataBuf[1] = (data >> 16) & 0xff;
121 dataBuf[2] = (data >> 8) & 0xff;
122 dataBuf[3] = (data >> 0) & 0xff;
123
124 SPI_Read(AD7280A_SLAVE_ID, (unsigned char*)dataBuf, 4);
125
126 receivedData = ((unsigned long)dataBuf[0] << 24) +
127 ((unsigned long)dataBuf[1] << 16) +
128 ((unsigned long)dataBuf[2] << 8) +
129 ((unsigned long)dataBuf[3] << 0);
130
131 return receivedData;
132 }
133
134 /******************************************************************************
135 * @brief Computes the CRC value for a write transmission, and prepares the
136 * complete write codeword
137 *
138 * @param message : The message that is to be transmitted. The least significant
139 * 11 bits are discarded
140 *
141 * @return Complete codeword that can be transmitted to AD7280A
142 ******************************************************************************/
143 unsigned long AD7280A_CRC_Write(unsigned long message)
144 {
145 long i;
146 long CRC;
147 long xor1 = 0;
148 long xor2 = 0;
149 long xor3 = 0;
150 long xor4 = 0;
151 long xor5 = 0;
152 unsigned long data_out = 0;
153 long CRC0 = 0;
154 long CRC1 = 0;
155 long CRC2 = 0;
156 long CRC3 = 0;
157 long CRC4 = 0;
158 long CRC5 = 0;
159 long CRC6 = 0;
160 long CRC7 = 0;
161
162 message = message >> 11;
163 for (i = NUMBITS_WRITE; i >= 0; i--)
164 {
165 xor5 = (CRC4 ^ CRC7) ? 1 : 0;
166 xor4 = (CRC2 ^ CRC7) ? 1 : 0;
167 xor3 = (CRC1 ^ CRC7) ? 1 : 0;
168 xor2 = (CRC0 ^ CRC7) ? 1 : 0;
169 xor1 = ((message & (1 << i)) ? 1 : 0) ^ (CRC7 ? 1 : 0);
170 CRC7 = CRC6;
171 CRC6 = CRC5;
172 CRC5 = xor5;
173 CRC4 = CRC3;
174 CRC3 = xor4;
175 CRC2 = xor3;
176 CRC1 = xor2;
177 CRC0 = xor1;
178 }
179 CRC = (CRC0 ? (1 << 0) : 0) | (CRC1 ? (1 << 1) : 0) | (CRC2 ? (1 << 2) : 0) |
180 (CRC3 ? (1 << 3) : 0) | (CRC4 ? (1 << 4) : 0) | (CRC5 ? (1 << 5) : 0) |
181 (CRC6 ? (1 << 6) : 0) | (CRC7 ? (1 << 7) : 0);
182 data_out = (message << 11) | (CRC << 3) | 2;
183
184 return data_out;
185 }
186
187 /******************************************************************************
188 * @brief Checks the received message if the received CRC and computed CRC are
189 * the same.
190 *
191 * @param message : The received message
192 *
193 * @return TRUE if the two CRC are identical
194 * FALSE otherwise
195 ******************************************************************************/
196 long AD7280A_CRC_Read(unsigned long message)
197 {
198 long i;
199 unsigned long data_in=0;
200 long CRC_REC;
201 long CRC_COMPUTED;
202 long xor1 = 0;
203 long xor2 = 0;
204 long xor3 = 0;
205 long xor4 = 0;
206 long xor5 = 0;
207 long CRC0 = 0;
208 long CRC1 = 0;
209 long CRC2 = 0;
210 long CRC3 = 0;
211 long CRC4 = 0;
212 long CRC5 = 0;
213 long CRC6 = 0;
214 long CRC7 = 0;
215
216 CRC_REC= (message >> 2) & 0xFF;
217 data_in = message >> 10;
218 for (i = NUMBITS_READ; i >= 0; i--)
219 {
220 xor5 = (CRC4 ^ CRC7) ? 1 : 0;
221 xor4 = (CRC2 ^ CRC7) ? 1 : 0;
222 xor3 = (CRC1 ^ CRC7) ? 1 : 0;
223 xor2 = (CRC0 ^ CRC7) ? 1 : 0;
224 xor1 = ((data_in & (1 << i)) ? 1 : 0) ^ (CRC7 ? 1 : 0);
225 CRC7 = CRC6;
226 CRC6 = CRC5;
227 CRC5 = xor5;
228 CRC4 = CRC3;
229 CRC3 = xor4;
230 CRC2 = xor3;
231 CRC1 = xor2;
232 CRC0 = xor1;
233 }
234 CRC_COMPUTED = (CRC0 ? (1 << 0) : 0) | (CRC1 ? (1 << 1) : 0) |
235 (CRC2 ? (1 << 2) : 0) | (CRC3 ? (1 << 3) : 0) |
236 (CRC4 ? (1 << 4) : 0) | (CRC5 ? (1 << 5) : 0) |
237 (CRC6 ? (1 << 6) : 0) | (CRC7 ? (1 << 7) : 0);
238 if (CRC_REC == CRC_COMPUTED)
239 {
240 return TRUE;
241 }
242 else
243 {
244 return FALSE;
245 }
246 }
247
248 /******************************************************************************
249 * @brief Performs a read from all registers on 2 devices.
250 *
251 * @param None.
252 *
253 * @return TRUE.
254 ******************************************************************************/
255 char AD7280A_Convert_Read_All(void)
256 {
257 unsigned char i;
258 //long err = TRUE;
259 unsigned long value;
260
261 /* Configure Control HB register. Read all register, convert all registers,
262 average 8 values for all devices */
263 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CONTROL_HB << 21) |
264 ((AD7280A_CTRL_HB_CONV_RES_READ_ALL |
265 AD7280A_CTRL_HB_CONV_INPUT_ALL |
266 AD7280A_CTRL_HB_CONV_AVG_8) << 13) |
267 (1 << 12));
268 /* Configure the Read register for all devices */
269 AD7280A_Transfer_32bits(value);
270 value = AD7280A_CRC_Write((unsigned long) (AD7280A_READ << 21) |
271 (AD7280A_CELL_VOLTAGE_1 << 15) |
272 (1 << 12));
273 AD7280A_Transfer_32bits(value);
274 /* Configure the CNVST register, allow single CNVST pulse */
275 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CNVST_N_CONTROL << 21) |
276 (2 << 13) |
277 (1 << 12));
278 AD7280A_Transfer_32bits(value);
279 /* Wait 100us */
280 TIME_DelayUs(100);
281 /* Toggle CNVST pin */
282 AD7280A_CNVST_LOW;
283 /* Wait 50us */
284 TIME_DelayUs(50);
285 AD7280A_CNVST_HIGH;
286 /* Wait 300us */
287 TIME_DelayUs(300);
288 /* Read data from both devices */
289 for(i = 0; i < 24; i++)
290 {
291 readData[i] = AD7280A_Transfer_32bits(AD7280A_READ_TXVAL);
292 }
293
294 // Optionally check all received data to make sure CRC is correct
295 /*for(i=0;i<24;i++)
296 {
297 if ( crc_read(readData[i]) == FALSE )
298 {
299 err = FALSE;
300 }
301 }*/
302
303 /* Convert the received data to float values. */
304 AD7280A_Convert_Data_All();
305
306 return (TRUE);
307 }
308
309 /******************************************************************************
310 * @brief Converts acquired data from all channels to float values.
311 *
312 * @param None.
313 *
314 * @return TRUE.
315 ******************************************************************************/
316 char AD7280A_Convert_Data_All(void)
317 {
318 unsigned char i;
319
320 for(i = 0; i < 6; i++)
321 {
322 cellVoltage[i] = 1 + ((readData[i] >> 11) & 0xfff) * 0.0009765625;
323 auxADC[i] = ((readData[i+6] >> 11) & 0xfff) * 0.001220703125;
324 cellVoltage[i + 6] = 1 + ((readData[i+12] >> 11) & 0xfff) * 0.0009765625;
325 auxADC[i + 6] = ((readData[i+18] >> 11) & 0xfff) * 0.001220703125;
326 }
327
328 return (TRUE);
329 }
330
331 /******************************************************************************
332 * @brief Reads the register content of one selected register.
333 *
334 * @param devAddr - the device address
335 * Example: 0 - master.
336 * 1 - slave.
337 * readReg - the address of the register to be read from the
338 * selected device (only registers from Register Map with
339 * address higher or equal to 0x0D).
340 *
341 * @return readContent - register content.
342 ******************************************************************************/
343 short AD7280A_Read_Register(unsigned char devAddr,
344 unsigned char readReg)
345 {
346 unsigned long value;
347 short readContent;
348 //unsigned char readDevAddress;
349 //unsigned char readRegAddress;
350 //unsigned char readWriteAck;
351
352
353 /* Example 6 from datasheet */
354 /* Disable reading on all devices */
355 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CONTROL_HB << 21) |
356 (AD7280A_CTRL_HB_CONV_RES_READ_NO << 13) |
357 (1 << 12));
358 AD7280A_Transfer_32bits(value);
359 /* Enable reading on the selected device */
360 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
361 (AD7280A_CONTROL_HB << 21) |
362 ((AD7280A_CTRL_HB_CONV_RES_READ_ALL |
363 AD7280A_CTRL_HB_CONV_INPUT_ALL) << 13));
364 AD7280A_Transfer_32bits(value);
365 /* Wait 100us */
366 TIME_DelayUs(100);
367 /* Configure the Read register */
368 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
369 (AD7280A_READ << 21) |
370 (readReg << 15));
371 AD7280A_Transfer_32bits(value);
372 /* Perform the read */
373 value = AD7280A_Transfer_32bits(AD7280A_READ_TXVAL);
374 if (AD7280A_CRC_Read(value) == TRUE)
375 {
376 //readDevAddress = value >> 27; // Extract the Device address
377 //readRegAddress = (value >> 21 ) & 0x2F; // Extract the Register address
378 readContent = (value >> 13 ) & 0xFF; // Extract the Register data
379 //readWriteAck = (value & 0x200) ? 1 : 0; // Extract the write acknowledge
380 }
381 else
382 {
383 readContent = -1;
384 }
385
386 return readContent;
387 }
388
389 /******************************************************************************
390 * @brief Reads the conversion of one channel.
391 *
392 * @param devAddr - the device address
393 * Example: 0 - master.
394 * 1 - slave.
395 * readReg - the address of the register to be read from
396 * selected device (only Cell_Voltage_x or AUX_ADC_x).
397 *
398 * @return readConversion - ADC code or -1 if an error occured
399 ******************************************************************************/
400 short AD7280A_Read_Conversion(unsigned char devAddr,
401 unsigned char readReg)
402 {
403 unsigned long value;
404 short readConversion;
405 //unsigned char readDevAddress;
406 //unsigned char readRegAddress;
407 //unsigned char readWriteAck;
408
409 /* Example 4 from datasheet */
410 /* Write the register address to Read Register */
411 value = AD7280A_CRC_Write((unsigned long)(devAddr << 31) |
412 (AD7280A_READ << 21) |
413 (readReg << 15));
414 AD7280A_Transfer_32bits(value);
415 /* Disable reading on all devices */
416 value = AD7280A_CRC_Write((unsigned long)(AD7280A_CONTROL_HB << 21) |
417 (AD7280A_CTRL_HB_CONV_RES_READ_NO << 13) |
418 (1 << 12));
419 AD7280A_Transfer_32bits(value);
420 /* Wait 100us */
421 TIME_DelayUs(100);
422 /* */
423 value = AD7280A_CRC_Write((unsigned long)(devAddr << 31) |
424 (AD7280A_CONTROL_HB << 21) |
425 ((AD7280A_CTRL_HB_CONV_RES_READ_ALL |
426 AD7280A_CTRL_HB_CONV_INPUT_ALL) << 13));
427 AD7280A_Transfer_32bits(value);
428 /* Wait 100us */
429 TIME_DelayUs(100);
430 /* Allow conversions to be initiated using CNVST pin on selected part */
431 value=AD7280A_CRC_Write((unsigned long)(devAddr << 31) |
432 (AD7280A_CNVST_N_CONTROL << 21) |
433 (2 << 13));
434 /* Write the CNVST_N register, allow single CNVST pulse */
435 AD7280A_Transfer_32bits(value);
436 /* Initiate conversions through the falling edge of CNVST */
437 AD7280A_CNVST_LOW;
438 /* Allow sufficient time for all conversions to be completed */
439 /* Wait 50us */
440 TIME_DelayUs(50);
441 AD7280A_CNVST_HIGH;
442 /* Wait 300us */
443 TIME_DelayUs(300);
444 /* Perform the read */
445 value = AD7280A_Transfer_32bits(AD7280A_READ_TXVAL);
446
447 if (AD7280A_CRC_Read(value) == TRUE)
448 {
449 //readDevAddress = value >> 27; // Extract the Device address
450 //readRegAddress = (value >> 23 ) & 0x2F; // Extract the Register address
451 readConversion = (value >> 11 ) & 0xFFF; // Extract the Register data
452 //readWriteAck = (value & 0x200) ? 1 :0; //Extract the write acknowledge
453 }
454 else
455 {
456 readConversion = -1;
457 }
458
459 return readConversion;
460 }
461
462 /******************************************************************************
463 * @brief Converts the input data to a voltage value.
464 *
465 * @param type - 0 = cell voltage input calculation.
466 * - 1 = auxiliary ADC input calculation.
467 * data - ADC code to be converted to volts.
468 *
469 * @return result - calculated voltage.
470 ******************************************************************************/
471 float AD7280A_Convert_Data(unsigned char type,
472 unsigned short data)
473 {
474 float result = 0;
475
476 data = data & 0xfff;
477 if (type == 0)
478 {
479 result = 1 + ((float)data * 0.0009765625);
480 }
481 else
482 {
483 if (type == 1)
484 {
485 result = (float)data * 0.001220703125;
486 }
487 }
488
489 return result;
490 }
491
492 /******************************************************************************
493 * @brief Writes the content of one selected register from the selected device.
494 *
495 * @param devAddr - the device address
496 * Example: 0 - master.
497 * 1 - slave.
498 * readReg - the address of the register to be read from the
499 * selected device (only registers from Register Map with
500 * address higher or equal to 0x0D).
501 *
502 * @return none.
503 ******************************************************************************/
504 void AD7280A_Write_Register(unsigned char devAddr,
505 unsigned char readReg,
506 unsigned char regVal)
507 {
508 unsigned long value = 0;
509
510 switch (readReg)
511 {
512 case 0x0D:
513 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
514 (AD7280A_CONTROL_HB << 21) |
515 (regVal << 13));
516 AD7280A_Transfer_32bits(value);
517 break;
518 case 0x0E:
519 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
520 (AD7280A_CONTROL_LB << 21) |
521 (regVal << 13));
522 AD7280A_Transfer_32bits(value);
523 break;
524 case 0x0F:
525 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
526 (AD7280A_CELL_OVERVOLTAGE << 21) |
527 (regVal << 13));
528 AD7280A_Transfer_32bits(value);
529 break;
530 case 0x10:
531 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
532 (AD7280A_CELL_UNDERVOLTAGE << 21) |
533 (regVal << 13));
534 AD7280A_Transfer_32bits(value);
535 break;
536 case 0x11:
537 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
538 (AD7280A_AUX_ADC_OVERVOLTAGE << 21) |
539 (regVal << 13));
540 AD7280A_Transfer_32bits(value);
541 break;
542 case 0x12:
543 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
544 (AD7280A_AUX_ADC_UNDERVOLTAGE << 21) |
545 (regVal << 13));
546 AD7280A_Transfer_32bits(value);
547 break;
548 case 0x13:
549 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
550 (AD7280A_ALERT << 21) |
551 (regVal << 13));
552 AD7280A_Transfer_32bits(value);
553 break;
554 case 0x14:
555 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
556 (AD7280A_CELL_BALANCE << 21) |
557 (regVal << 13));
558 AD7280A_Transfer_32bits(value);
559 break;
560 case 0x15:
561 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
562 (AD7280A_CB1_TIMER << 21) |
563 (regVal << 13));
564 AD7280A_Transfer_32bits(value);
565 break;
566 case 0x16:
567 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
568 (AD7280A_CB2_TIMER << 21) |
569 (regVal << 13));
570 AD7280A_Transfer_32bits(value);
571 break;
572 case 0x17:
573 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
574 (AD7280A_CB3_TIMER << 21) |
575 (regVal << 13));
576 AD7280A_Transfer_32bits(value);
577 break;
578 case 0x18:
579 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
580 (AD7280A_CB4_TIMER << 21) |
581 (regVal << 13));
582 AD7280A_Transfer_32bits(value);
583 break;
584 case 0x19:
585 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
586 (AD7280A_CB5_TIMER << 21) |
587 (regVal << 13));
588 AD7280A_Transfer_32bits(value);
589 break;
590 case 0x1A:
591 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
592 (AD7280A_CB6_TIMER << 21) |
593 (regVal << 13));
594 AD7280A_Transfer_32bits(value);
595 break;
596 case 0x1B:
597 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
598 (AD7280A_PD_TIMER << 21) |
599 (regVal << 13));
600 AD7280A_Transfer_32bits(value);
601 break;
602 case 0x1C:
603 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
604 (AD7280A_READ << 21) |
605 (regVal << 13));
606 AD7280A_Transfer_32bits(value);
607 break;
608 case 0x1D:
609 value = AD7280A_CRC_Write((unsigned long) (devAddr << 31) |
610 (AD7280A_CNVST_N_CONTROL << 21) |
611 (regVal << 13));
612 AD7280A_Transfer_32bits(value);
613 break;
614 }
615 }
616
617 /******************************************************************************
618 * @brief Performs the self test for two devices(one master and one slave).
619 *
620 * @param *selfTestRegA - the voltage corresponding to self test master
621 * register.
622 * *selfTestRegB - the voltage corresponding to self test slave
623 * register.
624 *
625 * @return none.
626 ******************************************************************************/
627 void AD7280A_Selftest_All(float *selfTestRegA, float *selfTestRegB)
628 {
629 unsigned long value;
630
631 /* Example 7 of AD7280 datasheet */
632 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CONTROL_HB << 21) |
633 ((AD7280A_CTRL_HB_CONV_RES_READ_ALL |
634 AD7280A_CTRL_HB_CONV_INPUT_SELF_TEST) << 13) |
635 (1 << 12));
636 /* Select self test conversion and read on all parts */
637 AD7280A_Transfer_32bits(value);
638 /* Wait 100us */
639 TIME_DelayUs(100);
640 value = AD7280A_CRC_Write((unsigned long) (AD7280A_READ << 21) |
641 (AD7280A_SELF_TEST << 15) |
642 (1 << 12));
643 /* Write the read register with the address of the Self test register */
644 AD7280A_Transfer_32bits(value);
645 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CNVST_N_CONTROL << 21) |
646 (2 << 13) |
647 (1 << 12));
648 /* Write the CNVST_N register, allow single CNVST pulse */
649 AD7280A_Transfer_32bits(value);
650 AD7280A_CNVST_LOW;
651 /* wait 100us */
652 TIME_DelayUs(100);
653 AD7280A_CNVST_HIGH;
654 /* wait 300us */
655 TIME_DelayUs(300);
656 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CNVST_N_CONTROL << 21) |
657 (1 << 13) |
658 (1 << 12));
659 /* Update registers */
660 AD7280A_Transfer_32bits(value);
661 value = (AD7280A_Transfer_32bits(AD7280A_READ_TXVAL) >> 11) & 0xfff;
662 /* Read master self test register */
663 *selfTestRegA = (float)value * 0.001220703125;
664 value = (AD7280A_Transfer_32bits(AD7280A_READ_TXVAL) >> 11) & 0xfff;
665 /* Read slave self test register */
666 *selfTestRegB = (float)value * 0.001220703125;
667 value = AD7280A_CRC_Write((unsigned long) (AD7280A_CONTROL_HB << 21) |
668 ((AD7280A_CTRL_HB_CONV_RES_READ_ALL |
669 AD7280A_CTRL_HB_CONV_INPUT_ALL) << 13) |
670 (1 << 12));
671 AD7280A_Transfer_32bits(value);
672 }
673
674 /******************************************************************************
675 * @brief Reads the value of Alert Pin from the device.
676 *
677 * @param none.
678 *
679 * @return alertAD7280A - the value read of the pin
680 * Example: 0 - the pin is low.
681 * 1 - the pin is high.
682 ******************************************************************************/
683 unsigned char AD7280A_Alert_Pin(void)
684 {
685 unsigned char alertAD7280A = 0;
686
687 alertAD7280A = (GPIO_GET_DATA(GPIO_BASEADDR) & AD7280_ALERT) ? 1 : 0;
688
689 return alertAD7280A;
690 }