1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 10:12:45 +01:00

drivers/ina3221: SAUL refactor

This commit is contained in:
Fabian Hüßler 2021-02-02 11:15:57 +01:00
parent c50149e488
commit c8d1cde380

View File

@ -26,116 +26,82 @@
#define ENABLE_DEBUG 0 #define ENABLE_DEBUG 0
#include "debug.h" #include "debug.h"
#define SAUL_INA3221_NO_VALUE (0)
static int read_bus_voltage(const void *dev, phydat_t *res) static int read_bus_voltage(const void *dev, phydat_t *res)
{ {
ina3221_t *_dev = (ina3221_t *)dev; ina3221_ch_t ch = 0;
uint16_t ench = 0; int32_t values[INA3221_NUM_CH] = { 0 };
int16_t voltage[INA3221_NUM_CH] = { 0 }; ina3221_get_enable_channel(dev, &ch);
int num_ch = _ina3221_get_enable_channel(_dev, &ench); if (ch) {
int16_t voltage[INA3221_NUM_CH];
if (!num_ch) { ch &= ina3221_read_bus_mv(dev, voltage, NULL);
return -ECANCELED; res->scale = -3; /* mV to V */
} res->unit = UNIT_V;
ina3221_channel_t ch = (((ench & INA3221_ENABLE_CH1) ? INA3221_CH1 : 0) | for (int i = 0; i < INA3221_NUM_CH; i++) {
((ench & INA3221_ENABLE_CH2) ? INA3221_CH2 : 0) | values[i] = voltage[i];
((ench & INA3221_ENABLE_CH3) ? INA3221_CH3 : 0)); }
ina3221_read_bus_mv(_dev, ch, voltage, NULL);
res->scale = -3; /* mV to V*/
res->unit = UNIT_V;
for (int i = 0, j = 0; i < INA3221_NUM_CH; i++) {
res->val[i] = (ch & (1 << i))
? voltage[j++] : SAUL_INA3221_NO_VALUE;
} }
phydat_fit(res, values, 3);
return INA3221_NUM_CH; return INA3221_NUM_CH;
} }
static int read_current(const void *dev, phydat_t *res) static int read_current(const void *dev, phydat_t *res)
{ {
ina3221_t *_dev = (ina3221_t *)dev; ina3221_ch_t ch = 0;
uint16_t ench = 0;
int32_t shunt_uv[INA3221_NUM_CH] = { 0 };
int32_t current[INA3221_NUM_CH] = { 0 }; int32_t current[INA3221_NUM_CH] = { 0 };
int num_ch = _ina3221_get_enable_channel(_dev, &ench); ina3221_get_enable_channel(dev, &ch);
if (ch) {
if (!num_ch) { int32_t shunt_uv[INA3221_NUM_CH];
return -ECANCELED; ch &= ina3221_read_shunt_uv(dev, shunt_uv, NULL);
ina3221_calculate_current_ua(ch, ((const ina3221_t *)dev)->params.rshunt_mohm,
shunt_uv, current);
res->scale = -6; /* uA to A */
res->unit = UNIT_A;
} }
ina3221_channel_t ch = (((ench & INA3221_ENABLE_CH1) ? INA3221_CH1 : 0) |
((ench & INA3221_ENABLE_CH2) ? INA3221_CH2 : 0) |
((ench & INA3221_ENABLE_CH3) ? INA3221_CH3 : 0));
ina3221_read_shunt_uv(_dev, ch, shunt_uv, NULL);
ina3221_calculate_current_ua(_dev, ch, shunt_uv, current);
res->scale = -6; /* uA to A*/
res->unit = UNIT_A;
for (int i = 0, j = 0; i < INA3221_NUM_CH; i++) {
res->val[i] = (ch & (1 << i))
? (int16_t)current[j++] : SAUL_INA3221_NO_VALUE;
}
ina3221_ch_align(ch, current, current, sizeof(int32_t));
phydat_fit(res, current, 3); phydat_fit(res, current, 3);
return INA3221_NUM_CH; return INA3221_NUM_CH;
} }
static int read_power(const void *dev, phydat_t *res) static int read_power(const void *dev, phydat_t *res)
{ {
ina3221_t *_dev = (ina3221_t *)dev; ina3221_ch_t ch = 0;
uint16_t ench = 0;
int32_t shunt_uv[INA3221_NUM_CH] = { 0 };
int32_t current_ua[INA3221_NUM_CH] = { 0 };
int16_t bus_mv[INA3221_NUM_CH] = { 0 };
int32_t power[INA3221_NUM_CH] = { 0 }; int32_t power[INA3221_NUM_CH] = { 0 };
int num_ch = _ina3221_get_enable_channel(_dev, &ench); ina3221_get_enable_channel(dev, &ch);
if (ch) {
if (!num_ch) { int32_t shunt_uv[INA3221_NUM_CH];
return -ECANCELED; int32_t current_ua[INA3221_NUM_CH];
int16_t bus_mv[INA3221_NUM_CH];
ch &= ina3221_read_shunt_uv(dev, shunt_uv, NULL);
ch &= ina3221_read_bus_mv(dev, bus_mv, NULL);
ina3221_calculate_current_ua(ch, ((const ina3221_t *)dev)->params.rshunt_mohm,
shunt_uv, current_ua);
ina3221_calculate_power_uw(ch, bus_mv, current_ua, power);
res->scale = -6; /* uW to W */
res->unit = UNIT_W;
} }
ina3221_channel_t ch = (((ench & INA3221_ENABLE_CH1) ? INA3221_CH1 : 0) |
((ench & INA3221_ENABLE_CH2) ? INA3221_CH2 : 0) |
((ench & INA3221_ENABLE_CH3) ? INA3221_CH3 : 0));
int num_ch_shunt_uv = ina3221_read_shunt_uv(_dev, ch, shunt_uv, NULL);
ina3221_calculate_current_ua(_dev, ch, shunt_uv, current_ua);
ina3221_read_bus_mv(_dev, ch, bus_mv, NULL);
ina3221_calculate_power_uw(bus_mv, current_ua, num_ch_shunt_uv, power);
res->scale = -6; /* uW to W*/
res->unit = UNIT_W;
for (int i = 0, j = 0; i < INA3221_NUM_CH; i++) {
res->val[i] = (ch & (1 << i))
? (int16_t)power[j++] : SAUL_INA3221_NO_VALUE;
}
ina3221_ch_align(ch, power, power, sizeof(int32_t));
phydat_fit(res, power, 3); phydat_fit(res, power, 3);
return INA3221_NUM_CH; return INA3221_NUM_CH;
} }
static int read_shunt_voltage_sum(const void *dev, phydat_t *res) static int read_shunt_voltage_sum(const void *dev, phydat_t *res)
{ {
ina3221_t *_dev = (ina3221_t *)dev; ina3221_ch_t ch = 0;
uint16_t ench = 0; int32_t shunt_voltage_sum = 0;
int32_t shunt_voltage_sum = SAUL_INA3221_NO_VALUE; ina3221_get_enable_channel(dev, &ch);
int num_ch = _ina3221_get_enable_channel(_dev, &ench); if (ch) {
ina3221_read_shunt_sum_uv(dev, &shunt_voltage_sum, NULL);
if (!num_ch) { res->scale = -6; /* uV to V */
return -ECANCELED; res->unit = UNIT_V;
} }
ina3221_read_shunt_sum_uv(_dev, &shunt_voltage_sum, NULL);
res->scale = -6; /* uV to V*/
res->unit = UNIT_V;
res->val[0] = shunt_voltage_sum;
phydat_fit(res, &shunt_voltage_sum, 1); phydat_fit(res, &shunt_voltage_sum, 1);
return 1; return 1;
} }
static int configure_channel(const void *dev, phydat_t *data) static int configure_channel(const void *dev, phydat_t *data)
{ {
ina3221_t *_dev = (ina3221_t *)dev; ina3221_ch_t ch = (data->val[0] ? INA3221_CH1 : 0) |
uint16_t ench = (data->val[1] ? INA3221_CH2 : 0) |
((data->val[0] & INA3221_CH1) ? INA3221_ENABLE_CH1 : 0) | (data->val[2] ? INA3221_CH3 : 0);
((data->val[0] & INA3221_CH2) ? INA3221_ENABLE_CH2 : 0) | if (ina3221_set_enable_channel((ina3221_t *)dev, ch) != 0) {
((data->val[0] & INA3221_CH3) ? INA3221_ENABLE_CH3 : 0);
if (_ina3221_set_enable_channel(_dev, ench) != INA3221_OK) {
return -ECANCELED; return -ECANCELED;
} }
return INA3221_NUM_CH; return INA3221_NUM_CH;
@ -143,13 +109,10 @@ static int configure_channel(const void *dev, phydat_t *data)
static int configure_channel_sum(const void *dev, phydat_t *data) static int configure_channel_sum(const void *dev, phydat_t *data)
{ {
ina3221_t *_dev = (ina3221_t *)dev; ina3221_ch_t ch = (data->val[0] ? INA3221_CH1 : 0) |
uint16_t esch = (data->val[1] ? INA3221_CH2 : 0) |
((data->val[0] & INA3221_CH1) ? INA3221_ENABLE_SUM_CH1 : 0) | (data->val[2] ? INA3221_CH3 : 0);
((data->val[0] & INA3221_CH2) ? INA3221_ENABLE_SUM_CH2 : 0) | if (ina3221_set_enable_sum_channel(dev, ch) != 0) {
((data->val[0] & INA3221_CH3) ? INA3221_ENABLE_SUM_CH3 : 0);
if (_ina3221_set_enable_sum_channel(_dev, esch) != INA3221_OK) {
return -ECANCELED; return -ECANCELED;
} }
return INA3221_NUM_CH; return INA3221_NUM_CH;