LibDriver INA226  1.0.0
INA226 full-featured driver
driver_ina226_read_test.c
Go to the documentation of this file.
1 
38 
39 static ina226_handle_t gs_handle;
51 uint8_t ina226_read_test(ina226_address_t addr_pin, double r, uint32_t times)
52 {
53  uint8_t res;
54  uint32_t i;
55  uint16_t calibration;
56  ina226_info_t info;
57 
58  /* link interface function */
67 
68  /* get information */
69  res = ina226_info(&info);
70  if (res != 0)
71  {
72  ina226_interface_debug_print("ina226: get info failed.\n");
73 
74  return 1;
75  }
76  else
77  {
78  /* print chip info */
79  ina226_interface_debug_print("ina226: chip is %s.\n", info.chip_name);
80  ina226_interface_debug_print("ina226: manufacturer is %s.\n", info.manufacturer_name);
81  ina226_interface_debug_print("ina226: interface is %s.\n", info.interface);
82  ina226_interface_debug_print("ina226: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
83  ina226_interface_debug_print("ina226: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
84  ina226_interface_debug_print("ina226: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
85  ina226_interface_debug_print("ina226: max current is %0.2fmA.\n", info.max_current_ma);
86  ina226_interface_debug_print("ina226: max temperature is %0.1fC.\n", info.temperature_max);
87  ina226_interface_debug_print("ina226: min temperature is %0.1fC.\n", info.temperature_min);
88  }
89 
90  /* start read test */
91  ina226_interface_debug_print("ina226: start read test.\n");
92 
93  /* set addr pin */
94  res = ina226_set_addr_pin(&gs_handle, addr_pin);
95  if (res != 0)
96  {
97  ina226_interface_debug_print("ina226: set addr pin failed.\n");
98 
99  return 1;
100  }
101 
102  /* set the r */
103  res = ina226_set_resistance(&gs_handle, r);
104  if (res != 0)
105  {
106  ina226_interface_debug_print("ina226: set resistance failed.\n");
107 
108  return 1;
109  }
110 
111  /* init */
112  res = ina226_init(&gs_handle);
113  if (res != 0)
114  {
115  ina226_interface_debug_print("ina226: init failed.\n");
116 
117  return 1;
118  }
119 
120  /* set average mode 16 */
121  res = ina226_set_average_mode(&gs_handle, INA226_AVG_16);
122  if (res != 0)
123  {
124  ina226_interface_debug_print("ina226: set average mode failed.\n");
125  (void)ina226_deinit(&gs_handle);
126 
127  return 1;
128  }
129 
130  /* set bus voltage conversion time 1.1ms */
132  if (res != 0)
133  {
134  ina226_interface_debug_print("ina226: set bus voltage conversion time failed.\n");
135  (void)ina226_deinit(&gs_handle);
136 
137  return 1;
138  }
139 
140  /* set shunt voltage conversion time 1.1ms */
142  if (res != 0)
143  {
144  ina226_interface_debug_print("ina226: set shunt voltage conversion time failed.\n");
145  (void)ina226_deinit(&gs_handle);
146 
147  return 1;
148  }
149 
150  /* calculate calibration */
151  res = ina226_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
152  if (res != 0)
153  {
154  ina226_interface_debug_print("ina226: calculate calibration failed.\n");
155  (void)ina226_deinit(&gs_handle);
156 
157  return 1;
158  }
159  res = ina226_set_calibration(&gs_handle, calibration);
160  if (res != 0)
161  {
162  ina226_interface_debug_print("ina226: set calibration failed.\n");
163  (void)ina226_deinit(&gs_handle);
164 
165  return 1;
166  }
167 
168  /* set shunt bus voltage continuous */
170  if (res != 0)
171  {
172  ina226_interface_debug_print("ina226: set mode failed.\n");
173  (void)ina226_deinit(&gs_handle);
174 
175  return 1;
176  }
177 
178  /* shunt bus voltage continuous test */
179  ina226_interface_debug_print("ina226: shunt bus voltage continuous test.\n");
180 
181  /* delay 1000ms */
183 
184  for (i = 0; i < times; i++)
185  {
186  int16_t s_raw;
187  uint16_t u_raw;
188  float m;
189 
190  /* read shunt voltage */
191  res = ina226_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
192  if (res != 0)
193  {
194  ina226_interface_debug_print("ina226: read shunt voltage failed.\n");
195  (void)ina226_deinit(&gs_handle);
196 
197  return 1;
198  }
199  ina226_interface_debug_print("ina226: shunt voltage is %0.3fmV.\n", m);
200 
201  /* read bus voltage */
202  res = ina226_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
203  if (res != 0)
204  {
205  ina226_interface_debug_print("ina226: read bus voltage failed.\n");
206  (void)ina226_deinit(&gs_handle);
207 
208  return 1;
209  }
210  ina226_interface_debug_print("ina226: bus voltage is %0.3fmV.\n", m);
211 
212  /* read current */
213  res = ina226_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
214  if (res != 0)
215  {
216  ina226_interface_debug_print("ina226: read current failed.\n");
217  (void)ina226_deinit(&gs_handle);
218 
219  return 1;
220  }
221  ina226_interface_debug_print("ina226: current is %0.3fmA.\n", m);
222 
223  /* read power */
224  res = ina226_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
225  if (res != 0)
226  {
227  ina226_interface_debug_print("ina226: read power failed.\n");
228  (void)ina226_deinit(&gs_handle);
229 
230  return 1;
231  }
232  ina226_interface_debug_print("ina226: power is %0.3fmW.\n", m);
233 
234  /* delay 1000ms */
236  }
237 
238  /* set power down */
239  res = ina226_set_mode(&gs_handle, INA226_MODE_POWER_DOWN);
240  if (res != 0)
241  {
242  ina226_interface_debug_print("ina226: set mode failed.\n");
243  (void)ina226_deinit(&gs_handle);
244 
245  return 1;
246  }
247 
248  /* shunt and bus triggered test */
249  ina226_interface_debug_print("ina226: shunt and bus triggered test.\n");
250 
251  /* delay 1000ms */
253 
254  for (i = 0; i < times; i++)
255  {
256  int16_t s_raw;
257  uint16_t u_raw;
258  float m;
259 
260  /* set shunt and bus triggered */
262  if (res != 0)
263  {
264  ina226_interface_debug_print("ina226: set mode failed.\n");
265  (void)ina226_deinit(&gs_handle);
266 
267  return 1;
268  }
269 
270  /* read shunt voltage */
271  res = ina226_read_shunt_voltage(&gs_handle, (int16_t *)&s_raw, (float *)&m);
272  if (res != 0)
273  {
274  ina226_interface_debug_print("ina226: read shunt voltage failed.\n");
275  (void)ina226_deinit(&gs_handle);
276 
277  return 1;
278  }
279  ina226_interface_debug_print("ina226: shunt voltage is %0.3fmV.\n", m);
280 
281  /* read bus voltage */
282  res = ina226_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
283  if (res != 0)
284  {
285  ina226_interface_debug_print("ina226: read bus voltage failed.\n");
286  (void)ina226_deinit(&gs_handle);
287 
288  return 1;
289  }
290  ina226_interface_debug_print("ina226: bus voltage is %0.3fmV.\n", m);
291 
292  /* read current */
293  res = ina226_read_current(&gs_handle, (int16_t *)&s_raw, (float *)&m);
294  if (res != 0)
295  {
296  ina226_interface_debug_print("ina226: read current failed.\n");
297  (void)ina226_deinit(&gs_handle);
298 
299  return 1;
300  }
301  ina226_interface_debug_print("ina226: current is %0.3fmA.\n", m);
302 
303  /* read power */
304  res = ina226_read_power(&gs_handle, (uint16_t *)&u_raw, (float *)&m);
305  if (res != 0)
306  {
307  ina226_interface_debug_print("ina226: read power failed.\n");
308  (void)ina226_deinit(&gs_handle);
309 
310  return 1;
311  }
312  ina226_interface_debug_print("ina226: power is %0.3fmW.\n", m);
313 
314  /* delay 1000ms */
316  }
317 
318  /* finish read test */
319  (void)ina226_deinit(&gs_handle);
320  ina226_interface_debug_print("ina226: finish read test.\n");
321 
322  return 0;
323 }
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
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_mode(ina226_handle_t *handle, ina226_mode_t mode)
set the mode
uint8_t ina226_set_addr_pin(ina226_handle_t *handle, ina226_address_t addr_pin)
set the iic address pin
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
ina226_address_t
ina226 address enumeration definition
Definition: driver_ina226.h:70
uint8_t ina226_read_shunt_voltage(ina226_handle_t *handle, int16_t *raw, float *mV)
read the shunt voltage
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_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_read_bus_voltage(ina226_handle_t *handle, uint16_t *raw, float *mV)
read the bus voltage
@ INA226_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
@ INA226_MODE_POWER_DOWN
@ INA226_MODE_SHUNT_BUS_VOLTAGE_CONTINUOUS
@ INA226_CONVERSION_TIME_1P1_MS
@ INA226_AVG_16
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_read_test(ina226_address_t addr_pin, double r, uint32_t times)
read 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]