8
8
#include "ina219.h"
9
9
10
10
/* 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 );
13
13
14
14
/* 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 )
16
16
{
17
17
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 );
19
19
return (uint16_t )((data [0 ] << 8 ) | data [1 ]);
20
20
}
21
21
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 )
23
23
{
24
24
uint8_t buffer [2 ];
25
25
buffer [0 ] = (data >> 8 ) & 0xFF ;
26
26
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 );
28
28
}
29
29
30
30
/* Sensor Functions */
@@ -35,18 +35,17 @@ INA219_Status_t INA219_Init(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t d
35
35
uint16_t calibration )
36
36
{
37
37
/* TO-DO */
38
- sensor -> i2c = hi2c ;
39
38
sensor -> address = deviceAddr ;
40
39
sensor -> currentCoeff = 0 ;
41
40
sensor -> powerCoeff = 0 ;
42
41
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 )
44
43
return INA219_ERROR ;
45
44
46
- INA219_Reset (sensor );
45
+ INA219_Reset (sensor , hi2c );
47
46
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 );
50
49
51
50
return INA219_OK ;
52
51
}
@@ -56,69 +55,70 @@ INA219_Status_t INA219_Init(INA219_t *sensor, I2C_HandleTypeDef *hi2c, uint8_t d
56
55
#ifdef INA219_32V_2A
57
56
sensor -> currentCoeff = 10.0f ;
58
57
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 )
60
60
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 ));
64
64
return INA219_OK ;
65
65
#endif /* INA219_32V_2A */
66
66
#ifdef INA219_32V_1A
67
67
sensor -> currentCoeff = 25.0f ;
68
68
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 )
70
70
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 ));
74
74
return INA219_OK ;
75
75
#endif /* INA219_32V_1A */
76
76
#ifdef INA219_16V_400mA
77
77
sensor -> currentCoeff = 20.0f ;
78
78
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 )
80
80
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 ));
84
84
return INA219_OK ;
85
85
#endif /* INA219_16V_400mA */
86
86
}
87
87
#endif
88
88
89
- void INA219_GetBusVoltage (INA219_t * sensor )
89
+ void INA219_GetBusVoltage (INA219_t * sensor , I2C_HandleTypeDef * hi2c )
90
90
{
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 */
93
93
}
94
94
95
- void INA219_GetShuntVoltage (INA219_t * sensor )
95
+ void INA219_GetShuntVoltage (INA219_t * sensor , I2C_HandleTypeDef * hi2c )
96
96
{
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 */
99
99
}
100
100
101
- void INA219_GetCurrent (INA219_t * sensor )
101
+ void INA219_GetCurrent (INA219_t * sensor , I2C_HandleTypeDef * hi2c )
102
102
{
103
- sensor -> current = (int16_t )(_Read (sensor , CURRENT ) / sensor -> currentCoeff );
103
+ sensor -> current = (int16_t )(_Read (sensor , hi2c , CURRENT ) / sensor -> currentCoeff ); /* mA */
104
104
}
105
105
106
- void INA219_GetPower (INA219_t * sensor )
106
+ void INA219_GetPower (INA219_t * sensor , I2C_HandleTypeDef * hi2c )
107
107
{
108
- sensor -> power = (int16_t )(_Read (sensor , POWER ) * sensor -> powerCoeff );
108
+ sensor -> power = (int16_t )(_Read (sensor , hi2c , POWER ) * sensor -> powerCoeff ); /* mW */
109
109
}
110
110
111
- void INA219_Reset (INA219_t * sensor )
111
+ void INA219_Reset (INA219_t * sensor , I2C_HandleTypeDef * hi2c )
112
112
{
113
- _Write (sensor , CONFIGURATION , 0x8000 );
113
+ _Write (sensor , hi2c , CONFIGURATION , 0x8000 );
114
114
}
115
115
116
- void INA219_SetCalibration (INA219_t * sensor , uint16_t calibration )
116
+ void INA219_SetCalibration (INA219_t * sensor , I2C_HandleTypeDef * hi2c , uint16_t calibration )
117
117
{
118
- _Write (sensor , CALIBRATION , calibration );
118
+ _Write (sensor , hi2c , CALIBRATION , calibration );
119
119
}
120
120
121
- void INA219_SetConfiguration (INA219_t * sensor , uint16_t config )
121
+ void INA219_SetConfiguration (INA219_t * sensor , I2C_HandleTypeDef * hi2c , uint16_t config )
122
122
{
123
- _Write (sensor , CONFIGURATION , config );
123
+ _Write (sensor , hi2c , CONFIGURATION , config );
124
124
}
0 commit comments