-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathina226.c
137 lines (105 loc) · 3.6 KB
/
ina226.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
/*
* ina226.c
*
* Created on: Oct 10, 2024
* Author: Pantelos
*/
#include "ina226.h"
ina226_status ina226_init(ina226_handle *ina226, I2C_HandleTypeDef *hi2c1, uint16_t configuration)
{
uint8_t configuration_data[2];
ina226->hi2c1 = hi2c1;
if (HAL_I2C_IsDeviceReady(ina226->hi2c1, INA226_I2C_ADDRESS, 10, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
// software reset
configuration_data[0] = (RESET_ENABLE & 0xff00) >> 8;
configuration_data[1] = 0 & 0x00ff;
if (HAL_I2C_Mem_Write(ina226->hi2c1, INA226_I2C_ADDRESS, (uint16_t) CONFIG_REG, 1, configuration_data, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
HAL_Delay(100);
configuration_data[0] = (configuration & 0xff00) >> 8;
configuration_data[1] = configuration & 0x00ff;
if (HAL_I2C_Mem_Write(ina226->hi2c1, INA226_I2C_ADDRESS, (uint16_t) CONFIG_REG, 1, configuration_data, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
return INA_STATUS_OK;
}
ina226_status ina226_set_cal_reg(ina226_handle *ina226)
{
uint8_t cal_reg_data[2];
uint16_t calibration_val = CAL_FINAL;
cal_reg_data[0] = (calibration_val & 0xff00) >> 8;
cal_reg_data[1] = calibration_val & 0x00ff;
if (HAL_I2C_Mem_Write(ina226->hi2c1, INA226_I2C_ADDRESS, (uint16_t) CAL_REG, 1, cal_reg_data, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
return INA_STATUS_OK;
}
uint16_t ina226_read_raw_shunt_voltage(ina226_handle *ina226)
{
uint8_t raw_shunt_reading[2];
if (HAL_I2C_Mem_Read(ina226->hi2c1, INA226_I2C_ADDRESS,(uint16_t) SHUNT_VOLTAGE, 1, raw_shunt_reading, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
return raw_shunt_reading[0] << 8 | raw_shunt_reading[1];
}
uint16_t ina226_read_raw_bus_voltage(ina226_handle *ina226)
{
uint8_t raw_bus_reading[2];
if (HAL_I2C_Mem_Read(ina226->hi2c1, INA226_I2C_ADDRESS,(uint16_t) BUS_VOLTAGE, 1, raw_bus_reading, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
return raw_bus_reading[0] << 8 | raw_bus_reading[1];
}
float ina226_read_bus_voltage(ina226_handle *ina226)
{
uint16_t raw_voltage = ina226_read_raw_bus_voltage(ina226);
return raw_voltage * BUS_VOL_STEP_VALUE;
}
float ina226_current_via_reg(ina226_handle *ina226)
{
uint8_t current_reg_data[2];
int16_t raw_current;
if (HAL_I2C_Mem_Read(ina226->hi2c1, INA226_I2C_ADDRESS, (uint16_t)CURRENT_REG, 1, current_reg_data, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
raw_current = (int16_t)((current_reg_data[0] << 8) | current_reg_data[1]);
return raw_current * CURRENT_LSB;
}
// refer page 16
float ina226_power_via_reg(ina226_handle *ina226)
{
uint8_t power_reg_data[2];
int16_t raw_power;
if (HAL_I2C_Mem_Read(ina226->hi2c1, INA226_I2C_ADDRESS, (uint16_t)POWER_REG, 1, power_reg_data, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
raw_power = (int16_t)((power_reg_data[0] << 8) | power_reg_data[1]);
return raw_power * POWER_LSB;
}
// Used in polling mode to check if conversion is ready so you make the register read only then.
ina226_status check_if_conversion_ready(ina226_handle *ina226)
{
uint8_t mask_reg_data[2];
uint16_t conversion_ready = 0;
if (HAL_I2C_Mem_Read(ina226->hi2c1, INA226_I2C_ADDRESS, (uint16_t)MASK_EN_REG, 1, mask_reg_data, 2, 100) != HAL_OK)
{
return INA_STATUS_I2C_FAIL;
}
conversion_ready = (int16_t)(((mask_reg_data[0] << 8) | mask_reg_data[1]) & 0x0008);
if (conversion_ready)
{
return INA_CONVERSION_READY;
}
return INA_CONVERSION_NOT_READY;
}