LibDriver INA219
Loading...
Searching...
No Matches
driver_ina219.c
Go to the documentation of this file.
1
36
37#include "driver_ina219.h"
38#include <math.h>
39
43#define CHIP_NAME "Texas Instruments INA219"
44#define MANUFACTURER_NAME "Texas Instruments"
45#define SUPPLY_VOLTAGE_MIN 3.0f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 1.0f
48#define TEMPERATURE_MIN -25.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 1000
51
55#define INA219_REG_CONF 0x00
56#define INA219_REG_SHUNT_VOLTAGE 0x01
57#define INA219_REG_BUS_VOLTAGE 0x02
58#define INA219_REG_POWER 0x03
59#define INA219_REG_CURRENT 0x04
60#define INA219_REG_CALIBRATION 0x05
61
72static uint8_t a_ina219_iic_read(ina219_handle_t *handle, uint8_t reg, uint16_t *data)
73{
74 uint8_t buf[2];
75
76 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
77 if (handle->iic_read(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* read data */
78 {
79 return 1; /* return error */
80 }
81 else
82 {
83 *data = (uint16_t)buf[0] << 8 | buf[1]; /* get data */
84
85 return 0; /* success return 0 */
86 }
87}
88
99static uint8_t a_ina219_iic_write(ina219_handle_t *handle, uint8_t reg, uint16_t data)
100{
101 uint8_t buf[2];
102
103 buf[0] = (uint8_t)((data >> 8) & 0xFF); /* get MSB */
104 buf[1] = (uint8_t)((data >> 0) & 0xFF); /* get LSB */
105 if (handle->iic_write(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* write data */
106 {
107 return 1; /* return error */
108 }
109 else
110 {
111 return 0; /* success return 0 */
112 }
113}
114
124uint8_t ina219_set_resistance(ina219_handle_t *handle, double resistance)
125{
126 if (handle == NULL) /* check handle */
127 {
128 return 2; /* return error */
129 }
130
131 handle->r = resistance; /* set resistance */
132
133 return 0; /* success return 0 */
134}
135
145uint8_t ina219_get_resistance(ina219_handle_t *handle, double *resistance)
146{
147 if (handle == NULL) /* check handle */
148 {
149 return 2; /* return error */
150 }
151
152 *resistance = handle->r; /* get resistance */
153
154 return 0; /* success return 0 */
155}
156
167{
168 if (handle == NULL) /* check handle */
169 {
170 return 2; /* return error */
171 }
172
173 handle->iic_addr = (uint8_t)addr_pin; /* set pin */
174
175 return 0; /* success return 0 */
176}
177
188{
189 if (handle == NULL) /* check handle */
190 {
191 return 2; /* return error */
192 }
193
194 *addr_pin = (ina219_address_t)(handle->iic_addr); /* get pin */
195
196 return 0; /* success return 0 */
197}
198
210{
211 uint8_t res;
212 uint16_t prev;
213
214 if (handle == NULL) /* check handle */
215 {
216 return 2; /* return error */
217 }
218 if (handle->inited != 1) /* check handle initialization */
219 {
220 return 3; /* return error */
221 }
222
223 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
224 if (res != 0) /* check result */
225 {
226 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
227
228 return 1; /* return error */
229 }
230 prev &= ~(1 << 15); /* clear soft reset */
231 prev |= 1 << 15; /* set soft reset */
232
233 return a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
234}
235
248{
249 uint8_t res;
250 uint16_t prev;
251
252 if (handle == NULL) /* check handle */
253 {
254 return 2; /* return error */
255 }
256 if (handle->inited != 1) /* check handle initialization */
257 {
258 return 3; /* return error */
259 }
260
261 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
262 if (res != 0) /* check result */
263 {
264 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
265
266 return 1; /* return error */
267 }
268 prev &= ~(1 << 13); /* clear range bit */
269 prev |= range << 13; /* set range bit */
270
271 return a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
272}
273
286{
287 uint8_t res;
288 uint16_t prev;
289
290 if (handle == NULL) /* check handle */
291 {
292 return 2; /* return error */
293 }
294 if (handle->inited != 1) /* check handle initialization */
295 {
296 return 3; /* return error */
297 }
298
299 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
300 if (res != 0) /* check result */
301 {
302 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
303
304 return 1; /* return error */
305 }
306 *range = (ina219_bus_voltage_range_t)((prev >> 13) & 0x01); /* get range */
307
308 return 0; /* success return 0 */
309}
310
323{
324 uint8_t res;
325 uint16_t prev;
326
327 if (handle == NULL) /* check handle */
328 {
329 return 2; /* return error */
330 }
331 if (handle->inited != 1) /* check handle initialization */
332 {
333 return 3; /* return error */
334 }
335
336 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
337 if (res != 0) /* check result */
338 {
339 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
340
341 return 1; /* return error */
342 }
343 prev &= ~(3 << 11); /* clear pga bit */
344 prev |= pga << 11; /* set pga bit */
345
346 return a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
347}
348
361{
362 uint8_t res;
363 uint16_t prev;
364
365 if (handle == NULL) /* check handle */
366 {
367 return 2; /* return error */
368 }
369 if (handle->inited != 1) /* check handle initialization */
370 {
371 return 3; /* return error */
372 }
373
374 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
375 if (res != 0) /* check result */
376 {
377 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
378
379 return 1; /* return error */
380 }
381 *pga = (ina219_pga_t)((prev >> 11) & 0x03); /* get pga */
382
383 return 0; /* success return 0 */
384}
385
398{
399 uint8_t res;
400 uint16_t prev;
401
402 if (handle == NULL) /* check handle */
403 {
404 return 2; /* return error */
405 }
406 if (handle->inited != 1) /* check handle initialization */
407 {
408 return 3; /* return error */
409 }
410
411 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
412 if (res != 0) /* check result */
413 {
414 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
415
416 return 1; /* return error */
417 }
418 prev &= ~(0xF << 7); /* clear mode bit */
419 prev |= mode << 7; /* set mode bit */
420
421 return a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
422}
423
436{
437 uint8_t res;
438 uint16_t prev;
439
440 if (handle == NULL) /* check handle */
441 {
442 return 2; /* return error */
443 }
444 if (handle->inited != 1) /* check handle initialization */
445 {
446 return 3; /* return error */
447 }
448
449 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
450 if (res != 0) /* check result */
451 {
452 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
453
454 return 1; /* return error */
455 }
456 *mode = (ina219_adc_mode_t)((prev >> 7) & 0xF); /* get mode */
457
458 return 0; /* success return 0 */
459}
460
473{
474 uint8_t res;
475 uint16_t prev;
476
477 if (handle == NULL) /* check handle */
478 {
479 return 2; /* return error */
480 }
481 if (handle->inited != 1) /* check handle initialization */
482 {
483 return 3; /* return error */
484 }
485
486 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
487 if (res != 0) /* check result */
488 {
489 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
490
491 return 1; /* return error */
492 }
493 prev &= ~(0xF << 3); /* clear mode bit */
494 prev |= mode << 3; /* set mode bit */
495
496 return a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
497}
498
511{
512 uint8_t res;
513 uint16_t prev;
514
515 if (handle == NULL) /* check handle */
516 {
517 return 2; /* return error */
518 }
519 if (handle->inited != 1) /* check handle initialization */
520 {
521 return 3; /* return error */
522 }
523
524 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
525 if (res != 0) /* check result */
526 {
527 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
528
529 return 1; /* return error */
530 }
531 *mode = (ina219_adc_mode_t)((prev >> 3) & 0xF); /* get mode */
532
533 return 0; /* success return 0 */
534}
535
548{
549 uint8_t res;
550 uint16_t prev;
551
552 if (handle == NULL) /* check handle */
553 {
554 return 2; /* return error */
555 }
556 if (handle->inited != 1) /* check handle initialization */
557 {
558 return 3; /* return error */
559 }
560
561 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
562 if (res != 0) /* check result */
563 {
564 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
565
566 return 1; /* return error */
567 }
568 prev &= ~(0x7 << 0); /* clear mode bit */
569 prev |= mode << 0; /* set mode bit */
570 res = a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
571 if (res != 0) /* check result */
572 {
573 handle->debug_print("ina219: write conf register failed.\n"); /* write conf register failed */
574
575 return 1; /* return error */
576 }
577 if ((mode >= INA219_MODE_SHUNT_VOLTAGE_TRIGGERED) && /* check mode */
579 {
580 uint8_t m, mode1, mode2, t;
581
582 mode1 = (prev >> 3) & 0xF; /* get shunt adc mode */
583 mode2 = (prev >> 7) & 0xF; /* get bus adc mode */
584 m = (mode1 > mode2) ? mode1 : mode2; /* get max mode */
585 if (m <= 8) /* check mode */
586 {
587 handle->delay_ms(2); /* delay 2 ms */
588 }
589 else
590 {
591 t = (uint8_t)(0.532 * pow(2, m - 8)) + 1; /* get time */
592 handle->delay_ms(t * 2); /* delay time */
593 }
594
595 return 0; /* success return 0 */
596 }
597 else
598 {
599 return 0; /* success return 0 */
600 }
601}
602
615{
616 uint8_t res;
617 uint16_t prev;
618
619 if (handle == NULL) /* check handle */
620 {
621 return 2; /* return error */
622 }
623 if (handle->inited != 1) /* check handle initialization */
624 {
625 return 3; /* return error */
626 }
627
628 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
629 if (res != 0) /* check result */
630 {
631 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
632
633 return 1; /* return error */
634 }
635 *mode = (ina219_mode_t)((prev >> 0) & 0x7); /* get mode */
636
637 return 0; /* success return 0 */
638}
639
652uint8_t ina219_read_shunt_voltage(ina219_handle_t *handle, int16_t *raw, float *mV)
653{
654 uint8_t res;
655 union
656 {
657 uint16_t u;
658 int16_t s;
659 } u;
660
661 if (handle == NULL) /* check handle */
662 {
663 return 2; /* return error */
664 }
665 if (handle->inited != 1) /* check handle initialization */
666 {
667 return 3; /* return error */
668 }
669
670 res = a_ina219_iic_read(handle, INA219_REG_SHUNT_VOLTAGE, (uint16_t *)&u.u); /* read shunt voltage */
671 if (res != 0) /* check result */
672 {
673 handle->debug_print("ina219: read shunt voltage register failed.\n"); /* read shunt voltage register failed */
674
675 return 1; /* return error */
676 }
677 *raw = u.s; /* set the raw */
678 *mV = (float)(*raw) / 100.0f; /* set the converted data */
679
680 return 0; /* success return 0 */
681}
682
696uint8_t ina219_read_bus_voltage(ina219_handle_t *handle, uint16_t *raw, float *mV)
697{
698 uint8_t res;
699
700 if (handle == NULL) /* check handle */
701 {
702 return 2; /* return error */
703 }
704 if (handle->inited != 1) /* check handle initialization */
705 {
706 return 3; /* return error */
707 }
708
709 res = a_ina219_iic_read(handle, INA219_REG_BUS_VOLTAGE, (uint16_t *)raw); /* read bus voltage */
710 if (res != 0) /* check result */
711 {
712 handle->debug_print("ina219: read bus voltage register failed.\n"); /* read bus voltage register failed */
713
714 return 1; /* return error */
715 }
716 if (((*raw) & (1 << 0)) != 0) /* check math overflow */
717 {
718 handle->debug_print("ina219: math overflow.\n"); /* math overflow */
719
720 return 4; /* return error */
721 }
722 *raw = (*raw) >> 3; /* right shift 3 */
723 *mV = (float)(*raw) * 4.0f; /* set the converted data */
724
725 return 0; /* success return 0 */
726}
727
740uint8_t ina219_read_current(ina219_handle_t *handle, int16_t *raw, float *mA)
741{
742 uint8_t res;
743 union
744 {
745 uint16_t u;
746 int16_t s;
747 } u;
748
749 if (handle == NULL) /* check handle */
750 {
751 return 2; /* return error */
752 }
753 if (handle->inited != 1) /* check handle initialization */
754 {
755 return 3; /* return error */
756 }
757
758 res = a_ina219_iic_read(handle, INA219_REG_CURRENT, (uint16_t *)&u.u); /* read current */
759 if (res != 0) /* check result */
760 {
761 handle->debug_print("ina219: read current register failed.\n"); /* read current register failed */
762
763 return 1; /* return error */
764 }
765 *raw = u.s; /* set the raw */
766 *mA = (float)((double)(*raw) * handle->current_lsb * 1000); /* set the converted data */
767
768 return 0; /* success return 0 */
769}
770
783uint8_t ina219_read_power(ina219_handle_t *handle, uint16_t *raw, float *mW)
784{
785 uint8_t res;
786
787 if (handle == NULL) /* check handle */
788 {
789 return 2; /* return error */
790 }
791 if (handle->inited != 1) /* check handle initialization */
792 {
793 return 3; /* return error */
794 }
795
796 res = a_ina219_iic_read(handle, INA219_REG_POWER, (uint16_t *)raw); /* read power */
797 if (res != 0) /* check result */
798 {
799 handle->debug_print("ina219: read power register failed.\n"); /* read power register failed */
800
801 return 1; /* return error */
802 }
803 *mW = (float)((double)(*raw) * handle->current_lsb * 20.0 * 1000.0); /* set the converted data */
804
805 return 0; /* success return 0 */
806}
807
819uint8_t ina219_get_calibration(ina219_handle_t *handle, uint16_t *data)
820{
821 uint8_t res;
822
823 if (handle == NULL) /* check handle */
824 {
825 return 2; /* return error */
826 }
827 if (handle->inited != 1) /* check handle initialization */
828 {
829 return 3; /* return error */
830 }
831
832 res = a_ina219_iic_read(handle, INA219_REG_CALIBRATION, (uint16_t *)data); /* read calibration */
833 if (res != 0) /* check result */
834 {
835 handle->debug_print("ina219: read calibration register failed.\n"); /* read calibration register failed */
836
837 return 1; /* return error */
838 }
839
840 return 0; /* success return 0 */
841}
842
856uint8_t ina219_calculate_calibration(ina219_handle_t *handle, uint16_t *calibration)
857{
858 uint8_t res;
859 uint16_t prev;
860 uint8_t pga;
861 double v;
862
863 if (handle == NULL) /* check handle */
864 {
865 return 2; /* return error */
866 }
867 if (handle->inited != 1) /* check handle initialization */
868 {
869 return 3; /* return error */
870 }
871 if ((handle->r >= -0.000001f) && (handle->r <= 0.000001f)) /* check the r */
872 {
873 handle->debug_print("ina219: r can't be zero.\n"); /* r can't be zero */
874
875 return 4; /* return error */
876 }
877
878 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read conf */
879 if (res != 0) /* check result */
880 {
881 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
882
883 return 1; /* return error */
884 }
885 pga = (prev >> 11) & 0x3; /* get pga */
886 switch (pga) /* select the pga */
887 {
888 case 0 :
889 {
890 v = 0.04; /* 0.04 V */
891 res = 0; /* set ok */
892
893 break;
894 }
895 case 1 :
896 {
897 v = 0.08; /* 0.08 V */
898 res = 0; /* set ok */
899
900 break;
901 }
902 case 2 :
903 {
904 v = 0.16; /* 0.16 V */
905 res = 0; /* set ok */
906
907 break;
908 }
909 case 3 :
910 {
911 v = 0.32; /* 0.32 V */
912 res = 0; /* set ok */
913
914 break;
915 }
916 default :
917 {
918 v = 0.0; /* 0.0 V */
919 res = 1; /* set failed */
920
921 break;
922 }
923 }
924 if (res == 1)
925 {
926 handle->debug_print("ina219: pga is invalid.\n"); /* pga is invalid*/
927
928 return 5; /* return error */
929 }
930 else
931 {
932 handle->current_lsb = v / handle->r / pow(2.0, 15.0); /* current lsb */
933 *calibration = (uint16_t)(0.04096 / (v / pow(2.0, 15.0))); /* set calibration */
934
935 return 0; /* success return 0 */
936 }
937}
938
950uint8_t ina219_set_calibration(ina219_handle_t *handle, uint16_t data)
951{
952 uint8_t res;
953
954 if (handle == NULL) /* check handle */
955 {
956 return 2; /* return error */
957 }
958 if (handle->inited != 1) /* check handle initialization */
959 {
960 return 3; /* return error */
961 }
962
963 res = a_ina219_iic_write(handle, INA219_REG_CALIBRATION, data); /* write calibration */
964 if (res != 0) /* check result */
965 {
966 handle->debug_print("ina219: write calibration register failed.\n"); /* write calibration register failed */
967
968 return 1; /* return error */
969 }
970
971 return 0; /* success return 0 */
972}
973
986{
987 uint8_t res;
988 uint16_t prev;
989
990 if (handle == NULL) /* check handle */
991 {
992 return 2; /* return error */
993 }
994 if (handle->debug_print == NULL) /* check debug_print */
995 {
996 return 3; /* return error */
997 }
998 if (handle->iic_init == NULL) /* check iic_init */
999 {
1000 handle->debug_print("ina219: iic_init is null.\n"); /* iic_init is null */
1001
1002 return 3; /* return error */
1003 }
1004 if (handle->iic_deinit == NULL) /* check iic_deinit */
1005 {
1006 handle->debug_print("ina219: iic_deinit is null.\n"); /* iic_deinit is null */
1007
1008 return 3; /* return error */
1009 }
1010 if (handle->iic_read == NULL) /* check iic_read */
1011 {
1012 handle->debug_print("ina219: iic_read is null.\n"); /* iic_read is null */
1013
1014 return 3; /* return error */
1015 }
1016 if (handle->iic_write == NULL) /* check iic_write */
1017 {
1018 handle->debug_print("ina219: iic_write is null.\n"); /* iic_write is null */
1019
1020 return 3; /* return error */
1021 }
1022 if (handle->delay_ms == NULL) /* check delay_ms */
1023 {
1024 handle->debug_print("ina219: delay_ms is null.\n"); /* delay_ms is null */
1025
1026 return 3; /* return error */
1027 }
1028
1029 if (handle->iic_init() != 0) /* iic init */
1030 {
1031 handle->debug_print("ina219: iic init failed.\n"); /* iic init failed */
1032
1033 return 1; /* return error */
1034 }
1035 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read conf */
1036 if (res != 0) /* check result */
1037 {
1038 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
1039 (void)handle->iic_deinit(); /* iic deinit */
1040
1041 return 4; /* return error */
1042 }
1043 prev |= 1 << 15;
1044 res = a_ina219_iic_write(handle, INA219_REG_CONF, prev); /* write conf */
1045 if (res != 0) /* check result */
1046 {
1047 handle->debug_print("ina219: write conf register failed.\n"); /* write conf register failed */
1048 (void)handle->iic_deinit(); /* iic deinit */
1049
1050 return 4; /* return error */
1051 }
1052 handle->delay_ms(10); /* delay 10 ms */
1053 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read conf */
1054 if (res != 0) /* check result */
1055 {
1056 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
1057 (void)handle->iic_deinit(); /* iic deinit */
1058
1059 return 4; /* return error */
1060 }
1061 if ((prev & (1 << 15)) != 0) /* check the result */
1062 {
1063 handle->debug_print("ina219: soft reset failed.\n"); /* soft reset failed */
1064 (void)handle->iic_deinit(); /* iic deinit */
1065
1066 return 4; /* return error */
1067 }
1068
1069 handle->inited = 1; /* flag inited */
1070
1071 return 0; /* success return 0 */
1072}
1073
1086{
1087 uint8_t res;
1088 uint16_t prev;
1089
1090 if (handle == NULL) /* check handle */
1091 {
1092 return 2; /* return error */
1093 }
1094 if (handle->inited != 1) /* check handle initialization */
1095 {
1096 return 3; /* return error */
1097 }
1098
1099 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
1100 if (res != 0) /* check result */
1101 {
1102 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
1103
1104 return 4; /* return error */
1105 }
1106 prev &= ~(0x07); /* clear mode */
1107 res = a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
1108 if (res != 0) /* check result */
1109 {
1110 handle->debug_print("ina219: write conf register failed.\n"); /* write conf register failed */
1111
1112 return 4; /* return error */
1113 }
1114 res = handle->iic_deinit(); /* iic deinit */
1115 if (res != 0) /* check result */
1116 {
1117 handle->debug_print("ina219: iic deinit failed.\n"); /* iic deinit failed */
1118
1119 return 1; /* return error */
1120 }
1121
1122 return 0; /* success return 0 */
1123}
1124
1137uint8_t ina219_set_reg(ina219_handle_t *handle, uint8_t reg, uint16_t data)
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 return a_ina219_iic_write(handle, reg, data); /* write data */
1149}
1150
1163uint8_t ina219_get_reg(ina219_handle_t *handle, uint8_t reg, uint16_t *data)
1164{
1165 if (handle == NULL) /* check handle */
1166 {
1167 return 2; /* return error */
1168 }
1169 if (handle->inited != 1) /* check handle initialization */
1170 {
1171 return 3; /* return error */
1172 }
1173
1174 return a_ina219_iic_read(handle, reg, data); /* read data */
1175}
1176
1186{
1187 if (info == NULL) /* check handle */
1188 {
1189 return 2; /* return error */
1190 }
1191
1192 memset(info, 0, sizeof(ina219_info_t)); /* initialize ina219 info structure */
1193 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1194 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1195 strncpy(info->interface, "IIC", 8); /* copy interface name */
1196 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1197 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1198 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1199 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1200 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1201 info->driver_version = DRIVER_VERSION; /* set driver version */
1202
1203 return 0; /* success return 0 */
1204}
#define MAX_CURRENT
#define INA219_REG_BUS_VOLTAGE
#define INA219_REG_CONF
chip register definition
#define SUPPLY_VOLTAGE_MAX
#define INA219_REG_CALIBRATION
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define INA219_REG_POWER
#define DRIVER_VERSION
#define INA219_REG_CURRENT
#define INA219_REG_SHUNT_VOLTAGE
driver ina219 header file
uint8_t ina219_get_pga(ina219_handle_t *handle, ina219_pga_t *pga)
get the pga
uint8_t ina219_get_resistance(ina219_handle_t *handle, double *resistance)
get the resistance
uint8_t ina219_get_bus_voltage_range(ina219_handle_t *handle, ina219_bus_voltage_range_t *range)
get the bus voltage range
uint8_t ina219_soft_reset(ina219_handle_t *handle)
soft reset the chip
struct ina219_info_s ina219_info_t
ina219 information structure definition
uint8_t ina219_set_addr_pin(ina219_handle_t *handle, ina219_address_t addr_pin)
set the iic address pin
uint8_t ina219_info(ina219_info_t *info)
get chip's information
uint8_t ina219_set_shunt_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t mode)
set the shunt voltage adc mode
uint8_t ina219_get_mode(ina219_handle_t *handle, ina219_mode_t *mode)
get the mode
uint8_t ina219_get_bus_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t *mode)
get the bus voltage adc mode
ina219_adc_mode_t
ina219 adc mode enumeration definition
ina219_pga_t
ina219 pga enumeration definition
uint8_t ina219_get_shunt_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t *mode)
get the shunt voltage adc mode
uint8_t ina219_get_addr_pin(ina219_handle_t *handle, ina219_address_t *addr_pin)
get the iic address pin
uint8_t ina219_set_calibration(ina219_handle_t *handle, uint16_t data)
set the calibration
ina219_bus_voltage_range_t
ina219 bus voltage enumeration definition
uint8_t ina219_deinit(ina219_handle_t *handle)
close the chip
uint8_t ina219_get_calibration(ina219_handle_t *handle, uint16_t *data)
get the calibration
uint8_t ina219_set_mode(ina219_handle_t *handle, ina219_mode_t mode)
set the mode
uint8_t ina219_set_pga(ina219_handle_t *handle, ina219_pga_t pga)
set the pga
uint8_t ina219_set_bus_voltage_range(ina219_handle_t *handle, ina219_bus_voltage_range_t range)
set the bus voltage range
uint8_t ina219_read_bus_voltage(ina219_handle_t *handle, uint16_t *raw, float *mV)
read the bus voltage
uint8_t ina219_read_current(ina219_handle_t *handle, int16_t *raw, float *mA)
read the current
uint8_t ina219_calculate_calibration(ina219_handle_t *handle, uint16_t *calibration)
calculate the calibration
uint8_t ina219_init(ina219_handle_t *handle)
initialize the chip
uint8_t ina219_set_resistance(ina219_handle_t *handle, double resistance)
set the resistance
ina219_mode_t
ina219 mode enumeration definition
ina219_address_t
ina219 address enumeration definition
uint8_t ina219_set_bus_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t mode)
set the bus voltage adc mode
uint8_t ina219_read_shunt_voltage(ina219_handle_t *handle, int16_t *raw, float *mV)
read the shunt voltage
struct ina219_handle_s ina219_handle_t
ina219 handle structure definition
uint8_t ina219_read_power(ina219_handle_t *handle, uint16_t *raw, float *mW)
read the power
@ INA219_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
@ INA219_MODE_SHUNT_VOLTAGE_TRIGGERED
uint8_t ina219_get_reg(ina219_handle_t *handle, uint8_t reg, uint16_t *data)
get the chip register
uint8_t ina219_set_reg(ina219_handle_t *handle, uint8_t reg, uint16_t data)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]