-
Notifications
You must be signed in to change notification settings - Fork 1
/
INA237.cpp
367 lines (301 loc) · 9.53 KB
/
INA237.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* @file INA237.cpp
* @author Wilho-Pekka Ilvesmäki ([email protected])
* @brief
* @version 0.1
* @date 2022-04-25
*
* @copyright Copyright (c) 2022
*
*/
#include "INA237.h"
INA237::INA237(TwoWire* i2c_handler, const uint8_t &addr):_i2c(i2c_handler),_device_address(addr)
{
}
INA237::~INA237()
{
}
void INA237::reset(void)
{
_writeRegister(INA237_REG_CONFIG, ((uint16_t)1<<15));
_current_lsb = 0;
_adc_range = INA237_ADC_RANGE_163_84mV;
_shunt_res = 0.0;
}
void INA237::calibrate(const double &res, const double &max_current, const bool &rounded)
{
const float COEFF = 819.2E6F;
_shunt_res = abs(res);
double new_current_lsb = (max_current != 0.0F) ? ldexp(abs(max_current), -15) : 0.0F;
if(rounded)
{
uint32_t multiplier = 1;
double rounded_current_lsb = new_current_lsb;
while (rounded_current_lsb < 1.0F)
{
multiplier *= 10;
rounded_current_lsb = new_current_lsb * multiplier;
}
new_current_lsb = (ceil(rounded_current_lsb) / multiplier);
}
_current_lsb = new_current_lsb;
uint16_t shunt_cal = (uint16_t)(COEFF * new_current_lsb * _shunt_res * (_adc_range?4:1));
_writeRegister(INA237_REG_SHUNT_CAL, shunt_cal);
}
bool INA237::configADC(const ina237_adc_config_t &config)
{
if(config.mode > 0xF || config.vbusct > 7 || config.vshct > 7 || \
config.vtct > 7 || config.avg > 7)
{
return false;
}
uint16_t data = (config.mode << 12) | (config.vbusct << 9) | \
(config.vshct << 6) | (config.vtct << 3) | (config.avg);
_writeRegister(INA237_REG_ADC_CONFIG, data);
return true;
}
void INA237::configAlert(const ina237_alrt_config_t &config)
{
uint8_t reg[2];
uint16_t regval = 0;
_readRegister(INA237_REG_DIAG_ALRT, 2, ®[0]);
regval = 0x2FD & ((reg[1]<<8) | reg[0]);
regval |= (config.alatch << 15) | (config.cnvr << 14) | \
(config.slowalert << 13) | (config.apol << 12);
_writeRegister(INA237_REG_DIAG_ALRT, regval);
}
void INA237::setConversionDelay(const uint8_t &delay)
{
uint8_t val[2];
uint16_t new_val;
_readRegister(INA237_REG_CONFIG, 2, &val[0]);
new_val = ((uint16_t)(0x3F & val[1])<<8) | (0x3F & val[0]);
new_val |= (delay<<6);
_writeRegister(INA237_REG_CONFIG, new_val);
}
void INA237::setADCRange(const uint8_t &range)
{
if(range != INA237_ADC_RANGE_40_96mV || range != INA237_ADC_RANGE_163_84mV)
{
return;
}
_adc_range = range;
uint8_t val[2];
uint16_t new_val;
_readRegister(INA237_REG_CONFIG, 2, &val[0]);
new_val = ((uint16_t)(0xFF & val[1])<<8) | (0xEF & val[0]);
new_val |= (range<<4);
_writeRegister(INA237_REG_CONFIG, new_val);
}
void INA237::setShuntOvervoltageTreshold(const double &voltage)
{
double value = voltage / INA237_VSHUNT_LSB_RES[_adc_range];
int16_t regval = (value > INT16_MAX ? INT16_MAX : \
(value < INT16_MIN ? INT16_MIN : (int16_t)value));
_writeRegister(INA237_REG_SOVL, regval);
}
void INA237::setShuntUndervoltageTreshold(const double &voltage)
{
double value = voltage / INA237_VSHUNT_LSB_RES[_adc_range];
int16_t regval = (value > INT16_MAX ? INT16_MAX : \
(value < INT16_MIN ? INT16_MIN : (int16_t)value));
_writeRegister(INA237_REG_SUVL, regval);
}
void INA237::setBusOvervoltageTreshold(const double &voltage)
{
double value = voltage / INA237_VBUS_LSB_RES;
uint16_t regval = (value > 0x7FFF ? 0x7FFF : \
(value < 0 ? 0 : (uint16_t)value));
_writeRegister(INA237_REG_BOVL, regval);
}
void INA237::setBusUndervoltageTreshold(const double &voltage)
{
double value = voltage / INA237_VBUS_LSB_RES;
uint16_t regval = (value > 0x7FFF ? 0x7FFF : \
(value < 0 ? 0 : (uint16_t)value));
_writeRegister(INA237_REG_BUVL, regval);
}
void INA237::setTempOverlimitTreshold(const double &temp)
{
double temp_limit = temp > 255 ? 255 : (temp < -255 ? -255 : temp);
double value = temp_limit / INA237_TEMP_LSB_RES;
int16_t regval = ((int16_t)round(value) << 4) & 0xFFF0;
_writeRegister(INA237_REG_TEMP_LIMIT, (uint16_t)regval);
}
void INA237::setPowerOverlimitTreshold(const double &power)
{
double divider = (_current_lsb * 0.2 * 256);
double value = divider != 0.0 ? ( power / divider) : 0.0;
uint16_t regval = (value > UINT16_MAX ? UINT16_MAX : \
(value < 0 ? 0 : (uint16_t)round(value)));
_writeRegister(INA237_REG_PWR_LIMIT, (uint16_t)regval);
}
void INA237::setOverCurrentTreshold(const double ¤t)
{
double voltage = current * _shunt_res;
setShuntOvervoltageTreshold(voltage);
}
void INA237::setUnderCurrentTreshold(const double ¤t)
{
double voltage = current * _shunt_res;
setShuntUndervoltageTreshold(voltage);
}
uint16_t INA237::getAlertFlag(void) const
{
uint8_t reg[2];
uint16_t regval;
_readRegister(INA237_REG_DIAG_ALRT, 2, ®[0]);
regval = 0x2FF & ((reg[1]<<8) | reg[0]);
return regval;
}
double INA237::getBusVoltage(void) const
{
uint8_t arr[2];
int16_t voltage = 0;
_readRegister(INA237_REG_VBUS, 2, &arr[0]);
voltage = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return voltage * INA237_VBUS_LSB_RES;
}
double INA237::getShuntVoltage(void) const
{
uint8_t arr[2];
int16_t voltage = 0;
_readRegister(INA237_REG_VSHUNT, 2, &arr[0]);
voltage = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return voltage * INA237_VSHUNT_LSB_RES[_adc_range];
}
double INA237::getCurrent(void) const
{
uint8_t arr[2];
int16_t current = 0;
_readRegister(INA237_REG_CURRENT, 2, &arr[0]);
current = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return current * _current_lsb;
}
double INA237::getPower(void) const
{
uint8_t arr[3];
uint32_t power = 0;
_readRegister(INA237_REG_POWER, 3, &arr[0]);
power = ((uint32_t)arr[2] << 16) | ((uint32_t)arr[1] << 8) | ((uint32_t)arr[0]);
return (double)power * _current_lsb * 0.2;
}
double INA237::getTemp(void) const
{
uint8_t arr[2];
int16_t temp = 0;
_readRegister(INA237_REG_DIETEMP, 2, &arr[0]);
temp = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
temp >>= 4;
return (double)temp * INA237_TEMP_LSB_RES;
}
double INA237::getShuntOvervoltageTreshold(void) const
{
uint8_t arr[2];
int16_t value = 0;
_readRegister(INA237_REG_SOVL, 2, &arr[0]);
value = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return (double)value * INA237_VSHUNT_LSB_RES[_adc_range];
}
double INA237::getShuntUndervoltageTreshold(void) const
{
uint8_t arr[2];
int16_t value = 0;
_readRegister(INA237_REG_SUVL, 2, &arr[0]);
value = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return (double)value * INA237_VSHUNT_LSB_RES[_adc_range];
}
double INA237::getBusOvervoltageTreshold(void) const
{
uint8_t arr[2];
uint16_t value = 0;
_readRegister(INA237_REG_BOVL, 2, &arr[0]);
value = (((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return (double)value * INA237_VBUS_LSB_RES;
}
double INA237::getBusUndervoltageTreshold(void) const
{
uint8_t arr[2];
uint16_t value = 0;
_readRegister(INA237_REG_BUVL, 2, &arr[0]);
value = (((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return (double)value * INA237_VBUS_LSB_RES;
}
double INA237::getTempOverlimitTreshold(void) const
{
uint8_t arr[2];
int16_t value = 0;
_readRegister(INA237_REG_TEMP_LIMIT, 2, &arr[0]);
value = (int16_t)(((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
value >>= 4;
return (double)value * INA237_TEMP_LSB_RES;
}
double INA237::getPowerOverlimitTreshold(void) const
{
uint8_t arr[2];
uint32_t value = 0;
_readRegister(INA237_REG_PWR_LIMIT, 2, &arr[0]);
value = (((uint32_t)arr[1] << 8) | ((uint32_t)arr[0]));
value <<= 8; // multiply by 256
return (double)value * 0.2 * _current_lsb;
}
double INA237::getOverCurrentTreshold(void) const
{
double voltage = getShuntOvervoltageTreshold();
double current = (_shunt_res != 0.0) ? (voltage / _shunt_res) : 0.0;
return current;
}
double INA237::getUnderCurrentTreshold(void) const
{
double voltage = getShuntUndervoltageTreshold();
double current = (_shunt_res != 0.0) ? (voltage / _shunt_res) : 0.0;
return current;
}
uint16_t INA237::getManufacturerID(void) const
{
uint8_t arr[2];
uint16_t value = 0;
_readRegister(INA237_REG_MANUFACTURER_ID, 2, &arr[0]);
value = (((uint16_t)arr[1] << 8) | ((uint16_t)arr[0]));
return value;
}
/*----------PRIVATE FUNCTIONS-------------------------------------------------*/
void INA237::_readRegister(uint8_t reg, size_t cnt, uint8_t *data) const
{
if(!_i2c)
{
return;
}
/* set device's register pointer to correct register before read */
_i2c->beginTransmission(_device_address);
_i2c->write(reg);
_i2c->endTransmission(true);
size_t read = _i2c->requestFrom(_device_address, cnt, true);
if(read != cnt)
{
return;
}
while(cnt > _i2c->available());
for(int i = (cnt - 1); i >= 0; i--)
{
int val = _i2c->read();
if(val == -1)
{
// failed
return;
}
data[i] = (uint8_t)val;
}
}
void INA237::_writeRegister(uint8_t reg, uint16_t data)
{
if(!_i2c)
{
return;
}
_i2c->beginTransmission(_device_address);
_i2c->write(reg);
_i2c->write((uint8_t)(data>>8));
_i2c->write((uint8_t)data);
_i2c->endTransmission(true);
}