ltc4150.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Otto-von-Guericke-Universität Magdeburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
62 #ifndef LTC4150_H
63 #define LTC4150_H
64 
65 #include <stdint.h>
66 
67 #include "mutex.h"
68 #include "periph/gpio.h"
69 #include "xtimer.h"
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
78 enum {
91 };
92 
96 typedef enum {
100 
104 typedef struct ltc4150_dev ltc4150_dev_t;
105 
121 typedef struct {
131  void (*pulse)(ltc4150_dev_t *dev, ltc4150_dir_t dir, uint64_t now_usec, void *arg);
142  void (*reset)(ltc4150_dev_t *dev, uint64_t now_usec, void *arg);
144 
148 typedef struct {
152  gpio_t interrupt;
159  gpio_t polarity;
165  gpio_t shutdown;
175  uint16_t pulses_per_ah;
183  uint16_t flags;
198 
202 struct ltc4150_dev {
204  uint32_t start_sec;
205  uint32_t last_update_sec;
206  uint32_t charged;
207  uint32_t discharged;
208 };
209 
213 typedef struct {
214  uint32_t last_rotate_sec;
218  uint16_t charged;
222  uint16_t discharged;
226  uint8_t buf_charged[7];
230  uint8_t buf_discharged[7];
231  uint8_t ring_pos;
233 
238 
249 int ltc4150_init(ltc4150_dev_t *dev, const ltc4150_params_t *params);
250 
259 
271 
286 int ltc4150_charge(ltc4150_dev_t *dev, uint32_t *charged, uint32_t *discharged);
287 
305 int ltc4150_avg_current(ltc4150_dev_t *dev, int16_t *dest);
306 
325  uint32_t *charged, uint32_t *discharged);
326 
339 void ltc4150_pulses2c(const ltc4150_dev_t *dev,
340  uint32_t *charged, uint32_t *discharged,
341  uint32_t raw_charged,
342  uint32_t raw_discharged);
343 #ifdef __cplusplus
344 }
345 #endif
346 
347 #endif /* LTC4150_H */
348 
ltc4150_dir_t
ltc4150_dir_t
Enumeration of directions in which the charge can be transferred.
Definition: ltc4150.h:96
ltc4150_reset_counters
int ltc4150_reset_counters(ltc4150_dev_t *dev)
Clear current counters of the given LTC4150 device.
ltc4150_dev
LTC4150 coulomb counter.
Definition: ltc4150.h:202
ltc4150_last_minute_data_t::charged
uint16_t charged
Pulses in charging direction recorded in the last minute.
Definition: ltc4150.h:218
ltc4150_params_t::pulses_per_ah
uint16_t pulses_per_ah
Pulse per ampere hour of charge.
Definition: ltc4150.h:175
ltc4150_last_minute_charge
int ltc4150_last_minute_charge(ltc4150_dev_t *dev, ltc4150_last_minute_data_t *data, uint32_t *charged, uint32_t *discharged)
Get the measured charge in the last minute.
LTC4150_CHARGE
@ LTC4150_CHARGE
The battery is charged.
Definition: ltc4150.h:97
ltc4150_params_t
Parameters required to set up the LTC4150 coulomb counter.
Definition: ltc4150.h:148
ltc4150_shutdown
int ltc4150_shutdown(ltc4150_dev_t *dev)
Disable the interrupt handler and turn the chip off.
ltc4150_params_t::recorder_data
void ** recorder_data
NULL or an array of the user defined data for each recorder
Definition: ltc4150.h:196
ltc4150_avg_current
int ltc4150_avg_current(ltc4150_dev_t *dev, int16_t *dest)
Get the average current drawn in E-01 milliampere.
LTC4150_DISCHARGE
@ LTC4150_DISCHARGE
Charge is drawn from the battery.
Definition: ltc4150.h:98
ltc4150_last_minute_data_t
Data structure used by ltc4150_last_minute.
Definition: ltc4150.h:213
ltc4150_last_minute
const ltc4150_recorder_t ltc4150_last_minute
Records the charge transferred within the last minute using.
ltc4150_params_t::interrupt
gpio_t interrupt
Pin going LOW every time a specific charge is drawn, labeled INT.
Definition: ltc4150.h:152
LTC4150_EXT_PULL_UP
@ LTC4150_EXT_PULL_UP
External pull on the /INT and the /POL pin is present.
Definition: ltc4150.h:90
ltc4150_params_t::shutdown
gpio_t shutdown
Pin to power off the LTC4150 coulomb counter, labeled SHDN.
Definition: ltc4150.h:165
ltc4150_last_minute_data_t::last_rotate_sec
uint32_t last_rotate_sec
Time stamp of the last ring "rotation".
Definition: ltc4150.h:214
ltc4150_dev::charged
uint32_t charged
Definition: ltc4150.h:206
ltc4150_charge
int ltc4150_charge(ltc4150_dev_t *dev, uint32_t *charged, uint32_t *discharged)
Get the measured charge since boot or last reset in millicoulomb.
ltc4150_last_minute_data_t::discharged
uint16_t discharged
Pulses in discharging direction recorded in the last minute.
Definition: ltc4150.h:222
ltc4150_params_t::polarity
gpio_t polarity
Pin indicating (dis-)charging, labeled POL.
Definition: ltc4150.h:159
ltc4150_dev::last_update_sec
uint32_t last_update_sec
Time stamp of last pulse.
Definition: ltc4150.h:205
ltc4150_params_t::flags
uint16_t flags
Configuration flags controlling if inter pull ups are required.
Definition: ltc4150.h:183
mutex.h
Mutex for thread synchronization.
gpio.h
Low-level GPIO peripheral driver interface definitions.
LTC4150_POL_EXT_PULL_UP
@ LTC4150_POL_EXT_PULL_UP
External pull on the /POL pin is present.
Definition: ltc4150.h:86
ltc4150_dev::params
ltc4150_params_t params
Parameter of the LTC4150 coulomb counter.
Definition: ltc4150.h:203
ltc4150_init
int ltc4150_init(ltc4150_dev_t *dev, const ltc4150_params_t *params)
Initialize the LTC4150 driver.
ltc4150_params_t::recorders
const ltc4150_recorder_t ** recorders
NULL or a NULL-terminated array of data recorders
Definition: ltc4150.h:188
ltc4150_dev::discharged
uint32_t discharged
Definition: ltc4150.h:207
LTC4150_INT_EXT_PULL_UP
@ LTC4150_INT_EXT_PULL_UP
External pull on the /INT pin is present.
Definition: ltc4150.h:82
ltc4150_pulses2c
void ltc4150_pulses2c(const ltc4150_dev_t *dev, uint32_t *charged, uint32_t *discharged, uint32_t raw_charged, uint32_t raw_discharged)
Convert the raw data (# pulses) acquired by the LTC4150 device to charge information in millicoulomb.
xtimer.h
xtimer interface definitions
ltc4150_last_minute_data_t::ring_pos
uint8_t ring_pos
Position in the ring buffer.
Definition: ltc4150.h:231
ltc4150_dev::start_sec
uint32_t start_sec
Time stamp when started counting.
Definition: ltc4150.h:204
ltc4150_recorder_t
Interface to allow recording of the drawn current in a user defined resolution.
Definition: ltc4150.h:121