Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / INA219 / v1 / alld_INA219.h @ ed9a1bf5

History | View | Annotate | Download (7.177 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  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
/**
20
 * @file    alld_INA219.h
21
 * @brief   Power Monitor macros and structures.
22
 *
23
 * @addtogroup lld_power
24
 * @{
25
 */
26

    
27
#ifndef AMIROLLD_INA219_H
28
#define AMIROLLD_INA219_H
29

    
30
#include <amiro-lld.h>
31

    
32
/******************************************************************************/
33
/* CONSTANTS                                                                  */
34
/******************************************************************************/
35

    
36
/**
37
 * @brief Maximum I2C frequency.
38
 */
39
#define INA219_LLD_I2C_MAXFREQUENCY   2560000
40

    
41
/******************************************************************************/
42
/* SETTINGS                                                                   */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* CHECKS                                                                     */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* DATA STRUCTURES AND TYPES                                                  */
51
/******************************************************************************/
52

    
53
/**
54
 * @brief I2C address masks.
55
 */
56
enum {
57
  INA219_LLD_I2C_ADDR_FIXED   = 0x0040u,
58
  INA219_LLD_I2C_ADDR_A0      = 0x0001u,
59
  INA219_LLD_I2C_ADDR_A1      = 0x0004u,
60
};
61

    
62
/**
63
 * @brief Registers.
64
 */
65
typedef enum {
66
  INA219_LLD_REGISTER_CONFIGURATION = 0x00,
67
  INA219_LLD_REGISTER_SHUNT_VOLTAGE = 0x01,
68
  INA219_LLD_REGISTER_BUS_VOLTAGE   = 0x02,
69
  INA219_LLD_REGISTER_POWER         = 0x03,
70
  INA219_LLD_REGISTER_CURRENT       = 0x04,
71
  INA219_LLD_REGISTER_CALIBRATION   = 0x05,
72
} ina219_lld_register_t;
73

    
74
/**
75
 * @brief BRNG.
76
 */
77
typedef enum {
78
  INA219_LLD_BRNG_16 = 0x00,
79
  INA219_LLD_BRNG_32 = 0x01,
80
} ina219_lld_brng_t;
81

    
82
/**
83
 * @brief Gain settings.
84
 */
85
typedef enum {
86
  INA219_LLD_GAIN_1_40mV = 0x00,
87
  INA219_LLD_GAIN_2_80mV = 0x01,
88
  INA219_LLD_GAIN_4_160mV = 0x02,
89
  INA219_LLD_GAIN_8_320mV = 0x03,
90
} ina219_lld_gain_t;
91

    
92
/**
93
 * @brief ADC settings.
94
 */
95
typedef enum {
96
  INA219_LLD_ADC_9BIT = 0x00,
97
  INA219_LLD_ADC_10BIT = 0x01,
98
  INA219_LLD_ADC_11BIT = 0x02,
99
  INA219_LLD_ADC_12BIT = 0x03,
100
  INA219_LLD_ADC_2 = 0x09,
101
  INA219_LLD_ADC_4 = 0x0A,
102
  INA219_LLD_ADC_8 = 0x0B,
103
  INA219_LLD_ADC_16 = 0x0C,
104
  INA219_LLD_ADC_32 = 0x0D,
105
  INA219_LLD_ADC_64 = 0x0E,
106
  INA219_LLD_ADC_128 = 0x0f,
107
} ina219_lld_adc_t;
108

    
109
/**
110
 * @brief Mode settings.
111
 */
112
typedef enum {
113
  INA219_LLD_MODE_POWERDOWN = 0x00,
114
  INA219_LLD_MODE_SHUNT_TRIGGERED = 0x01,
115
  INA219_LLD_MODE_BUS_TRIGGERED = 0x02,
116
  INA219_LLD_MODE_SHUNT_BUS_TRIGGERED = 0x03,
117
  INA219_LLD_MODE_ADC_OFF = 0x04,
118
  INA219_LLD_MODE_SHUNT_CONTINUOUS = 0x05,
119
  INA219_LLD_MODE_BUS_CONTINUOUS = 0x06,
120
  INA219_LLD_MODE_SHUNT_BUS_CONTINUOUS = 0x07,
121
} ina219_lld_mode_t;
122

    
123
/**
124
 * @brief Config register.
125
 */
126
typedef union {
127
  uint16_t data;
128
  struct {
129
    ina219_lld_mode_t mode : 3;
130
    ina219_lld_adc_t sadc : 4;
131
    ina219_lld_adc_t badc : 4;
132
    ina219_lld_gain_t gain : 2;
133
    ina219_lld_brng_t brng : 1;
134
    uint8_t zero : 1;
135
    uint8_t reset : 1;
136
  } options;
137
} ina219_lld_cfg_t;
138

    
139
/**
140
 * @brief Calibration input struct.
141
 */
142
typedef struct {
143
  float shunt_resistance_0;
144
  float max_expected_current_A;
145
  uint16_t current_lsb_uA;
146
  ina219_lld_cfg_t cfg_reg;
147
} ina219_lld_calib_input_t;
148

    
149
/**
150
 * @brief Calibration output struct.
151
 */
152
typedef struct {
153
  float max_current_before_overflow_A;
154
  float max_shunt_voltage_before_overflow_V;
155
  uint16_t current_lsb_uA;
156
  uint16_t calibration;
157
} ina219_lld_calib_output_t;
158

    
159
/**
160
 * @brief The INA219Driver struct
161
 */
162
typedef struct {
163
  apalI2CDriver_t* i2cd;
164
  apalI2Caddr_t addr;   /**<The address of the INA219 for I2C communication, which is defined by the wiring of the A0 and A1 pins */
165
  uint16_t current_lsb_uA;
166
  ina219_lld_cfg_t *config;
167
} INA219Driver;
168

    
169
/******************************************************************************/
170
/* MACROS                                                                     */
171
/******************************************************************************/
172

    
173
/******************************************************************************/
174
/* EXTERN DECLARATIONS                                                        */
175
/******************************************************************************/
176

    
177
#ifdef __cplusplus
178
extern "C" {
179
#endif
180
  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);
181
  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);
