Statistics
| Branch: | Tag: | Revision:

amiro-lld / include / alld_ina219.h @ 1d4fc180

History | View | Annotate | Download (5.62 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
/**
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
#if defined(AMIROLLD_CFG_USE_INA219) || defined(__DOXYGEN__)
33

    
34
/**
35
 * @brief Maximum I2C frequency.
36
 */
37
#define INA219_LLD_I2C_MAXFREQUENCY   2560000
38

    
39
/**
40
 * @brief I2C address masks.
41
 */
42
enum {
43
  INA219_LLD_I2C_ADDR_FIXED   = 0x0040u,
44
  INA219_LLD_I2C_ADDR_A0      = 0x0001u,
45
  INA219_LLD_I2C_ADDR_A1      = 0x0004u,
46
};
47

    
48
/**
49
 * @brief Registers.
50
 */
51
typedef enum {
52
  INA219_LLD_REGISTER_CONFIGURATION = 0x00,
53
  INA219_LLD_REGISTER_SHUNT_VOLTAGE = 0x01,
54
  INA219_LLD_REGISTER_BUS_VOLTAGE   = 0x02,
55
  INA219_LLD_REGISTER_POWER         = 0x03,
56
  INA219_LLD_REGISTER_CURRENT       = 0x04,
57
  INA219_LLD_REGISTER_CALIBRATION   = 0x05,
58
} ina219_lld_register_t;
59

    
60
/**
61
 * @brief BRNG.
62
 */
63
typedef enum {
64
  INA219_LLD_BRNG_16 = 0x00,
65
  INA219_LLD_BRNG_32 = 0x01,
66
} ina219_lld_brng_t;
67

    
68
/**
69
 * @brief Gain settings.
70
 */
71
typedef enum {
72
  INA219_LLD_GAIN_1_40mV = 0x00,
73
  INA219_LLD_GAIN_2_80mV = 0x01,
74
  INA219_LLD_GAIN_4_160mV = 0x02,
75
  INA219_LLD_GAIN_8_320mV = 0x03,
76
} ina219_lld_gain_t;
77

    
78
/**
79
 * @brief ADC settings.
80
 */
81
typedef enum {
82
  INA219_LLD_ADC_9BIT = 0x00,
83
  INA219_LLD_ADC_10BIT = 0x01,
84
  INA219_LLD_ADC_11BIT = 0x02,
85
  INA219_LLD_ADC_12BIT = 0x03,
86
  INA219_LLD_ADC_2 = 0x09,
87
  INA219_LLD_ADC_4 = 0x0A,
88
  INA219_LLD_ADC_8 = 0x0B,
89
  INA219_LLD_ADC_16 = 0x0C,
90
  INA219_LLD_ADC_32 = 0x0D,
91
  INA219_LLD_ADC_64 = 0x0E,
92
  INA219_LLD_ADC_128 = 0x0f,
93
} ina219_lld_adc_t;
94

    
95
/**
96
 * @brief Mode settings.
97
 */
98
typedef enum {
99
  INA219_LLD_MODE_POWERDOWN = 0x00,
100
  INA219_LLD_MODE_SHUNT_TRIGGERED = 0x01,
101
  INA219_LLD_MODE_BUS_TRIGGERED = 0x02,
102
  INA219_LLD_MODE_SHUNT_BUS_TRIGGERED = 0x03,
103
  INA219_LLD_MODE_ADC_OFF = 0x04,
104
  INA219_LLD_MODE_SHUNT_CONTINUOUS = 0x05,
105
  INA219_LLD_MODE_BUS_CONTINUOUS = 0x06,
106
  INA219_LLD_MODE_SHUNT_BUS_CONTINUOUS = 0x07,
107
} ina219_lld_mode_t;
108

    
109
/**
110
 * @brief Config register.
111
 */
112
typedef union {
113
  uint16_t data;
114
  struct {
115
    ina219_lld_mode_t mode : 3;
116
    ina219_lld_adc_t sadc : 4;
117
    ina219_lld_adc_t badc : 4;
118
    ina219_lld_gain_t gain : 2;
119
    ina219_lld_brng_t brng : 1;
120
    uint8_t zero : 1;
121
    uint8_t reset : 1;
122
  } options;
123
} ina219_lld_cfg_t;
124

    
125
/**
126
 * @brief Calibration input struct.
127
 */
128
typedef struct {
129
  float shunt_resistance_0;
130
  float max_expected_current_A;
131
  uint16_t current_lsb_uA;
132
  ina219_lld_cfg_t cfg_reg;
133
} ina219_lld_calib_input_t;
134

    
135
/**
136
 * @brief Calibration output struct.
137
 */
138
typedef struct {
139
  float max_current_before_overflow_A;
140
  float max_shunt_voltage_before_overflow_V;
141
  uint16_t current_lsb_uA;
142
  uint16_t calibration;
143
} ina219_lld_calib_output_t;
144

    
145
/**
146
 * @brief The INA219Driver struct
147
 */
148
typedef struct {
149
  apalI2CDriver_t* i2cd;
150
  apalI2Caddr_t addr;   /**<The address of the INA219 for I2C communication, which is defined by the wiring of the A0 and A1 pins */
151
  uint16_t current_lsb_uA;
152
  ina219_lld_cfg_t *config;
153
} INA219Driver;
154

    
155
#ifdef __cplusplus
156
extern "C" {
157
#endif
158
  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);
159
  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);
160
  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);
161
  apalExitStatus_t ina219_lld_read_config(const INA219Driver* const ina219, ina219_lld_cfg_t* const cfg, const apalTime_t timeout);
162
  apalExitStatus_t ina219_lld_write_config(const INA219Driver* const ina219, const ina219_lld_cfg_t cfg, const apalTime_t timeout);
163
  apalExitStatus_t ina219_lld_read_calibration(const INA219Driver* const ina219, uint16_t* const calib, const apalTime_t timeout);
164
  apalExitStatus_t ina219_lld_write_calibration(const INA219Driver* const ina219, const uint16_t calib, const apalTime_t timeout);
165
  apalExitStatus_t ina219_lld_reset(const INA219Driver* const ina219, const apalTime_t timeout);
166
  apalExitStatus_t ina219_lld_read_shunt_voltage(const INA219Driver* const ina219, int32_t* const data, const apalTime_t timeout);
167
  apalExitStatus_t ina219_lld_read_bus_voltage(const INA219Driver* const ina219, uint32_t* const data, const apalTime_t timeout);
168
  apalExitStatus_t ina219_lld_read_power(const INA219Driver* const ina219, uint32_t* const data, const apalTime_t timeout);
169
  apalExitStatus_t ina219_lld_read_current(const INA219Driver* const ina219, int16_t* const data, const apalTime_t timeout);
170
  apalExitStatus_t ina219_lld_bus_conversion_ready(const INA219Driver* const ina219, uint16_t* const buscnv, const apalTime_t timeout);
171
#ifdef __cplusplus
172
}
173
#endif
174

    
175
#endif /* defined(AMIROLLD_CFG_USE_INA219) */
176

    
177
#endif /* _AMIROLLD_INA219_H_ */
178

    
179
/** @} */