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(1); /* delay 1 ms */
588 }
589 else
590 {
591 t = (uint8_t)(0.532 * pow(2, m - 8)) + 1; /* get time */
592 handle->delay_ms(t); /* 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
697uint8_t ina219_read_bus_voltage(ina219_handle_t *handle, uint16_t *raw, float *mV)
698{
699 uint8_t res;
700
701 if (handle == NULL) /* check handle */
702 {
703 return 2; /* return error */
704 }
705 if (handle->inited != 1) /* check handle initialization */
706 {
707 return 3; /* return error */
708 }
709
710 res = a_ina219_iic_read(handle, INA219_REG_BUS_VOLTAGE, (uint16_t *)raw); /* read bus voltage */
711 if (res != 0) /* check result */
712 {
713 handle->debug_print("ina219: read bus voltage register failed.\n"); /* read bus voltage register failed */
714
715 return 1; /* return error */
716 }
717 if (((*raw) & (1 << 0)) != 0)
718 {
719 handle->debug_print("ina219: math overflow.\n"); /* math overflow */
720
721 return 4; /* return error */
722 }
723 if (((*raw) & (1 << 1)) == 0)
724 {
725 handle->debug_print("ina219: conversion not ready.\n"); /* math overflow */
726
727 return 5; /* return error */
728 }
729 *raw = (*raw) >> 3; /* right shift 3 */
730 *mV = (float)(*raw) * 4.0f; /* set the converted data */
731
732 return 0; /* success return 0 */
733}
734
747uint8_t ina219_read_current(ina219_handle_t *handle, int16_t *raw, float *mA)
748{
749 uint8_t res;
750 union
751 {
752 uint16_t u;
753 int16_t s;
754 } u;
755
756 if (handle == NULL) /* check handle */
757 {
758 return 2; /* return error */
759 }
760 if (handle->inited != 1) /* check handle initialization */
761 {
762 return 3; /* return error */
763 }
764
765 res = a_ina219_iic_read(handle, INA219_REG_CURRENT, (uint16_t *)&u.u); /* read current */
766 if (res != 0) /* check result */
767 {
768 handle->debug_print("ina219: read current register failed.\n"); /* read current register failed */
769
770 return 1; /* return error */
771 }
772 *raw = u.s; /* set the raw */
773 *mA = (float)((double)(*raw) * handle->current_lsb * 1000); /* set the converted data */
774
775 return 0; /* success return 0 */
776}
777
790uint8_t ina219_read_power(ina219_handle_t *handle, uint16_t *raw, float *mW)
791{
792 uint8_t res;
793
794 if (handle == NULL) /* check handle */
795 {
796 return 2; /* return error */
797 }
798 if (handle->inited != 1) /* check handle initialization */
799 {
800 return 3; /* return error */
801 }
802
803 res = a_ina219_iic_read(handle, INA219_REG_POWER, (uint16_t *)raw); /* read power */
804 if (res != 0) /* check result */
805 {
806 handle->debug_print("ina219: read power register failed.\n"); /* read power register failed */
807
808 return 1; /* return error */
809 }
810 *mW = (float)((double)(*raw) * handle->current_lsb * 20.0 * 1000.0); /* set the converted data */
811
812 return 0; /* success return 0 */
813}
814
826uint8_t ina219_get_calibration(ina219_handle_t *handle, uint16_t *data)
827{
828 uint8_t res;
829
830 if (handle == NULL) /* check handle */
831 {
832 return 2; /* return error */
833 }
834 if (handle->inited != 1) /* check handle initialization */
835 {
836 return 3; /* return error */
837 }
838
839 res = a_ina219_iic_read(handle, INA219_REG_CALIBRATION, (uint16_t *)data); /* read calibration */
840 if (res != 0) /* check result */
841 {
842 handle->debug_print("ina219: read calibration register failed.\n"); /* read calibration register failed */
843
844 return 1; /* return error */
845 }
846
847 return 0; /* success return 0 */
848}
849
863uint8_t ina219_calculate_calibration(ina219_handle_t *handle, uint16_t *calibration)
864{
865 uint8_t res;
866 uint16_t prev;
867 uint8_t pga;
868 double v;
869
870 if (handle == NULL) /* check handle */
871 {
872 return 2; /* return error */
873 }
874 if (handle->inited != 1) /* check handle initialization */
875 {
876 return 3; /* return error */
877 }
878 if ((handle->r >= -0.000001f) && (handle->r <= 0.000001f)) /* check the r */
879 {
880 handle->debug_print("ina219: r can't be zero.\n"); /* r can't be zero */
881
882 return 4; /* return error */
883 }
884
885 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read conf */
886 if (res != 0) /* check result */
887 {
888 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
889
890 return 1; /* return error */
891 }
892 pga = (prev >> 11) & 0x3; /* get pga */
893 switch (pga) /* select the pga */
894 {
895 case 0 :
896 {
897 v = 0.04; /* 0.04 V */
898 res = 0; /* set ok */
899
900 break;
901 }
902 case 1 :
903 {
904 v = 0.08; /* 0.08 V */
905 res = 0; /* set ok */
906
907 break;
908 }
909 case 2 :
910 {
911 v = 0.16; /* 0.16 V */
912 res = 0; /* set ok */
913
914 break;
915 }
916 case 3 :
917 {
918 v = 0.32; /* 0.32 V */
919 res = 0; /* set ok */
920
921 break;
922 }
923 default :
924 {
925 v = 0.0; /* 0.0 V */
926 res = 1; /* set failed */
927
928 break;
929 }
930 }
931 if (res == 1)
932 {
933 handle->debug_print("ina219: pga is invalid.\n"); /* pga is invalid*/
934
935 return 5; /* return error */
936 }
937 else
938 {
939 handle->current_lsb = v / handle->r / pow(2.0, 15.0); /* current lsb */
940 *calibration = (uint16_t)(0.04096 / (v / pow(2.0, 15.0))); /* set calibration */
941
942 return 0; /* success return 0 */
943 }
944}
945
957uint8_t ina219_set_calibration(ina219_handle_t *handle, uint16_t data)
958{
959 uint8_t res;
960
961 if (handle == NULL) /* check handle */
962 {
963 return 2; /* return error */
964 }
965 if (handle->inited != 1) /* check handle initialization */
966 {
967 return 3; /* return error */
968 }
969
970 res = a_ina219_iic_write(handle, INA219_REG_CALIBRATION, data); /* write calibration */
971 if (res != 0) /* check result */
972 {
973 handle->debug_print("ina219: write calibration register failed.\n"); /* write calibration register failed */
974
975 return 1; /* return error */
976 }
977
978 return 0; /* success return 0 */
979}
980
993{
994 uint8_t res;
995 uint16_t prev;
996
997 if (handle == NULL) /* check handle */
998 {
999 return 2; /* return error */
1000 }
1001 if (handle->debug_print == NULL) /* check debug_print */
1002 {
1003 return 3; /* return error */
1004 }
1005 if (handle->iic_init == NULL) /* check iic_init */
1006 {
1007 handle->debug_print("ina219: iic_init is null.\n"); /* iic_init is null */
1008
1009 return 3; /* return error */
1010 }
1011 if (handle->iic_deinit == NULL) /* check iic_deinit */
1012 {
1013 handle->debug_print("ina219: iic_deinit is null.\n"); /* iic_deinit is null */
1014
1015 return 3; /* return error */
1016 }
1017 if (handle->iic_read == NULL) /* check iic_read */
1018 {
1019 handle->debug_print("ina219: iic_read is null.\n"); /* iic_read is null */
1020
1021 return 3; /* return error */
1022 }
1023 if (handle->iic_write == NULL) /* check iic_write */
1024 {
1025 handle->debug_print("ina219: iic_write is null.\n"); /* iic_write is null */
1026
1027 return 3; /* return error */
1028 }
1029 if (handle->delay_ms == NULL) /* check delay_ms */
1030 {
1031 handle->debug_print("ina219: delay_ms is null.\n"); /* delay_ms is null */
1032
1033 return 3; /* return error */
1034 }
1035
1036 if (handle->iic_init() != 0) /* iic init */
1037 {
1038 handle->debug_print("ina219: iic init failed.\n"); /* iic init failed */
1039
1040 return 1; /* return error */
1041 }
1042 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read conf */
1043 if (res != 0) /* check result */
1044 {
1045 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
1046 (void)handle->iic_deinit(); /* iic deinit */
1047
1048 return 4; /* return error */
1049 }
1050 prev |= 1 << 15;
1051 res = a_ina219_iic_write(handle, INA219_REG_CONF, prev); /* write conf */
1052 if (res != 0) /* check result */
1053 {
1054 handle->debug_print("ina219: write conf register failed.\n"); /* write conf register failed */
1055 (void)handle->iic_deinit(); /* iic deinit */
1056
1057 return 4; /* return error */
1058 }
1059 handle->delay_ms(10); /* delay 10 ms */
1060 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read conf */
1061 if (res != 0) /* check result */
1062 {
1063 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
1064 (void)handle->iic_deinit(); /* iic deinit */
1065
1066 return 4; /* return error */
1067 }
1068 if ((prev & (1 << 15)) != 0) /* check the result */
1069 {
1070 handle->debug_print("ina219: soft reset failed.\n"); /* soft reset failed */
1071 (void)handle->iic_deinit(); /* iic deinit */
1072
1073 return 4; /* return error */
1074 }
1075
1076 handle->inited = 1; /* flag inited */
1077
1078 return 0; /* success return 0 */
1079}
1080
1093{
1094 uint8_t res;
1095 uint16_t prev;
1096
1097 if (handle == NULL) /* check handle */
1098 {
1099 return 2; /* return error */
1100 }
1101 if (handle->inited != 1) /* check handle initialization */
1102 {
1103 return 3; /* return error */
1104 }
1105
1106 res = a_ina219_iic_read(handle, INA219_REG_CONF, (uint16_t *)&prev); /* read config */
1107 if (res != 0) /* check result */
1108 {
1109 handle->debug_print("ina219: read conf register failed.\n"); /* read conf register failed */
1110
1111 return 4; /* return error */
1112 }
1113 prev &= ~(0x07); /* clear mode */
1114 res = a_ina219_iic_write(handle, INA219_REG_CONF, (uint16_t )prev); /* write config */
1115 if (res != 0) /* check result */
1116 {
1117 handle->debug_print("ina219: write conf register failed.\n"); /* write conf register failed */
1118
1119 return 4; /* return error */
1120 }
1121 res = handle->iic_deinit(); /* iic deinit */
1122 if (res != 0) /* check result */
1123 {
1124 handle->debug_print("ina219: iic deinit failed.\n"); /* iic deinit failed */
1125
1126 return 1; /* return error */
1127 }
1128
1129 return 0; /* success return 0 */
1130}
1131
1144uint8_t ina219_set_reg(ina219_handle_t *handle, uint8_t reg, uint16_t data)
1145{
1146 if (handle == NULL) /* check handle */
1147 {
1148 return 2; /* return error */
1149 }
1150 if (handle->inited != 1) /* check handle initialization */
1151 {
1152 return 3; /* return error */
1153 }
1154
1155 return a_ina219_iic_write(handle, reg, data); /* write data */
1156}
1157
1170uint8_t ina219_get_reg(ina219_handle_t *handle, uint8_t reg, uint16_t *data)
1171{
1172 if (handle == NULL) /* check handle */
1173 {
1174 return 2; /* return error */
1175 }
1176 if (handle->inited != 1) /* check handle initialization */
1177 {
1178 return 3; /* return error */
1179 }
1180
1181 return a_ina219_iic_read(handle, reg, data); /* read data */
1182}
1183
1193{
1194 if (info == NULL) /* check handle */
1195 {
1196 return 2; /* return error */
1197 }
1198
1199 memset(info, 0, sizeof(ina219_info_t)); /* initialize ina219 info structure */
1200 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1201 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1202 strncpy(info->interface, "IIC", 8); /* copy interface name */
1203 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1204 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1205 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1206 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1207 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1208 info->driver_version = DRIVER_VERSION; /* set driver version */
1209
1210 return 0; /* success return 0 */
1211}
#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]