LibDriver INA226  1.0.0
INA226 full-featured driver
driver_ina226_register_test.c
Go to the documentation of this file.
1 
38 #include <stdlib.h>
39 
40 static ina226_handle_t gs_handle;
51 {
52  uint8_t res;
53  uint8_t die_revision_id;
54  float f;
55  float f_check;
56  double r;
57  double r_check;
58  uint16_t data;
59  uint16_t data_check;
60  uint16_t device_id;
61  ina226_info_t info;
62  ina226_address_t addr;
63  ina226_avg_t mode;
65  ina226_mode_t chip_mode;
66  ina226_bool_t enable;
68 
69  /* link interface function */
78 
79  /* get information */
80  res = ina226_info(&info);
81  if (res != 0)
82  {
83  ina226_interface_debug_print("ina226: get info failed.\n");
84 
85  return 1;
86  }
87  else
88  {
89  /* print chip info */
90  ina226_interface_debug_print("ina226: chip is %s.\n", info.chip_name);
91  ina226_interface_debug_print("ina226: manufacturer is %s.\n", info.manufacturer_name);
92  ina226_interface_debug_print("ina226: interface is %s.\n", info.interface);
93  ina226_interface_debug_print("ina226: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
94  ina226_interface_debug_print("ina226: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
95  ina226_interface_debug_print("ina226: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
96  ina226_interface_debug_print("ina226: max current is %0.2fmA.\n", info.max_current_ma);
97  ina226_interface_debug_print("ina226: max temperature is %0.1fC.\n", info.temperature_max);
98  ina226_interface_debug_print("ina226: min temperature is %0.1fC.\n", info.temperature_min);
99  }
100 
101  /* start register test */
102  ina226_interface_debug_print("ina226: start register test.\n");
103 
104  /* ina226_set_addr_pin/ina226_get_addr_pin test */
105  ina226_interface_debug_print("ina226: ina226_set_addr_pin/ina226_get_addr_pin test.\n");
106 
107  /* set address 0 */
108  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_0);
109  if (res != 0)
110  {
111  ina226_interface_debug_print("ina226: set addr pin failed.\n");
112 
113  return 1;
114  }
115  ina226_interface_debug_print("ina226: set addr pin 0.\n");
116  res = ina226_get_addr_pin(&gs_handle, &addr);
117  if (res != 0)
118  {
119  ina226_interface_debug_print("ina226: get addr pin failed.\n");
120 
121  return 1;
122  }
123  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_0 ? "ok" : "error");
124 
125  /* set address 1 */
126  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_1);
127  if (res != 0)
128  {
129  ina226_interface_debug_print("ina226: set addr pin failed.\n");
130 
131  return 1;
132  }
133  ina226_interface_debug_print("ina226: set addr pin 1.\n");
134  res = ina226_get_addr_pin(&gs_handle, &addr);
135  if (res != 0)
136  {
137  ina226_interface_debug_print("ina226: get addr pin failed.\n");
138 
139  return 1;
140  }
141  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_1 ? "ok" : "error");
142 
143  /* set address 2 */
144  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_2);
145  if (res != 0)
146  {
147  ina226_interface_debug_print("ina226: set addr pin failed.\n");
148 
149  return 1;
150  }
151  ina226_interface_debug_print("ina226: set addr pin 2.\n");
152  res = ina226_get_addr_pin(&gs_handle, &addr);
153  if (res != 0)
154  {
155  ina226_interface_debug_print("ina226: get addr pin failed.\n");
156 
157  return 1;
158  }
159  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_2 ? "ok" : "error");
160 
161  /* set address 3 */
162  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_3);
163  if (res != 0)
164  {
165  ina226_interface_debug_print("ina226: set addr pin failed.\n");
166 
167  return 1;
168  }
169  ina226_interface_debug_print("ina226: set addr pin 3.\n");
170  res = ina226_get_addr_pin(&gs_handle, &addr);
171  if (res != 0)
172  {
173  ina226_interface_debug_print("ina226: get addr pin failed.\n");
174 
175  return 1;
176  }
177  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_3 ? "ok" : "error");
178 
179  /* set address 4 */
180  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_4);
181  if (res != 0)
182  {
183  ina226_interface_debug_print("ina226: set addr pin failed.\n");
184 
185  return 1;
186  }
187  ina226_interface_debug_print("ina226: set addr pin 4.\n");
188  res = ina226_get_addr_pin(&gs_handle, &addr);
189  if (res != 0)
190  {
191  ina226_interface_debug_print("ina226: get addr pin failed.\n");
192 
193  return 1;
194  }
195  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_4 ? "ok" : "error");
196 
197  /* set address 5 */
198  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_5);
199  if (res != 0)
200  {
201  ina226_interface_debug_print("ina226: set addr pin failed.\n");
202 
203  return 1;
204  }
205  ina226_interface_debug_print("ina226: set addr pin 5.\n");
206  res = ina226_get_addr_pin(&gs_handle, &addr);
207  if (res != 0)
208  {
209  ina226_interface_debug_print("ina226: get addr pin failed.\n");
210 
211  return 1;
212  }
213  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_5 ? "ok" : "error");
214 
215  /* set address 6 */
216  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_6);
217  if (res != 0)
218  {
219  ina226_interface_debug_print("ina226: set addr pin failed.\n");
220 
221  return 1;
222  }
223  ina226_interface_debug_print("ina226: set addr pin 6.\n");
224  res = ina226_get_addr_pin(&gs_handle, &addr);
225  if (res != 0)
226  {
227  ina226_interface_debug_print("ina226: get addr pin failed.\n");
228 
229  return 1;
230  }
231  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_6 ? "ok" : "error");
232 
233  /* set address 7 */
234  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_7);
235  if (res != 0)
236  {
237  ina226_interface_debug_print("ina226: set addr pin failed.\n");
238 
239  return 1;
240  }
241  ina226_interface_debug_print("ina226: set addr pin 7.\n");
242  res = ina226_get_addr_pin(&gs_handle, &addr);
243  if (res != 0)
244  {
245  ina226_interface_debug_print("ina226: get addr pin failed.\n");
246 
247  return 1;
248  }
249  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_7 ? "ok" : "error");
250 
251  /* set address 8 */
252  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_8);
253  if (res != 0)
254  {
255  ina226_interface_debug_print("ina226: set addr pin failed.\n");
256 
257  return 1;
258  }
259  ina226_interface_debug_print("ina226: set addr pin 8.\n");
260  res = ina226_get_addr_pin(&gs_handle, &addr);
261  if (res != 0)
262  {
263  ina226_interface_debug_print("ina226: get addr pin failed.\n");
264 
265  return 1;
266  }
267  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_8 ? "ok" : "error");
268 
269  /* set address 9 */
270  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_9);
271  if (res != 0)
272  {
273  ina226_interface_debug_print("ina226: set addr pin failed.\n");
274 
275  return 1;
276  }
277  ina226_interface_debug_print("ina226: set addr pin 9.\n");
278  res = ina226_get_addr_pin(&gs_handle, &addr);
279  if (res != 0)
280  {
281  ina226_interface_debug_print("ina226: get addr pin failed.\n");
282 
283  return 1;
284  }
285  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_9 ? "ok" : "error");
286 
287  /* set address 10 */
288  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_A);
289  if (res != 0)
290  {
291  ina226_interface_debug_print("ina226: set addr pin failed.\n");
292 
293  return 1;
294  }
295  ina226_interface_debug_print("ina226: set addr pin 10.\n");
296  res = ina226_get_addr_pin(&gs_handle, &addr);
297  if (res != 0)
298  {
299  ina226_interface_debug_print("ina226: get addr pin failed.\n");
300 
301  return 1;
302  }
303  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_A ? "ok" : "error");
304 
305  /* set address 11 */
306  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_B);
307  if (res != 0)
308  {
309  ina226_interface_debug_print("ina226: set addr pin failed.\n");
310 
311  return 1;
312  }
313  ina226_interface_debug_print("ina226: set addr pin 11.\n");
314  res = ina226_get_addr_pin(&gs_handle, &addr);
315  if (res != 0)
316  {
317  ina226_interface_debug_print("ina226: get addr pin failed.\n");
318 
319  return 1;
320  }
321  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_B ? "ok" : "error");
322 
323  /* set address 12 */
324  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_C);
325  if (res != 0)
326  {
327  ina226_interface_debug_print("ina226: set addr pin failed.\n");
328 
329  return 1;
330  }
331  ina226_interface_debug_print("ina226: set addr pin 12.\n");
332  res = ina226_get_addr_pin(&gs_handle, &addr);
333  if (res != 0)
334  {
335  ina226_interface_debug_print("ina226: get addr pin failed.\n");
336 
337  return 1;
338  }
339  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_C ? "ok" : "error");
340 
341  /* set address 13 */
342  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_D);
343  if (res != 0)
344  {
345  ina226_interface_debug_print("ina226: set addr pin failed.\n");
346 
347  return 1;
348  }
349  ina226_interface_debug_print("ina226: set addr pin 13.\n");
350  res = ina226_get_addr_pin(&gs_handle, &addr);
351  if (res != 0)
352  {
353  ina226_interface_debug_print("ina226: get addr pin failed.\n");
354 
355  return 1;
356  }
357  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_D ? "ok" : "error");
358 
359  /* set address 14 */
360  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_E);
361  if (res != 0)
362  {
363  ina226_interface_debug_print("ina226: set addr pin failed.\n");
364 
365  return 1;
366  }
367  ina226_interface_debug_print("ina226: set addr pin 14.\n");
368  res = ina226_get_addr_pin(&gs_handle, &addr);
369  if (res != 0)
370  {
371  ina226_interface_debug_print("ina226: get addr pin failed.\n");
372 
373  return 1;
374  }
375  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_E ? "ok" : "error");
376 
377  /* set address 15 */
378  res = ina226_set_addr_pin(&gs_handle, INA226_ADDRESS_F);
379  if (res != 0)
380  {
381  ina226_interface_debug_print("ina226: set addr pin failed.\n");
382 
383  return 1;
384  }
385  ina226_interface_debug_print("ina226: set addr pin 15.\n");
386  res = ina226_get_addr_pin(&gs_handle, &addr);
387  if (res != 0)
388  {
389  ina226_interface_debug_print("ina226: get addr pin failed.\n");
390 
391  return 1;
392  }
393  ina226_interface_debug_print("ina226: check addr pin %s.\n", addr == INA226_ADDRESS_F ? "ok" : "error");
394 
395  /* ina226_set_resistance/ina226_get_resistance test */
396  ina226_interface_debug_print("ina226: ina226_set_resistance/ina226_get_resistance test.\n");
397 
398  /* generate the r */
399  r = (double)(rand() % 100) / 1000.0;
400  res = ina226_set_resistance(&gs_handle, r);
401  if (res != 0)
402  {
403  ina226_interface_debug_print("ina226: set resistance failed.\n");
404 
405  return 1;
406  }
407  ina226_interface_debug_print("ina226: set resistance %f.\n", r);
408  res = ina226_get_resistance(&gs_handle, (double *)&r_check);
409  if (res != 0)
410  {
411  ina226_interface_debug_print("ina226: get resistance failed.\n");
412 
413  return 1;
414  }
415  ina226_interface_debug_print("ina226: check resistance %f.\n", r_check);
416 
417  /* set addr pin */
418  res = ina226_set_addr_pin(&gs_handle, addr_pin);
419  if (res != 0)
420  {
421  ina226_interface_debug_print("ina226: set addr pin failed.\n");
422 
423  return 1;
424  }
425 
426  /* init */
427  res = ina226_init(&gs_handle);
428  if (res != 0)
429  {
430  ina226_interface_debug_print("ina226: init failed.\n");
431 
432  return 1;
433  }
434 
435  /* ina226_set_average_mode/ina226_get_average_mode test */
436  ina226_interface_debug_print("ina226: ina226_set_average_mode/ina226_get_average_mode test.\n");
437 
438  /* set average 1 */
439  res = ina226_set_average_mode(&gs_handle, INA226_AVG_1);
440  if (res != 0)
441  {
442  ina226_interface_debug_print("ina226: set average mode failed.\n");
443  (void)ina226_deinit(&gs_handle);
444 
445  return 1;
446  }
447  ina226_interface_debug_print("ina226: set average 1.\n");
448  res = ina226_get_average_mode(&gs_handle, &mode);
449  if (res != 0)
450  {
451  ina226_interface_debug_print("ina226: get average mode failed.\n");
452  (void)ina226_deinit(&gs_handle);
453 
454  return 1;
455  }
456  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_1 ? "ok" : "error");
457 
458  /* set average 4 */
459  res = ina226_set_average_mode(&gs_handle, INA226_AVG_4);
460  if (res != 0)
461  {
462  ina226_interface_debug_print("ina226: set average mode failed.\n");
463  (void)ina226_deinit(&gs_handle);
464 
465  return 1;
466  }
467  ina226_interface_debug_print("ina226: set average 4.\n");
468  res = ina226_get_average_mode(&gs_handle, &mode);
469  if (res != 0)
470  {
471  ina226_interface_debug_print("ina226: get average mode failed.\n");
472  (void)ina226_deinit(&gs_handle);
473 
474  return 1;
475  }
476  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_4 ? "ok" : "error");
477 
478  /* set average 16 */
479  res = ina226_set_average_mode(&gs_handle, INA226_AVG_16);
480  if (res != 0)
481  {
482  ina226_interface_debug_print("ina226: set average mode failed.\n");
483  (void)ina226_deinit(&gs_handle);
484 
485  return 1;
486  }
487  ina226_interface_debug_print("ina226: set average 16.\n");
488  res = ina226_get_average_mode(&gs_handle, &mode);
489  if (res != 0)
490  {
491  ina226_interface_debug_print("ina226: get average mode failed.\n");
492  (void)ina226_deinit(&gs_handle);
493 
494  return 1;
495  }
496  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_16 ? "ok" : "error");
497 
498  /* set average 64 */
499  res = ina226_set_average_mode(&gs_handle, INA226_AVG_64);
500  if (res != 0)
501  {
502  ina226_interface_debug_print("ina226: set average mode failed.\n");
503  (void)ina226_deinit(&gs_handle);
504 
505  return 1;
506  }
507  ina226_interface_debug_print("ina226: set average 64.\n");
508  res = ina226_get_average_mode(&gs_handle, &mode);
509  if (res != 0)
510  {
511  ina226_interface_debug_print("ina226: get average mode failed.\n");
512  (void)ina226_deinit(&gs_handle);
513 
514  return 1;
515  }
516  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_64 ? "ok" : "error");
517 
518  /* set average 128 */
519  res = ina226_set_average_mode(&gs_handle, INA226_AVG_128);
520  if (res != 0)
521  {
522  ina226_interface_debug_print("ina226: set average mode failed.\n");
523  (void)ina226_deinit(&gs_handle);
524 
525  return 1;
526  }
527  ina226_interface_debug_print("ina226: set average 128.\n");
528  res = ina226_get_average_mode(&gs_handle, &mode);
529  if (res != 0)
530  {
531  ina226_interface_debug_print("ina226: get average mode failed.\n");
532  (void)ina226_deinit(&gs_handle);
533 
534  return 1;
535  }
536  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_128 ? "ok" : "error");
537 
538  /* set average 256 */
539  res = ina226_set_average_mode(&gs_handle, INA226_AVG_256);
540  if (res != 0)
541  {
542  ina226_interface_debug_print("ina226: set average mode failed.\n");
543  (void)ina226_deinit(&gs_handle);
544 
545  return 1;
546  }
547  ina226_interface_debug_print("ina226: set average 256.\n");
548  res = ina226_get_average_mode(&gs_handle, &mode);
549  if (res != 0)
550  {
551  ina226_interface_debug_print("ina226: get average mode failed.\n");
552  (void)ina226_deinit(&gs_handle);
553 
554  return 1;
555  }
556  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_256 ? "ok" : "error");
557 
558  /* set average 512 */
559  res = ina226_set_average_mode(&gs_handle, INA226_AVG_512);
560  if (res != 0)
561  {
562  ina226_interface_debug_print("ina226: set average mode failed.\n");
563  (void)ina226_deinit(&gs_handle);
564 
565  return 1;
566  }
567  ina226_interface_debug_print("ina226: set average 512.\n");
568  res = ina226_get_average_mode(&gs_handle, &mode);
569  if (res != 0)
570  {
571  ina226_interface_debug_print("ina226: get average mode failed.\n");
572  (void)ina226_deinit(&gs_handle);
573 
574  return 1;
575  }
576  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_512 ? "ok" : "error");
577 
578  /* set average 1024 */
579  res = ina226_set_average_mode(&gs_handle, INA226_AVG_1024);
580  if (res != 0)
581  {
582  ina226_interface_debug_print("ina226: set average mode failed.\n");
583  (void)ina226_deinit(&gs_handle);
584 
585  return 1;
586  }
587  ina226_interface_debug_print("ina226: set average 1024.\n");
588  res = ina226_get_average_mode(&gs_handle, &mode);
589  if (res != 0)
590  {
591  ina226_interface_debug_print("ina226: get average mode failed.\n");
592  (void)ina226_deinit(&gs_handle);
593 
594  return 1;
595  }
596  ina226_interface_debug_print("ina226: check average mode %s.\n", mode == INA226_AVG_1024 ? "ok" : "error");
597 
598  /* ina226_set_bus_voltage_conversion_time/ina226_get_bus_voltage_conversion_time test */
599  ina226_interface_debug_print("ina226: ina226_set_bus_voltage_conversion_time/ina226_get_bus_voltage_conversion_time test.\n");
600 
601  /* set bus voltage conversion time 140us */
603  if (res != 0)
604  {
605  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
606  (void)ina226_deinit(&gs_handle);
607 
608  return 1;
609  }
610  ina226_interface_debug_print("ina226: set bus voltage conversion time 140us.\n");
611  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
612  if (res != 0)
613  {
614  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
615  (void)ina226_deinit(&gs_handle);
616 
617  return 1;
618  }
619  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_140_US ? "ok" : "error");
620 
621  /* set bus voltage conversion time 204us */
623  if (res != 0)
624  {
625  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
626  (void)ina226_deinit(&gs_handle);
627 
628  return 1;
629  }
630  ina226_interface_debug_print("ina226: set bus voltage conversion time 204us.\n");
631  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
632  if (res != 0)
633  {
634  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
635  (void)ina226_deinit(&gs_handle);
636 
637  return 1;
638  }
639  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_204_US ? "ok" : "error");
640 
641  /* set bus voltage conversion time 332us */
643  if (res != 0)
644  {
645  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
646  (void)ina226_deinit(&gs_handle);
647 
648  return 1;
649  }
650  ina226_interface_debug_print("ina226: set bus voltage conversion time 332us.\n");
651  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
652  if (res != 0)
653  {
654  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
655  (void)ina226_deinit(&gs_handle);
656 
657  return 1;
658  }
659  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_332_US ? "ok" : "error");
660 
661  /* set bus voltage conversion time 588us */
663  if (res != 0)
664  {
665  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
666  (void)ina226_deinit(&gs_handle);
667 
668  return 1;
669  }
670  ina226_interface_debug_print("ina226: set bus voltage conversion time 588us.\n");
671  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
672  if (res != 0)
673  {
674  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
675  (void)ina226_deinit(&gs_handle);
676 
677  return 1;
678  }
679  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_588_US ? "ok" : "error");
680 
681  /* set bus voltage conversion time 1.1ms */
683  if (res != 0)
684  {
685  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
686  (void)ina226_deinit(&gs_handle);
687 
688  return 1;
689  }
690  ina226_interface_debug_print("ina226: set bus voltage conversion time 1.1ms.\n");
691  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
692  if (res != 0)
693  {
694  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
695  (void)ina226_deinit(&gs_handle);
696 
697  return 1;
698  }
699  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_1P1_MS ? "ok" : "error");
700 
701  /* set bus voltage conversion time 2.116ms */
703  if (res != 0)
704  {
705  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
706  (void)ina226_deinit(&gs_handle);
707 
708  return 1;
709  }
710  ina226_interface_debug_print("ina226: set bus voltage conversion time 2.116ms.\n");
711  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
712  if (res != 0)
713  {
714  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
715  (void)ina226_deinit(&gs_handle);
716 
717  return 1;
718  }
719  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_2P116_MS ? "ok" : "error");
720 
721  /* set bus voltage conversion time 4.156ms */
723  if (res != 0)
724  {
725  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
726  (void)ina226_deinit(&gs_handle);
727 
728  return 1;
729  }
730  ina226_interface_debug_print("ina226: set bus voltage conversion time 4.156ms.\n");
731  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
732  if (res != 0)
733  {
734  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
735  (void)ina226_deinit(&gs_handle);
736 
737  return 1;
738  }
739  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_4P156_MS ? "ok" : "error");
740 
741  /* set bus voltage conversion time 8.244ms */
743  if (res != 0)
744  {
745  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
746  (void)ina226_deinit(&gs_handle);
747 
748  return 1;
749  }
750  ina226_interface_debug_print("ina226: set bus voltage conversion time 8.244ms.\n");
751  res = ina226_get_bus_voltage_conversion_time(&gs_handle, &t);
752  if (res != 0)
753  {
754  ina226_interface_debug_print("ina226: get bus voltage conversion time failed.\n");
755  (void)ina226_deinit(&gs_handle);
756 
757  return 1;
758  }
759  ina226_interface_debug_print("ina226: check bus voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_8P244_MS ? "ok" : "error");
760 
761  /* ina226_set_shunt_voltage_conversion_time/ina226_get_shunt_voltage_conversion_time test */
762  ina226_interface_debug_print("ina226: ina226_set_shunt_voltage_conversion_time/ina226_get_shunt_voltage_conversion_time test.\n");
763 
764  /* set shunt voltage conversion time 140us */
766  if (res != 0)
767  {
768  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
769  (void)ina226_deinit(&gs_handle);
770 
771  return 1;
772  }
773  ina226_interface_debug_print("ina226: set shunt voltage conversion time 140us.\n");
774  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
775  if (res != 0)
776  {
777  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
778  (void)ina226_deinit(&gs_handle);
779 
780  return 1;
781  }
782  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_140_US ? "ok" : "error");
783 
784  /* set shunt voltage conversion time 204us */
786  if (res != 0)
787  {
788  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
789  (void)ina226_deinit(&gs_handle);
790 
791  return 1;
792  }
793  ina226_interface_debug_print("ina226: set shunt voltage conversion time 204us.\n");
794  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
795  if (res != 0)
796  {
797  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
798  (void)ina226_deinit(&gs_handle);
799 
800  return 1;
801  }
802  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_204_US ? "ok" : "error");
803 
804  /* set shunt voltage conversion time 332us */
806  if (res != 0)
807  {
808  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
809  (void)ina226_deinit(&gs_handle);
810 
811  return 1;
812  }
813  ina226_interface_debug_print("ina226: set shunt voltage conversion time 332us.\n");
814  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
815  if (res != 0)
816  {
817  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
818  (void)ina226_deinit(&gs_handle);
819 
820  return 1;
821  }
822  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_332_US ? "ok" : "error");
823 
824  /* set shunt voltage conversion time 588us */
826  if (res != 0)
827  {
828  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
829  (void)ina226_deinit(&gs_handle);
830 
831  return 1;
832  }
833  ina226_interface_debug_print("ina226: set shunt voltage conversion time 588us.\n");
834  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
835  if (res != 0)
836  {
837  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
838  (void)ina226_deinit(&gs_handle);
839 
840  return 1;
841  }
842  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_588_US ? "ok" : "error");
843 
844  /* set shunt voltage conversion time 1.1ms */
846  if (res != 0)
847  {
848  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
849  (void)ina226_deinit(&gs_handle);
850 
851  return 1;
852  }
853  ina226_interface_debug_print("ina226: set shunt voltage conversion time 1.1ms.\n");
854  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
855  if (res != 0)
856  {
857  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
858  (void)ina226_deinit(&gs_handle);
859 
860  return 1;
861  }
862  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_1P1_MS ? "ok" : "error");
863 
864  /* set shunt voltage conversion time 2.116ms */
866  if (res != 0)
867  {
868  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
869  (void)ina226_deinit(&gs_handle);
870 
871  return 1;
872  }
873  ina226_interface_debug_print("ina226: set shunt voltage conversion time 2.116ms.\n");
874  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
875  if (res != 0)
876  {
877  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
878  (void)ina226_deinit(&gs_handle);
879 
880  return 1;
881  }
882  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_2P116_MS ? "ok" : "error");
883 
884  /* set shunt voltage conversion time 4.156ms */
886  if (res != 0)
887  {
888  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
889  (void)ina226_deinit(&gs_handle);
890 
891  return 1;
892  }
893  ina226_interface_debug_print("ina226: set shunt voltage conversion time 4.156ms.\n");
894  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
895  if (res != 0)
896  {
897  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
898  (void)ina226_deinit(&gs_handle);
899 
900  return 1;
901  }
902  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_4P156_MS ? "ok" : "error");
903 
904  /* set shunt voltage conversion time 8.244ms */
906  if (res != 0)
907  {
908  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
909  (void)ina226_deinit(&gs_handle);
910 
911  return 1;
912  }
913  ina226_interface_debug_print("ina226: set shunt voltage conversion time 8.244ms.\n");
914  res = ina226_get_shunt_voltage_conversion_time(&gs_handle, &t);
915  if (res != 0)
916  {
917  ina226_interface_debug_print("ina226: get shunt voltage conversion time failed.\n");
918  (void)ina226_deinit(&gs_handle);
919 
920  return 1;
921  }
922  ina226_interface_debug_print("ina226: check shunt voltage conversion time %s.\n", t == INA226_CONVERSION_TIME_8P244_MS ? "ok" : "error");
923 
924  /* ina226_set_mode/ina226_get_mode test */
925  ina226_interface_debug_print("ina226: ina226_set_mode/ina226_get_mode test.\n");
926 
927  /* set power down */
928  res = ina226_set_mode(&gs_handle, INA226_MODE_POWER_DOWN);
929  if (res != 0)
930  {
931  ina226_interface_debug_print("ina226: set mode failed.\n");
932  (void)ina226_deinit(&gs_handle);
933 
934  return 1;
935  }
936  ina226_interface_debug_print("ina226: set mode power down.\n");
937  res = ina226_get_mode(&gs_handle, &chip_mode);
938  if (res != 0)
939  {
940  ina226_interface_debug_print("ina226: get mode failed.\n");
941  (void)ina226_deinit(&gs_handle);
942 
943  return 1;
944  }
945  ina226_interface_debug_print("ina226: check mode %s.\n", chip_mode == INA226_MODE_POWER_DOWN ? "ok" : "error");
946 
947  /* set shutdown */
948  res = ina226_set_mode(&gs_handle, INA226_MODE_SHUTDOWN);
949  if (res != 0)
950  {
951  ina226_interface_debug_print("ina226: set mode failed.\n");
952  (void)ina226_deinit(&gs_handle);
953 
954  return 1;
955  }
956  ina226_interface_debug_print("ina226: set mode shutdown.\n");
957  res = ina226_get_mode(&gs_handle, &chip_mode);
958  if (res != 0)
959  {
960  ina226_interface_debug_print("ina226: get mode failed.\n");
961  (void)ina226_deinit(&gs_handle);
962 
963  return 1;
964  }
965  ina226_interface_debug_print("ina226: check mode %s.\n", chip_mode == INA226_MODE_SHUTDOWN ? "ok" : "error");
966 
967  /* set shunt voltage continuous */
969  if (res != 0)
970  {
971  ina226_interface_debug_print("ina226: set mode failed.\n");
972  (void)ina226_deinit(&gs_handle);
973 
974  return 1;
975  }
976  ina226_interface_debug_print("ina226: set mode shunt voltage continuous.\n");
977  res = ina226_get_mode(&gs_handle, &chip_mode);
978  if (res != 0)
979  {
980  ina226_interface_debug_print("ina226: get mode failed.\n");
981  (void)ina226_deinit(&gs_handle);
982 
983  return 1;
984  }
985  ina226_interface_debug_print("ina226: check mode %s.\n", chip_mode == INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS ? "ok" : "error");
986 
987  /* set bus voltage continuous */
989  if (res != 0)
990  {
991  ina226_interface_debug_print("ina226: set mode failed.\n");
992  (void)ina226_deinit(&gs_handle);
993 
994  return 1;
995  }
996  ina226_interface_debug_print("ina226: set mode bus voltage continuous.\n");
997  res = ina226_get_mode(&gs_handle, &chip_mode);
998  if (res != 0)
999  {
1000  ina226_interface_debug_print("ina226: get mode failed.\n");
1001  (void)ina226_deinit(&gs_handle);
1002 
1003  return 1;
1004  }
1005  ina226_interface_debug_print("ina226: check mode %s.\n", chip_mode == INA226_MODE_BUS_VOLTAGE_CONTINUOUS ? "ok" : "error");
1006 
1007  /* set shunt and bus voltage continuous */
1009  if (res != 0)
1010  {
1011  ina226_interface_debug_print("ina226: set mode failed.\n");
1012  (void)ina226_deinit(&gs_handle);
1013 
1014  return 1;
1015  }
1016  ina226_interface_debug_print("ina226: set mode shunt and bus voltage continuous.\n");
1017  res = ina226_get_mode(&gs_handle, &chip_mode);
1018  if (res != 0)
1019  {
1020  ina226_interface_debug_print("ina226: get mode failed.\n");
1021  (void)ina226_deinit(&gs_handle);
1022 
1023  return 1;
1024  }
1025  ina226_interface_debug_print("ina226: check mode %s.\n", chip_mode == INA226_MODE_SHUNT_BUS_VOLTAGE_CONTINUOUS ? "ok" : "error");
1026 
1027  /* ina226_set_calibration/ina226_get_calibration test */
1028  ina226_interface_debug_print("ina226: ina226_set_calibration/ina226_get_calibration test.\n");
1029 
1030  data = rand() % 0x7FFFU;
1031  res = ina226_set_calibration(&gs_handle, data);
1032  if (res != 0)
1033  {
1034  ina226_interface_debug_print("ina226: set calibration failed.\n");
1035  (void)ina226_deinit(&gs_handle);
1036 
1037  return 1;
1038  }
1039  ina226_interface_debug_print("ina226: set calibration 0x%04X.\n", data);
1040  res = ina226_get_calibration(&gs_handle, &data_check);
1041  if (res != 0)
1042  {
1043  ina226_interface_debug_print("ina226: get calibration failed.\n");
1044  (void)ina226_deinit(&gs_handle);
1045 
1046  return 1;
1047  }
1048  ina226_interface_debug_print("ina226: check calibration %s.\n", data == data_check ? "ok" : "error");
1049 
1050  /* ina226_calculate_calibration test */
1051  ina226_interface_debug_print("ina226: ina226_calculate_calibration test.\n");
1052 
1053  /* calculate calibration */
1054  res = ina226_calculate_calibration(&gs_handle, &data_check);
1055  if (res != 0)
1056  {
1057  ina226_interface_debug_print("ina226: calculate calibration failed.\n");
1058  (void)ina226_deinit(&gs_handle);
1059 
1060  return 1;
1061  }
1062  ina226_interface_debug_print("ina226: calculate calibration is 0x%04X.\n", data_check);
1063 
1064  /* ina226_set_mask/ina226_get_mask test */
1065  ina226_interface_debug_print("ina226: ina226_set_mask/ina226_get_mask test.\n");
1066 
1067  /* enable shunt voltage over voltage */
1069  if (res != 0)
1070  {
1071  ina226_interface_debug_print("ina226: set mask failed.\n");
1072  (void)ina226_deinit(&gs_handle);
1073 
1074  return 1;
1075  }
1076  ina226_interface_debug_print("ina226: enable shunt voltage over voltage.\n");
1077  res = ina226_get_mask(&gs_handle, INA226_MASK_SHUNT_VOLTAGE_OVER_VOLTAGE, &enable);
1078  if (res != 0)
1079  {
1080  ina226_interface_debug_print("ina226: get mask failed.\n");
1081  (void)ina226_deinit(&gs_handle);
1082 
1083  return 1;
1084  }
1085  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1086 
1087  /* disable shunt voltage over voltage */
1089  if (res != 0)
1090  {
1091  ina226_interface_debug_print("ina226: set mask failed.\n");
1092  (void)ina226_deinit(&gs_handle);
1093 
1094  return 1;
1095  }
1096  ina226_interface_debug_print("ina226: disable shunt voltage over voltage.\n");
1097  res = ina226_get_mask(&gs_handle, INA226_MASK_SHUNT_VOLTAGE_OVER_VOLTAGE, &enable);
1098  if (res != 0)
1099  {
1100  ina226_interface_debug_print("ina226: get mask failed.\n");
1101  (void)ina226_deinit(&gs_handle);
1102 
1103  return 1;
1104  }
1105  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1106 
1107  /* enable shunt voltage under voltage */
1109  if (res != 0)
1110  {
1111  ina226_interface_debug_print("ina226: set mask failed.\n");
1112  (void)ina226_deinit(&gs_handle);
1113 
1114  return 1;
1115  }
1116  ina226_interface_debug_print("ina226: enable shunt voltage under voltage.\n");
1117  res = ina226_get_mask(&gs_handle, INA226_MASK_SHUNT_VOLTAGE_UNDER_VOLTAGE, &enable);
1118  if (res != 0)
1119  {
1120  ina226_interface_debug_print("ina226: get mask failed.\n");
1121  (void)ina226_deinit(&gs_handle);
1122 
1123  return 1;
1124  }
1125  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1126 
1127  /* disable shunt voltage under voltage */
1129  if (res != 0)
1130  {
1131  ina226_interface_debug_print("ina226: set mask failed.\n");
1132  (void)ina226_deinit(&gs_handle);
1133 
1134  return 1;
1135  }
1136  ina226_interface_debug_print("ina226: disable shunt voltage under voltage.\n");
1137  res = ina226_get_mask(&gs_handle, INA226_MASK_SHUNT_VOLTAGE_UNDER_VOLTAGE, &enable);
1138  if (res != 0)
1139  {
1140  ina226_interface_debug_print("ina226: get mask failed.\n");
1141  (void)ina226_deinit(&gs_handle);
1142 
1143  return 1;
1144  }
1145  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1146 
1147  /* enable bus voltage over voltage */
1149  if (res != 0)
1150  {
1151  ina226_interface_debug_print("ina226: set mask failed.\n");
1152  (void)ina226_deinit(&gs_handle);
1153 
1154  return 1;
1155  }
1156  ina226_interface_debug_print("ina226: enable bus voltage over voltage.\n");
1157  res = ina226_get_mask(&gs_handle, INA226_MASK_BUS_VOLTAGE_OVER_VOLTAGE, &enable);
1158  if (res != 0)
1159  {
1160  ina226_interface_debug_print("ina226: get mask failed.\n");
1161  (void)ina226_deinit(&gs_handle);
1162 
1163  return 1;
1164  }
1165  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1166 
1167  /* disable bus voltage over voltage */
1169  if (res != 0)
1170  {
1171  ina226_interface_debug_print("ina226: set mask failed.\n");
1172  (void)ina226_deinit(&gs_handle);
1173 
1174  return 1;
1175  }
1176  ina226_interface_debug_print("ina226: disable bus voltage over voltage.\n");
1177  res = ina226_get_mask(&gs_handle, INA226_MASK_BUS_VOLTAGE_OVER_VOLTAGE, &enable);
1178  if (res != 0)
1179  {
1180  ina226_interface_debug_print("ina226: get mask failed.\n");
1181  (void)ina226_deinit(&gs_handle);
1182 
1183  return 1;
1184  }
1185  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1186 
1187  /* enable bus voltage under voltage */
1189  if (res != 0)
1190  {
1191  ina226_interface_debug_print("ina226: set mask failed.\n");
1192  (void)ina226_deinit(&gs_handle);
1193 
1194  return 1;
1195  }
1196  ina226_interface_debug_print("ina226: enable bus voltage under voltage.\n");
1197  res = ina226_get_mask(&gs_handle, INA226_MASK_BUS_VOLTAGE_UNDER_VOLTAGE, &enable);
1198  if (res != 0)
1199  {
1200  ina226_interface_debug_print("ina226: get mask failed.\n");
1201  (void)ina226_deinit(&gs_handle);
1202 
1203  return 1;
1204  }
1205  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1206 
1207  /* disable bus voltage under voltage */
1209  if (res != 0)
1210  {
1211  ina226_interface_debug_print("ina226: set mask failed.\n");
1212  (void)ina226_deinit(&gs_handle);
1213 
1214  return 1;
1215  }
1216  ina226_interface_debug_print("ina226: disable bus voltage under voltage.\n");
1217  res = ina226_get_mask(&gs_handle, INA226_MASK_BUS_VOLTAGE_UNDER_VOLTAGE, &enable);
1218  if (res != 0)
1219  {
1220  ina226_interface_debug_print("ina226: get mask failed.\n");
1221  (void)ina226_deinit(&gs_handle);
1222 
1223  return 1;
1224  }
1225  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1226 
1227  /* enable power over limit */
1229  if (res != 0)
1230  {
1231  ina226_interface_debug_print("ina226: set mask failed.\n");
1232  (void)ina226_deinit(&gs_handle);
1233 
1234  return 1;
1235  }
1236  ina226_interface_debug_print("ina226: enable power over limit.\n");
1237  res = ina226_get_mask(&gs_handle, INA226_MASK_POWER_OVER_LIMIT, &enable);
1238  if (res != 0)
1239  {
1240  ina226_interface_debug_print("ina226: get mask failed.\n");
1241  (void)ina226_deinit(&gs_handle);
1242 
1243  return 1;
1244  }
1245  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1246 
1247  /* disable power over limit */
1249  if (res != 0)
1250  {
1251  ina226_interface_debug_print("ina226: set mask failed.\n");
1252  (void)ina226_deinit(&gs_handle);
1253 
1254  return 1;
1255  }
1256  ina226_interface_debug_print("ina226: disable power over limit.\n");
1257  res = ina226_get_mask(&gs_handle, INA226_MASK_POWER_OVER_LIMIT, &enable);
1258  if (res != 0)
1259  {
1260  ina226_interface_debug_print("ina226: get mask failed.\n");
1261  (void)ina226_deinit(&gs_handle);
1262 
1263  return 1;
1264  }
1265  ina226_interface_debug_print("ina226: check mask %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1266 
1267  /* ina226_set_conversion_ready_alert_pin/ina226_get_conversion_ready_alert_pin test */
1268  ina226_interface_debug_print("ina226: ina226_set_conversion_ready_alert_pin/ina226_get_conversion_ready_alert_pin test.\n");
1269 
1270  /* enable conversion ready alert pin */
1272  if (res != 0)
1273  {
1274  ina226_interface_debug_print("ina226: set conversion ready alert pin failed.\n");
1275  (void)ina226_deinit(&gs_handle);
1276 
1277  return 1;
1278  }
1279  ina226_interface_debug_print("ina226: enable conversion ready alert pin.\n");
1280  res = ina226_get_conversion_ready_alert_pin(&gs_handle, &enable);
1281  if (res != 0)
1282  {
1283  ina226_interface_debug_print("ina226: get conversion ready alert pin failed.\n");
1284  (void)ina226_deinit(&gs_handle);
1285 
1286  return 1;
1287  }
1288  ina226_interface_debug_print("ina226: check conversion ready alert pin %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1289 
1290  /* disable conversion ready alert pin */
1292  if (res != 0)
1293  {
1294  ina226_interface_debug_print("ina226: set conversion ready alert pin failed.\n");
1295  (void)ina226_deinit(&gs_handle);
1296 
1297  return 1;
1298  }
1299  ina226_interface_debug_print("ina226: disable conversion ready alert pin.\n");
1300  res = ina226_get_conversion_ready_alert_pin(&gs_handle, &enable);
1301  if (res != 0)
1302  {
1303  ina226_interface_debug_print("ina226: get conversion ready alert pin failed.\n");
1304  (void)ina226_deinit(&gs_handle);
1305 
1306  return 1;
1307  }
1308  ina226_interface_debug_print("ina226: check conversion ready alert pin %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1309 
1310  /* ina226_set_alert_polarity_pin/ina226_get_alert_polarity_pin test */
1311  ina226_interface_debug_print("ina226: ina226_set_alert_polarity_pin/ina226_get_alert_polarity_pin test.\n");
1312 
1313  /* set alert polarity pin normal */
1315  if (res != 0)
1316  {
1317  ina226_interface_debug_print("ina226: set alert polarity pin failed.\n");
1318  (void)ina226_deinit(&gs_handle);
1319 
1320  return 1;
1321  }
1322  ina226_interface_debug_print("ina226: set alert polarity pin normal.\n");
1323  res = ina226_get_alert_polarity_pin(&gs_handle, &pin);
1324  if (res != 0)
1325  {
1326  ina226_interface_debug_print("ina226: get alert polarity pin failed.\n");
1327  (void)ina226_deinit(&gs_handle);
1328 
1329  return 1;
1330  }
1331  ina226_interface_debug_print("ina226: check alert polarity pin %s.\n", pin == INA226_ALERT_POLARITY_NORMAL ? "ok" : "error");
1332 
1333  /* set alert polarity pin inverted */
1335  if (res != 0)
1336  {
1337  ina226_interface_debug_print("ina226: set alert polarity pin failed.\n");
1338  (void)ina226_deinit(&gs_handle);
1339 
1340  return 1;
1341  }
1342  ina226_interface_debug_print("ina226: set alert polarity pin inverted.\n");
1343  res = ina226_get_alert_polarity_pin(&gs_handle, &pin);
1344  if (res != 0)
1345  {
1346  ina226_interface_debug_print("ina226: get alert polarity pin failed.\n");
1347  (void)ina226_deinit(&gs_handle);
1348 
1349  return 1;
1350  }
1351  ina226_interface_debug_print("ina226: check alert polarity pin %s.\n", pin == INA226_ALERT_POLARITY_INVERTED ? "ok" : "error");
1352 
1353  /* ina226_set_alert_latch/ina226_get_alert_latch test */
1354  ina226_interface_debug_print("ina226: ina226_set_alert_latch/ina226_get_alert_latch test.\n");
1355 
1356  /* enable alert latch */
1357  res = ina226_set_alert_latch(&gs_handle, INA226_BOOL_TRUE);
1358  if (res != 0)
1359  {
1360  ina226_interface_debug_print("ina226: set alert latch failed.\n");
1361  (void)ina226_deinit(&gs_handle);
1362 
1363  return 1;
1364  }
1365  ina226_interface_debug_print("ina226: enable alert latch.\n");
1366  res = ina226_get_alert_latch(&gs_handle, &enable);
1367  if (res != 0)
1368  {
1369  ina226_interface_debug_print("ina226: get alert latch failed.\n");
1370  (void)ina226_deinit(&gs_handle);
1371 
1372  return 1;
1373  }
1374  ina226_interface_debug_print("ina226: check alert latch %s.\n", enable == INA226_BOOL_TRUE ? "ok" : "error");
1375 
1376  /* disable alert latch */
1377  res = ina226_set_alert_latch(&gs_handle, INA226_BOOL_FALSE);
1378  if (res != 0)
1379  {
1380  ina226_interface_debug_print("ina226: set alert latch failed.\n");
1381  (void)ina226_deinit(&gs_handle);
1382 
1383  return 1;
1384  }
1385  ina226_interface_debug_print("ina226: disable alert latch.\n");
1386  res = ina226_get_alert_latch(&gs_handle, &enable);
1387  if (res != 0)
1388  {
1389  ina226_interface_debug_print("ina226: get alert latch failed.\n");
1390  (void)ina226_deinit(&gs_handle);
1391 
1392  return 1;
1393  }
1394  ina226_interface_debug_print("ina226: check alert latch %s.\n", enable == INA226_BOOL_FALSE ? "ok" : "error");
1395 
1396  /* ina226_set_alert_limit/ina226_get_alert_limit test */
1397  ina226_interface_debug_print("ina226: ina226_set_alert_limit/ina226_get_alert_limit test.\n");
1398 
1399  data = rand() % 0xFFFFU;
1400  res = ina226_set_alert_limit(&gs_handle, data);
1401  if (res != 0)
1402  {
1403  ina226_interface_debug_print("ina226: set alert limit failed.\n");
1404  (void)ina226_deinit(&gs_handle);
1405 
1406  return 1;
1407  }
1408  ina226_interface_debug_print("ina226: set alert limit 0x%04X.\n", data);
1409  res = ina226_get_alert_limit(&gs_handle, &data_check);
1410  if (res != 0)
1411  {
1412  ina226_interface_debug_print("ina226: get alert limit failed.\n");
1413  (void)ina226_deinit(&gs_handle);
1414 
1415  return 1;
1416  }
1417  ina226_interface_debug_print("ina226: check alert limit %s.\n", data == data_check ? "ok" : "error");
1418 
1419  /* ina226_shunt_voltage_convert_to_register/ina226_shunt_voltage_convert_to_data test */
1420  ina226_interface_debug_print("ina226: ina226_shunt_voltage_convert_to_register/ina226_shunt_voltage_convert_to_data test.\n");
1421 
1422  f = (float)(rand() % 1000) / 100.0f;
1423  res = ina226_shunt_voltage_convert_to_register(&gs_handle, f, &data);
1424  if (res != 0)
1425  {
1426  ina226_interface_debug_print("ina226: shunt voltage convert to register failed.\n");
1427  (void)ina226_deinit(&gs_handle);
1428 
1429  return 1;
1430  }
1431  ina226_interface_debug_print("ina226: set shunt voltage %0.2fmV.\n", f);
1432  res = ina226_shunt_voltage_convert_to_data(&gs_handle, data, &f_check);
1433  if (res != 0)
1434  {
1435  ina226_interface_debug_print("ina226: shunt voltage convert to data failed.\n");
1436  (void)ina226_deinit(&gs_handle);
1437 
1438  return 1;
1439  }
1440  ina226_interface_debug_print("ina226: check shunt voltage %0.2fmV.\n", f_check);
1441 
1442  /* ina226_bus_voltage_convert_to_register/ina226_bus_voltage_convert_to_data test */
1443  ina226_interface_debug_print("ina226: ina226_bus_voltage_convert_to_register/ina226_bus_voltage_convert_to_data test.\n");
1444 
1445  f = (float)(rand() % 1000) / 100.0f;
1446  res = ina226_bus_voltage_convert_to_register(&gs_handle, f, &data);
1447  if (res != 0)
1448  {
1449  ina226_interface_debug_print("ina226: bus voltage convert to register failed.\n");
1450  (void)ina226_deinit(&gs_handle);
1451 
1452  return 1;
1453  }
1454  ina226_interface_debug_print("ina226: set bus voltage %0.2fmV.\n", f);
1455  res = ina226_bus_voltage_convert_to_data(&gs_handle, data, &f_check);
1456  if (res != 0)
1457  {
1458  ina226_interface_debug_print("ina226: bus voltage convert to data failed.\n");
1459  (void)ina226_deinit(&gs_handle);
1460 
1461  return 1;
1462  }
1463  ina226_interface_debug_print("ina226: check bus voltage %0.2fmV.\n", f_check);
1464 
1465  /* ina226_power_convert_to_register/ina226_power_convert_to_data test */
1466  ina226_interface_debug_print("ina226: ina226_power_convert_to_register/ina226_power_convert_to_data test.\n");
1467 
1468  f = (float)(rand() % 1000) / 100.0f;
1469  res = ina226_power_convert_to_register(&gs_handle, f, &data);
1470  if (res != 0)
1471  {
1472  ina226_interface_debug_print("ina226: power convert to register failed.\n");
1473  (void)ina226_deinit(&gs_handle);
1474 
1475  return 1;
1476  }
1477  ina226_interface_debug_print("ina226: set power %0.2fmW.\n", f);
1478  res = ina226_power_convert_to_data(&gs_handle, data, &f_check);
1479  if (res != 0)
1480  {
1481  ina226_interface_debug_print("ina226: power convert to data failed.\n");
1482  (void)ina226_deinit(&gs_handle);
1483 
1484  return 1;
1485  }
1486  ina226_interface_debug_print("ina226: check power %0.2fmW.\n", f_check);
1487 
1488  /* ina226_get_die_id test */
1489  ina226_interface_debug_print("ina226: ina226_get_die_id test.\n");
1490 
1491  /* get die id */
1492  res = ina226_get_die_id(&gs_handle, &device_id, &die_revision_id);
1493  if (res != 0)
1494  {
1495  ina226_interface_debug_print("ina226: get die id failed.\n");
1496  (void)ina226_deinit(&gs_handle);
1497 
1498  return 1;
1499  }
1500 
1501  /* output */
1502  ina226_interface_debug_print("ina226: device id is 0x%04X.\n", device_id);
1503 
1504  /* output */
1505  ina226_interface_debug_print("ina226: die revision id is 0x%02X.\n", die_revision_id);
1506 
1507  /* ina226_soft_reset test */
1508  ina226_interface_debug_print("ina226: ina226_soft_reset test.\n");
1509 
1510  /* soft reset */
1511  res = ina226_soft_reset(&gs_handle);
1512  if (res != 0)
1513  {
1514  ina226_interface_debug_print("ina226: soft reset failed.\n");
1515  (void)ina226_deinit(&gs_handle);
1516 
1517  return 1;
1518  }
1519 
1520  /* finish register test */
1521  (void)ina226_deinit(&gs_handle);
1522  ina226_interface_debug_print("ina226: finish register test.\n");
1523 
1524  return 0;
1525 }
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
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_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
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_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_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_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
@ 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_ADDRESS_3
Definition: driver_ina226.h:74
@ INA226_ADDRESS_F
Definition: driver_ina226.h:86
@ INA226_ADDRESS_4
Definition: driver_ina226.h:75
@ INA226_ADDRESS_6
Definition: driver_ina226.h:77
@ INA226_ADDRESS_0
Definition: driver_ina226.h:71
@ INA226_ADDRESS_9
Definition: driver_ina226.h:80
@ INA226_ADDRESS_7
Definition: driver_ina226.h:78
@ INA226_ADDRESS_1
Definition: driver_ina226.h:72
@ INA226_ADDRESS_8
Definition: driver_ina226.h:79
@ INA226_ADDRESS_A
Definition: driver_ina226.h:81
@ INA226_ADDRESS_B
Definition: driver_ina226.h:82
@ INA226_ADDRESS_2
Definition: driver_ina226.h:73
@ INA226_ADDRESS_E
Definition: driver_ina226.h:85
@ INA226_ADDRESS_5
Definition: driver_ina226.h:76
@ INA226_ADDRESS_D
Definition: driver_ina226.h:84
@ INA226_ADDRESS_C
Definition: driver_ina226.h:83
@ INA226_MODE_BUS_VOLTAGE_CONTINUOUS
@ INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS
@ INA226_MODE_POWER_DOWN
@ INA226_MODE_SHUNT_BUS_VOLTAGE_CONTINUOUS
@ INA226_MODE_SHUTDOWN
@ INA226_BOOL_TRUE
Definition: driver_ina226.h:95
@ INA226_BOOL_FALSE
Definition: driver_ina226.h:94
@ INA226_CONVERSION_TIME_4P156_MS
@ INA226_CONVERSION_TIME_588_US
@ INA226_CONVERSION_TIME_1P1_MS
@ INA226_CONVERSION_TIME_332_US
@ INA226_CONVERSION_TIME_140_US
@ INA226_CONVERSION_TIME_204_US
@ INA226_CONVERSION_TIME_8P244_MS
@ INA226_CONVERSION_TIME_2P116_MS
@ INA226_AVG_64
@ INA226_AVG_1024
@ INA226_AVG_512
@ INA226_AVG_4
@ INA226_AVG_128
@ INA226_AVG_16
@ INA226_AVG_1
@ INA226_AVG_256
@ INA226_ALERT_POLARITY_NORMAL
@ INA226_ALERT_POLARITY_INVERTED
void ina226_interface_receive_callback(uint8_t type)
interface receive callback
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_register_test(ina226_address_t addr_pin)
register test
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]