LibDriver INA226  1.0.0
INA226 full-featured driver
driver_ina226_alert_test.c
Go to the documentation of this file.
1 
38 
39 static ina226_handle_t gs_handle;
40 static volatile uint16_t gs_flag;
47 static void a_receive_callback(uint8_t type)
48 {
49  switch (type)
50  {
52  {
53  gs_flag |= 1 << 0;
54  ina226_interface_debug_print("ina226: irq shunt voltage over voltage.\n");
55 
56  break;
57  }
59  {
60  gs_flag |= 1 << 1;
61  ina226_interface_debug_print("ina226: irq shunt voltage under voltage.\n");
62 
63  break;
64  }
66  {
67  gs_flag |= 1 << 2;
68  ina226_interface_debug_print("ina226: irq bus voltage over voltage.\n");
69 
70  break;
71  }
73  {
74  gs_flag |= 1 << 3;
75  ina226_interface_debug_print("ina226: irq bus voltage under voltage.\n");
76 
77  break;
78  }
80  {
81  gs_flag |= 1 << 4;
82  ina226_interface_debug_print("ina226: irq power over limit.\n");
83 
84  break;
85  }
86  default :
87  {
88  ina226_interface_debug_print("ina226: unknown code.\n");
89 
90  break;
91  }
92  }
93 }
94 
103 {
104  if (ina226_irq_handler(&gs_handle) != 0)
105  {
106  return 1;
107  }
108 
109  return 0;
110 }
111 
124 uint8_t ina226_alert_test(ina226_address_t addr_pin, double r, ina226_mask_t mask, float threshold, uint32_t timeout_ms)
125 {
126  uint8_t res;
127  uint16_t reg;
128  uint16_t flag;
129  uint32_t timeout;
130  uint16_t calibration;
131  ina226_info_t info;
132 
133  /* link interface function */
141  DRIVER_INA226_LINK_RECEIVE_CALLBACK(&gs_handle, a_receive_callback);
142 
143  /* get information */
144  res = ina226_info(&info);
145  if (res != 0)
146  {
147  ina226_interface_debug_print("ina226: get info failed.\n");
148 
149  return 1;
150  }
151  else
152  {
153  /* print chip info */
154  ina226_interface_debug_print("ina226: chip is %s.\n", info.chip_name);
155  ina226_interface_debug_print("ina226: manufacturer is %s.\n", info.manufacturer_name);
156  ina226_interface_debug_print("ina226: interface is %s.\n", info.interface);
157  ina226_interface_debug_print("ina226: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
158  ina226_interface_debug_print("ina226: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
159  ina226_interface_debug_print("ina226: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
160  ina226_interface_debug_print("ina226: max current is %0.2fmA.\n", info.max_current_ma);
161  ina226_interface_debug_print("ina226: max temperature is %0.1fC.\n", info.temperature_max);
162  ina226_interface_debug_print("ina226: min temperature is %0.1fC.\n", info.temperature_min);
163  }
164 
165  /* start alert test */
166  ina226_interface_debug_print("ina226: start alert test.\n");
167 
168  /* set addr pin */
169  res = ina226_set_addr_pin(&gs_handle, addr_pin);
170  if (res != 0)
171  {
172  ina226_interface_debug_print("ina226: set addr pin failed.\n");
173 
174  return 1;
175  }
176 
177  /* set the r */
178  res = ina226_set_resistance(&gs_handle, r);
179  if (res != 0)
180  {
181  ina226_interface_debug_print("ina226: set resistance failed.\n");
182 
183  return 1;
184  }
185 
186  /* init */
187  res = ina226_init(&gs_handle);
188  if (res != 0)
189  {
190  ina226_interface_debug_print("ina226: init failed.\n");
191 
192  return 1;
193  }
194 
195  /* set average mode 16 */
196  res = ina226_set_average_mode(&gs_handle, INA226_AVG_16);
197  if (res != 0)
198  {
199  ina226_interface_debug_print("ina226: set average mode failed.\n");
200  (void)ina226_deinit(&gs_handle);
201 
202  return 1;
203  }
204 
205  /* set bus voltage conversion time 1.1ms */
207  if (res != 0)
208  {
209  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
210  (void)ina226_deinit(&gs_handle);
211 
212  return 1;
213  }
214 
215  /* set shunt voltage conversion time 1.1ms */
217  if (res != 0)
218  {
219  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
220  (void)ina226_deinit(&gs_handle);
221 
222  return 1;
223  }
224 
225  /* calculate calibration */
226  res = ina226_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
227  if (res != 0)
228  {
229  ina226_interface_debug_print("ina226: calculate calibration failed.\n");
230  (void)ina226_deinit(&gs_handle);
231 
232  return 1;
233  }
234  res = ina226_set_calibration(&gs_handle, calibration);
235  if (res != 0)
236  {
237  ina226_interface_debug_print("ina226: set calibration failed.\n");
238  (void)ina226_deinit(&gs_handle);
239 
240  return 1;
241  }
242 
243  /* set shunt bus voltage continuous */
245  if (res != 0)
246  {
247  ina226_interface_debug_print("ina226: set mode failed.\n");
248  (void)ina226_deinit(&gs_handle);
249 
250  return 1;
251  }
252 
253  /* disable conversion ready alert pin */
255  if (res != 0)
256  {
257  ina226_interface_debug_print("ina226: set conversion ready alert pin failed.\n");
258  (void)ina226_deinit(&gs_handle);
259 
260  return 1;
261  }
262 
263  /* set alert polarity pin normal */
265  if (res != 0)
266  {
267  ina226_interface_debug_print("ina226: set alert polarity pin failed.\n");
268  (void)ina226_deinit(&gs_handle);
269 
270  return 1;
271  }
272 
273  /* disable alert latch */
274  res = ina226_set_alert_latch(&gs_handle, INA226_BOOL_FALSE);
275  if (res != 0)
276  {
277  ina226_interface_debug_print("ina226: set alert latch failed.\n");
278  (void)ina226_deinit(&gs_handle);
279 
280  return 1;
281  }
282 
283  gs_flag = 0;
285  {
286  /* shunt voltage convert to register */
287  res = ina226_shunt_voltage_convert_to_register(&gs_handle, threshold, &reg);
288  if (res != 0)
289  {
290  ina226_interface_debug_print("ina226: shunt voltage convert to register failed.\n");
291  (void)ina226_deinit(&gs_handle);
292 
293  return 1;
294  }
295 
296  /* set alert limit */
297  res = ina226_set_alert_limit(&gs_handle, reg);
298  if (res != 0)
299  {
300  ina226_interface_debug_print("ina226: set alert limit failed.\n");
301  (void)ina226_deinit(&gs_handle);
302 
303  return 1;
304  }
305 
306  /* enable */
308  if (res != 0)
309  {
310  ina226_interface_debug_print("ina226: set mask failed.\n");
311  (void)ina226_deinit(&gs_handle);
312 
313  return 1;
314  }
315 
316  /* disable */
318  if (res != 0)
319  {
320  ina226_interface_debug_print("ina226: set mask failed.\n");
321  (void)ina226_deinit(&gs_handle);
322 
323  return 1;
324  }
325 
326  /* disable */
328  if (res != 0)
329  {
330  ina226_interface_debug_print("ina226: set mask failed.\n");
331  (void)ina226_deinit(&gs_handle);
332 
333  return 1;
334  }
335 
336  /* disable */
338  if (res != 0)
339  {
340  ina226_interface_debug_print("ina226: set mask failed.\n");
341  (void)ina226_deinit(&gs_handle);
342 
343  return 1;
344  }
345 
346  /* disable */
348  if (res != 0)
349  {
350  ina226_interface_debug_print("ina226: set mask failed.\n");
351  (void)ina226_deinit(&gs_handle);
352 
353  return 1;
354  }
355  flag = 1 << 0;
356  }
358  {
359  /* shunt voltage convert to register */
360  res = ina226_shunt_voltage_convert_to_register(&gs_handle, threshold, &reg);
361  if (res != 0)
362  {
363  ina226_interface_debug_print("ina226: shunt voltage convert to register failed.\n");
364  (void)ina226_deinit(&gs_handle);
365 
366  return 1;
367  }
368 
369  /* set alert limit */
370  res = ina226_set_alert_limit(&gs_handle, reg);
371  if (res != 0)
372  {
373  ina226_interface_debug_print("ina226: set alert limit failed.\n");
374  (void)ina226_deinit(&gs_handle);
375 
376  return 1;
377  }
378 
379  /* disable */
381  if (res != 0)
382  {
383  ina226_interface_debug_print("ina226: set mask failed.\n");
384  (void)ina226_deinit(&gs_handle);
385 
386  return 1;
387  }
388 
389  /* enable */
391  if (res != 0)
392  {
393  ina226_interface_debug_print("ina226: set mask failed.\n");
394  (void)ina226_deinit(&gs_handle);
395 
396  return 1;
397  }
398 
399  /* disable */
401  if (res != 0)
402  {
403  ina226_interface_debug_print("ina226: set mask failed.\n");
404  (void)ina226_deinit(&gs_handle);
405 
406  return 1;
407  }
408 
409  /* disable */
411  if (res != 0)
412  {
413  ina226_interface_debug_print("ina226: set mask failed.\n");
414  (void)ina226_deinit(&gs_handle);
415 
416  return 1;
417  }
418 
419  /* disable */
421  if (res != 0)
422  {
423  ina226_interface_debug_print("ina226: set mask failed.\n");
424  (void)ina226_deinit(&gs_handle);
425 
426  return 1;
427  }
428  flag = 1 << 1;
429  }
430  else if (mask == INA226_MASK_BUS_VOLTAGE_OVER_VOLTAGE)
431  {
432  /* bus voltage convert to register */
433  res = ina226_bus_voltage_convert_to_register(&gs_handle, threshold, &reg);
434  if (res != 0)
435  {
436  ina226_interface_debug_print("ina226: shunt voltage convert to register failed.\n");
437  (void)ina226_deinit(&gs_handle);
438 
439  return 1;
440  }
441 
442  /* set alert limit */
443  res = ina226_set_alert_limit(&gs_handle, reg);
444  if (res != 0)
445  {
446  ina226_interface_debug_print("ina226: set alert limit failed.\n");
447  (void)ina226_deinit(&gs_handle);
448 
449  return 1;
450  }
451 
452  /* disable */
454  if (res != 0)
455  {
456  ina226_interface_debug_print("ina226: set mask failed.\n");
457  (void)ina226_deinit(&gs_handle);
458 
459  return 1;
460  }
461 
462  /* disable */
464  if (res != 0)
465  {
466  ina226_interface_debug_print("ina226: set mask failed.\n");
467  (void)ina226_deinit(&gs_handle);
468 
469  return 1;
470  }
471 
472  /* enable */
474  if (res != 0)
475  {
476  ina226_interface_debug_print("ina226: set mask failed.\n");
477  (void)ina226_deinit(&gs_handle);
478 
479  return 1;
480  }
481 
482  /* disable */
484  if (res != 0)
485  {
486  ina226_interface_debug_print("ina226: set mask failed.\n");
487  (void)ina226_deinit(&gs_handle);
488 
489  return 1;
490  }
491 
492  /* disable */
494  if (res != 0)
495  {
496  ina226_interface_debug_print("ina226: set mask failed.\n");
497  (void)ina226_deinit(&gs_handle);
498 
499  return 1;
500  }
501  flag = 1 << 2;
502  }
503  else if (mask == INA226_MASK_BUS_VOLTAGE_UNDER_VOLTAGE)
504  {
505  /* bus voltage convert to register */
506  res = ina226_bus_voltage_convert_to_register(&gs_handle, threshold, &reg);
507  if (res != 0)
508  {
509  ina226_interface_debug_print("ina226: shunt voltage convert to register failed.\n");
510  (void)ina226_deinit(&gs_handle);
511 
512  return 1;
513  }
514 
515  /* set alert limit */
516  res = ina226_set_alert_limit(&gs_handle, reg);
517  if (res != 0)
518  {
519  ina226_interface_debug_print("ina226: set alert limit failed.\n");
520  (void)ina226_deinit(&gs_handle);
521 
522  return 1;
523  }
524 
525  /* disable */
527  if (res != 0)
528  {
529  ina226_interface_debug_print("ina226: set mask failed.\n");
530  (void)ina226_deinit(&gs_handle);
531 
532  return 1;
533  }
534 
535  /* disable */
537  if (res != 0)
538  {
539  ina226_interface_debug_print("ina226: set mask failed.\n");
540  (void)ina226_deinit(&gs_handle);
541 
542  return 1;
543  }
544 
545  /* disable */
547  if (res != 0)
548  {
549  ina226_interface_debug_print("ina226: set mask failed.\n");
550  (void)ina226_deinit(&gs_handle);
551 
552  return 1;
553  }
554 
555  /* enable */
557  if (res != 0)
558  {
559  ina226_interface_debug_print("ina226: set mask failed.\n");
560  (void)ina226_deinit(&gs_handle);
561 
562  return 1;
563  }
564 
565  /* disable */
567  if (res != 0)
568  {
569  ina226_interface_debug_print("ina226: set mask failed.\n");
570  (void)ina226_deinit(&gs_handle);
571 
572  return 1;
573  }
574  flag = 1 << 3;
575  }
576  else
577  {
578  /* power convert to register */
579  res = ina226_power_convert_to_register(&gs_handle, threshold, &reg);
580  if (res != 0)
581  {
582  ina226_interface_debug_print("ina226: shunt voltage convert to register failed.\n");
583  (void)ina226_deinit(&gs_handle);
584 
585  return 1;
586  }
587 
588  /* set alert limit */
589  res = ina226_set_alert_limit(&gs_handle, reg);
590  if (res != 0)
591  {
592  ina226_interface_debug_print("ina226: set alert limit failed.\n");
593  (void)ina226_deinit(&gs_handle);
594 
595  return 1;
596  }
597 
598  /* disable */
600  if (res != 0)
601  {
602  ina226_interface_debug_print("ina226: set mask failed.\n");
603  (void)ina226_deinit(&gs_handle);
604 
605  return 1;
606  }
607 
608  /* disable */
610  if (res != 0)
611  {
612  ina226_interface_debug_print("ina226: set mask failed.\n");
613  (void)ina226_deinit(&gs_handle);
614 
615  return 1;
616  }
617 
618  /* disable */
620  if (res != 0)
621  {
622  ina226_interface_debug_print("ina226: set mask failed.\n");
623  (void)ina226_deinit(&gs_handle);
624 
625  return 1;
626  }
627 
628  /* disable */
630  if (res != 0)
631  {
632  ina226_interface_debug_print("ina226: set mask failed.\n");
633  (void)ina226_deinit(&gs_handle);
634 
635  return 1;
636  }
637 
638  /* enable */
640  if (res != 0)
641  {
642  ina226_interface_debug_print("ina226: set mask failed.\n");
643  (void)ina226_deinit(&gs_handle);
644 
645  return 1;
646  }
647  flag = 1 << 4;
648  }
649 
650  timeout = timeout_ms;
651  while (timeout != 0)
652  {
653  if ((gs_flag & flag) != 0)
654  {
655  break;
656  }
657  timeout--;
659  }
660 
661  /* check timeout */
662  if (timeout == 0)
663  {
664  ina226_interface_debug_print("ina226: alert timeout.\n");
665  (void)ina226_deinit(&gs_handle);
666 
667  return 1;
668  }
669 
670  /* finish alert test */
671  (void)ina226_deinit(&gs_handle);
672  ina226_interface_debug_print("ina226: finish alert test.\n");
673 
674  return 0;
675 }
uint8_t ina226_calculate_calibration(ina226_handle_t *handle, uint16_t *calibration)
calculate the calibration
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
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_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_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
uint8_t ina226_set_mask(ina226_handle_t *handle, ina226_mask_t mask, ina226_bool_t enable)
enable or disable mask
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_set_average_mode(ina226_handle_t *handle, ina226_avg_t mode)
set average mode
ina226_address_t
ina226 address enumeration definition
Definition: driver_ina226.h:70
uint8_t ina226_irq_handler(ina226_handle_t *handle)
irq handler
uint8_t ina226_init(ina226_handle_t *handle)
initialize the chip
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
uint8_t ina226_set_calibration(ina226_handle_t *handle, uint16_t data)
set the calibration
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_MASK_POWER_OVER_LIMIT
@ INA226_MASK_SHUNT_VOLTAGE_UNDER_VOLTAGE
@ INA226_MASK_SHUNT_VOLTAGE_OVER_VOLTAGE
@ INA226_MASK_BUS_VOLTAGE_OVER_VOLTAGE
@ INA226_MASK_BUS_VOLTAGE_UNDER_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_SHUNT_BUS_VOLTAGE_CONTINUOUS
@ INA226_BOOL_TRUE
Definition: driver_ina226.h:95
@ INA226_BOOL_FALSE
Definition: driver_ina226.h:94
@ INA226_CONVERSION_TIME_1P1_MS
@ INA226_AVG_16
@ INA226_ALERT_POLARITY_NORMAL
uint8_t ina226_interface_iic_deinit(void)
interface iic bus deinit
uint8_t ina226_interface_iic_init(void)
interface iic bus init
void ina226_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t ina226_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
void ina226_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ina226_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t ina226_alert_test(ina226_address_t addr_pin, double r, ina226_mask_t mask, float threshold, uint32_t timeout_ms)
alert test
uint8_t ina226_alert_test_irq_handler(void)
alert test irq handler
ina226 handle structure definition
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]