LibDriver INA219
Loading...
Searching...
No Matches
driver_ina219_read_test.c
Go to the documentation of this file.
1
36
38
39static ina219_handle_t gs_handle;
40
51uint8_t ina219_read_test(ina219_address_t addr_pin, double r, uint32_t times)
52{
53 uint8_t res;
54 uint32_t i;
55 uint16_t calibration;
56 ina219_info_t info;
57
58 /* link interface function */
66
67 /* get information */
68 res = ina219_info(&info);
69 if (res != 0)
70 {
71 ina219_interface_debug_print("ina219: get info failed.\n");
72
73 return 1;
74 }
75 else
76 {
77 /* print chip info */
78 ina219_interface_debug_print("ina219: chip is %s.\n", info.chip_name);
79 ina219_interface_debug_print("ina219: manufacturer is %s.\n", info.manufacturer_name);
80 ina219_interface_debug_print("ina219: interface is %s.\n", info.interface);
81 ina219_interface_debug_print("ina219: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
82 ina219_interface_debug_print("ina219: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
83 ina219_interface_debug_print("ina219: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
84 ina219_interface_debug_print("ina219: max current is %0.2fmA.\n", info.max_current_ma);
85 ina219_interface_debug_print("ina219: max temperature is %0.1fC.\n", info.temperature_max);
86 ina219_interface_debug_print("ina219: min temperature is %0.1fC.\n", info.temperature_min);
87 }
88
89 /* start read test */
90 ina219_interface_debug_print("ina219: start read test.\n");
91
92 /* set addr pin */
93 res = ina219_set_addr_pin(&gs_handle, addr_pin);
94 if (res != 0)
95 {
96 ina219_interface_debug_print("ina219: set addr pin failed.\n");
97
98 return 1;
99 }
100
101 /* set the r */
102 res = ina219_set_resistance(&gs_handle, r);
103 if (res != 0)
104 {
105 ina219_interface_debug_print("ina219: set resistance failed.\n");
106
107 return 1;
108 }
109
110 /* init */
111 res = ina219_init(&gs_handle);
112 if (res != 0)
113 {
114 ina219_interface_debug_print("ina219: init failed.\n");
115
116 return 1;
117 }
118
119 /* set bus voltage range 32V */
121 if (res != 0)
122 {
123 ina219_interface_debug_print("ina219: set bus voltage range failed.\n");
124 (void)ina219_deinit(&gs_handle);
125
126 return 1;
127 }
128
129 /* set bus voltage adc mode 12 bit 1 sample */
131 if (res != 0)
132 {
133 ina219_interface_debug_print("ina219: set bus voltage adc mode failed.\n");
134 (void)ina219_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* set shunt voltage adc mode 12 bit 1 sample */
141 if (res != 0)
142 {
143 ina219_interface_debug_print("ina219: set shunt voltage adc mode failed.\n");
144 (void)ina219_deinit(&gs_handle);
145
146 return 1;
147 }
148
149 /* set shunt bus voltage continuous */
151 if (res != 0)
152 {
153 ina219_interface_debug_print("ina219: set mode failed.\n");
154 (void)ina219_deinit(&gs_handle);
155
156 return 1;
157 }
158
159 ina219_interface_debug_print("ina219: set pga 40 mV.\n");
160
161 /* set pga 40 mV */
162 res = ina219_set_pga(&gs_handle, INA219_PGA_40_MV);
163 if (res != 0)
164 {
165 ina219_interface_debug_print("ina219: set pga failed.\n");
166 (void)ina219_deinit(&gs_handle);
167
168 return 1;
169 }
170 res = ina219_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
171 if (res != 0)
172 {
173 ina219_interface_debug_print("ina219: calculate calibration failed.\n");
174 (void)ina219_deinit(&gs_handle);
175
176 return 1;
177 }
178 res = ina219_set_calibration(&gs_handle, calibration);
179 if (res != 0)
180 {
181 ina219_interface_debug_print("ina219: set calibration failed.\n");
182 (void)ina219_deinit(&gs_handle);
183
184 return 1;
185 }
186 for (i = 0; i < times; i++)
187 {
188 int16_t s_raw;
189 uint16_t u_raw;
190 float m;
191
192 /* read shunt voltage */
193 res = ina219_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
194 if (res != 0)
195 {
196 ina219_interface_debug_print("ina219: read shunt voltage failed.\n");
197 (void)ina219_deinit(&gs_handle);
198
199 return 1;
200 }
201 ina219_interface_debug_print("ina219: shunt voltage is %0.3fmV.\n", m);
202
203 /* read bus voltage */
204 res = ina219_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
205 if (res != 0)
206 {
207 ina219_interface_debug_print("ina219: read bus voltage failed.\n");
208 (void)ina219_deinit(&gs_handle);
209
210 return 1;
211 }
212 ina219_interface_debug_print("ina219: bus voltage is %0.3fmV.\n", m);
213
214 /* read current */
215 res = ina219_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
216 if (res != 0)
217 {
218 ina219_interface_debug_print("ina219: read current failed.\n");
219 (void)ina219_deinit(&gs_handle);
220
221 return 1;
222 }
223 ina219_interface_debug_print("ina219: current is %0.3fmA.\n", m);
224
225 /* read power */
226 res = ina219_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
227 if (res != 0)
228 {
229 ina219_interface_debug_print("ina219: read power failed.\n");
230 (void)ina219_deinit(&gs_handle);
231
232 return 1;
233 }
234 ina219_interface_debug_print("ina219: power is %0.3fmW.\n", m);
235
237 }
238
239 ina219_interface_debug_print("ina219: set pga 80 mV.\n");
240
241 /* set pga 80 mV */
242 res = ina219_set_pga(&gs_handle, INA219_PGA_80_MV);
243 if (res != 0)
244 {
245 ina219_interface_debug_print("ina219: set pga failed.\n");
246 (void)ina219_deinit(&gs_handle);
247
248 return 1;
249 }
250 res = ina219_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
251 if (res != 0)
252 {
253 ina219_interface_debug_print("ina219: calculate calibration failed.\n");
254 (void)ina219_deinit(&gs_handle);
255
256 return 1;
257 }
258 res = ina219_set_calibration(&gs_handle, calibration);
259 if (res != 0)
260 {
261 ina219_interface_debug_print("ina219: set calibration failed.\n");
262 (void)ina219_deinit(&gs_handle);
263
264 return 1;
265 }
266 for (i = 0; i < times; i++)
267 {
268 int16_t s_raw;
269 uint16_t u_raw;
270 float m;
271
272 /* read shunt voltage */
273 res = ina219_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
274 if (res != 0)
275 {
276 ina219_interface_debug_print("ina219: read shunt voltage failed.\n");
277 (void)ina219_deinit(&gs_handle);
278
279 return 1;
280 }
281 ina219_interface_debug_print("ina219: shunt voltage is %0.3fmV.\n", m);
282
283 /* read bus voltage */
284 res = ina219_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
285 if (res != 0)
286 {
287 ina219_interface_debug_print("ina219: read bus voltage failed.\n");
288 (void)ina219_deinit(&gs_handle);
289
290 return 1;
291 }
292 ina219_interface_debug_print("ina219: bus voltage is %0.3fmV.\n", m);
293
294 /* read current */
295 res = ina219_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
296 if (res != 0)
297 {
298 ina219_interface_debug_print("ina219: read current failed.\n");
299 (void)ina219_deinit(&gs_handle);
300
301 return 1;
302 }
303 ina219_interface_debug_print("ina219: current is %0.3fmA.\n", m);
304
305 /* read power */
306 res = ina219_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
307 if (res != 0)
308 {
309 ina219_interface_debug_print("ina219: read power failed.\n");
310 (void)ina219_deinit(&gs_handle);
311
312 return 1;
313 }
314 ina219_interface_debug_print("ina219: power is %0.3fmW.\n", m);
315
317 }
318
319 ina219_interface_debug_print("ina219: set pga 160 mV.\n");
320
321 /* set pga 160 mV */
322 res = ina219_set_pga(&gs_handle, INA219_PGA_160_MV);
323 if (res != 0)
324 {
325 ina219_interface_debug_print("ina219: set pga failed.\n");
326 (void)ina219_deinit(&gs_handle);
327
328 return 1;
329 }
330 res = ina219_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
331 if (res != 0)
332 {
333 ina219_interface_debug_print("ina219: calculate calibration failed.\n");
334 (void)ina219_deinit(&gs_handle);
335
336 return 1;
337 }
338 res = ina219_set_calibration(&gs_handle, calibration);
339 if (res != 0)
340 {
341 ina219_interface_debug_print("ina219: set calibration failed.\n");
342 (void)ina219_deinit(&gs_handle);
343
344 return 1;
345 }
346 for (i = 0; i < times; i++)
347 {
348 int16_t s_raw;
349 uint16_t u_raw;
350 float m;
351
352 /* read shunt voltage */
353 res = ina219_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
354 if (res != 0)
355 {
356 ina219_interface_debug_print("ina219: read shunt voltage failed.\n");
357 (void)ina219_deinit(&gs_handle);
358
359 return 1;
360 }
361 ina219_interface_debug_print("ina219: shunt voltage is %0.3fmV.\n", m);
362
363 /* read bus voltage */
364 res = ina219_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
365 if (res != 0)
366 {
367 ina219_interface_debug_print("ina219: read bus voltage failed.\n");
368 (void)ina219_deinit(&gs_handle);
369
370 return 1;
371 }
372 ina219_interface_debug_print("ina219: bus voltage is %0.3fmV.\n", m);
373
374 /* read current */
375 res = ina219_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
376 if (res != 0)
377 {
378 ina219_interface_debug_print("ina219: read current failed.\n");
379 (void)ina219_deinit(&gs_handle);
380
381 return 1;
382 }
383 ina219_interface_debug_print("ina219: current is %0.3fmA.\n", m);
384
385 /* read power */
386 res = ina219_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
387 if (res != 0)
388 {
389 ina219_interface_debug_print("ina219: read power failed.\n");
390 (void)ina219_deinit(&gs_handle);
391
392 return 1;
393 }
394 ina219_interface_debug_print("ina219: power is %0.3fmW.\n", m);
395
397 }
398
399 ina219_interface_debug_print("ina219: set pga 320 mV.\n");
400
401 /* set pga 320 mV */
402 res = ina219_set_pga(&gs_handle, INA219_PGA_320_MV);
403 if (res != 0)
404 {
405 ina219_interface_debug_print("ina219: set pga failed.\n");
406 (void)ina219_deinit(&gs_handle);
407
408 return 1;
409 }
410 res = ina219_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
411 if (res != 0)
412 {
413 ina219_interface_debug_print("ina219: calculate calibration failed.\n");
414 (void)ina219_deinit(&gs_handle);
415
416 return 1;
417 }
418 res = ina219_set_calibration(&gs_handle, calibration);
419 if (res != 0)
420 {
421 ina219_interface_debug_print("ina219: set calibration failed.\n");
422 (void)ina219_deinit(&gs_handle);
423
424 return 1;
425 }
426 for (i = 0; i < times; i++)
427 {
428 int16_t s_raw;
429 uint16_t u_raw;
430 float m;
431
432 /* read shunt voltage */
433 res = ina219_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
434 if (res != 0)
435 {
436 ina219_interface_debug_print("ina219: read shunt voltage failed.\n");
437 (void)ina219_deinit(&gs_handle);
438
439 return 1;
440 }
441 ina219_interface_debug_print("ina219: shunt voltage is %0.3fmV.\n", m);
442
443 /* read bus voltage */
444 res = ina219_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
445 if (res != 0)
446 {
447 ina219_interface_debug_print("ina219: read bus voltage failed.\n");
448 (void)ina219_deinit(&gs_handle);
449
450 return 1;
451 }
452 ina219_interface_debug_print("ina219: bus voltage is %0.3fmV.\n", m);
453
454 /* read current */
455 res = ina219_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
456 if (res != 0)
457 {
458 ina219_interface_debug_print("ina219: read current failed.\n");
459 (void)ina219_deinit(&gs_handle);
460
461 return 1;
462 }
463 ina219_interface_debug_print("ina219: current is %0.3fmA.\n", m);
464
465 /* read power */
466 res = ina219_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
467 if (res != 0)
468 {
469 ina219_interface_debug_print("ina219: read power failed.\n");
470 (void)ina219_deinit(&gs_handle);
471
472 return 1;
473 }
474 ina219_interface_debug_print("ina219: power is %0.3fmW.\n", m);
475
477 }
478
479 ina219_interface_debug_print("ina219: set triggered mode.\n");
480
481 for (i = 0; i < times; i++)
482 {
483 int16_t s_raw;
484 uint16_t u_raw;
485 float m;
486
487 /* set shunt bus voltage triggered */
489 if (res != 0)
490 {
491 ina219_interface_debug_print("ina219: set mode failed.\n");
492 (void)ina219_deinit(&gs_handle);
493
494 return 1;
495 }
496
497 /* read shunt voltage */
498 res = ina219_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
499 if (res != 0)
500 {
501 ina219_interface_debug_print("ina219: read shunt voltage failed.\n");
502 (void)ina219_deinit(&gs_handle);
503
504 return 1;
505 }
506 ina219_interface_debug_print("ina219: shunt voltage is %0.3fmV.\n", m);
507
508 /* read bus voltage */
509 res = ina219_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
510 if (res != 0)
511 {
512 ina219_interface_debug_print("ina219: read bus voltage failed.\n");
513 (void)ina219_deinit(&gs_handle);
514
515 return 1;
516 }
517 ina219_interface_debug_print("ina219: bus voltage is %0.3fmV.\n", m);
518
519 /* read current */
520 res = ina219_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
521 if (res != 0)
522 {
523 ina219_interface_debug_print("ina219: read current failed.\n");
524 (void)ina219_deinit(&gs_handle);
525
526 return 1;
527 }
528 ina219_interface_debug_print("ina219: current is %0.3fmA.\n", m);
529
530 /* read power */
531 res = ina219_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
532 if (res != 0)
533 {
534 ina219_interface_debug_print("ina219: read power failed.\n");
535 (void)ina219_deinit(&gs_handle);
536
537 return 1;
538 }
539 ina219_interface_debug_print("ina219: power is %0.3fmW.\n", m);
540
542 }
543
544 /* finish read test */
545 (void)ina219_deinit(&gs_handle);
546 ina219_interface_debug_print("ina219: finish read test.\n");
547
548 return 0;
549}
struct ina219_info_s ina219_info_t
ina219 information structure definition
uint8_t ina219_set_addr_pin(ina219_handle_t *handle, ina219_address_t addr_pin)
set the iic address pin
uint8_t ina219_info(ina219_info_t *info)
get chip's information
uint8_t ina219_set_shunt_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t mode)
set the shunt voltage adc mode
uint8_t ina219_set_calibration(ina219_handle_t *handle, uint16_t data)
set the calibration
uint8_t ina219_deinit(ina219_handle_t *handle)
close the chip
uint8_t ina219_set_mode(ina219_handle_t *handle, ina219_mode_t mode)
set the mode
uint8_t ina219_set_pga(ina219_handle_t *handle, ina219_pga_t pga)
set the pga
uint8_t ina219_set_bus_voltage_range(ina219_handle_t *handle, ina219_bus_voltage_range_t range)
set the bus voltage range
uint8_t ina219_read_bus_voltage(ina219_handle_t *handle, uint16_t *raw, float *mV)
read the bus voltage
uint8_t ina219_read_current(ina219_handle_t *handle, int16_t *raw, float *mA)
read the current
uint8_t ina219_calculate_calibration(ina219_handle_t *handle, uint16_t *calibration)
calculate the calibration
uint8_t ina219_init(ina219_handle_t *handle)
initialize the chip
uint8_t ina219_set_resistance(ina219_handle_t *handle, double resistance)
set the resistance
ina219_address_t
ina219 address enumeration definition
uint8_t ina219_set_bus_voltage_adc_mode(ina219_handle_t *handle, ina219_adc_mode_t mode)
set the bus voltage adc mode
uint8_t ina219_read_shunt_voltage(ina219_handle_t *handle, int16_t *raw, float *mV)
read the shunt voltage
struct ina219_handle_s ina219_handle_t
ina219 handle structure definition
uint8_t ina219_read_power(ina219_handle_t *handle, uint16_t *raw, float *mW)
read the power
@ INA219_ADC_MODE_12_BIT_1_SAMPLES
@ INA219_PGA_320_MV
@ INA219_PGA_40_MV
@ INA219_PGA_80_MV
@ INA219_PGA_160_MV
@ INA219_BUS_VOLTAGE_RANGE_32V
@ INA219_MODE_SHUNT_BUS_VOLTAGE_CONTINUOUS
@ INA219_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
uint8_t ina219_interface_iic_init(void)
interface iic bus init
uint8_t ina219_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t ina219_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
void ina219_interface_delay_ms(uint32_t ms)
interface delay ms
void ina219_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t ina219_interface_iic_deinit(void)
interface iic bus deinit
uint8_t ina219_read_test(ina219_address_t addr_pin, double r, uint32_t times)
read test
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]