LibDriver INA219
Loading...
Searching...
No Matches
driver_ina219_shot.c
Go to the documentation of this file.
1
36
37#include "driver_ina219_shot.h"
38
39static ina219_handle_t gs_handle;
40
50uint8_t ina219_shot_init(ina219_address_t addr_pin, double r)
51{
52 uint8_t res;
53 uint16_t calibration;
54
55 /* link interface function */
63
64 /* set addr pin */
65 res = ina219_set_addr_pin(&gs_handle, addr_pin);
66 if (res != 0)
67 {
68 ina219_interface_debug_print("ina219: set addr pin failed.\n");
69
70 return 1;
71 }
72
73 /* set the r */
74 res = ina219_set_resistance(&gs_handle, r);
75 if (res != 0)
76 {
77 ina219_interface_debug_print("ina219: set resistance failed.\n");
78
79 return 1;
80 }
81
82 /* init */
83 res = ina219_init(&gs_handle);
84 if (res != 0)
85 {
86 ina219_interface_debug_print("ina219: init failed.\n");
87
88 return 1;
89 }
90
91 /* set bus voltage range */
93 if (res != 0)
94 {
95 ina219_interface_debug_print("ina219: set bus voltage range failed.\n");
96 (void)ina219_deinit(&gs_handle);
97
98 return 1;
99 }
100
101 /* set bus voltage adc mode */
103 if (res != 0)
104 {
105 ina219_interface_debug_print("ina219: set bus voltage adc mode failed.\n");
106 (void)ina219_deinit(&gs_handle);
107
108 return 1;
109 }
110
111 /* set shunt voltage adc mode */
113 if (res != 0)
114 {
115 ina219_interface_debug_print("ina219: set shunt voltage adc mode failed.\n");
116 (void)ina219_deinit(&gs_handle);
117
118 return 1;
119 }
120
121 /* set pga */
122 res = ina219_set_pga(&gs_handle, INA219_SHOT_DEFAULT_PGA);
123 if (res != 0)
124 {
125 ina219_interface_debug_print("ina219: set pga failed.\n");
126 (void)ina219_deinit(&gs_handle);
127
128 return 1;
129 }
130
131 /* calculate calibration */
132 res = ina219_calculate_calibration(&gs_handle, (uint16_t *)&calibration);
133 if (res != 0)
134 {
135 ina219_interface_debug_print("ina219: calculate calibration failed.\n");
136 (void)ina219_deinit(&gs_handle);
137
138 return 1;
139 }
140
141 /* set calibration */
142 res = ina219_set_calibration(&gs_handle, calibration);
143 if (res != 0)
144 {
145 ina219_interface_debug_print("ina219: set calibration failed.\n");
146 (void)ina219_deinit(&gs_handle);
147
148 return 1;
149 }
150
151 return 0;
152}
153
164uint8_t ina219_shot_read(float *mV, float *mA, float *mW)
165{
166 uint8_t res;
167 int16_t s_raw;
168 uint16_t u_raw;
169
170 /* set shunt bus voltage triggered */
172 if (res != 0)
173 {
174 return 1;
175 }
176
177 /* read bus voltage */
178 res = ina219_read_bus_voltage(&gs_handle, (uint16_t *)&u_raw, mV);
179 if (res != 0)
180 {
181 return 1;
182 }
183
184 /* read current */
185 res = ina219_read_current(&gs_handle, (int16_t *)&s_raw, mA);
186 if (res != 0)
187 {
188 return 1;
189 }
190
191 /* read power */
192 res = ina219_read_power(&gs_handle, (uint16_t *)&u_raw, mW);
193 if (res != 0)
194 {
195 return 1;
196 }
197
198 return 0;
199}
200
209{
210 uint8_t res;
211
212 res = ina219_deinit(&gs_handle);
213 if (res != 0)
214 {
215 return 1;
216 }
217
218 return 0;
219}
uint8_t ina219_set_addr_pin(ina219_handle_t *handle, ina219_address_t addr_pin)
set the iic address pin
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
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_MODE_SHUNT_BUS_VOLTAGE_TRIGGERED
#define INA219_SHOT_DEFAULT_BUS_VOLTAGE_RANGE
ina219 shot example default definition
uint8_t ina219_shot_read(float *mV, float *mA, float *mW)
shot example read
uint8_t ina219_shot_init(ina219_address_t addr_pin, double r)
shot example init
#define INA219_SHOT_DEFAULT_PGA
#define INA219_SHOT_DEFAULT_BUS_VOLTAGE_ADC_MODE
uint8_t ina219_shot_deinit(void)
shot example deinit
#define INA219_SHOT_DEFAULT_SHUNT_VOLTAGE_ADC_MODE
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