-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathina219.c
More file actions
179 lines (142 loc) · 5.21 KB
/
ina219.c
File metadata and controls
179 lines (142 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "ina219.h"
static error_t ina219_read_register(const ina219_device *device,
ina219_register address, uint16_t *value) {
if (!device || !value) {
return E_NULL_PTR;
}
uint8_t tx[1] = {address}, rx[2];
check_error(i2c_master_transaction_write_read(device->i2c, device->address,
tx, 1, rx, 2));
*value = (rx[0] << 8u) + rx[1];
return E_SUCCESS;
}
static error_t ina219_write_register(const ina219_device *device,
ina219_register address,
uint16_t new_value) {
if (!device) {
return E_NULL_PTR;
}
uint8_t tx[3] = {
address,
(uint8_t)(new_value >> 8u),
(uint8_t)(new_value & 0xFFu),
};
check_error(i2c_master_write(device->i2c, device->address, tx, 3));
return E_SUCCESS;
}
error_t ina219_init(ina219_device *device, uint32_t i2c, uint8_t address) {
if (!device) {
return E_NULL_PTR;
}
(*device).i2c = i2c;
(*device).address = address;
check_error(ina219_read_register(device, ina219_register_configuration,
&device->config.raw));
// Check if device requires reset
if (device->config.raw != 0x399F) {
usart1_print("Restarting device\r\n");
device->config.rst = 1;
check_error(ina219_write_register(device, ina219_register_configuration,
device->config.raw));
do {
// Wait until device is ready
check_error(ina219_read_register(device, ina219_register_configuration,
&device->config.raw));
} while (device->config.raw != 0x399F);
}
device->config = (ina219_config){
// Mode - Shunt and Bus, Continuous
.mode = INA219_MODE_SHUNT_AND_BUS_VOLTAGE_CONTINUOUS,
// PGA gain and range - +8 / 320 mA
.pga = INA219_GAIN_2,
.brng = 0b0, // Bus Voltage Range - 16V
.badc = 0b1111, // Bus ADC Settings - 128x12bit samples / 68.1 ms
// conversion time
.sadc = 0b1111, // Shunt ADC Settings - 128x12bit samples / 68.1 ms
// conversion time
};
return E_SUCCESS;
}
error_t ina219_write_config(ina219_device *device) {
if (!device) {
return E_NULL_PTR;
}
usart1_printf("New configuration: %X\r\n", device->config.raw);
check_error(ina219_write_register(device, ina219_register_configuration,
device->config.raw));
return E_SUCCESS;
}
error_t ina219_perform_calibration(ina219_device *device) {
return ina219_write_register(device, ina219_register_calibration,
device->calibration_register);
}
error_t ina219_get_bus_voltage(ina219_device *device) {
if (!device) {
return E_NULL_PTR;
}
// Read bus voltage
uint16_t val = 0;
check_error(ina219_read_register(device, ina219_register_bus_voltage, &val));
// TODO: Check and handle overflows
device->raw_bus_voltage = val >> 3u;
// 1 LSB = 4mV (value from datasheet)
device->bus_voltage = device->raw_bus_voltage * 4 / 1000.0f;
return E_SUCCESS;
}
error_t ina219_get_shunt_voltage(ina219_device *device) {
if (!device) {
return E_NULL_PTR;
}
// Read drop of voltage across shunt
check_error(ina219_read_register(device, ina219_register_shunt_voltage,
(uint16_t *)&device->raw_shunt_voltage));
device->shunt_voltage = device->raw_shunt_voltage / 1000.0f;
return E_SUCCESS;
}
error_t ina219_get_current(ina219_device *device) {
if (!device) {
return E_NULL_PTR;
}
check_error(ina219_read_register(device, ina219_register_current,
(uint16_t *)&device->raw_current));
// 1 LSB = 0.1 mA (calculated value)
device->current = device->raw_current / device->calibration.current_div;
return E_SUCCESS;
}
error_t ina219_get_power(ina219_device *device) {
if (!device) {
return E_NULL_PTR;
}
check_error(ina219_read_register(device, ina219_register_power,
(uint16_t *)&device->raw_power));
// 1 LSB = 2 mW (calculated value) = 20 * Current LSB
device->power = device->raw_power * device->calibration.power_multiplier;
return E_SUCCESS;
}
error_t ina219_get_all_readings(ina219_device *device) {
check_error(ina219_get_bus_voltage(device));
check_error(ina219_get_shunt_voltage(device));
check_error(ina219_get_current(device));
check_error(ina219_get_power(device));
return E_SUCCESS;
}
error_t ina219_calibration_32v_2a(ina219_device *device) {
device->calibration_register = 10240;
device->calibration.current_div = 25.f;
device->calibration.power_multiplier = 0.8f;
check_error(ina219_perform_calibration(device));
device->config.brng = 1; // 32V
device->config.pga = INA219_GAIN_8;
check_error(ina219_write_config(device));
return E_SUCCESS;
}
error_t ina219_calibration_16v_400ma(ina219_device *device) {
device->calibration_register = 8192;
device->calibration.current_div = 20.f;
device->calibration.power_multiplier = 1.f;
check_error(ina219_perform_calibration(device));
device->config.brng = 0; // 16V
device->config.pga = INA219_GAIN_1;
check_error(ina219_write_config(device));
return E_SUCCESS;
}