182
  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);
183
  apalExitStatus_t ina219_lld_read_config(const INA219Driver* const ina219, ina219_lld_cfg_t* const cfg, const apalTime_t timeout);
184
  apalExitStatus_t ina219_lld_write_config(const INA219Driver* const ina219, const ina219_lld_cfg_t cfg, const apalTime_t timeout);
185
  apalExitStatus_t ina219_lld_read_calibration(const INA219Driver* const ina219, uint16_t* const calib, const apalTime_t timeout);
186
  apalExitStatus_t ina219_lld_write_calibration(const INA219Driver* const ina219, const uint16_t calib, const apalTime_t timeout);
187
  apalExitStatus_t ina219_lld_reset(const INA219Driver* const ina219, const apalTime_t timeout);
188
  apalExitStatus_t ina219_lld_read_shunt_voltage(const INA219Driver* const ina219, int32_t* const data, const apalTime_t timeout);
189
  apalExitStatus_t ina219_lld_read_bus_voltage(const INA219Driver* const ina219, uint32_t* const data, const apalTime_t timeout);
190
  apalExitStatus_t ina219_lld_read_power(const INA219Driver* const ina219, uint32_t* const data, const apalTime_t timeout);
191
  apalExitStatus_t ina219_lld_read_current(const INA219Driver* const ina219, int16_t* const data, const apalTime_t timeout);
192
  apalExitStatus_t ina219_lld_bus_conversion_ready(const INA219Driver* const ina219, uint16_t* const buscnv, const apalTime_t timeout);
193
#ifdef __cplusplus
194
}
195
#endif
196

    
197
/******************************************************************************/
198
/* INLINE FUNCTIONS                                                           */
199
/******************************************************************************/
200

    
201
#endif /* AMIROLLD_INA219_H */
202

    
203
/** @} */
204