Statistics
| Branch: | Tag: | Revision:

amiro-lld / include / INA219 / v1 / alld_INA219_v1.h @ 5d67f4db

History | View | Annotate | Download (7.347 KB)

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

    
27
#ifndef AMIROLLD_INA219_V1_H
28
#define AMIROLLD_INA219_V1_H
29

    
30
#include <amiro-lld.h>
31

    
32
#if (defined(AMIROLLD_CFG_INA219) && (AMIROLLD_CFG_INA219 == 1)) || defined(__DOXYGEN__)
33

    
34
/******************************************************************************/
35
/* CONSTANTS                                                                  */
36
/******************************************************************************/
37

    
38
/**
39
 * @brief Maximum I2C frequency.
40
 */
41
#define INA219_LLD_I2C_MAXFREQUENCY   2560000
42

    
43
/******************************************************************************/
44
/* SETTINGS                                                                   */
45
/******************************************************************************/
46

    
47
/******************************************************************************/
48
/* CHECKS                                                                     */
49
/******************************************************************************/
50

    
51
/******************************************************************************/
52
/* DATA STRUCTURES AND TYPES                                                  */
53
/******************************************************************************/
54

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

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

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

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

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

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

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

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

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

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

    
171
/******************************************************************************/
172
/* MACROS                                                                     */
173
/******************************************************************************/
174

    
175
/******************************************************************************/
176
/* EXTERN DECLARATIONS                                                        */
177
/******************************************************************************/
178

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

    
199
/******************************************************************************/
200
/* INLINE FUNCTIONS                                                           */
201
/******************************************************************************/
202

    
203
#endif /* defined(AMIROLLD_CFG_INA219) && (AMIROLLD_CFG_INA219 == 1) */
204

    
205
#endif /* AMIROLLD_INA219_V1_H */
206

    
207
/** @} */