Skip to content

Commit 62a215a

Browse files
committed
bug fix
i2c etc.
1 parent 0278bc5 commit 62a215a

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

inc/ina219.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
/* Sensor Handler */
4141
typedef struct INA219 {
42-
I2C_HandleTypeDef *i2c;
4342
uint8_t address;
4443
uint16_t shuntVoltage;
4544
uint16_t busVoltage;
@@ -122,15 +121,15 @@ INA219_Status_t INA219_Init(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t d
122121
INA219_Status_t INA219_Init(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t deviceAddr);
123122
#endif
124123

125-
void INA219_GetBusVoltage(INA219_t *sensor);
126-
void INA219_GetShuntVoltage(INA219_t *sensor);
127-
void INA219_GetCurrent(INA219_t *sensor);
128-
void INA219_GetPower(INA219_t *sensor);
124+
void INA219_GetBusVoltage(INA219_t *sensor, I2C_HandleTypeDef *hi2c);
125+
void INA219_GetShuntVoltage(INA219_t *sensor, I2C_HandleTypeDef *hi2c);
126+
void INA219_GetCurrent(INA219_t *sensor, I2C_HandleTypeDef *hi2c);
127+
void INA219_GetPower(INA219_t *sensor, I2C_HandleTypeDef *hi2c);
129128

130-
void INA219_Reset(INA219_t *sensor);
129+
void INA219_Reset(INA219_t *sensor, I2C_HandleTypeDef *hi2c);
131130

132-
void INA219_SetCalibration(INA219_t *sensor, uint16_t calibration);
133-
void INA219_SetConfiguration(INA219_t *sensor, uint16_t config);
131+
void INA219_SetCalibration(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint16_t calibration);
132+
void INA219_SetConfiguration(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint16_t config);
134133

135134

136135
#endif /* INC_INA219_H_ */

src/ina219.c

+39-39
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
#include "ina219.h"
99

1010
/* Private Function Prototypes */
11-
static uint16_t _Read(INA219_t *sensor, uint8_t registerAddr);
12-
static void _Write(INA219_t *sensor, uint8_t registerAddr, uint16_t data);
11+
static uint16_t _Read(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t registerAddr);
12+
static void _Write(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t registerAddr, uint16_t data);
1313

1414
/* Private Functions */
15-
static uint16_t _Read(INA219_t *sensor, uint8_t registerAddr)
15+
static uint16_t _Read(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t registerAddr)
1616
{
1717
uint8_t data[2];
18-
HAL_I2C_Mem_Read_IT(sensor->i2c, (sensor->address << 1), registerAddr, 1, data, 2);
18+
HAL_I2C_Mem_Read_IT(hi2c, (sensor->address << 1), registerAddr, 1, data, 2);
1919
return (uint16_t)((data[0] << 8) | data[1]);
2020
}
2121

22-
static void _Write(INA219_t *sensor, uint8_t registerAddr, uint16_t data)
22+
static void _Write(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t registerAddr, uint16_t data)
2323
{
2424
uint8_t buffer[2];
2525
buffer[0] = (data >> 8) & 0xFF;
2626
buffer[1] = (data >> 0) & 0xFF;
27-
HAL_I2C_Mem_Write_IT(sensor->i2c, (sensor->address << 1), registerAddr, 1, buffer, 2);
27+
HAL_I2C_Mem_Write_IT(hi2c, (sensor->address << 1), registerAddr, 1, buffer, 2);
2828
}
2929

3030
/* Sensor Functions */
@@ -35,18 +35,17 @@ INA219_Status_t INA219_Init(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t d
3535
uint16_t calibration)
3636
{
3737
/* TO-DO */
38-
sensor->i2c = hi2c;
3938
sensor->address = deviceAddr;
4039
sensor->currentCoeff = 0;
4140
sensor->powerCoeff = 0;
4241

43-
if (HAL_I2C_IsDeviceReady(sensor->i2c, (sensor->address), 3, 2) != HAL_OK)
42+
if (HAL_I2C_IsDeviceReady(hi2c, (sensor->address), 3, 2) != HAL_OK)
4443
return INA219_ERROR;
4544

46-
INA219_Reset(sensor);
45+
INA219_Reset(sensor, hi2c);
4746
uint16_t buffer = busRange | gain | busADC | shuntADC | mode;
48-
INA219_SetConfiguration(sensor, buffer);
49-
INA219_SetCalibration(sensor, calibration);
47+
INA219_SetConfiguration(sensor, hi2c, buffer);
48+
INA219_SetCalibration(sensor, hi2c, calibration);
5049

5150
return INA219_OK;
5251
}
@@ -56,69 +55,70 @@ INA219_Status_t INA219_Init(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t d
5655
#ifdef INA219_32V_2A
5756
sensor->currentCoeff = 10.0f;
5857
sensor->powerCoeff = 2.0f;
59-
if (HAL_I2C_IsDeviceReady(sensor->i2c, (sensor->address), 3, 2) != HAL_OK)
58+
sensor->address = (uint8_t)deviceAddr;
59+
if (HAL_I2C_IsDeviceReady(hi2c, (sensor->address << 1), 3, 2) != HAL_OK)
6060
return INA219_ERROR;
61-
INA219_Reset(sensor);
62-
INA219_SetCalibration(sensor, 0x1000);
63-
INA219_SetConfiguration(sensor, (FSR32V | GAIN_8 | BADC_12bit | SADC_12bit | MODE_SHUNT_BUS_CONTINUOUS));
61+
INA219_Reset(sensor, hi2c);
62+
INA219_SetCalibration(sensor, hi2c, 0x1000);
63+
INA219_SetConfiguration(sensor, hi2c, (FSR32V | GAIN_8 | BADC_12bit | SADC_12bit | MODE_SHUNT_BUS_CONTINUOUS));
6464
return INA219_OK;
6565
#endif /* INA219_32V_2A */
6666
#ifdef INA219_32V_1A
6767
sensor->currentCoeff = 25.0f;
6868
sensor->powerCoeff = 0.8f;
69-
if (HAL_I2C_IsDeviceReady(sensor->i2c, (sensor->address), 3, 2) != HAL_OK)
69+
if (HAL_I2C_IsDeviceReady(hi2c, (sensor->address), 3, 2) != HAL_OK)
7070
return INA219_ERROR;
71-
INA219_Reset(sensor);
72-
INA219_SetCalibration(sensor, 0x2800);
73-
INA219_SetConfiguration(sensor, (FSR32V | GAIN_8 | BADC_12bit | SADC_12bit | MODE_SHUNT_BUS_CONTINUOUS));
71+
INA219_Reset(sensor, hi2c);
72+
INA219_SetCalibration(sensor, hi2c, 0x2800);
73+
INA219_SetConfiguration(sensor, hi2c, (FSR32V | GAIN_8 | BADC_12bit | SADC_12bit | MODE_SHUNT_BUS_CONTINUOUS));
7474
return INA219_OK;
7575
#endif /* INA219_32V_1A */
7676
#ifdef INA219_16V_400mA
7777
sensor->currentCoeff = 20.0f;
7878
sensor->powerCoeff = 1.0f;
79-
if (HAL_I2C_IsDeviceReady(sensor->i2c, (sensor->address), 3, 2) != HAL_OK)
79+
if (HAL_I2C_IsDeviceReady(hi2c, (sensor->address), 3, 2) != HAL_OK)
8080
return INA219_ERROR;
81-
INA219_Reset(sensor);
82-
INA219_SetCalibration(sensor, 0x2000);
83-
INA219_SetConfiguration(sensor, (FSR16V | GAIN_1 | BADC_12bit | SADC_12bit | MODE_SHUNT_BUS_CONTINUOUS));
81+
INA219_Reset(sensor, hi2c);
82+
INA219_SetCalibration(sensor, hi2c, 0x2000);
83+
INA219_SetConfiguration(sensor, hi2c, (FSR16V | GAIN_1 | BADC_12bit | SADC_12bit | MODE_SHUNT_BUS_CONTINUOUS));
8484
return INA219_OK;
8585
#endif /* INA219_16V_400mA */
8686
}
8787
#endif
8888

89-
void INA219_GetBusVoltage(INA219_t *sensor)
89+
void INA219_GetBusVoltage(INA219_t *sensor, I2C_HandleTypeDef *hi2c)
9090
{
91-
uint16_t data = _Read(sensor, BUS_VOLTAGE);
92-
sensor->busVoltage = (uint16_t)((data >> 3) * 4);
91+
uint16_t data = _Read(sensor, hi2c, BUS_VOLTAGE);
92+
sensor->busVoltage = (uint16_t)(((data >> 3) * 4) * 0.01f); /* mV */
9393
}
9494

95-
void INA219_GetShuntVoltage(INA219_t *sensor)
95+
void INA219_GetShuntVoltage(INA219_t *sensor, I2C_HandleTypeDef *hi2c)
9696
{
97-
uint16_t data = _Read(sensor, SHUNT_VOLTAGE);
98-
sensor->shuntVoltage = (uint16_t)(data * 0.01f);
97+
uint16_t data = _Read(sensor, hi2c, SHUNT_VOLTAGE);
98+
sensor->shuntVoltage = (uint16_t)(data * 0.01f); /* mV */
9999
}
100100

101-
void INA219_GetCurrent(INA219_t *sensor)
101+
void INA219_GetCurrent(INA219_t *sensor, I2C_HandleTypeDef *hi2c)
102102
{
103-
sensor->current = (int16_t)(_Read(sensor, CURRENT) / sensor->currentCoeff);
103+
sensor->current = (int16_t)(_Read(sensor, hi2c, CURRENT) / sensor->currentCoeff); /* mA */
104104
}
105105

106-
void INA219_GetPower(INA219_t *sensor)
106+
void INA219_GetPower(INA219_t *sensor, I2C_HandleTypeDef *hi2c)
107107
{
108-
sensor->power = (int16_t)(_Read(sensor, POWER) * sensor->powerCoeff);
108+
sensor->power = (int16_t)(_Read(sensor, hi2c, POWER) * sensor->powerCoeff); /* mW */
109109
}
110110

111-
void INA219_Reset(INA219_t *sensor)
111+
void INA219_Reset(INA219_t *sensor, I2C_HandleTypeDef *hi2c)
112112
{
113-
_Write(sensor, CONFIGURATION, 0x8000);
113+
_Write(sensor, hi2c, CONFIGURATION, 0x8000);
114114
}
115115

116-
void INA219_SetCalibration(INA219_t *sensor, uint16_t calibration)
116+
void INA219_SetCalibration(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint16_t calibration)
117117
{
118-
_Write(sensor, CALIBRATION, calibration);
118+
_Write(sensor, hi2c, CALIBRATION, calibration);
119119
}
120120

121-
void INA219_SetConfiguration(INA219_t *sensor, uint16_t config)
121+
void INA219_SetConfiguration(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint16_t config)
122122
{
123-
_Write(sensor, CONFIGURATION, config);
123+
_Write(sensor, hi2c, CONFIGURATION, config);
124124
}

0 commit comments

Comments
 (0)