LibDriver AD7705
Loading...
Searching...
No Matches
driver_ad7705.c
Go to the documentation of this file.
1
36
37#include "driver_ad7705.h"
38#include <math.h>
39
43#define CHIP_NAME "Analog Devices AD7705"
44#define MANUFACTURER_NAME "Analog Devices"
45#define SUPPLY_VOLTAGE_MIN 2.7f
46#define SUPPLY_VOLTAGE_MAX 5.25f
47#define MAX_CURRENT 1.3f
48#define TEMPERATURE_MIN -20.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 1000
51
55#define AD7705_REG_COMMUNICATION 0x00
56#define AD7705_REG_SETUP 0x01
57#define AD7705_REG_CLOCK 0x02
58#define AD7705_REG_DATA 0x03
59#define AD7705_REG_TEST 0x04
60#define AD7705_REG_NO_OPERATION 0x05
61#define AD7705_REG_OFFSET 0x06
62#define AD7705_REG_GAIN 0x07
63
75static uint8_t a_ad7705_read(ad7705_handle_t *handle, uint8_t addr, uint8_t *buf, uint16_t len)
76{
77 uint8_t command;
78
79 command = ((addr & 0x7) << 4) | (1 << 3) |
80 (handle->standby << 2) | (handle->channel & 0x03); /* make command */
81 if (handle->spi_read(command, buf, len) != 0) /* read data */
82 {
83 return 1; /* return error */
84 }
85
86 return 0; /* success return 0 */
87}
88
100static uint8_t a_ad7705_write(ad7705_handle_t *handle, uint8_t addr, uint8_t *buf, uint16_t len)
101{
102 uint8_t command;
103
104 command = ((addr & 0x7) << 4) |
105 (handle->standby << 2) | (handle->channel & 0x03); /* make command */
106 if (handle->spi_write(command, buf, len) != 0) /* write data */
107 {
108 return 1; /* return error */
109 }
110
111 return 0; /* success return 0 */
112}
113
125{
126 if (handle == NULL) /* check handle */
127 {
128 return 2; /* return error */
129 }
130 if (handle->inited != 1) /* check handle initialization */
131 {
132 return 3; /* return error */
133 }
134
135 handle->channel = (uint8_t)channel; /* set channel */
136
137 return 0; /* success return 0 */
138}
139
151{
152 if (handle == NULL) /* check handle */
153 {
154 return 2; /* return error */
155 }
156 if (handle->inited != 1) /* check handle initialization */
157 {
158 return 3; /* return error */
159 }
160
161 *channel = (ad7705_channel_t)(handle->channel); /* set channel */
162
163 return 0; /* success return 0 */
164}
165
177{
178 uint8_t res;
179
180 if (handle == NULL) /* check handle */
181 {
182 return 2; /* return error */
183 }
184 if (handle->inited != 1) /* check handle initialization */
185 {
186 return 3; /* return error */
187 }
188
189 handle->standby = 1; /* set enable */
190 res = a_ad7705_write(handle, AD7705_REG_NO_OPERATION, NULL, 0); /* write data */
191 if (res != 0) /* check result */
192 {
193 handle->debug_print("ad7705: power down failed.\n"); /* power down failed */
194
195 return 1; /* return error */
196 }
197
198 return 0; /* success return 0 */
199}
200
212{
213 uint8_t res;
214
215 if (handle == NULL) /* check handle */
216 {
217 return 2; /* return error */
218 }
219 if (handle->inited != 1) /* check handle initialization */
220 {
221 return 3; /* return error */
222 }
223
224 handle->standby = 0; /* set disable */
225 res = a_ad7705_write(handle, AD7705_REG_NO_OPERATION, NULL, 0); /* write data */
226 if (res != 0) /* check result */
227 {
228 handle->debug_print("ad7705: power on failed.\n"); /* power on failed */
229
230 return 1; /* return error */
231 }
232 handle->delay_ms(10); /* delay 10ms */
233
234 return 0; /* success return 0 */
235}
236
249{
250 uint8_t res;
251 uint8_t prev;
252
253 if (handle == NULL) /* check handle */
254 {
255 return 2; /* return error */
256 }
257 if (handle->inited != 1) /* check handle initialization */
258 {
259 return 3; /* return error */
260 }
261
262 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
263 if (res != 0) /* check result */
264 {
265 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
266
267 return 1; /* return error */
268 }
269 prev &= ~(3 << 6); /* clear settings */
270 prev |= mode << 6; /* set mode */
271 res = a_ad7705_write(handle, AD7705_REG_SETUP, &prev, 1); /* write data */
272 if (res != 0) /* check result */
273 {
274 handle->debug_print("ad7705: write setup failed.\n"); /* write setup failed */
275
276 return 1; /* return error */
277 }
278
279 return 0; /* success return 0 */
280}
281
294{
295 uint8_t res;
296 uint8_t prev;
297
298 if (handle == NULL) /* check handle */
299 {
300 return 2; /* return error */
301 }
302 if (handle->inited != 1) /* check handle initialization */
303 {
304 return 3; /* return error */
305 }
306
307 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
308 if (res != 0) /* check result */
309 {
310 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
311
312 return 1; /* return error */
313 }
314 *mode = (ad7705_adc_mode_t)((prev >> 6) & 0x03); /* set mode */
315
316 return 0; /* success return 0 */
317}
318
331{
332 uint8_t res;
333 uint8_t prev;
334
335 if (handle == NULL) /* check handle */
336 {
337 return 2; /* return error */
338 }
339 if (handle->inited != 1) /* check handle initialization */
340 {
341 return 3; /* return error */
342 }
343
344 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
345 if (res != 0) /* check result */
346 {
347 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
348
349 return 1; /* return error */
350 }
351 prev &= ~(7 << 3); /* clear settings */
352 prev |= gain << 3; /* set gain */
353 res = a_ad7705_write(handle, AD7705_REG_SETUP, &prev, 1); /* write data */
354 if (res != 0) /* check result */
355 {
356 handle->debug_print("ad7705: write setup failed.\n"); /* write setup failed */
357
358 return 1; /* return error */
359 }
360
361 return 0; /* success return 0 */
362}
363
376{
377 uint8_t res;
378 uint8_t prev;
379
380 if (handle == NULL) /* check handle */
381 {
382 return 2; /* return error */
383 }
384 if (handle->inited != 1) /* check handle initialization */
385 {
386 return 3; /* return error */
387 }
388
389 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
390 if (res != 0) /* check result */
391 {
392 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
393
394 return 1; /* return error */
395 }
396 *gain = (ad7705_adc_gain_t)((prev >> 3) & 0x07); /* set gain */
397
398 return 0; /* success return 0 */
399}
400
413{
414 uint8_t res;
415 uint8_t prev;
416
417 if (handle == NULL) /* check handle */
418 {
419 return 2; /* return error */
420 }
421 if (handle->inited != 1) /* check handle initialization */
422 {
423 return 3; /* return error */
424 }
425
426 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
427 if (res != 0) /* check result */
428 {
429 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
430
431 return 1; /* return error */
432 }
433 prev &= ~(1 << 2); /* clear settings */
434 prev |= polar << 2; /* set polar */
435 res = a_ad7705_write(handle, AD7705_REG_SETUP, &prev, 1); /* write data */
436 if (res != 0) /* check result */
437 {
438 handle->debug_print("ad7705: write setup failed.\n"); /* write setup failed */
439
440 return 1; /* return error */
441 }
442
443 return 0; /* success return 0 */
444}
445
458{
459 uint8_t res;
460 uint8_t prev;
461
462 if (handle == NULL) /* check handle */
463 {
464 return 2; /* return error */
465 }
466 if (handle->inited != 1) /* check handle initialization */
467 {
468 return 3; /* return error */
469 }
470
471 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
472 if (res != 0) /* check result */
473 {
474 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
475
476 return 1; /* return error */
477 }
478 *polar = (ad7705_adc_polar_t)((prev >> 2) & 0x01); /* set polar */
479
480 return 0; /* success return 0 */
481}
482
495{
496 uint8_t res;
497 uint8_t prev;
498
499 if (handle == NULL) /* check handle */
500 {
501 return 2; /* return error */
502 }
503 if (handle->inited != 1) /* check handle initialization */
504 {
505 return 3; /* return error */
506 }
507
508 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
509 if (res != 0) /* check result */
510 {
511 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
512
513 return 1; /* return error */
514 }
515 prev &= ~(1 << 1); /* clear settings */
516 prev |= enable << 1; /* set bool */
517 res = a_ad7705_write(handle, AD7705_REG_SETUP, &prev, 1); /* write data */
518 if (res != 0) /* check result */
519 {
520 handle->debug_print("ad7705: write setup failed.\n"); /* write setup failed */
521
522 return 1; /* return error */
523 }
524
525 return 0; /* success return 0 */
526}
527
540{
541 uint8_t res;
542 uint8_t prev;
543
544 if (handle == NULL) /* check handle */
545 {
546 return 2; /* return error */
547 }
548 if (handle->inited != 1) /* check handle initialization */
549 {
550 return 3; /* return error */
551 }
552
553 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
554 if (res != 0) /* check result */
555 {
556 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
557
558 return 1; /* return error */
559 }
560 *enable = (ad7705_bool_t)((prev >> 1) & 0x01); /* set buffer */
561
562 return 0; /* success return 0 */
563}
564
577{
578 uint8_t res;
579 uint8_t prev;
580
581 if (handle == NULL) /* check handle */
582 {
583 return 2; /* return error */
584 }
585 if (handle->inited != 1) /* check handle initialization */
586 {
587 return 3; /* return error */
588 }
589
590 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
591 if (res != 0) /* check result */
592 {
593 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
594
595 return 1; /* return error */
596 }
597 prev &= ~(1 << 0); /* clear settings */
598 prev |= (!enable) << 0; /* set bool */
599 res = a_ad7705_write(handle, AD7705_REG_SETUP, &prev, 1); /* write data */
600 if (res != 0) /* check result */
601 {
602 handle->debug_print("ad7705: write setup failed.\n"); /* write setup failed */
603
604 return 1; /* return error */
605 }
606
607 return 0; /* success return 0 */
608}
609
622{
623 uint8_t res;
624 uint8_t prev;
625
626 if (handle == NULL) /* check handle */
627 {
628 return 2; /* return error */
629 }
630 if (handle->inited != 1) /* check handle initialization */
631 {
632 return 3; /* return error */
633 }
634
635 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
636 if (res != 0) /* check result */
637 {
638 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
639
640 return 1; /* return error */
641 }
642 *enable = (ad7705_bool_t)(!((prev >> 0) & 0x01)); /* set bool */
643
644 return 0; /* success return 0 */
645}
646
659{
660 uint8_t res;
661 uint8_t prev;
662
663 if (handle == NULL) /* check handle */
664 {
665 return 2; /* return error */
666 }
667 if (handle->inited != 1) /* check handle initialization */
668 {
669 return 3; /* return error */
670 }
671
672 res = a_ad7705_read(handle, AD7705_REG_CLOCK, &prev, 1); /* read data */
673 if (res != 0) /* check result */
674 {
675 handle->debug_print("ad7705: read clock failed.\n"); /* read clock failed */
676
677 return 1; /* return error */
678 }
679 prev &= ~(7 << 5); /* all set zeros */
680 prev &= ~(1 << 4); /* clear settings */
681 prev |= enable << 4; /* set bool */
682 res = a_ad7705_write(handle, AD7705_REG_CLOCK, &prev, 1); /* write data */
683 if (res != 0) /* check result */
684 {
685 handle->debug_print("ad7705: write clock failed.\n"); /* write clock failed */
686
687 return 1; /* return error */
688 }
689
690 return 0; /* success return 0 */
691}
692
705{
706 uint8_t res;
707 uint8_t prev;
708
709 if (handle == NULL) /* check handle */
710 {
711 return 2; /* return error */
712 }
713 if (handle->inited != 1) /* check handle initialization */
714 {
715 return 3; /* return error */
716 }
717
718 res = a_ad7705_read(handle, AD7705_REG_CLOCK, &prev, 1); /* read data */
719 if (res != 0) /* check result */
720 {
721 handle->debug_print("ad7705: read clock failed.\n"); /* read clock failed */
722
723 return 1; /* return error */
724 }
725 *enable = (ad7705_bool_t)((prev >> 4) & 0x01); /* set bool */
726
727 return 0; /* success return 0 */
728}
729
742{
743 uint8_t res;
744 uint8_t prev;
745
746 if (handle == NULL) /* check handle */
747 {
748 return 2; /* return error */
749 }
750 if (handle->inited != 1) /* check handle initialization */
751 {
752 return 3; /* return error */
753 }
754
755 res = a_ad7705_read(handle, AD7705_REG_CLOCK, &prev, 1); /* read data */
756 if (res != 0) /* check result */
757 {
758 handle->debug_print("ad7705: read clock failed.\n"); /* read clock failed */
759
760 return 1; /* return error */
761 }
762 prev &= ~(7 << 5); /* all set zeros */
763 prev &= ~(1 << 3); /* clear settings */
764 prev |= enable << 3; /* set bool */
765 res = a_ad7705_write(handle, AD7705_REG_CLOCK, &prev, 1); /* write data */
766 if (res != 0) /* check result */
767 {
768 handle->debug_print("ad7705: write clock failed.\n"); /* write clock failed */
769
770 return 1; /* return error */
771 }
772
773 return 0; /* success return 0 */
774}
775
788{
789 uint8_t res;
790 uint8_t prev;
791
792 if (handle == NULL) /* check handle */
793 {
794 return 2; /* return error */
795 }
796 if (handle->inited != 1) /* check handle initialization */
797 {
798 return 3; /* return error */
799 }
800
801 res = a_ad7705_read(handle, AD7705_REG_CLOCK, &prev, 1); /* read data */
802 if (res != 0) /* check result */
803 {
804 handle->debug_print("ad7705: read clock failed.\n"); /* read clock failed */
805
806 return 1; /* return error */
807 }
808 *enable = (ad7705_bool_t)((prev >> 3) & 0x01); /* set bool */
809
810 return 0; /* success return 0 */
811}
812
825{
826 uint8_t res;
827 uint8_t prev;
828
829 if (handle == NULL) /* check handle */
830 {
831 return 2; /* return error */
832 }
833 if (handle->inited != 1) /* check handle initialization */
834 {
835 return 3; /* return error */
836 }
837
838 res = a_ad7705_read(handle, AD7705_REG_CLOCK, &prev, 1); /* read data */
839 if (res != 0) /* check result */
840 {
841 handle->debug_print("ad7705: read clock failed.\n"); /* read clock failed */
842
843 return 1; /* return error */
844 }
845 prev &= ~(7 << 5); /* all set zeros */
846 prev &= ~(7 << 0); /* clear settings */
847 prev |= rate << 0; /* set rate */
848 res = a_ad7705_write(handle, AD7705_REG_CLOCK, &prev, 1); /* write data */
849 if (res != 0) /* check result */
850 {
851 handle->debug_print("ad7705: write clock failed.\n"); /* write clock failed */
852
853 return 1; /* return error */
854 }
855
856 return 0; /* success return 0 */
857}
858
871{
872 uint8_t res;
873 uint8_t prev;
874
875 if (handle == NULL) /* check handle */
876 {
877 return 2; /* return error */
878 }
879 if (handle->inited != 1) /* check handle initialization */
880 {
881 return 3; /* return error */
882 }
883
884 res = a_ad7705_read(handle, AD7705_REG_CLOCK, &prev, 1); /* read data */
885 if (res != 0) /* check result */
886 {
887 handle->debug_print("ad7705: read clock failed.\n"); /* read clock failed */
888
889 return 1; /* return error */
890 }
891 *rate = (ad7705_rate_t)(prev & 0x07); /* set rate */
892
893 return 0; /* success return 0 */
894}
895
907uint8_t ad7705_get_data(ad7705_handle_t *handle, uint16_t *data)
908{
909 uint8_t res;
910 uint8_t buf[2];
911
912 if (handle == NULL) /* check handle */
913 {
914 return 2; /* return error */
915 }
916 if (handle->inited != 1) /* check handle initialization */
917 {
918 return 3; /* return error */
919 }
920
921 res = a_ad7705_read(handle, AD7705_REG_DATA, buf, 2); /* read data */
922 if (res != 0) /* check result */
923 {
924 handle->debug_print("ad7705: read data failed.\n"); /* read data failed */
925
926 return 1; /* return error */
927 }
928 *data = (uint16_t)((((uint16_t)buf[0]) << 8) | buf[1]); /* set data */
929
930
931 return 0; /* success return 0 */
932}
933
945uint8_t ad7705_set_test(ad7705_handle_t *handle, uint8_t data)
946{
947 uint8_t res;
948 uint8_t prev;
949
950 if (handle == NULL) /* check handle */
951 {
952 return 2; /* return error */
953 }
954 if (handle->inited != 1) /* check handle initialization */
955 {
956 return 3; /* return error */
957 }
958
959 prev = data; /* set data */
960 res = a_ad7705_write(handle, AD7705_REG_TEST, &prev, 1); /* write data */
961 if (res != 0) /* check result */
962 {
963 handle->debug_print("ad7705: write test failed.\n"); /* write test failed */
964
965 return 1; /* return error */
966 }
967
968 return 0; /* success return 0 */
969}
970
982uint8_t ad7705_get_test(ad7705_handle_t *handle, uint8_t *data)
983{
984 uint8_t res;
985
986 if (handle == NULL) /* check handle */
987 {
988 return 2; /* return error */
989 }
990 if (handle->inited != 1) /* check handle initialization */
991 {
992 return 3; /* return error */
993 }
994
995 res = a_ad7705_read(handle, AD7705_REG_TEST, data, 1); /* read data */
996 if (res != 0) /* check result */
997 {
998 handle->debug_print("ad7705: read test failed.\n"); /* read test failed */
999
1000 return 1; /* return error */
1001 }
1002
1003 return 0; /* success return 0 */
1004}
1005
1017uint8_t ad7705_set_zero_scale_calibration(ad7705_handle_t *handle, uint32_t calibration)
1018{
1019 uint8_t res;
1020 uint8_t buf[3];
1021
1022 if (handle == NULL) /* check handle */
1023 {
1024 return 2; /* return error */
1025 }
1026 if (handle->inited != 1) /* check handle initialization */
1027 {
1028 return 3; /* return error */
1029 }
1030
1031 buf[0] = (calibration >> 16) & 0xFF; /* set part1 */
1032 buf[1] = (calibration >> 8) & 0xFF; /* set part2 */
1033 buf[2] = (calibration >> 0) & 0xFF; /* set part3 */
1034 res = a_ad7705_write(handle, AD7705_REG_OFFSET, buf, 3); /* write data */
1035 if (res != 0) /* check result */
1036 {
1037 handle->debug_print("ad7705: write offset failed.\n"); /* write offset failed */
1038
1039 return 1; /* return error */
1040 }
1041
1042 return 0; /* success return 0 */
1043}
1044
1056uint8_t ad7705_get_zero_scale_calibration(ad7705_handle_t *handle, uint32_t *calibration)
1057{
1058 uint8_t res;
1059 uint8_t buf[3];
1060
1061 if (handle == NULL) /* check handle */
1062 {
1063 return 2; /* return error */
1064 }
1065 if (handle->inited != 1) /* check handle initialization */
1066 {
1067 return 3; /* return error */
1068 }
1069
1070 res = a_ad7705_read(handle, AD7705_REG_OFFSET, buf, 3); /* read data */
1071 if (res != 0) /* check result */
1072 {
1073 handle->debug_print("ad7705: read offset failed.\n"); /* read offset failed */
1074
1075 return 1; /* return error */
1076 }
1077 *calibration = (uint32_t)((uint32_t)buf[0] << 16) |
1078 (uint32_t)((uint32_t)buf[1] << 8) |
1079 (uint32_t)((uint32_t)buf[2] << 0); /* set calibration */
1080
1081 return 0; /* success return 0 */
1082}
1083
1095uint8_t ad7705_set_full_scale_calibration(ad7705_handle_t *handle, uint32_t calibration)
1096{
1097 uint8_t res;
1098 uint8_t buf[3];
1099
1100 if (handle == NULL) /* check handle */
1101 {
1102 return 2; /* return error */
1103 }
1104 if (handle->inited != 1) /* check handle initialization */
1105 {
1106 return 3; /* return error */
1107 }
1108
1109 buf[0] = (calibration >> 16) & 0xFF; /* set part1 */
1110 buf[1] = (calibration >> 8) & 0xFF; /* set part2 */
1111 buf[2] = (calibration >> 0) & 0xFF; /* set part3 */
1112 res = a_ad7705_write(handle, AD7705_REG_GAIN, buf, 3); /* write data */
1113 if (res != 0) /* check result */
1114 {
1115 handle->debug_print("ad7705: write gain failed.\n"); /* write gain failed */
1116
1117 return 1; /* return error */
1118 }
1119
1120 return 0; /* success return 0 */
1121}
1122
1134uint8_t ad7705_get_full_scale_calibration(ad7705_handle_t *handle, uint32_t *calibration)
1135{
1136 uint8_t res;
1137 uint8_t buf[3];
1138
1139 if (handle == NULL) /* check handle */
1140 {
1141 return 2; /* return error */
1142 }
1143 if (handle->inited != 1) /* check handle initialization */
1144 {
1145 return 3; /* return error */
1146 }
1147
1148 res = a_ad7705_read(handle, AD7705_REG_GAIN, buf, 3); /* read data */
1149 if (res != 0) /* check result */
1150 {
1151 handle->debug_print("ad7705: read gain failed.\n"); /* read gain failed */
1152
1153 return 1; /* return error */
1154 }
1155 *calibration = (uint32_t)((uint32_t)buf[0] << 16) |
1156 (uint32_t)((uint32_t)buf[1] << 8) |
1157 (uint32_t)((uint32_t)buf[2] << 0); /* set calibration */
1158
1159 return 0; /* success return 0 */
1160}
1161
1172uint8_t ad7705_set_reference_voltage(ad7705_handle_t *handle, float voltage)
1173{
1174 if (handle == NULL) /* check handle */
1175 {
1176 return 2; /* return error */
1177 }
1178 if (handle->inited != 1) /* check handle initialization */
1179 {
1180 return 3; /* return error */
1181 }
1182
1183 handle->reference_voltage = voltage; /* set reference voltage */
1184
1185 return 0; /* success return 0 */
1186}
1187
1198uint8_t ad7705_get_reference_voltage(ad7705_handle_t *handle, float *voltage)
1199{
1200 if (handle == NULL) /* check handle */
1201 {
1202 return 2; /* return error */
1203 }
1204 if (handle->inited != 1) /* check handle initialization */
1205 {
1206 return 3; /* return error */
1207 }
1208
1209 *voltage = handle->reference_voltage; /* set reference voltage */
1210
1211 return 0; /* success return 0 */
1212}
1213
1226{
1227 uint8_t res;
1228 uint8_t prev;
1229
1230 if (handle == NULL) /* check handle */
1231 {
1232 return 2; /* return error */
1233 }
1234 if (handle->inited != 1) /* check handle initialization */
1235 {
1236 return 3; /* return error */
1237 }
1238
1239 res = a_ad7705_read(handle, AD7705_REG_COMMUNICATION, &prev, 1); /* read data */
1240 if (res != 0) /* check result */
1241 {
1242 handle->debug_print("ad7705: read communication failed.\n"); /* read communication failed */
1243
1244 return 1; /* return error */
1245 }
1246 *enable = (ad7705_bool_t)(!((prev >> 7) & 0x01)); /* set bool */
1247
1248 return 0; /* success return 0 */
1249}
1250
1264uint8_t ad7705_read(ad7705_handle_t *handle, uint16_t *raw, float *volt)
1265{
1266 uint8_t res;
1267 uint8_t prev;
1268 uint8_t gain;
1269 uint8_t polar;
1270 uint16_t timeout;
1271 uint8_t buf[2];
1272
1273 if (handle == NULL) /* check handle */
1274 {
1275 return 2; /* return error */
1276 }
1277 if (handle->inited != 1) /* check handle initialization */
1278 {
1279 return 3; /* return error */
1280 }
1281
1282 res = a_ad7705_read(handle, AD7705_REG_SETUP, &prev, 1); /* read data */
1283 if (res != 0) /* check result */
1284 {
1285 handle->debug_print("ad7705: read setup failed.\n"); /* read setup failed */
1286
1287 return 1; /* return error */
1288 }
1289 polar = (prev >> 2) & 0x01; /* set polar */
1290 gain = (prev >> 3) & 0x07; /* set gain */
1291
1292 timeout = 1000; /* set timeout 1s */
1293 while (timeout != 0) /* wait 1s */
1294 {
1295 res = a_ad7705_read(handle, AD7705_REG_COMMUNICATION, &prev, 1); /* read data */
1296 if (res != 0) /* check result */
1297 {
1298 handle->debug_print("ad7705: read communication failed.\n"); /* read communication failed */
1299
1300 return 1; /* return error */
1301 }
1302 if (((prev >> 7) & 0x01) == 0) /* check flag */
1303 {
1304 break; /* break */
1305 }
1306 handle->delay_ms(1); /* delay 1ms */
1307 timeout--; /* timeout-- */
1308 }
1309 if (timeout == 0) /* check timeout */
1310 {
1311 handle->debug_print("ad7705: read timeout.\n"); /* read timeout */
1312
1313 return 4; /* return error */
1314 }
1315
1316 res = a_ad7705_read(handle, AD7705_REG_DATA, buf, 2); /* read data */
1317 if (res != 0) /* check result */
1318 {
1319 handle->debug_print("ad7705: read data failed.\n"); /* read data failed */
1320
1321 return 1; /* return error */
1322 }
1323 if (polar != 0) /* unipolar */
1324 {
1325 *raw = (uint16_t)((((uint16_t)buf[0]) << 8) | buf[1]); /* set data */
1326 *volt = (float)(*raw) / 65536.0f * handle->reference_voltage /
1327 (float)powf(2.0f, (float)(gain)); /* convert voltage */
1328 }
1329 else /* bipolar */
1330 {
1331 *raw = (uint16_t)((((uint16_t)buf[0]) << 8) | buf[1]); /* set data */
1332 *volt = ((float)(*raw) - 32768.0f) / 32768.0f *
1333 handle->reference_voltage / (float)powf(2.0f, (float)(gain)); /* convert voltage */
1334 }
1335
1336 return 0; /* success return 0 */
1337}
1338
1351{
1352 uint8_t res;
1353 uint8_t prev;
1354 uint16_t timeout;
1355
1356 if (handle == NULL) /* check handle */
1357 {
1358 return 2; /* return error */
1359 }
1360 if (handle->inited != 1) /* check handle initialization */
1361 {
1362 return 3; /* return error */
1363 }
1364
1365 handle->delay_ms(100); /* delay 100ms */
1366 timeout = 100; /* set timeout 1s */
1367 while (timeout != 0) /* wait 1s */
1368 {
1369 res = a_ad7705_read(handle, AD7705_REG_COMMUNICATION, &prev, 1); /* read data */
1370 if (res != 0) /* check result */
1371 {
1372 handle->debug_print("ad7705: read communication failed.\n"); /* read communication failed */
1373
1374 return 1; /* return error */
1375 }
1376 if (((prev >> 7) & 0x01) == 0) /* check flag */
1377 {
1378 break; /* break */
1379 }
1380 handle->delay_ms(10); /* delay 10ms */
1381 timeout--; /* timeout-- */
1382 }
1383 if (timeout == 0) /* check timeout */
1384 {
1385 handle->debug_print("ad7705: sync timeout.\n"); /* sync timeout */
1386
1387 return 4; /* return error */
1388 }
1389
1390 return 0; /* success return 0 */
1391}
1392
1406{
1407 uint8_t res;
1408
1409 if (handle == NULL) /* check handle */
1410 {
1411 return 2; /* return error */
1412 }
1413 if (handle->debug_print == NULL) /* check debug_print */
1414 {
1415 return 3; /* return error */
1416 }
1417 if (handle->gpio_reset_init == NULL) /* check gpio_reset_init */
1418 {
1419 handle->debug_print("ad7705: gpio_reset_init is null.\n"); /* gpio_reset_init is null */
1420
1421 return 3; /* return error */
1422 }
1423 if (handle->gpio_reset_deinit == NULL) /* check gpio_reset_deinit */
1424 {
1425 handle->debug_print("ad7705: gpio_reset_deinit is null.\n"); /* gpio_reset_deinit is null */
1426
1427 return 3; /* return error */
1428 }
1429 if (handle->gpio_reset_write == NULL) /* check gpio_reset_write */
1430 {
1431 handle->debug_print("ad7705: gpio_reset_write is null.\n"); /* gpio_reset_write is null */
1432
1433 return 3; /* return error */
1434 }
1435 if (handle->spi_init == NULL) /* check spi_init */
1436 {
1437 handle->debug_print("ad7705: spi_init is null.\n"); /* spi_init is null */
1438
1439 return 3; /* return error */
1440 }
1441 if (handle->spi_deinit == NULL) /* check spi_deinit */
1442 {
1443 handle->debug_print("ad7705: spi_deinit is null.\n"); /* spi_deinit is null */
1444
1445 return 3; /* return error */
1446 }
1447 if (handle->spi_read == NULL) /* check spi_read */
1448 {
1449 handle->debug_print("ad7705: spi_read is null.\n"); /* spi_read is null */
1450
1451 return 3; /* return error */
1452 }
1453 if (handle->spi_write == NULL) /* check spi_write */
1454 {
1455 handle->debug_print("ad7705: spi_write is null.\n"); /* spi_write is null */
1456
1457 return 3; /* return error */
1458 }
1459 if (handle->delay_ms == NULL) /* check delay_ms */
1460 {
1461 handle->debug_print("ad7705: delay_ms is null.\n"); /* delay_ms is null */
1462
1463 return 3; /* return error */
1464 }
1465
1466 if (handle->gpio_reset_init() != 0) /* gpio reset init */
1467 {
1468 handle->debug_print("ad7705: gpio reset init failed.\n"); /* gpio reset init failed */
1469
1470 return 1; /* return error */
1471 }
1472 if (handle->spi_init() != 0) /* spi init */
1473 {
1474 handle->debug_print("ad7705: spi init failed.\n"); /* spi init failed */
1475 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1476
1477 return 1; /* return error */
1478 }
1479
1480 res = handle->gpio_reset_write(1); /* set high */
1481 if (res != 0) /* check result */
1482 {
1483 handle->debug_print("ad7705: gpio reset write failed.\n"); /* gpio reset write failed */
1484 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1485 (void)handle->spi_deinit(); /* spi deinit */
1486
1487 return 4; /* return error */
1488 }
1489 handle->delay_ms(10); /* delay 10ms */
1490 res = handle->gpio_reset_write(0); /* set low */
1491 if (res != 0) /* check result */
1492 {
1493 handle->debug_print("ad7705: gpio reset write failed.\n"); /* gpio reset write failed */
1494 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1495 (void)handle->spi_deinit(); /* spi deinit */
1496
1497 return 4; /* return error */
1498 }
1499 handle->delay_ms(10); /* delay 10ms */
1500 res = handle->gpio_reset_write(1); /* set high */
1501 if (res != 0) /* check result */
1502 {
1503 handle->debug_print("ad7705: gpio reset write failed.\n"); /* gpio reset write failed */
1504 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1505 (void)handle->spi_deinit(); /* spi deinit */
1506
1507 return 4; /* return error */
1508 }
1509 handle->delay_ms(10); /* delay 10ms */
1510
1511 handle->standby = 0; /* power on */
1512 res = a_ad7705_write(handle, AD7705_REG_NO_OPERATION, NULL, 0); /* write data */
1513 if (res != 0) /* check result */
1514 {
1515 handle->debug_print("ad7705: power on failed.\n"); /* power on failed */
1516 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1517 (void)handle->spi_deinit(); /* spi deinit */
1518
1519 return 5; /* return error */
1520 }
1521 handle->delay_ms(10); /* delay 10ms */
1522 handle->inited = 1; /* flag finish initialization */
1523
1524 return 0; /* success return 0 */
1525}
1526
1539{
1540 uint8_t res;
1541
1542 if (handle == NULL) /* check handle */
1543 {
1544 return 2; /* return error */
1545 }
1546 if (handle->inited != 1) /* check handle initialization */
1547 {
1548 return 3; /* return error */
1549 }
1550
1551 handle->standby = 1; /* power down */
1552 res = a_ad7705_write(handle, AD7705_REG_NO_OPERATION, NULL, 0); /* write data */
1553 if (res != 0) /* check result */
1554 {
1555 handle->debug_print("ad7705: power down failed.\n"); /* power down failed */
1556
1557 return 4; /* return error */
1558 }
1559
1560 res = handle->spi_deinit(); /* spi deinit */
1561 if (res != 0) /* check result */
1562 {
1563 handle->debug_print("ad7705: spi deinit failed.\n"); /* spi deinit failed */
1564
1565 return 1; /* return error */
1566 }
1567 handle->inited = 0; /* flag close */
1568
1569 return 0; /* success return 0 */
1570}
1571
1585uint8_t ad7705_set_reg(ad7705_handle_t *handle, uint8_t addr, uint8_t *buf, uint16_t len)
1586{
1587 if (handle == NULL) /* check handle */
1588 {
1589 return 2; /* return error */
1590 }
1591 if (handle->inited != 1) /* check handle initialization */
1592 {
1593 return 3; /* return error */
1594 }
1595
1596 if (a_ad7705_write(handle, addr, buf, len) != 0) /* write data */
1597 {
1598 return 1; /* return error */
1599 }
1600
1601 return 0; /* success return 0 */
1602}
1603
1617uint8_t ad7705_get_reg(ad7705_handle_t *handle, uint8_t addr, uint8_t *buf, uint16_t len)
1618{
1619 if (handle == NULL) /* check handle */
1620 {
1621 return 2; /* return error */
1622 }
1623 if (handle->inited != 1) /* check handle initialization */
1624 {
1625 return 3; /* return error */
1626 }
1627
1628 if (a_ad7705_read(handle, addr, buf, len) != 0) /* read data */
1629 {
1630 return 1; /* return error */
1631 }
1632
1633 return 0; /* success return 0 */
1634}
1635
1645{
1646 if (info == NULL) /* check handle */
1647 {
1648 return 2; /* return error */
1649 }
1650
1651 memset(info, 0, sizeof(ad7705_info_t)); /* initialize ad7705 info structure */
1652 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1653 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1654 strncpy(info->interface, "SPI", 8); /* copy interface name */
1655 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1656 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1657 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1658 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1659 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1660 info->driver_version = DRIVER_VERSION; /* set driver version */
1661
1662 return 0; /* success return 0 */
1663}
#define AD7705_REG_NO_OPERATION
#define MAX_CURRENT
#define AD7705_REG_CLOCK
#define SUPPLY_VOLTAGE_MAX
#define AD7705_REG_OFFSET
#define AD7705_REG_COMMUNICATION
chip register definition
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define AD7705_REG_GAIN
#define AD7705_REG_TEST
#define CHIP_NAME
chip information definition
#define AD7705_REG_DATA
#define DRIVER_VERSION
#define AD7705_REG_SETUP
driver ad7705 header file
uint8_t ad7705_set_full_scale_calibration(ad7705_handle_t *handle, uint32_t calibration)
set full scale calibration
uint8_t ad7705_get_adc_polar(ad7705_handle_t *handle, ad7705_adc_polar_t *polar)
get adc polar
uint8_t ad7705_set_master_clock_output_disable(ad7705_handle_t *handle, ad7705_bool_t enable)
enable or disable master clock output disable
ad7705_channel_t
ad7705 channel enumeration definition
ad7705_bool_t
ad7705 bool enumeration definition
uint8_t ad7705_get_channel(ad7705_handle_t *handle, ad7705_channel_t *channel)
get channel
struct ad7705_handle_s ad7705_handle_t
ad7705 handle structure definition
uint8_t ad7705_get_adc_buffer(ad7705_handle_t *handle, ad7705_bool_t *enable)
get adc buffer status
uint8_t ad7705_get_rate(ad7705_handle_t *handle, ad7705_rate_t *rate)
get rate
uint8_t ad7705_get_clock_div2(ad7705_handle_t *handle, ad7705_bool_t *enable)
get clock div2 status
ad7705_adc_mode_t
ad7705 adc mode enumeration definition
uint8_t ad7705_get_adc_gain(ad7705_handle_t *handle, ad7705_adc_gain_t *gain)
get adc gain
uint8_t ad7705_get_test(ad7705_handle_t *handle, uint8_t *data)
get test data
uint8_t ad7705_init(ad7705_handle_t *handle)
initialize the chip
uint8_t ad7705_get_full_scale_calibration(ad7705_handle_t *handle, uint32_t *calibration)
get full scale calibration
uint8_t ad7705_get_adc_mode(ad7705_handle_t *handle, ad7705_adc_mode_t *mode)
get adc mode
uint8_t ad7705_set_adc_buffer(ad7705_handle_t *handle, ad7705_bool_t enable)
enable or disable adc buffer
uint8_t ad7705_get_data(ad7705_handle_t *handle, uint16_t *data)
get data
ad7705_adc_polar_t
ad7705 adc polar enumeration definition
uint8_t ad7705_set_reference_voltage(ad7705_handle_t *handle, float voltage)
set reference voltage
uint8_t ad7705_operate_sync(ad7705_handle_t *handle)
operate sync
uint8_t ad7705_deinit(ad7705_handle_t *handle)
close the chip
uint8_t ad7705_set_adc_gain(ad7705_handle_t *handle, ad7705_adc_gain_t gain)
set adc gain
uint8_t ad7705_set_zero_scale_calibration(ad7705_handle_t *handle, uint32_t calibration)
set zero scale calibration
uint8_t ad7705_set_rate(ad7705_handle_t *handle, ad7705_rate_t rate)
set rate
uint8_t ad7705_get_filter_synchronize(ad7705_handle_t *handle, ad7705_bool_t *enable)
get filter synchronize status
uint8_t ad7705_get_zero_scale_calibration(ad7705_handle_t *handle, uint32_t *calibration)
get zero scale calibration
uint8_t ad7705_get_data_ready(ad7705_handle_t *handle, ad7705_bool_t *enable)
get data ready
uint8_t ad7705_read(ad7705_handle_t *handle, uint16_t *raw, float *volt)
read adc
uint8_t ad7705_get_reference_voltage(ad7705_handle_t *handle, float *voltage)
get reference voltage
ad7705_rate_t
ad7705 rate enumeration definition
uint8_t ad7705_set_clock_div2(ad7705_handle_t *handle, ad7705_bool_t enable)
enable or disable clock div2
uint8_t ad7705_power_down(ad7705_handle_t *handle)
power down
uint8_t ad7705_set_adc_mode(ad7705_handle_t *handle, ad7705_adc_mode_t mode)
set adc mode
uint8_t ad7705_power_on(ad7705_handle_t *handle)
power on
struct ad7705_info_s ad7705_info_t
ad7705 information structure definition
uint8_t ad7705_set_test(ad7705_handle_t *handle, uint8_t data)
set test data
uint8_t ad7705_set_channel(ad7705_handle_t *handle, ad7705_channel_t channel)
set channel
ad7705_adc_gain_t
ad7705 adc gain enumeration definition
uint8_t ad7705_info(ad7705_info_t *info)
get chip's information
uint8_t ad7705_get_master_clock_output_disable(ad7705_handle_t *handle, ad7705_bool_t *enable)
get master clock output disable status
uint8_t ad7705_set_adc_polar(ad7705_handle_t *handle, ad7705_adc_polar_t polar)
set adc polar
uint8_t ad7705_set_filter_synchronize(ad7705_handle_t *handle, ad7705_bool_t enable)
enable or disable filter synchronize
uint8_t ad7705_set_reg(ad7705_handle_t *handle, uint8_t addr, uint8_t *buf, uint16_t len)
set the chip register
uint8_t ad7705_get_reg(ad7705_handle_t *handle, uint8_t addr, uint8_t *buf, uint16_t len)
get the chip register
uint8_t(* gpio_reset_deinit)(void)
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t(* gpio_reset_write)(uint8_t level)
uint8_t(* spi_write)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* gpio_reset_init)(void)
uint8_t(* spi_read)(uint8_t addr, uint8_t *buf, uint16_t len)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]