LibDriver INA226  1.0.0
INA226 full-featured driver
driver_ina226.c
Go to the documentation of this file.
1 
37 #include "driver_ina226.h"
38 #include <math.h>
39 
43 #define CHIP_NAME "Texas Instruments INA226"
44 #define MANUFACTURER_NAME "Texas Instruments"
45 #define SUPPLY_VOLTAGE_MIN 2.7f
46 #define SUPPLY_VOLTAGE_MAX 5.5f
47 #define MAX_CURRENT 0.33f
48 #define TEMPERATURE_MIN -40.0f
49 #define TEMPERATURE_MAX 125.0f
50 #define DRIVER_VERSION 1000
55 #define INA226_REG_CONF 0x00
56 #define INA226_REG_SHUNT_VOLTAGE 0x01
57 #define INA226_REG_BUS_VOLTAGE 0x02
58 #define INA226_REG_POWER 0x03
59 #define INA226_REG_CURRENT 0x04
60 #define INA226_REG_CALIBRATION 0x05
61 #define INA226_REG_MASK 0x06
62 #define INA226_REG_ALERT_LIMIT 0x07
63 #define INA226_REG_MANUFACTURER 0xFE
64 #define INA226_REG_DIE 0xFF
76 static uint8_t a_ina226_iic_read(ina226_handle_t *handle, uint8_t reg, uint16_t *data)
77 {
78  uint8_t buf[2];
79 
80  memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
81  if (handle->iic_read(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* read data */
82  {
83  return 1; /* return error */
84  }
85  else
86  {
87  *data = (uint16_t)buf[0] << 8 | buf[1]; /* get data */
88 
89  return 0; /* success return 0 */
90  }
91 }
92 
103 static uint8_t a_ina226_iic_write(ina226_handle_t *handle, uint8_t reg, uint16_t data)
104 {
105  uint8_t buf[2];
106 
107  buf[0] = (uint8_t)((data >> 8) & 0xFF); /* get MSB */
108  buf[1] = (uint8_t)((data >> 0) & 0xFF); /* get LSB */
109  if (handle->iic_write(handle->iic_addr, reg, (uint8_t *)buf, 2) != 0) /* write data */
110  {
111  return 1; /* return error */
112  }
113  else
114  {
115  return 0; /* success return 0 */
116  }
117 }
118 
128 uint8_t ina226_set_resistance(ina226_handle_t *handle, double resistance)
129 {
130  if (handle == NULL) /* check handle */
131  {
132  return 2; /* return error */
133  }
134 
135  handle->r = resistance; /* set resistance */
136 
137  return 0; /* success return 0 */
138 }
139 
149 uint8_t ina226_get_resistance(ina226_handle_t *handle, double *resistance)
150 {
151  if (handle == NULL) /* check handle */
152  {
153  return 2; /* return error */
154  }
155 
156  *resistance = handle->r; /* get resistance */
157 
158  return 0; /* success return 0 */
159 }
160 
171 {
172  if (handle == NULL) /* check handle */
173  {
174  return 2; /* return error */
175  }
176 
177  handle->iic_addr = (uint8_t)addr_pin; /* set pin */
178 
179  return 0; /* success return 0 */
180 }
181 
192 {
193  if (handle == NULL) /* check handle */
194  {
195  return 2; /* return error */
196  }
197 
198  *addr_pin = (ina226_address_t)(handle->iic_addr); /* get pin */
199 
200  return 0; /* success return 0 */
201 }
202 
214 {
215  uint8_t res;
216  uint16_t prev;
217 
218  if (handle == NULL) /* check handle */
219  {
220  return 2; /* return error */
221  }
222  if (handle->inited != 1) /* check handle initialization */
223  {
224  return 3; /* return error */
225  }
226 
227  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
228  if (res != 0) /* check result */
229  {
230  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
231 
232  return 1; /* return error */
233  }
234  prev &= ~(1 << 15); /* clear soft reset */
235  prev |= 1 << 15; /* set soft reset */
236 
237  return a_ina226_iic_write(handle, INA226_REG_CONF, (uint16_t )prev); /* write config */
238 }
239 
252 {
253  uint8_t res;
254  uint16_t prev;
255 
256  if (handle == NULL) /* check handle */
257  {
258  return 2; /* return error */
259  }
260  if (handle->inited != 1) /* check handle initialization */
261  {
262  return 3; /* return error */
263  }
264 
265  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
266  if (res != 0) /* check result */
267  {
268  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
269 
270  return 1; /* return error */
271  }
272  prev &= ~(0x7 << 9); /* clear mode bits */
273  prev |= mode << 9; /* set mode bits */
274 
275  return a_ina226_iic_write(handle, INA226_REG_CONF, (uint16_t )prev); /* write config */
276 }
277 
290 {
291  uint8_t res;
292  uint16_t prev;
293 
294  if (handle == NULL) /* check handle */
295  {
296  return 2; /* return error */
297  }
298  if (handle->inited != 1) /* check handle initialization */
299  {
300  return 3; /* return error */
301  }
302 
303  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
304  if (res != 0) /* check result */
305  {
306  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
307 
308  return 1; /* return error */
309  }
310  *mode = (ina226_avg_t)((prev >> 9) & 0x07); /* get mode */
311 
312  return 0; /* success return 0 */
313 }
314 
327 {
328  uint8_t res;
329  uint16_t prev;
330 
331  if (handle == NULL) /* check handle */
332  {
333  return 2; /* return error */
334  }
335  if (handle->inited != 1) /* check handle initialization */
336  {
337  return 3; /* return error */
338  }
339 
340  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
341  if (res != 0) /* check result */
342  {
343  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
344 
345  return 1; /* return error */
346  }
347  prev &= ~(7 << 6); /* clear time bits */
348  prev |= t << 6; /* set time bits */
349 
350  return a_ina226_iic_write(handle, INA226_REG_CONF, (uint16_t )prev); /* write config */
351 }
352 
365 {
366  uint8_t res;
367  uint16_t prev;
368 
369  if (handle == NULL) /* check handle */
370  {
371  return 2; /* return error */
372  }
373  if (handle->inited != 1) /* check handle initialization */
374  {
375  return 3; /* return error */
376  }
377 
378  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
379  if (res != 0) /* check result */
380  {
381  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
382 
383  return 1; /* return error */
384  }
385  *t = (ina226_conversion_time_t)((prev >> 6) & 0x07); /* get time */
386 
387  return 0; /* success return 0 */
388 }
389 
402 {
403  uint8_t res;
404  uint16_t prev;
405 
406  if (handle == NULL) /* check handle */
407  {
408  return 2; /* return error */
409  }
410  if (handle->inited != 1) /* check handle initialization */
411  {
412  return 3; /* return error */
413  }
414 
415  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
416  if (res != 0) /* check result */
417  {
418  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
419 
420  return 1; /* return error */
421  }
422  prev &= ~(7 << 3); /* clear time bits */
423  prev |= t << 3; /* set time bits */
424 
425  return a_ina226_iic_write(handle, INA226_REG_CONF, (uint16_t )prev); /* write config */
426 }
427 
440 {
441  uint8_t res;
442  uint16_t prev;
443 
444  if (handle == NULL) /* check handle */
445  {
446  return 2; /* return error */
447  }
448  if (handle->inited != 1) /* check handle initialization */
449  {
450  return 3; /* return error */
451  }
452 
453  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
454  if (res != 0) /* check result */
455  {
456  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
457 
458  return 1; /* return error */
459  }
460  *t = (ina226_conversion_time_t)((prev >> 3) & 0x07); /* get time */
461 
462  return 0; /* success return 0 */
463 }
464 
477 {
478  uint8_t res;
479  uint16_t prev;
480 
481  if (handle == NULL) /* check handle */
482  {
483  return 2; /* return error */
484  }
485  if (handle->inited != 1) /* check handle initialization */
486  {
487  return 3; /* return error */
488  }
489 
490  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
491  if (res != 0) /* check result */
492  {
493  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
494 
495  return 1; /* return error */
496  }
497  prev &= ~(0x7 << 0); /* clear mode bit */
498  prev |= mode << 0; /* set mode bit */
499  res = a_ina226_iic_write(handle, INA226_REG_CONF, (uint16_t )prev); /* write config */
500  if (res != 0) /* check result */
501  {
502  handle->debug_print("ina226: write conf register failed.\n"); /* write conf register failed */
503 
504  return 1; /* return error */
505  }
506  if ((mode == INA226_MODE_SHUNT_VOLTAGE_TRIGGERED) ||
508  (mode == INA226_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED)) /* check triggered mode */
509  {
510  handle->trigger = 1; /* set 1 */
511  }
512  else
513  {
514  handle->trigger = 0; /* set 0 */
515  }
516 
517  return 0; /* success return 0 */
518 }
519 
532 {
533  uint8_t res;
534  uint16_t prev;
535 
536  if (handle == NULL) /* check handle */
537  {
538  return 2; /* return error */
539  }
540  if (handle->inited != 1) /* check handle initialization */
541  {
542  return 3; /* return error */
543  }
544 
545  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
546  if (res != 0) /* check result */
547  {
548  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
549 
550  return 1; /* return error */
551  }
552  *mode = (ina226_mode_t)((prev >> 0) & 0x7); /* get mode */
553 
554  return 0; /* success return 0 */
555 }
556 
569 uint8_t ina226_get_die_id(ina226_handle_t *handle, uint16_t *device_id, uint8_t *die_revision_id)
570 {
571  uint8_t res;
572  uint16_t prev;
573 
574  if (handle == NULL) /* check handle */
575  {
576  return 2; /* return error */
577  }
578  if (handle->inited != 1) /* check handle initialization */
579  {
580  return 3; /* return error */
581  }
582 
583  res = a_ina226_iic_read(handle, INA226_REG_DIE, (uint16_t *)&prev); /* read config */
584  if (res != 0) /* check result */
585  {
586  handle->debug_print("ina226: read die register failed.\n"); /* read die register failed */
587 
588  return 1; /* return error */
589  }
590  *device_id = (prev >> 4) & 0xFFF; /* get device id */
591  *die_revision_id = prev & 0xF; /* get die revision id */
592 
593  return 0; /* success return 0 */
594 }
595 
610 uint8_t ina226_read_shunt_voltage(ina226_handle_t *handle, int16_t *raw, float *mV)
611 {
612  uint8_t res;
613  union
614  {
615  uint16_t u;
616  int16_t s;
617  } u;
618  uint16_t prev;
619 
620  if (handle == NULL) /* check handle */
621  {
622  return 2; /* return error */
623  }
624  if (handle->inited != 1) /* check handle initialization */
625  {
626  return 3; /* return error */
627  }
628 
629  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
630  if (res != 0) /* check result */
631  {
632  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
633 
634  return 1; /* return error */
635  }
636  if ((prev & (1 << 2)) != 0) /* check math overflow */
637  {
638  handle->debug_print("ina226: math overflow.\n"); /* math overflow */
639 
640  return 4; /* return error */
641  }
642  if (handle->trigger != 0) /* trigger mode */
643  {
644  uint16_t i;
645  uint16_t timeout;
646 
647  if ((prev & (1 << 3)) == 0) /* check last mask conversion ready flag */
648  {
649  timeout = INA226_READ_TIMEOUT; /* set timeout */
650  for (i = 0; i< timeout; i++) /* loop all */
651  {
652  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
653  if (res != 0) /* check result */
654  {
655  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
656 
657  return 1; /* return error */
658  }
659  if ((prev & (1 << 3)) != 0) /* check conversion ready flag */
660  {
661  break; /* break */
662  }
663  handle->delay_ms(1); /* delay 1ms */
664  timeout--; /* timeout-- */
665  }
666  if (timeout == 0) /* check timeout */
667  {
668  handle->debug_print("ina226: read timeout.\n"); /* timeout */
669 
670  return 5; /* return error */
671  }
672  }
673  handle->trigger = 0; /* set 0 */
674  }
675 
676  res = a_ina226_iic_read(handle, INA226_REG_SHUNT_VOLTAGE, (uint16_t *)&u.u); /* read shunt voltage */
677  if (res != 0) /* check result */
678  {
679  handle->debug_print("ina226: read shunt voltage register failed.\n"); /* read shunt voltage register failed */
680 
681  return 1; /* return error */
682  }
683  *raw = u.s; /* set the raw */
684  *mV = (float)(*raw) / 400.0f; /* set the converted data */
685 
686  return 0; /* success return 0 */
687 }
688 
703 uint8_t ina226_read_bus_voltage(ina226_handle_t *handle, uint16_t *raw, float *mV)
704 {
705  uint8_t res;
706  uint16_t prev;
707 
708  if (handle == NULL) /* check handle */
709  {
710  return 2; /* return error */
711  }
712  if (handle->inited != 1) /* check handle initialization */
713  {
714  return 3; /* return error */
715  }
716 
717  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
718  if (res != 0) /* check result */
719  {
720  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
721 
722  return 1; /* return error */
723  }
724  if ((prev & (1 << 2)) != 0) /* check math overflow */
725  {
726  handle->debug_print("ina226: math overflow.\n"); /* math overflow */
727 
728  return 4; /* return error */
729  }
730  if (handle->trigger != 0) /* trigger mode */
731  {
732  uint16_t i;
733  uint16_t timeout;
734 
735  if ((prev & (1 << 3)) == 0) /* check last mask conversion ready flag */
736  {
737  timeout = INA226_READ_TIMEOUT; /* set timeout */
738  for (i = 0; i< timeout; i++) /* loop all */
739  {
740  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
741  if (res != 0) /* check result */
742  {
743  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
744 
745  return 1; /* return error */
746  }
747  if ((prev & (1 << 3)) != 0) /* check conversion ready flag */
748  {
749  break; /* break */
750  }
751  handle->delay_ms(1); /* delay 1ms */
752  timeout--; /* timeout-- */
753  }
754  if (timeout == 0) /* check timeout */
755  {
756  handle->debug_print("ina226: read timeout.\n"); /* timeout */
757 
758  return 5; /* return error */
759  }
760  }
761  handle->trigger = 0; /* set 0 */
762  }
763 
764  res = a_ina226_iic_read(handle, INA226_REG_BUS_VOLTAGE, (uint16_t *)raw); /* read bus voltage */
765  if (res != 0) /* check result */
766  {
767  handle->debug_print("ina226: read bus voltage register failed.\n"); /* read bus voltage register failed */
768 
769  return 1; /* return error */
770  }
771  *mV = (float)(*raw) * 1.25f; /* set the converted data */
772 
773  return 0; /* success return 0 */
774 }
775 
790 uint8_t ina226_read_current(ina226_handle_t *handle, int16_t *raw, float *mA)
791 {
792  uint8_t res;
793  union
794  {
795  uint16_t u;
796  int16_t s;
797  } u;
798  uint16_t prev;
799 
800  if (handle == NULL) /* check handle */
801  {
802  return 2; /* return error */
803  }
804  if (handle->inited != 1) /* check handle initialization */
805  {
806  return 3; /* return error */
807  }
808 
809  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
810  if (res != 0) /* check result */
811  {
812  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
813 
814  return 1; /* return error */
815  }
816  if ((prev & (1 << 2)) != 0) /* check math overflow */
817  {
818  handle->debug_print("ina226: math overflow.\n"); /* math overflow */
819 
820  return 4; /* return error */
821  }
822  if (handle->trigger != 0) /* trigger mode */
823  {
824  uint16_t i;
825  uint16_t timeout;
826 
827  if ((prev & (1 << 3)) == 0) /* check last mask conversion ready flag */
828  {
829  timeout = INA226_READ_TIMEOUT; /* set timeout */
830  for (i = 0; i< timeout; i++) /* loop all */
831  {
832  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
833  if (res != 0) /* check result */
834  {
835  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
836 
837  return 1; /* return error */
838  }
839  if ((prev & (1 << 3)) != 0) /* check conversion ready flag */
840  {
841  break; /* break */
842  }
843  handle->delay_ms(1); /* delay 1ms */
844  timeout--; /* timeout-- */
845  }
846  if (timeout == 0) /* check timeout */
847  {
848  handle->debug_print("ina226: read timeout.\n"); /* timeout */
849 
850  return 5; /* return error */
851  }
852  }
853  handle->trigger = 0; /* set 0 */
854  }
855 
856  res = a_ina226_iic_read(handle, INA226_REG_CURRENT, (uint16_t *)&u.u); /* read current */
857  if (res != 0) /* check result */
858  {
859  handle->debug_print("ina226: read current register failed.\n"); /* read current register failed */
860 
861  return 1; /* return error */
862  }
863  *raw = u.s; /* set the raw */
864  *mA = (float)((double)(*raw) * handle->current_lsb * 1000); /* set the converted data */
865 
866  return 0; /* success return 0 */
867 }
868 
883 uint8_t ina226_read_power(ina226_handle_t *handle, uint16_t *raw, float *mW)
884 {
885  uint8_t res;
886  uint16_t prev;
887 
888  if (handle == NULL) /* check handle */
889  {
890  return 2; /* return error */
891  }
892  if (handle->inited != 1) /* check handle initialization */
893  {
894  return 3; /* return error */
895  }
896 
897  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
898  if (res != 0) /* check result */
899  {
900  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
901 
902  return 1; /* return error */
903  }
904  if ((prev & (1 << 2)) != 0) /* check math overflow */
905  {
906  handle->debug_print("ina226: math overflow.\n"); /* math overflow */
907 
908  return 4; /* return error */
909  }
910  if (handle->trigger != 0) /* trigger mode */
911  {
912  uint16_t i;
913  uint16_t timeout;
914 
915  if ((prev & (1 << 3)) == 0) /* check last mask conversion ready flag */
916  {
917  timeout = INA226_READ_TIMEOUT; /* set timeout */
918  for (i = 0; i< timeout; i++) /* loop all */
919  {
920  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
921  if (res != 0) /* check result */
922  {
923  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
924 
925  return 1; /* return error */
926  }
927  if ((prev & (1 << 3)) != 0) /* check conversion ready flag */
928  {
929  break; /* break */
930  }
931  handle->delay_ms(1); /* delay 1ms */
932  timeout--; /* timeout-- */
933  }
934  if (timeout == 0) /* check timeout */
935  {
936  handle->debug_print("ina226: read timeout.\n"); /* timeout */
937 
938  return 5; /* return error */
939  }
940  }
941  handle->trigger = 0; /* set 0 */
942  }
943 
944  res = a_ina226_iic_read(handle, INA226_REG_POWER, (uint16_t *)raw); /* read power */
945  if (res != 0) /* check result */
946  {
947  handle->debug_print("ina226: read power register failed.\n"); /* read power register failed */
948 
949  return 1; /* return error */
950  }
951  *mW = (float)((double)(*raw) * handle->current_lsb * 25.0 * 1000.0); /* set the converted data */
952 
953  return 0; /* success return 0 */
954 }
955 
967 uint8_t ina226_get_calibration(ina226_handle_t *handle, uint16_t *data)
968 {
969  uint8_t res;
970 
971  if (handle == NULL) /* check handle */
972  {
973  return 2; /* return error */
974  }
975  if (handle->inited != 1) /* check handle initialization */
976  {
977  return 3; /* return error */
978  }
979 
980  res = a_ina226_iic_read(handle, INA226_REG_CALIBRATION, (uint16_t *)data); /* read calibration */
981  if (res != 0) /* check result */
982  {
983  handle->debug_print("ina226: read calibration register failed.\n"); /* read calibration register failed */
984 
985  return 1; /* return error */
986  }
987 
988  return 0; /* success return 0 */
989 }
990 
1003 uint8_t ina226_calculate_calibration(ina226_handle_t *handle, uint16_t *calibration)
1004 {
1005  double v;
1006 
1007  if (handle == NULL) /* check handle */
1008  {
1009  return 2; /* return error */
1010  }
1011  if (handle->inited != 1) /* check handle initialization */
1012  {
1013  return 3; /* return error */
1014  }
1015  if ((handle->r >= -0.000001f) && (handle->r <= 0.000001f)) /* check the r */
1016  {
1017  handle->debug_print("ina226: r can't be zero.\n"); /* r can't be zero */
1018 
1019  return 4; /* return error */
1020  }
1021 
1022  v = 0.08192; /* set max range */
1023  handle->current_lsb = v / handle->r / pow(2.0, 15.0); /* current lsb */
1024  *calibration = (uint16_t)(0.00512 / (v / pow(2.0, 15.0))); /* set calibration */
1025 
1026  return 0; /* success return 0 */
1027 }
1028 
1040 uint8_t ina226_set_calibration(ina226_handle_t *handle, uint16_t data)
1041 {
1042  uint8_t res;
1043 
1044  if (handle == NULL) /* check handle */
1045  {
1046  return 2; /* return error */
1047  }
1048  if (handle->inited != 1) /* check handle initialization */
1049  {
1050  return 3; /* return error */
1051  }
1052 
1053  res = a_ina226_iic_write(handle, INA226_REG_CALIBRATION, data); /* write calibration */
1054  if (res != 0) /* check result */
1055  {
1056  handle->debug_print("ina226: write calibration register failed.\n"); /* write calibration register failed */
1057 
1058  return 1; /* return error */
1059  }
1060 
1061  return 0; /* success return 0 */
1062 }
1063 
1077 {
1078  uint8_t res;
1079  uint16_t prev;
1080 
1081  if (handle == NULL) /* check handle */
1082  {
1083  return 2; /* return error */
1084  }
1085  if (handle->inited != 1) /* check handle initialization */
1086  {
1087  return 3; /* return error */
1088  }
1089 
1090  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1091  if (res != 0) /* check result */
1092  {
1093  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1094 
1095  return 1; /* return error */
1096  }
1097  prev &= ~(1 << mask); /* clear settings */
1098  prev |= (enable << mask); /* set mask */
1099  res = a_ina226_iic_write(handle, INA226_REG_MASK, prev); /* write mask */
1100  if (res != 0) /* check result */
1101  {
1102  handle->debug_print("ina226: write mask register failed.\n"); /* write mask register failed */
1103 
1104  return 1; /* return error */
1105  }
1106 
1107  return 0; /* success return 0 */
1108 }
1109 
1123 {
1124  uint8_t res;
1125  uint16_t prev;
1126 
1127  if (handle == NULL) /* check handle */
1128  {
1129  return 2; /* return error */
1130  }
1131  if (handle->inited != 1) /* check handle initialization */
1132  {
1133  return 3; /* return error */
1134  }
1135 
1136  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1137  if (res != 0) /* check result */
1138  {
1139  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1140 
1141  return 1; /* return error */
1142  }
1143  *enable = (ina226_bool_t)((prev >> mask) & 0x01); /* get bool */
1144 
1145  return 0; /* success return 0 */
1146 }
1147 
1160 {
1161  uint8_t res;
1162  uint16_t prev;
1163 
1164  if (handle == NULL) /* check handle */
1165  {
1166  return 2; /* return error */
1167  }
1168  if (handle->inited != 1) /* check handle initialization */
1169  {
1170  return 3; /* return error */
1171  }
1172 
1173  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1174  if (res != 0) /* check result */
1175  {
1176  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1177 
1178  return 1; /* return error */
1179  }
1180  prev &= ~(1 << 10); /* clear settings */
1181  prev |= (enable << 10); /* set mask */
1182  res = a_ina226_iic_write(handle, INA226_REG_MASK, prev); /* write mask */
1183  if (res != 0) /* check result */
1184  {
1185  handle->debug_print("ina226: write mask register failed.\n"); /* write mask register failed */
1186 
1187  return 1; /* return error */
1188  }
1189 
1190  return 0; /* success return 0 */
1191 }
1192 
1205 {
1206  uint8_t res;
1207  uint16_t prev;
1208 
1209  if (handle == NULL) /* check handle */
1210  {
1211  return 2; /* return error */
1212  }
1213  if (handle->inited != 1) /* check handle initialization */
1214  {
1215  return 3; /* return error */
1216  }
1217 
1218  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1219  if (res != 0) /* check result */
1220  {
1221  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1222 
1223  return 1; /* return error */
1224  }
1225  *enable = (ina226_bool_t)((prev >> 10) & 0x01); /* get bool */
1226 
1227  return 0; /* success return 0 */
1228 }
1229 
1242 {
1243  uint8_t res;
1244  uint16_t prev;
1245 
1246  if (handle == NULL) /* check handle */
1247  {
1248  return 2; /* return error */
1249  }
1250  if (handle->inited != 1) /* check handle initialization */
1251  {
1252  return 3; /* return error */
1253  }
1254 
1255  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1256  if (res != 0) /* check result */
1257  {
1258  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1259 
1260  return 1; /* return error */
1261  }
1262  prev &= ~(1 << 1); /* clear settings */
1263  prev |= (pin << 1); /* set mask */
1264  res = a_ina226_iic_write(handle, INA226_REG_MASK, prev); /* write mask */
1265  if (res != 0) /* check result */
1266  {
1267  handle->debug_print("ina226: write mask register failed.\n"); /* write mask register failed */
1268 
1269  return 1; /* return error */
1270  }
1271 
1272  return 0; /* success return 0 */
1273 }
1274 
1287 {
1288  uint8_t res;
1289  uint16_t prev;
1290 
1291  if (handle == NULL) /* check handle */
1292  {
1293  return 2; /* return error */
1294  }
1295  if (handle->inited != 1) /* check handle initialization */
1296  {
1297  return 3; /* return error */
1298  }
1299 
1300  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1301  if (res != 0) /* check result */
1302  {
1303  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1304 
1305  return 1; /* return error */
1306  }
1307  *pin = (ina226_alert_polarity_t)((prev >> 1) & 0x01); /* get alert polarity pin */
1308 
1309  return 0; /* success return 0 */
1310 }
1311 
1324 {
1325  uint8_t res;
1326  uint16_t prev;
1327 
1328  if (handle == NULL) /* check handle */
1329  {
1330  return 2; /* return error */
1331  }
1332  if (handle->inited != 1) /* check handle initialization */
1333  {
1334  return 3; /* return error */
1335  }
1336 
1337  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1338  if (res != 0) /* check result */
1339  {
1340  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1341 
1342  return 1; /* return error */
1343  }
1344  prev &= ~(1 << 0); /* clear settings */
1345  prev |= (enable << 0); /* set mask */
1346  res = a_ina226_iic_write(handle, INA226_REG_MASK, prev); /* write mask */
1347  if (res != 0) /* check result */
1348  {
1349  handle->debug_print("ina226: write mask register failed.\n"); /* write mask register failed */
1350 
1351  return 1; /* return error */
1352  }
1353 
1354  return 0; /* success return 0 */
1355 }
1356 
1369 {
1370  uint8_t res;
1371  uint16_t prev;
1372 
1373  if (handle == NULL) /* check handle */
1374  {
1375  return 2; /* return error */
1376  }
1377  if (handle->inited != 1) /* check handle initialization */
1378  {
1379  return 3; /* return error */
1380  }
1381 
1382  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1383  if (res != 0) /* check result */
1384  {
1385  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1386 
1387  return 1; /* return error */
1388  }
1389  *enable = (ina226_bool_t)((prev >> 0) & 0x01); /* get bool */
1390 
1391  return 0; /* success return 0 */
1392 }
1393 
1405 uint8_t ina226_set_alert_limit(ina226_handle_t *handle, uint16_t reg)
1406 {
1407  uint8_t res;
1408 
1409  if (handle == NULL) /* check handle */
1410  {
1411  return 2; /* return error */
1412  }
1413  if (handle->inited != 1) /* check handle initialization */
1414  {
1415  return 3; /* return error */
1416  }
1417 
1418  res = a_ina226_iic_write(handle, INA226_REG_ALERT_LIMIT, reg); /* write config */
1419  if (res != 0) /* check result */
1420  {
1421  handle->debug_print("ina226: write alert limit register failed.\n"); /* write alert limit register failed */
1422 
1423  return 1; /* return error */
1424  }
1425 
1426  return 0; /* success return 0 */
1427 }
1428 
1440 uint8_t ina226_get_alert_limit(ina226_handle_t *handle, uint16_t *reg)
1441 {
1442  uint8_t res;
1443 
1444  if (handle == NULL) /* check handle */
1445  {
1446  return 2; /* return error */
1447  }
1448  if (handle->inited != 1) /* check handle initialization */
1449  {
1450  return 3; /* return error */
1451  }
1452 
1453  res = a_ina226_iic_read(handle, INA226_REG_ALERT_LIMIT, reg); /* read config */
1454  if (res != 0) /* check result */
1455  {
1456  handle->debug_print("ina226: read alert limit register failed.\n"); /* read alert limit register failed */
1457 
1458  return 1; /* return error */
1459  }
1460 
1461  return 0; /* success return 0 */
1462 }
1463 
1475 uint8_t ina226_shunt_voltage_convert_to_register(ina226_handle_t *handle, float mV, uint16_t *reg)
1476 {
1477  if (handle == NULL) /* check handle */
1478  {
1479  return 2; /* return error */
1480  }
1481  if (handle->inited != 1) /* check handle initialization */
1482  {
1483  return 3; /* return error */
1484  }
1485 
1486  *reg = (uint16_t)(mV * 400.0f); /* convert real data to register data */
1487 
1488  return 0; /* success return 0 */
1489 }
1490 
1502 uint8_t ina226_shunt_voltage_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mV)
1503 {
1504  if (handle == NULL) /* check handle */
1505  {
1506  return 2; /* return error */
1507  }
1508  if (handle->inited != 1) /* check handle initialization */
1509  {
1510  return 3; /* return error */
1511  }
1512 
1513  *mV = (float)(reg) / 400.0f; /* convert real data to register data */
1514 
1515  return 0; /* success return 0 */
1516 }
1517 
1529 uint8_t ina226_bus_voltage_convert_to_register(ina226_handle_t *handle, float mV, uint16_t *reg)
1530 {
1531  if (handle == NULL) /* check handle */
1532  {
1533  return 2; /* return error */
1534  }
1535  if (handle->inited != 1) /* check handle initialization */
1536  {
1537  return 3; /* return error */
1538  }
1539 
1540  *reg = (uint16_t)(mV / 1.25f); /* convert real data to register data */
1541 
1542  return 0; /* success return 0 */
1543 }
1544 
1556 uint8_t ina226_bus_voltage_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mV)
1557 {
1558  if (handle == NULL) /* check handle */
1559  {
1560  return 2; /* return error */
1561  }
1562  if (handle->inited != 1) /* check handle initialization */
1563  {
1564  return 3; /* return error */
1565  }
1566 
1567  *mV = (float)(reg) * 1.25f; /* convert real data to register data */
1568 
1569  return 0; /* success return 0 */
1570 }
1571 
1583 uint8_t ina226_power_convert_to_register(ina226_handle_t *handle, float mW, uint16_t *reg)
1584 {
1585  if (handle == NULL) /* check handle */
1586  {
1587  return 2; /* return error */
1588  }
1589  if (handle->inited != 1) /* check handle initialization */
1590  {
1591  return 3; /* return error */
1592  }
1593 
1594  *reg = (uint16_t)((double)(mW) / (handle->current_lsb * 25.0 * 1000.0)); /* set the converted data */
1595 
1596  return 0; /* success return 0 */
1597 }
1598 
1610 uint8_t ina226_power_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mW)
1611 {
1612  if (handle == NULL) /* check handle */
1613  {
1614  return 2; /* return error */
1615  }
1616  if (handle->inited != 1) /* check handle initialization */
1617  {
1618  return 3; /* return error */
1619  }
1620 
1621  *mW = (float)((double)(reg) * handle->current_lsb * 25.0 * 1000.0); /* set the converted data */
1622 
1623  return 0; /* success return 0 */
1624 }
1625 
1637 {
1638  uint8_t res;
1639  uint16_t prev;
1640 
1641  if (handle == NULL) /* check handle */
1642  {
1643  return 2; /* return error */
1644  }
1645  if (handle->inited != 1) /* check handle initialization */
1646  {
1647  return 3; /* return error */
1648  }
1649 
1650  res = a_ina226_iic_read(handle, INA226_REG_MASK, (uint16_t *)&prev); /* read mask */
1651  if (res != 0) /* check result */
1652  {
1653  handle->debug_print("ina226: read mask register failed.\n"); /* read mask register failed */
1654 
1655  return 1; /* return error */
1656  }
1657  if ((prev & (1 << 4)) != 0) /* check alert flag */
1658  {
1659  if ((prev & (1 << 15)) != 0) /* shunt voltage over voltage */
1660  {
1661  if (handle->receive_callback != NULL) /* check not null */
1662  {
1663  handle->receive_callback(INA226_STATUS_SHUNT_VOLTAGE_OVER_VOLTAGE); /* run callback */
1664  }
1665  }
1666  else if ((prev & (1 << 14)) != 0) /* shunt voltage under voltage */
1667  {
1668  if (handle->receive_callback != NULL) /* check not null */
1669  {
1670  handle->receive_callback(INA226_STATUS_SHUNT_VOLTAGE_UNDER_VOLTAGE); /* run callback */
1671  }
1672  }
1673  else if ((prev & (1 << 13)) != 0) /* bus voltage over voltage */
1674  {
1675  if (handle->receive_callback != NULL) /* check not null */
1676  {
1677  handle->receive_callback(INA226_STATUS_BUS_VOLTAGE_OVER_VOLTAGE); /* run callback */
1678  }
1679  }
1680  else if ((prev & (1 << 12)) != 0) /* bus voltage under voltage */
1681  {
1682  if (handle->receive_callback != NULL) /* check not null */
1683  {
1684  handle->receive_callback(INA226_STATUS_BUS_VOLTAGE_UNDER_VOLTAGE); /* run callback */
1685  }
1686  }
1687  else if ((prev & (1 << 11)) != 0) /* power over limit */
1688  {
1689  if (handle->receive_callback != NULL) /* check not null */
1690  {
1691  handle->receive_callback(INA226_STATUS_POWER_OVER_LIMIT); /* run callback */
1692  }
1693  }
1694  else
1695  {
1696  /* do nothing */
1697  }
1698  }
1699 
1700  return 0; /* success return 0 */
1701 }
1702 
1716 {
1717  uint8_t res;
1718  uint16_t prev;
1719 
1720  if (handle == NULL) /* check handle */
1721  {
1722  return 2; /* return error */
1723  }
1724  if (handle->debug_print == NULL) /* check debug_print */
1725  {
1726  return 3; /* return error */
1727  }
1728  if (handle->iic_init == NULL) /* check iic_init */
1729  {
1730  handle->debug_print("ina226: iic_init is null.\n"); /* iic_init is null */
1731 
1732  return 3; /* return error */
1733  }
1734  if (handle->iic_deinit == NULL) /* check iic_deinit */
1735  {
1736  handle->debug_print("ina226: iic_deinit is null.\n"); /* iic_deinit is null */
1737 
1738  return 3; /* return error */
1739  }
1740  if (handle->iic_read == NULL) /* check iic_read */
1741  {
1742  handle->debug_print("ina226: iic_read is null.\n"); /* iic_read is null */
1743 
1744  return 3; /* return error */
1745  }
1746  if (handle->iic_write == NULL) /* check iic_write */
1747  {
1748  handle->debug_print("ina226: iic_write is null.\n"); /* iic_write is null */
1749 
1750  return 3; /* return error */
1751  }
1752  if (handle->delay_ms == NULL) /* check delay_ms */
1753  {
1754  handle->debug_print("ina226: delay_ms is null.\n"); /* delay_ms is null */
1755 
1756  return 3; /* return error */
1757  }
1758  if (handle->receive_callback == NULL) /* check receive_callback */
1759  {
1760  handle->debug_print("ina226: receive_callback is null.\n"); /* receive_callback is null */
1761 
1762  return 3; /* return error */
1763  }
1764 
1765  if (handle->iic_init() != 0) /* iic init */
1766  {
1767  handle->debug_print("ina226: iic init failed.\n"); /* iic init failed */
1768 
1769  return 1; /* return error */
1770  }
1771  res = a_ina226_iic_read(handle, INA226_REG_MANUFACTURER, (uint16_t *)&prev); /* read manufacturer */
1772  if (res != 0) /* check result */
1773  {
1774  handle->debug_print("ina226: read manufacturer failed.\n"); /* read manufacturer failed */
1775  (void)handle->iic_deinit(); /* iic deinit */
1776 
1777  return 4; /* return error */
1778  }
1779  if (prev != 0x5449) /* check id */
1780  {
1781  handle->debug_print("ina226: id is invalid.\n"); /* id is invalid */
1782  (void)handle->iic_deinit(); /* iic deinit */
1783 
1784  return 4; /* return error */
1785  }
1786  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read conf */
1787  if (res != 0) /* check result */
1788  {
1789  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
1790  (void)handle->iic_deinit(); /* iic deinit */
1791 
1792  return 5; /* return error */
1793  }
1794  prev |= 1 << 15; /* set bit */
1795  res = a_ina226_iic_write(handle, INA226_REG_CONF, prev); /* write conf */
1796  if (res != 0) /* check result */
1797  {
1798  handle->debug_print("ina226: write conf register failed.\n"); /* write conf register failed */
1799  (void)handle->iic_deinit(); /* iic deinit */
1800 
1801  return 5; /* return error */
1802  }
1803  handle->delay_ms(10); /* delay 10 ms */
1804  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read conf */
1805  if (res != 0) /* check result */
1806  {
1807  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
1808  (void)handle->iic_deinit(); /* iic deinit */
1809 
1810  return 5; /* return error */
1811  }
1812  if ((prev & (1 << 15)) != 0) /* check the result */
1813  {
1814  handle->debug_print("ina226: soft reset failed.\n"); /* soft reset failed */
1815  (void)handle->iic_deinit(); /* iic deinit */
1816 
1817  return 5; /* return error */
1818  }
1819  handle->trigger = 0; /* none */
1820  handle->inited = 1; /* flag inited */
1821 
1822  return 0; /* success return 0 */
1823 }
1824 
1837 {
1838  uint8_t res;
1839  uint16_t prev;
1840 
1841  if (handle == NULL) /* check handle */
1842  {
1843  return 2; /* return error */
1844  }
1845  if (handle->inited != 1) /* check handle initialization */
1846  {
1847  return 3; /* return error */
1848  }
1849 
1850  res = a_ina226_iic_read(handle, INA226_REG_CONF, (uint16_t *)&prev); /* read config */
1851  if (res != 0) /* check result */
1852  {
1853  handle->debug_print("ina226: read conf register failed.\n"); /* read conf register failed */
1854 
1855  return 4; /* return error */
1856  }
1857  prev &= ~(0x07); /* clear mode */
1858  res = a_ina226_iic_write(handle, INA226_REG_CONF, (uint16_t )prev); /* write config */
1859  if (res != 0) /* check result */
1860  {
1861  handle->debug_print("ina226: write conf register failed.\n"); /* write conf register failed */
1862 
1863  return 4; /* return error */
1864  }
1865  res = handle->iic_deinit(); /* iic deinit */
1866  if (res != 0) /* check result */
1867  {
1868  handle->debug_print("ina226: iic deinit failed.\n"); /* iic deinit failed */
1869 
1870  return 1; /* return error */
1871  }
1872 
1873  return 0; /* success return 0 */
1874 }
1875 
1888 uint8_t ina226_set_reg(ina226_handle_t *handle, uint8_t reg, uint16_t data)
1889 {
1890  if (handle == NULL) /* check handle */
1891  {
1892  return 2; /* return error */
1893  }
1894  if (handle->inited != 1) /* check handle initialization */
1895  {
1896  return 3; /* return error */
1897  }
1898 
1899  return a_ina226_iic_write(handle, reg, data); /* write data */
1900 }
1901 
1914 uint8_t ina226_get_reg(ina226_handle_t *handle, uint8_t reg, uint16_t *data)
1915 {
1916  if (handle == NULL) /* check handle */
1917  {
1918  return 2; /* return error */
1919  }
1920  if (handle->inited != 1) /* check handle initialization */
1921  {
1922  return 3; /* return error */
1923  }
1924 
1925  return a_ina226_iic_read(handle, reg, data); /* read data */
1926 }
1927 
1937 {
1938  if (info == NULL) /* check handle */
1939  {
1940  return 2; /* return error */
1941  }
1942 
1943  memset(info, 0, sizeof(ina226_info_t)); /* initialize ina226 info structure */
1944  strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1945  strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1946  strncpy(info->interface, "IIC", 8); /* copy interface name */
1947  info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1948  info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1949  info->max_current_ma = MAX_CURRENT; /* set maximum current */
1950  info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1951  info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1952  info->driver_version = DRIVER_VERSION; /* set driver version */
1953 
1954  return 0; /* success return 0 */
1955 }
#define INA226_REG_DIE
Definition: driver_ina226.c:64
#define MAX_CURRENT
Definition: driver_ina226.c:47
#define INA226_REG_POWER
Definition: driver_ina226.c:58
#define INA226_REG_MANUFACTURER
Definition: driver_ina226.c:63
#define SUPPLY_VOLTAGE_MAX
Definition: driver_ina226.c:46
#define INA226_REG_SHUNT_VOLTAGE
Definition: driver_ina226.c:56
#define TEMPERATURE_MAX
Definition: driver_ina226.c:49
#define MANUFACTURER_NAME
Definition: driver_ina226.c:44
#define INA226_REG_ALERT_LIMIT
Definition: driver_ina226.c:62
#define TEMPERATURE_MIN
Definition: driver_ina226.c:48
#define SUPPLY_VOLTAGE_MIN
Definition: driver_ina226.c:45
#define INA226_REG_CALIBRATION
Definition: driver_ina226.c:60
#define INA226_REG_BUS_VOLTAGE
Definition: driver_ina226.c:57
#define INA226_REG_MASK
Definition: driver_ina226.c:61
#define INA226_REG_CONF
chip register definition
Definition: driver_ina226.c:55
#define CHIP_NAME
chip information definition
Definition: driver_ina226.c:43
#define DRIVER_VERSION
Definition: driver_ina226.c:50
#define INA226_REG_CURRENT
Definition: driver_ina226.c:59
driver ina226 header file
uint8_t ina226_calculate_calibration(ina226_handle_t *handle, uint16_t *calibration)
calculate the calibration
uint8_t ina226_get_addr_pin(ina226_handle_t *handle, ina226_address_t *addr_pin)
get the iic address pin
uint8_t ina226_set_bus_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t t)
set bus voltage conversion time
uint8_t ina226_info(ina226_info_t *info)
get chip's information
uint8_t ina226_bus_voltage_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mV)
convert the register raw data to the bus voltage
uint8_t ina226_power_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mW)
convert the register raw data to the power
ina226_mask_t
ina226 mask enumeration definition
uint8_t ina226_bus_voltage_convert_to_register(ina226_handle_t *handle, float mV, uint16_t *reg)
convert the bus voltage to the register raw data
uint8_t ina226_read_current(ina226_handle_t *handle, int16_t *raw, float *mA)
read the current
uint8_t ina226_deinit(ina226_handle_t *handle)
close the chip
uint8_t ina226_set_conversion_ready_alert_pin(ina226_handle_t *handle, ina226_bool_t enable)
enable or disable conversion ready alert pin
uint8_t ina226_get_alert_latch(ina226_handle_t *handle, ina226_bool_t *enable)
get alert latch status
uint8_t ina226_get_bus_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t *t)
get bus voltage conversion time
uint8_t ina226_set_mode(ina226_handle_t *handle, ina226_mode_t mode)
set the mode
uint8_t ina226_set_alert_polarity_pin(ina226_handle_t *handle, ina226_alert_polarity_t pin)
set alert polarity pin
uint8_t ina226_shunt_voltage_convert_to_register(ina226_handle_t *handle, float mV, uint16_t *reg)
convert the shunt voltage to the register raw data
#define INA226_READ_TIMEOUT
ina226 read timeout definition
Definition: driver_ina226.h:63
uint8_t ina226_set_mask(ina226_handle_t *handle, ina226_mask_t mask, ina226_bool_t enable)
enable or disable mask
uint8_t ina226_get_conversion_ready_alert_pin(ina226_handle_t *handle, ina226_bool_t *enable)
get conversion ready alert pin status
uint8_t ina226_get_alert_limit(ina226_handle_t *handle, uint16_t *reg)
get alert limit
uint8_t ina226_set_addr_pin(ina226_handle_t *handle, ina226_address_t addr_pin)
set the iic address pin
uint8_t ina226_set_alert_latch(ina226_handle_t *handle, ina226_bool_t enable)
enable or disable alert latch
uint8_t ina226_read_power(ina226_handle_t *handle, uint16_t *raw, float *mW)
read the power
uint8_t ina226_set_average_mode(ina226_handle_t *handle, ina226_avg_t mode)
set average mode
uint8_t ina226_get_shunt_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t *t)
get shunt voltage conversion time
uint8_t ina226_get_calibration(ina226_handle_t *handle, uint16_t *data)
get the calibration
uint8_t ina226_get_mode(ina226_handle_t *handle, ina226_mode_t *mode)
get the mode
ina226_address_t
ina226 address enumeration definition
Definition: driver_ina226.h:70
uint8_t ina226_soft_reset(ina226_handle_t *handle)
soft reset the chip
uint8_t ina226_get_mask(ina226_handle_t *handle, ina226_mask_t mask, ina226_bool_t *enable)
get mask
uint8_t ina226_irq_handler(ina226_handle_t *handle)
irq handler
uint8_t ina226_get_alert_polarity_pin(ina226_handle_t *handle, ina226_alert_polarity_t *pin)
get alert polarity pin
ina226_mode_t
ina226 mode enumeration definition
uint8_t ina226_read_shunt_voltage(ina226_handle_t *handle, int16_t *raw, float *mV)
read the shunt voltage
uint8_t ina226_get_average_mode(ina226_handle_t *handle, ina226_avg_t *mode)
get average mode
uint8_t ina226_init(ina226_handle_t *handle)
initialize the chip
uint8_t ina226_get_resistance(ina226_handle_t *handle, double *resistance)
get the resistance
ina226_bool_t
ina226 bool enumeration definition
Definition: driver_ina226.h:93
uint8_t ina226_set_shunt_voltage_conversion_time(ina226_handle_t *handle, ina226_conversion_time_t t)
set shunt voltage conversion time
uint8_t ina226_set_alert_limit(ina226_handle_t *handle, uint16_t reg)
set alert limit
ina226_conversion_time_t
uint8_t ina226_set_calibration(ina226_handle_t *handle, uint16_t data)
set the calibration
uint8_t ina226_shunt_voltage_convert_to_data(ina226_handle_t *handle, uint16_t reg, float *mV)
convert the register raw data to the shunt voltage
uint8_t ina226_set_resistance(ina226_handle_t *handle, double resistance)
set the resistance
uint8_t ina226_power_convert_to_register(ina226_handle_t *handle, float mW, uint16_t *reg)
convert the power to the register raw data
ina226_avg_t
ina226 average enumeration definition
ina226_alert_polarity_t
ina226 alert polarity enumeration definition
uint8_t ina226_get_die_id(ina226_handle_t *handle, uint16_t *device_id, uint8_t *die_revision_id)
get the die id
uint8_t ina226_read_bus_voltage(ina226_handle_t *handle, uint16_t *raw, float *mV)
read the bus voltage
@ INA226_STATUS_BUS_VOLTAGE_OVER_VOLTAGE
@ INA226_STATUS_POWER_OVER_LIMIT
@ INA226_STATUS_SHUNT_VOLTAGE_UNDER_VOLTAGE
@ INA226_STATUS_BUS_VOLTAGE_UNDER_VOLTAGE
@ INA226_STATUS_SHUNT_VOLTAGE_OVER_VOLTAGE
@ INA226_MODE_BUS_VOLTAGE_TRIGGERED
@ INA226_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
@ INA226_MODE_SHUNT_VOLTAGE_TRIGGERED
uint8_t ina226_set_reg(ina226_handle_t *handle, uint8_t reg, uint16_t data)
set the chip register
uint8_t ina226_get_reg(ina226_handle_t *handle, uint8_t reg, uint16_t *data)
get the chip register
ina226 handle structure definition
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
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)
ina226 information structure definition
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]