-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisl28022_driver.c
450 lines (394 loc) · 12.8 KB
/
isl28022_driver.c
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* @file isl28022_driver.c
* @brief Implementation of the ISL28022 Digital Power Monitor driver
*
* @copyright Copyright (c) 2024 Devin Headrick
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*/
#include <stdio.h>
#include <esp_log.h>
#include <math.h>
#include <freertos/FreeRTOS.h>
#include "isl28022_driver.h"
static const char *TAG = "ISL28022 driver";
/// ------------------------ Static Helper fxns -------------------------- ///
static float calculate_current_full_scale(float v_shunt_fs, float r_shunt)
{
return v_shunt_fs / r_shunt;
}
static float calculate_current_lsb(float current_full_scale, uint32_t adc_resolution)
{
return current_full_scale / adc_resolution;
}
static uint16_t calculate_calibration_value(float current_lsb, float r_shunt)
{
// Using equation from datasheet: Cal = trunc(0.04096 / (Current_LSB * R_SHUNT))
float cal_float = 0.04096 / (current_lsb * r_shunt);
return (uint16_t)cal_float;
}
/// @brief Write to a register of the ISL28022 using the I2C bus
/// @param address
/// @param value
/// @note Write op requires: START condition, id byte, address byte, two data bytes, and STOP condition
/// @note First data byte contains MSB of data. Last data byte contains MLSB of data.
/// @note After each byte, the ISL28022 responds with an ACK, then the I2C interface enters a standby state.
/// @return
static esp_err_t isl28022_write_reg(isl28022_driver_t *isl28022_dev, uint8_t reg_addr, uint16_t reg_value)
{
if (isl28022_dev == NULL || reg_addr > AUX_CONTROL)
{
return ESP_ERR_INVALID_ARG;
}
uint8_t write_bytes[3];
write_bytes[0] = reg_addr;
write_bytes[1] = (uint8_t)(reg_value >> 8);
write_bytes[2] = (uint8_t)(reg_value & 0xFFU);
esp_err_t err = i2c_master_transmit(isl28022_dev->dev_handle, write_bytes, sizeof(write_bytes), pdMS_TO_TICKS(I2C_WRITE_TIMEOUT_MS));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to write register 0x%02x: %s", reg_addr, esp_err_to_name(err));
}
return err;
}
/// @brief Read from a register of the ISL28022 using the I2C bus
/// @param isl28022_dev
/// @param reg_addr
/// @param dout
/// @param size
/// @note
/// @return
static esp_err_t isl28022_read_reg(isl28022_driver_t *isl28022_dev, uint8_t reg_addr, uint16_t *reg_value)
{
if (isl28022_dev == NULL || reg_addr > AUX_CONTROL)
{
return ESP_ERR_INVALID_ARG;
}
uint8_t bytes_read[2];
esp_err_t err = i2c_master_transmit(isl28022_dev->dev_handle, ®_addr, 1, pdMS_TO_TICKS(I2C_WRITE_TIMEOUT_MS));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to write register address 0x%02X: %s", reg_addr, esp_err_to_name(err));
return err;
}
err = i2c_master_receive(isl28022_dev->dev_handle, bytes_read, sizeof(bytes_read), pdMS_TO_TICKS(I2C_READ_TIMEOUT_MS));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to write register address 0x%02X: %s", reg_addr, esp_err_to_name(err));
return err;
}
*reg_value = ((uint16_t)bytes_read[0] << 8) | bytes_read[1];
return err;
}
/// ---------------------------------------------------------------------- ///
isl28022_driver_t *isl28022_create(i2c_master_dev_handle_t dev_handle, float shunt_resistor_ohms)
{
if (dev_handle == NULL || shunt_resistor_ohms <= 0.0)
{
ESP_LOGE(TAG, "Invalid parameters");
return NULL;
}
isl28022_driver_t *isl28022_dev = calloc(1, sizeof(isl28022_driver_t));
if (isl28022_dev == NULL)
{
ESP_LOGE(TAG, "Failed to allocate memory for driver");
return NULL;
}
isl28022_dev->dev_handle = dev_handle;
isl28022_dev->shunt_resistor_ohms = shunt_resistor_ohms;
return isl28022_dev;
}
void isl28022_destroy(isl28022_driver_t *isl28022_dev)
{
free(isl28022_dev);
}
esp_err_t isl28022_init(isl28022_driver_t *isl28022_dev)
{
// TODO - probe for device - verify device is on the i2c bus using its slave address
esp_err_t err = isl28022_reset(isl28022_dev);
return err;
}
esp_err_t isl28022_reset(isl28022_driver_t *isl28022_dev)
{
if (isl28022_dev == NULL)
{
return ESP_ERR_INVALID_ARG;
}
uint16_t reset_reg_value = 0x8000U;
esp_err_t err = isl28022_write_reg(isl28022_dev, CONFIG, reset_reg_value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to reset device: %s", esp_err_to_name(err));
return err;
}
// Add a delay to allow the device to reset, and system to callibrate
vTaskDelay(pdMS_TO_TICKS(ISL28022_RESET_DELAY_MS));
return ESP_OK;
}
esp_err_t isl28022_get_config(isl28022_driver_t *dev, uint16_t *register_bits)
{
if (dev == NULL || register_bits == NULL)
{
return ESP_ERR_INVALID_ARG;
}
uint16_t reg_value;
esp_err_t err = isl28022_read_reg(dev, CONFIG, ®_value);
if (err == ESP_OK)
{
*register_bits = reg_value;
ESP_LOGI(TAG, "Config register read value: 0x%04x", *register_bits);
}
else
{
ESP_LOGE(TAG, "Failed to read config register");
}
return err;
}
esp_err_t isl28022_set_config(isl28022_driver_t *dev, const isl28022_config_t *config)
{
if (dev == NULL || config == NULL)
{
return ESP_ERR_INVALID_ARG;
}
uint16_t config_value = (uint16_t)((config->brng << BRNG0) |
(config->pg << PGA0) |
(config->badc << BADC0) |
(config->sadc << SADC0) |
config->mode);
esp_err_t err = isl28022_write_reg(dev, CONFIG, config_value);
return err;
}
esp_err_t isl28022_get_calibration(isl28022_driver_t *isl28022_dev, uint16_t *register_bits)
{
if (isl28022_dev == NULL || register_bits == NULL)
{
return ESP_ERR_INVALID_ARG;
}
uint16_t reg_value;
esp_err_t err = isl28022_read_reg(isl28022_dev, CALIB, ®_value);
if (err == ESP_OK)
{
*register_bits = reg_value;
ESP_LOGI(TAG, "Config register read value: 0x%04x", *register_bits);
}
else
{
ESP_LOGE(TAG, "Failed to read config register");
}
return err;
}
esp_err_t isl28022_set_calibration(isl28022_driver_t *dev)
{
if (dev == NULL || dev->shunt_resistor_ohms <= 0)
{
return ESP_ERR_INVALID_ARG;
}
// Step 1: Read the configuration register to get PGA and SADC settings
uint16_t reg_value;
esp_err_t err = isl28022_read_reg(dev, CONFIG, ®_value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to read configuration register: %s", esp_err_to_name(err));
return err;
}
// Extract PGA and SADC settings
isl28022_pg_t pga = (reg_value >> PGA0) & 0x03;
isl28022_sadc_t sadc = (reg_value >> SADC0) & 0x0F;
// Determine V_SHUNT_FS based on PGA setting
float v_shunt_fs;
switch (pga)
{
case PGA_1_40MV:
v_shunt_fs = 0.04;
break;
case PGA_2_80MV:
v_shunt_fs = 0.08;
break;
case PGA_4_160MV:
v_shunt_fs = 0.16;
break;
case PGA_8_320MV:
v_shunt_fs = 0.32;
break;
default:
return ESP_ERR_INVALID_STATE;
}
// Determine ADC resolution based on SADC setting
uint32_t adc_resolution;
switch (sadc)
{
case SADC_12BIT:
adc_resolution = 1 << 12; // 12 bit ADC accuracy
break;
case SADC_13BIT:
adc_resolution = 1 << 13;
break;
case SADC_14BIT:
adc_resolution = 1 << 14;
break;
// case SADC_15BIT:
// adc_resolution = 1 << 15;
// break;
// case SADC_15BIT_1:
// adc_resolution = 1 << 15;
// break;
default:
adc_resolution = 1 << 15;
break;
}
// Step 2: Calculate the Current Full Scale
float current_full_scale = calculate_current_full_scale(v_shunt_fs, dev->shunt_resistor_ohms);
// Step 3: Calculate the Current_LSB
float current_lsb = calculate_current_lsb(current_full_scale, adc_resolution);
// Step 4: Calculate the Calibration value
uint16_t cal_reg_value = calculate_calibration_value(current_lsb, dev->shunt_resistor_ohms);
// Step 5: Write the Calibration value to the Calibration Register
err = isl28022_write_reg(dev, CALIB, cal_reg_value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to write calibration register: %s", esp_err_to_name(err));
return err;
}
// Step 6: Calculate and store the Power_LSB
float power_lsb = 20 * current_lsb;
// Store calculated values in the device structure for future use
dev->current_lsb = current_lsb;
dev->power_lsb = power_lsb;
ESP_LOGI(TAG, "Calibration set: Cal = %u, Current_FS = %.6f A, Current_LSB = %.9f A, Power_LSB = %.9f W",
cal_reg_value, current_full_scale, current_lsb, power_lsb);
return err;
}
esp_err_t isl28022_read_bus_voltage(isl28022_driver_t *isl28022_dev, float *voltage_reading)
{
if (isl28022_dev == NULL || voltage_reading == NULL)
{
return ESP_ERR_INVALID_ARG;
}
// Get the BRNG bits from config register
uint16_t reg_value;
esp_err_t err = isl28022_read_reg(isl28022_dev, CONFIG, ®_value);
if (err != ESP_OK)
{
return err;
}
isl28022_brng_t brng = (reg_value >> BRNG0) & 0x03;
// Get the bus voltage register bits
err = isl28022_read_reg(isl28022_dev, BUS_V, ®_value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to read bus voltage register");
return err;
}
// Calculate the bus voltage using register bits and bit weights associated with the BRNG setting
uint32_t vbus = 0;
uint16_t bit_mask;
uint16_t bit_weight;
switch (brng)
{
case BRNG_16V:
// 12 bits wide, starting from bit 14
for (int i = 0; i < 12; i++)
{
bit_mask = 1 << (14 - i);
bit_weight = 1 << (11 - i); // Weight starts at 2048 (2^11) -> 1 (2^0)
if (reg_value & bit_mask)
{
vbus += bit_weight;
}
}
break;
case BRNG_32V:
// 13 bits wide, starting from bit 15
for (int i = 0; i < 13; i++)
{
bit_mask = 1 << (15 - i);
bit_weight = 1 << (12 - i); // Weight starts at 4096 (2^12) -> 1 (2^0)
if (reg_value & bit_mask)
{
vbus += bit_weight;
}
}
break;
case BRNG_60V:
default:
// 14 bits wide, starting from bit 15
for (int i = 0; i < 14; i++)
{
bit_mask = 1 << (15 - i);
bit_weight = 1 << (13 - i); // Weight starts at 8192 (2^13) -> 1 (2^0)
if (reg_value & bit_mask)
{
vbus += bit_weight;
}
}
break;
}
*voltage_reading = (float)vbus * ISL28022_BUS_VOLTAGE_LSB_MILLIVOLT;
return err;
}
esp_err_t isl28022_read_current(isl28022_driver_t *isl28022_dev, float *current_reading)
{
if (isl28022_dev == NULL || current_reading == NULL)
{
return ESP_ERR_INVALID_ARG;
}
uint16_t reg_value;
esp_err_t err = isl28022_read_reg(isl28022_dev, CURRENT, ®_value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to read current register");
return err;
}
// Calculate the current using the register reading
float current = 0;
// The current register is always 16 bits wide, with the MSB being the sign bit
for (int i = 0; i < 16; i++)
{
int16_t bit_weight = 1 << (15 - i);
if (reg_value & bit_weight)
{
if (i == 0)
{
// Handle the sign bit
current -= 32768;
}
else
{
current += bit_weight;
}
}
}
current *= isl28022_dev->current_lsb;
*current_reading = current;
return err;
}
esp_err_t isl28022_read_power(isl28022_driver_t *isl28022_dev, float *power_reading)
{
if (isl28022_dev == NULL || power_reading == NULL)
{
return ESP_ERR_INVALID_ARG;
}
uint16_t reg_value;
esp_err_t err = isl28022_read_reg(isl28022_dev, POWER, ®_value);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to read current register");
return err;
}
float power = 0;
for (int i = 0; i < 16; i++)
{
int16_t bit_weight = 1 << (15 - i);
if (reg_value & bit_weight)
{
power += bit_weight;
}
}
power *= isl28022_dev->power_lsb;
power *= 5000; // Data sheet specifies to multiply resulting value by 5000, but does not give context for why
// TODO - if Vbus range (BRNG) is set to 60V, the result is multiplied by 2 (this involes reading the config register to get brng value)
*power_reading = power;
return err;
}