openMMC
Open Source Modular MMC for AMCs
Loading...
Searching...
No Matches
ina220.h
Go to the documentation of this file.
1/*
2 * openMMC -- Open Source modular IPM Controller firmware
3 *
4 * Copyright (C) 2015-2016 Henrique Silva <henrique.silva@lnls.br>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
20 */
21
40#ifndef INA220_H_
41#define INA220_H_
42
43#include "FreeRTOS.h"
44#include "port.h"
45
46#define MAX_INA220_COUNT 12
47#define INA220_UPDATE_RATE 100
48
62#define INA220_CONFIG 0x00
63
70#define INA220_SHUNT_VOLTAGE 0x01
71
78#define INA220_BUS_VOLTAGE 0x02
79
87#define INA220_POWER 0x03
88
96#define INA220_CURRENT 0x04
97
105#define INA220_CALIBRATION 0x05
106
114#define INA220_REGISTERS 6
115
116/* Scale range values */
117#define INA220_16V_SCALE_RANGE 0x00
118#define INA220_32V_SCALE_RANGE 0x01
119
120/* PGA gain */
121#define INA220_PGA_GAIN_40MV 0x00
122#define INA220_PGA_GAIN_80MV 0x01
123#define INA220_PGA_GAIN_160MV 0x02
124#define INA220_PGA_GAIN_320MV 0x03
125
126/* ADC Resolution/Averaging */
127#define INA220_RES_SAMPLES_9BIT 0x0
128#define INA220_RES_SAMPLES_10BIT 0x1
129#define INA220_RES_SAMPLES_11BIT 0x2
130#define INA220_RES_SAMPLES_12BIT 0x3
131
132#define INA220_RES_SAMPLES_2SMP 0x9
133#define INA220_RES_SAMPLES_4SMP 0xA
134#define INA220_RES_SAMPLES_8SMP 0xB
135#define INA220_RES_SAMPLES_16SMP 0xC
136#define INA220_RES_SAMPLES_32SMP 0xD
137#define INA220_RES_SAMPLES_64SMP 0xE
138#define INA220_RES_SAMPLES_128SMP 0xF
139
140/* Operating modes */
141#define INA220_MODE_POWER_DOWN 0x0
142#define INA220_MODE_SHUNT_TRIG 0x1
143#define INA220_MODE_BUS_TRIG 0x2
144#define INA220_MODE_SHUNT_BUS_TRIG 0x3
145#define INA220_MODE_ADC_OFF 0x4
146#define INA220_MODE_SHUNT_CONT 0x5
147#define INA220_MODE_BUS_CONT 0x6
148#define INA220_MODE_SHUNT_BUS_CONT 0x7
149
150/* worst case is 68.10 ms (~14.6Hz, ina219) */
151#define INA220_CONVERSION_RATE 15
152#define INA220_MAX_DELAY 69 /* worst case delay in ms */
153
154#define INA220_RSHUNT_DEFAULT 10000
155
156typedef union {
157 struct {
158#ifdef BF_MS_FIRST
159 uint16_t mode:3;
160 uint16_t shunt_adc_resolution:4;
161 uint16_t bus_adc_resolution:4;
162 uint16_t pga_gain:2;
163 uint16_t bus_voltage_range:1;
164 uint16_t reserved:1;
165 uint16_t reset:1;
166#else
167 uint16_t reset:1;
168 uint16_t reserved:1;
170 uint16_t pga_gain:2;
173 uint16_t mode:3;
174#endif
175 } cfg_struct;
176 uint16_t cfg_word;
178
189
197
198extern TaskHandle_t vTaskINA220_Handle;
199
200uint8_t ina220_config( ina220_data_t * data );
201Bool ina220_calibrate( ina220_data_t * data );
202Bool ina220_readvalue( ina220_data_t * data, uint8_t reg, uint16_t *read );
203void ina220_readall( ina220_data_t * data );
204void ina220_init( void );
205void vTaskINA220( void* Parameters );
206
207#endif
#define INA220_REGISTERS
INA220 Register Count.
Definition ina220.h:114
Bool ina220_calibrate(ina220_data_t *data)
Definition ina220.c:162
uint8_t ina220_config(ina220_data_t *data)
Definition ina220.c:111
Bool ina220_readvalue(ina220_data_t *data, uint8_t reg, uint16_t *read)
Definition ina220.c:136
void vTaskINA220(void *Parameters)
Definition ina220.c:64
void ina220_readall(ina220_data_t *data)
Definition ina220.c:154
TaskHandle_t vTaskINA220_Handle
Definition ina220.c:45
void ina220_init(void)
Definition ina220.c:179
Port layer (includes all portable functions headers)
Definition ina220.h:179
uint16_t power_lsb
Definition ina220.h:187
uint8_t registers
Definition ina220.h:183
uint16_t calibration_reg
Definition ina220.h:182
uint16_t bus_voltage_lsb
Definition ina220.h:186
uint8_t bus_voltage_shift
Definition ina220.h:185
ina220_config_reg_t config_reg_default
Definition ina220.h:180
uint32_t calibration_factor
Definition ina220.h:181
uint8_t shunt_div
Definition ina220.h:184
Definition ina220.h:190
uint32_t rshunt
Definition ina220.h:193
const ina220_config_t * config
Definition ina220.h:192
ina220_config_reg_t curr_reg_config
Definition ina220.h:194
sensor_t * sensor
Definition ina220.h:191
Definition sdr.h:181
Definition ina220.h:156
uint16_t mode
Definition ina220.h:173
uint16_t shunt_adc_resolution
Definition ina220.h:172
uint16_t cfg_word
Definition ina220.h:176
uint16_t bus_adc_resolution
Definition ina220.h:171
uint16_t pga_gain
Definition ina220.h:170
uint16_t reset
Definition ina220.h:167
uint16_t bus_voltage_range
Definition ina220.h:169
uint16_t reserved
Definition ina220.h:168