amiro-lld / include / alld_ina219.h @ 616f3584
History | View | Annotate | Download (5.48 KB)
1 |
/*
|
---|---|
2 |
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2018 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU Lesser General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU Lesser General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU Lesser General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
#ifndef _AMIROLLD_INA219_H_
|
20 |
#define _AMIROLLD_INA219_H_
|
21 |
|
22 |
#include <amiro-lld.h> |
23 |
|
24 |
#if defined(AMIROLLD_CFG_USE_INA219) || defined(__DOXYGEN__)
|
25 |
|
26 |
/**
|
27 |
* @brief Maximum I2C frequency.
|
28 |
*/
|
29 |
#define INA219_LLD_I2C_MAXFREQUENCY 2560000 |
30 |
|
31 |
/**
|
32 |
* @brief I2C address masks.
|
33 |
*/
|
34 |
enum {
|
35 |
INA219_LLD_I2C_ADDR_FIXED = 0x40u,
|
36 |
INA219_LLD_I2C_ADDR_A0 = 0x01u,
|
37 |
INA219_LLD_I2C_ADDR_A1 = 0x04u,
|
38 |
}; |
39 |
|
40 |
/**
|
41 |
* @brief Registers.
|
42 |
*/
|
43 |
typedef enum { |
44 |
INA219_LLD_REGISTER_CONFIGURATION = 0x00,
|
45 |
INA219_LLD_REGISTER_SHUNT_VOLTAGE = 0x01,
|
46 |
INA219_LLD_REGISTER_BUS_VOLTAGE = 0x02,
|
47 |
INA219_LLD_REGISTER_POWER = 0x03,
|
48 |
INA219_LLD_REGISTER_CURRENT = 0x04,
|
49 |
INA219_LLD_REGISTER_CALIBRATION = 0x05,
|
50 |
} ina219_lld_register_t; |
51 |
|
52 |
/**
|
53 |
* @brief BRNG.
|
54 |
*/
|
55 |
typedef enum { |
56 |
INA219_LLD_BRNG_16 = 0x00,
|
57 |
INA219_LLD_BRNG_32 = 0x01,
|
58 |
} ina219_lld_brng_t; |
59 |
|
60 |
/**
|
61 |
* @brief Gain settings.
|
62 |
*/
|
63 |
typedef enum { |
64 |
INA219_LLD_GAIN_1_40mV = 0x00,
|
65 |
INA219_LLD_GAIN_2_80mV = 0x01,
|
66 |
INA219_LLD_GAIN_4_160mV = 0x02,
|
67 |
INA219_LLD_GAIN_8_320mV = 0x03,
|
68 |
} ina219_lld_gain_t; |
69 |
|
70 |
/**
|
71 |
* @brief ADC settings.
|
72 |
*/
|
73 |
typedef enum { |
74 |
INA219_LLD_ADC_9BIT = 0x00,
|
75 |
INA219_LLD_ADC_10BIT = 0x01,
|
76 |
INA219_LLD_ADC_11BIT = 0x02,
|
77 |
INA219_LLD_ADC_12BIT = 0x03,
|
78 |
INA219_LLD_ADC_2 = 0x09,
|
79 |
INA219_LLD_ADC_4 = 0x0A,
|
80 |
INA219_LLD_ADC_8 = 0x0B,
|
81 |
INA219_LLD_ADC_16 = 0x0C,
|
82 |
INA219_LLD_ADC_32 = 0x0D,
|
83 |
INA219_LLD_ADC_64 = 0x0E,
|
84 |
INA219_LLD_ADC_128 = 0x0f,
|
85 |
} ina219_lld_adc_t; |
86 |
|
87 |
/**
|
88 |
* @brief Mode settings.
|
89 |
*/
|
90 |
typedef enum { |
91 |
INA219_LLD_MODE_POWERDOWN = 0x00,
|
92 |
INA219_LLD_MODE_SHUNT_TRIGGERED = 0x01,
|
93 |
INA219_LLD_MODE_BUS_TRIGGERED = 0x02,
|
94 |
INA219_LLD_MODE_SHUNT_BUS_TRIGGERED = 0x03,
|
95 |
INA219_LLD_MODE_ADC_OFF = 0x04,
|
96 |
INA219_LLD_MODE_SHUNT_CONTINUOUS = 0x05,
|
97 |
INA219_LLD_MODE_BUS_CONTINUOUS = 0x06,
|
98 |
INA219_LLD_MODE_SHUNT_BUS_CONTINUOUS = 0x07,
|
99 |
} ina219_lld_mode_t; |
100 |
|
101 |
/**
|
102 |
* @brief Config register.
|
103 |
*/
|
104 |
typedef union { |
105 |
uint16_t data; |
106 |
struct {
|
107 |
ina219_lld_mode_t mode : 3;
|
108 |
ina219_lld_adc_t sadc : 4;
|
109 |
ina219_lld_adc_t badc : 4;
|
110 |
ina219_lld_gain_t gain : 2;
|
111 |
ina219_lld_brng_t brng : 1;
|
112 |
uint8_t zero : 1;
|
113 |
uint8_t reset : 1;
|
114 |
} options; |
115 |
} ina219_lld_cfg_t; |
116 |
|
117 |
/**
|
118 |
* @brief Calibration input struct.
|
119 |
*/
|
120 |
typedef struct { |
121 |
float shunt_resistance_0;
|
122 |
float max_expected_current_A;
|
123 |
uint16_t current_lsb_uA; |
124 |
ina219_lld_cfg_t cfg_reg; |
125 |
} ina219_lld_calib_input_t; |
126 |
|
127 |
/**
|
128 |
* @brief Calibration output struct.
|
129 |
*/
|
130 |
typedef struct { |
131 |
float max_current_before_overflow_A;
|
132 |
float max_shunt_voltage_before_overflow_V;
|
133 |
uint16_t current_lsb_uA; |
134 |
uint16_t calibration; |
135 |
} ina219_lld_calib_output_t; |
136 |
|
137 |
/**
|
138 |
* @brief The INA219Driver struct
|
139 |
*/
|
140 |
typedef struct { |
141 |
apalI2CDriver_t* i2cd; |
142 |
uint8_t addr; /**<The address of the INA219 for I2C communication, which is defined by the wiring of the A0 and A1 pins */
|
143 |
uint16_t current_lsb_uA; |
144 |
ina219_lld_cfg_t *config; |
145 |
} INA219Driver; |
146 |
|
147 |
#ifdef __cplusplus
|
148 |
extern "C" { |
149 |
#endif
|
150 |
apalExitStatus_t ina219_lld_calibration(INA219Driver* const ina219, const ina219_lld_calib_input_t* const calib_data, ina219_lld_calib_output_t* const calib_out); |
151 |
apalExitStatus_t ina219_lld_read_register(const INA219Driver* const ina219, const ina219_lld_register_t addr, uint16_t* const data, const uint8_t num, const apalTime_t timeout); |
152 |
apalExitStatus_t ina219_lld_write_register(const INA219Driver* const ina219, const ina219_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout); |
153 |
apalExitStatus_t ina219_lld_read_config(const INA219Driver* const ina219, ina219_lld_cfg_t* const cfg, const apalTime_t timeout); |
154 |
apalExitStatus_t ina219_lld_write_config(const INA219Driver* const ina219, const ina219_lld_cfg_t cfg, const apalTime_t timeout); |
155 |
apalExitStatus_t ina219_lld_read_calibration(const INA219Driver* const ina219, uint16_t* const calib, const apalTime_t timeout); |
156 |
apalExitStatus_t ina219_lld_write_calibration(const INA219Driver* const ina219, const uint16_t calib, const apalTime_t timeout); |
157 |
apalExitStatus_t ina219_lld_reset(const INA219Driver* const ina219, const apalTime_t timeout); |
158 |
apalExitStatus_t ina219_lld_read_shunt_voltage(const INA219Driver* const ina219, int32_t* const data, const apalTime_t timeout); |
159 |
apalExitStatus_t ina219_lld_read_bus_voltage(const INA219Driver* const ina219, uint32_t* const data, const apalTime_t timeout); |
160 |
apalExitStatus_t ina219_lld_read_power(const INA219Driver* const ina219, uint32_t* const data, const apalTime_t timeout); |
161 |
apalExitStatus_t ina219_lld_read_current(const INA219Driver* const ina219, int16_t* const data, const apalTime_t timeout); |
162 |
apalExitStatus_t ina219_lld_bus_conversion_ready(const INA219Driver* const ina219, uint16_t* const buscnv, const apalTime_t timeout); |
163 |
#ifdef __cplusplus
|
164 |
} |
165 |
#endif
|
166 |
|
167 |
#endif /* defined(AMIROLLD_CFG_USE_INA219) */ |
168 |
|
169 |
#endif /* _AMIROLLD_INA219_H_ */ |