-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINA226.cpp
309 lines (274 loc) · 5.73 KB
/
INA226.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
/*
* INA226.cpp
*
* Created on: May 13, 2013
* Author: joao
*/
#include "INA226.h"
#include <inc/lm3s2776.h>
#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <driverlib/debug.h>
#include <driverlib/sysctl.h>
#include <driverlib/systick.h>
#include <driverlib/gpio.h>
#include <driverlib/rom.h>
#include "driverlib/interrupt.h"
#include "driverlib/i2c.h"
#include "utils/cmdline.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include <stdint.h>
#include <math.h>
extern "C"
{
#include "libraries/I2Cdev/i2cutil.h"
}
// CAL 2560
// max current 0.000152587890625 LSB
INA226::INA226()
{
addr_ = 0;
conf_reg_high_ = 0x41;
conf_reg_low_ = 0x27;
}
INA226::INA226(char addr)
{
addr_ = addr;
conf_reg_high_ = 0x41;
conf_reg_low_ = 0x27;
}
INA226::~INA226()
{
// TODO Auto-generated destructor stub
}
uint16_t INA226::read_register(uint8_t reg)
{
if(addr_)
{
unsigned char data[2];
data[0] = reg;
i2cWrite(addr_, data, 1);
i2cRead(addr_, data, 2);
conf_reg_high_ = data[0];
conf_reg_low_ = data[1];
return (conf_reg_high_ << 8) | conf_reg_low_;
}
}
void INA226::write_register(uint8_t reg, uint16_t value)
{
if(addr_)
{
unsigned char data[3];
data[0] = reg;
data[1] = (value & 0xFF00) >> 8;
data[2] = value & 0xFF;
i2cWrite(addr_, data, 3);
}
}
void INA226::set_sample_average(int avg)
{
uint8_t new_conf_reg_high = conf_reg_high_ & 0xF1;
switch (avg)
{
case 4:
new_conf_reg_high |= 0b00000010;
break;
case 16:
new_conf_reg_high |= 0b00000100;
break;
case 64:
new_conf_reg_high |= 0b00000110;
break;
case 128:
new_conf_reg_high |= 0b00001000;
break;
case 256:
new_conf_reg_high |= 0b00001010;
break;
case 512:
new_conf_reg_high |= 0b00001100;
break;
case 1024:
new_conf_reg_high |= 0b00001110;
break;
}
write_register(REG_CONFGURATION, (new_conf_reg_high << 8) | conf_reg_low_);
conf_reg_high_ = new_conf_reg_high;
}
int INA226::get_bus_voltage(bool return_raw_data)
{
unsigned int read_data = read_register(REG_BUS_VOLTAGE);
if(return_raw_data)
{
read_data *= 125;
read_data /= 100;
return read_data;
}
else
{
return read_data;
}
}
int INA226::get_bus_current(void)
{
unsigned int read_data = read_register(REG_CURRENT);
read_data = TwoComplement2ModSig_16bit(read_data);
}
int INA226::get_shunt_voltage(bool return_raw_data)
{
unsigned int read_data = read_register(REG_CURRENT);
read_data = TwoComplement2ModSig_16bit(read_data);
if(return_raw_data)
{
read_data *= 25;
read_data /= 10;
return read_data;
}
else
{
return read_data;
}
}
void INA226::set_vbus_conv_timer(uint16_t ct)
{
uint8_t new_conf_reg_high = conf_reg_high_ & 0xFE;
uint16_t new_conf_reg_low = conf_reg_low_ & 0x3F;
if(ct < 8)
{
new_conf_reg_high |= ((ct & 0b00000111) >> 2);
new_conf_reg_low |= (((ct & 0b00000111) << 6) && 0xFF);
}
new_conf_reg_low |= (new_conf_reg_high << 8);
write_register(REG_CONFGURATION, new_conf_reg_low);
conf_reg_low_ = new_conf_reg_low && 0xFF;
conf_reg_high_ = new_conf_reg_high;
}
void INA226::set_vshunt_conv_timer(uint16_t ct)
{
uint16_t new_conf_reg_low = conf_reg_low_ & 0x38;
if(ct < 8)
{
new_conf_reg_low |= ((ct & 0b00000111) << 3);
}
new_conf_reg_low |= (conf_reg_high_ << 8);
write_register(REG_CONFGURATION, new_conf_reg_low);
conf_reg_low_ = new_conf_reg_low && 0xFF;
}
void INA226::set_operating_mode(uint8_t mode)
{
uint16_t new_conf_reg_low = conf_reg_low_ & 0x07;
if(mode < 8)
{
new_conf_reg_low |= (mode & 0b00000111);
}
new_conf_reg_low |= (conf_reg_high_ << 8);
write_register(REG_CONFGURATION, new_conf_reg_low);
conf_reg_low_ = new_conf_reg_low && 0xFF;
}
void INA226::set_calibration_value(uint16_t calib)
{
write_register(REG_CALIBRATION, calib & 0xFFFF);
}
uint16_t INA226::get_calibration_value(void)
{
return read_register(REG_CALIBRATION);
}
void INA226::set_alert_limit_register(uint16_t value)
{
write_register(REG_ALERT_LIMIT, value & 0xFFFF);
}
uint16_t INA226::get_alert_limit_register(void)
{
return read_register(REG_ALERT_LIMIT);
}
void INA226::set_mask_enable_register(uint16_t bits)
{
write_register(REG_MASK_ENABLE, bits & 0xFFFF);
}
uint16_t INA226::get_mask_enable_register(void)
{
return read_register(REG_MASK_ENABLE);
}
void INA226::set_shunt_over_limit_bit(void)
{
write_register(REG_MASK_ENABLE, SHUNT_OVER_LIMIT);
}
void INA226::set_shunt_under_limit_bit(void)
{
write_register(REG_MASK_ENABLE, SHUNT_UNDER_LIMIT);
}
void INA226::set_bus_over_limit_bit(void)
{
write_register(REG_MASK_ENABLE, BUS_OVER_LIMIT);
}
void INA226::set_bus_under_limit_bit(void)
{
write_register(REG_MASK_ENABLE, BUS_UNDER_LIMIT);
}
void INA226::set_over_power_limit_bit(void)
{
write_register(REG_MASK_ENABLE, OVER_POWER_LIMIT);
}
void INA226::set_conversion_ready_alert_bit(void)
{
write_register(REG_MASK_ENABLE, CONVERSION_READY);
}
void INA226::set_bus_voltage_limit(float volts)
{
uint16_t temp = volts * 100000 / 125;
write_register(REG_ALERT_LIMIT, temp & 0xFFFF);
}
void INA226::set_shunt_voltage_limit(float microvolts)
{
uint16_t temp = microvolts * 10 / 25;
write_register(REG_ALERT_LIMIT, temp & 0xFFFF);
}
void INA226::set_power_limit(float watts)
{
//TODO:
}
void INA226::set_alert_polarity_bit(bool mode)
{
uint16_t temp = get_mask_enable_register();
if(mode)
{
temp |= ALERT_POLARITY_BIT;
}
else
{
temp &= ~ALERT_POLARITY_BIT;
}
set_mask_enable_register(temp);
}
bool INA226::read_math_overflow_flag(void)
{
if((get_mask_enable_register() & MATH_OVERFLOW_FLAG) == MATH_OVERFLOW_FLAG)
{
return true;
}
else
{
return false;
}
}
int TwoComplement2ModSig_16bit(uint16_t a)
{
int mask = 0;
mask = 0x8000;
if((a & mask) == mask)
{
a = ~a;
a = a - 1;
return -a;
}
else
{
return a;
}
}
uint16_t ModSig_16bit2TwoComplement(int a)
{
uint16_t temp = ~a + 1;
return temp | 0x0000;
}