no-OS
ltc3337.h
Go to the documentation of this file.
1 /***************************************************************************/
33 #ifndef __LTC3337_H__
34 #define __LTC3337_H__
35 
36 #include <stdint.h>
37 #include "no_os_util.h"
38 #include "no_os_i2c.h"
39 
40 #define LTC3337_I2C_ADDR 0x64 // b1100100[r/w] 0xC8, 0xC9
41 
42 #define LTC3337_MAX_TEMP_C 159 //Maximum temperature for alarms
43 #define LTC3337_MIN_TEMP_C -41 //Minimum temperature for alarms
44 #define LTC3337_MAX_PRESCALE 15 //Maximum prescaler value
45 
46 #define LTC3337_OVERFLOW_ALARM_MAX 0xFF //Max value for overflow alarm
47 
48 #define LTC3337_REG_A 0x01 //Prescaler Select, IRQ Clear, Shutdown, Alarm Thresh
49 
50 #define LTC3337_RA_PRESCALE_MSK NO_OS_GENMASK(3, 0)
51 #define LTC3337_RA_CLEAR_INT NO_OS_BIT(4)
52 #define LTC3337_RA_CNT_CHK NO_OS_BIT(5)
53 #define LTC3337_RA_SHTDN NO_OS_BIT(6)
54 #define LTC3337_RA_ADC_CONV NO_OS_BIT(7)
55 #define LTC3337_RA_ALARM_LVL_MSK NO_OS_GENMASK(15, 8)
56 
57 #define LTC3337_REG_B 0x02 //Accumulated Charge
58 
59 #define LTC3337_REG_C 0x03 //Status, Temperature
60 //Use the following definitions for derterming interrupt status
61 #define LTC3337_RC_OVERFLOW NO_OS_BIT(0)
62 #define LTC3337_RC_ALARM_TRIP NO_OS_BIT(1)
63 #define LTC3337_RC_ALARM_MIN NO_OS_BIT(2)
64 #define LTC3337_RC_ALARM_MAX NO_OS_BIT(3)
65 #define LTC3337_RC_ADC_READY NO_OS_BIT(4)
66 #define LTC3337_RC_IPK_PIN_MSK NO_OS_GENMASK(7, 5)
67 #define LTC3337_RC_DIE_TEMP_MSK NO_OS_GENMASK(15, 8)
68 
69 #define LTC3337_REG_D 0x04 //BAT_IN V, Ipeak On
70 #define LTC3337_REG_E 0x05 //BAT_IN V, Ipeak Off
71 #define LTC3337_REG_F 0x06 //BAT_OUT V, Ipeak On
72 #define LTC3337_REG_G 0x07 //BAT_OUT V, Ipeak Off
73 #define LTC3337_BATV_MSK NO_OS_GENMASK(11, 0)
74 
75 #define LTC3337_REG_H 0x08 //Temp Alarm Config
76 #define LTC3337_RH_HOT_ALARM_MSK NO_OS_GENMASK(15, 8)
77 #define LTC3337_RH_COLD_ALARM_MSK NO_OS_GENMASK(7, 0)
78 
79 #define LTC3337_VLSB_UV 1465 //1.465 mV = 1465 uV
80 #define LTC3337_TLSB_MC 784 //0.784 Deg C = 784 mC
81 #define LTC3337_TEMP_MIN_C -41
82 
83 #define LTC3337_NUM_IPEAKS 8 //3-bit iPeak Input
84 
85 //Scale values used in integer match to calulate A/nA-hrs from counters
86 #define LTC3337_NANO_AMP 1000000000
87 #define LTC3337_CALC_SCALE 10000
88 #define LTC3337_CALC_TO_WHOLE (LTC3337_NANO_AMP / LTC3337_CALC_SCALE)
89 
90 //Scale value for converting uV to mV
91 #define LTC3337_UV_TO_MV_SCALE 1000
92 
93 //Scale Value for converting mC to Deg C
94 #define LTC3337_MC_TO_C_SCALE 1000
95 
101 struct ltc3337_dev {
102  struct no_os_i2c_desc *i2c_desc; //I2C device
103  uint8_t ipeak_latched; //iPeak value read at init
104  uint16_t latched_reg_a; //Latched Register A value
105 };
106 
111  struct no_os_i2c_init_param i2c_init; //I2C Configuration
112  uint8_t prescale; //Initial device prescaler value
113 };
114 
119  uint32_t a_hr; //A-hrs
120  uint32_t na_hr; //nA-hrs
121 };
122 
131 };
132 
133 /* Initializes the device instance */
134 int ltc3337_init(struct ltc3337_dev** dev,
136 
137 /* Removes the device instance */
138 int ltc3337_remove(struct ltc3337_dev* dev);
139 
140 /* Sets the device prescaler value */
141 int ltc3337_set_prescaler(struct ltc3337_dev* dev, uint8_t prescale);
142 
143 /* Sets the temperature alarms, in Deg C */
144 int ltc3337_set_temperature_alarms_c(struct ltc3337_dev* dev, int16_t hot_alarm,
145  int16_t cold_alarm);
146 
147 /* Enabled / Disables the Coulomb counter shutdown */
148 int ltc3337_set_counter_shutdown(struct ltc3337_dev* dev, uint8_t shutdown_en);
149 
150 /* Sets the couloumb counter alarm threshold */
151 int ltc3337_set_counter_alarm(struct ltc3337_dev* dev, uint16_t reg_value,
152  uint8_t round_up);
153 
154 /* Manually sets the accumulated charge register */
155 int ltc3337_set_accumulated_charge(struct ltc3337_dev* dev, uint16_t reg_value,
156  uint8_t round_up);
157 
158 /* Gets the current value of the accumulated charge register */
160  struct charge_count_t* value,
161  uint16_t* raw_value);
162 
163 /* Reads the specified voltage source, in millivolts */
164 int ltc3337_get_voltage_mv(struct ltc3337_dev* dev,
165  enum ltc3337_voltage_src_t source,
166  uint32_t* value);
167 
168 /* Gets the current die temperature, in Deg C */
169 int ltc3337_get_temperature_c(struct ltc3337_dev* dev, int16_t* value);
170 
171 /* Gets the device interrupt status, and clears interrupts */
173  uint16_t* int_field,
174  int16_t* temp_c);
175 
176 /* Calculates a charge register value based on A-Hr units */
178  struct charge_count_t* charge_a,
179  uint16_t* reg_value);
180 
181 #endif
ltc3337_get_and_clear_interrupts
int ltc3337_get_and_clear_interrupts(struct ltc3337_dev *dev, uint16_t *int_field, int16_t *temp_c)
Definition: ltc3337.c:441
no_os_alloc.h
ltc3337_set_prescaler
int ltc3337_set_prescaler(struct ltc3337_dev *dev, uint8_t prescale)
Definition: ltc3337.c:271
LTC3337_RA_ALARM_LVL_MSK
#define LTC3337_RA_ALARM_LVL_MSK
Definition: ltc3337.h:55
no_os_i2c_write
int32_t no_os_i2c_write(struct no_os_i2c_desc *desc, uint8_t *data, uint8_t bytes_number, uint8_t stop_bit)
I2C Write data to slave device.
Definition: no_os_i2c.c:159
no_os_i2c_init
int32_t no_os_i2c_init(struct no_os_i2c_desc **desc, const struct no_os_i2c_init_param *param)
Initialize the I2C communication peripheral.
Definition: no_os_i2c.c:52
ltc3337_voltage_src_t
ltc3337_voltage_src_t
Definition: ltc3337.h:126
ltc3337_init
int ltc3337_init(struct ltc3337_dev **device, struct ltc3337_init_param *init_param)
Definition: ltc3337.c:86
ipeak_entry_t::qlsb_na_hr
uint32_t qlsb_na_hr
Definition: ltc3337.c:45
no_os_i2c_remove
int32_t no_os_i2c_remove(struct no_os_i2c_desc *desc)
Free the resources allocated by no_os_i2c_init().
Definition: no_os_i2c.c:113
LTC3337_NANO_AMP
#define LTC3337_NANO_AMP
Definition: ltc3337.h:86
LTC3337_BATV_MSK
#define LTC3337_BATV_MSK
Definition: ltc3337.h:73
ltc3337_get_temperature_c
int ltc3337_get_temperature_c(struct ltc3337_dev *dev, int16_t *value)
Definition: ltc3337.c:199
ltc3337_set_temperature_alarms_c
int ltc3337_set_temperature_alarms_c(struct ltc3337_dev *dev, int16_t hot_alarm, int16_t cold_alarm)
Definition: ltc3337.c:299
LTC3337_REG_B
#define LTC3337_REG_B
Definition: ltc3337.h:57
ltc3337_dev::ipeak_latched
uint8_t ipeak_latched
Definition: ltc3337.h:103
ltc3337_set_counter_alarm
int ltc3337_set_counter_alarm(struct ltc3337_dev *dev, uint16_t reg_value, uint8_t round_up)
Definition: ltc3337.c:363
LTC3337_NUM_IPEAKS
#define LTC3337_NUM_IPEAKS
Definition: ltc3337.h:83
BAT_OUT_IPEAK_OFF
@ BAT_OUT_IPEAK_OFF
Definition: ltc3337.h:130
LTC3337_RH_COLD_ALARM_MSK
#define LTC3337_RH_COLD_ALARM_MSK
Definition: ltc3337.h:77
LTC3337_REG_H
#define LTC3337_REG_H
Definition: ltc3337.h:75
LTC3337_REG_E
#define LTC3337_REG_E
Definition: ltc3337.h:70
LTC3337_REG_G
#define LTC3337_REG_G
Definition: ltc3337.h:72
device
Definition: ad9361_util.h:69
no_os_calloc
void * no_os_calloc(size_t nitems, size_t size)
Allocate memory and return a pointer to it, set memory to 0.
Definition: chibios_alloc.c:54
LTC3337_RC_IPK_PIN_MSK
#define LTC3337_RC_IPK_PIN_MSK
Definition: ltc3337.h:66
ltc3337_init
int ltc3337_init(struct ltc3337_dev **dev, struct ltc3337_init_param *init_param)
Definition: ltc3337.c:86
LTC3337_OVERFLOW_ALARM_MAX
#define LTC3337_OVERFLOW_ALARM_MAX
Definition: ltc3337.h:46
ltc3337_set_counter_shutdown
int ltc3337_set_counter_shutdown(struct ltc3337_dev *dev, uint8_t shutdown_en)
Definition: ltc3337.c:327
LTC3337_REG_C
#define LTC3337_REG_C
Definition: ltc3337.h:59
LTC3337_VLSB_UV
#define LTC3337_VLSB_UV
Definition: ltc3337.h:79
no_os_field_prep
uint32_t no_os_field_prep(uint32_t mask, uint32_t val)
ltc3337_init_param
Definition: ltc3337.h:110
LTC3337_UV_TO_MV_SCALE
#define LTC3337_UV_TO_MV_SCALE
Definition: ltc3337.h:91
LTC3337_MC_TO_C_SCALE
#define LTC3337_MC_TO_C_SCALE
Definition: ltc3337.h:94
ltc3337_dev
Definition: ltc3337.h:101
ltc3337_set_accumulated_charge
int ltc3337_set_accumulated_charge(struct ltc3337_dev *dev, uint16_t reg_value, uint8_t round_up)
Definition: ltc3337.c:407
ltc3337_get_accumulated_charge
int ltc3337_get_accumulated_charge(struct ltc3337_dev *dev, struct charge_count_t *value, uint16_t *raw_value)
Definition: ltc3337.c:227
ltc3337_set_counter_alarm
int ltc3337_set_counter_alarm(struct ltc3337_dev *dev, uint16_t counter_reg_val, uint8_t round_up)
Definition: ltc3337.c:363
LTC3337_REG_A
#define LTC3337_REG_A
Definition: ltc3337.h:48
LTC3337_TLSB_MC
#define LTC3337_TLSB_MC
Definition: ltc3337.h:80
ltc3337_dev::i2c_desc
struct no_os_i2c_desc * i2c_desc
Definition: ltc3337.h:102
LTC3337_MAX_TEMP_C
#define LTC3337_MAX_TEMP_C
Definition: ltc3337.h:42
ltc3337_set_counter_shutdown
int ltc3337_set_counter_shutdown(struct ltc3337_dev *dev, uint8_t shutdown_en)
Definition: ltc3337.c:327
LTC3337_RC_DIE_TEMP_MSK
#define LTC3337_RC_DIE_TEMP_MSK
Definition: ltc3337.h:67
charge_count_t
Definition: ltc3337.h:118
ltc3337_set_temperature_alarms_c
int ltc3337_set_temperature_alarms_c(struct ltc3337_dev *dev, int16_t hot_alarm, int16_t cold_alarm)
Definition: ltc3337.c:299
BAT_IN_IPEAK_OFF
@ BAT_IN_IPEAK_OFF
Definition: ltc3337.h:128
no_os_i2c_desc
Structure holding I2C address descriptor.
Definition: no_os_i2c.h:101
ltc3337_get_voltage_mv
int ltc3337_get_voltage_mv(struct ltc3337_dev *dev, enum ltc3337_voltage_src_t source, uint32_t *value)
Definition: ltc3337.c:153
no_os_i2c_read
int32_t no_os_i2c_read(struct no_os_i2c_desc *desc, uint8_t *data, uint8_t bytes_number, uint8_t stop_bit)
I2C Read data from slave device.
Definition: no_os_i2c.c:190
ltc3337_set_accumulated_charge
int ltc3337_set_accumulated_charge(struct ltc3337_dev *dev, uint16_t reg_value, uint8_t round_up)
Definition: ltc3337.c:407
ltc3337_get_accumulated_charge
int ltc3337_get_accumulated_charge(struct ltc3337_dev *dev, struct charge_count_t *value, uint16_t *raw_value)
Definition: ltc3337.c:227
ltc3337_remove
int ltc3337_remove(struct ltc3337_dev *dev)
Definition: ltc3337.c:132
no_os_i2c.h
Header file of I2C Interface.
LTC3337_MIN_TEMP_C
#define LTC3337_MIN_TEMP_C
Definition: ltc3337.h:43
no_os_field_get
uint32_t no_os_field_get(uint32_t mask, uint32_t word)
LTC3337_RH_HOT_ALARM_MSK
#define LTC3337_RH_HOT_ALARM_MSK
Definition: ltc3337.h:76
no_os_free
void no_os_free(void *ptr)
Deallocate memory previously allocated by a call to no_os_calloc or no_os_malloc.
Definition: chibios_alloc.c:69
no_os_get_unaligned_le16
uint16_t no_os_get_unaligned_le16(uint8_t *buf)
ltc3337_calculate_charge_register
int ltc3337_calculate_charge_register(struct ltc3337_dev *dev, struct charge_count_t *charge_a, uint16_t *reg_value)
Definition: ltc3337.c:479
BAT_OUT_IPEAK_ON
@ BAT_OUT_IPEAK_ON
Definition: ltc3337.h:129
charge_count_t::na_hr
uint32_t na_hr
Definition: ltc3337.h:120
no_os_i2c_init_param
Structure holding the parameters for I2C initialization.
Definition: no_os_i2c.h:64
ltc3337_get_temperature_c
int ltc3337_get_temperature_c(struct ltc3337_dev *dev, int16_t *value)
Definition: ltc3337.c:199
init_param
struct ad7616_init_param init_param
Definition: ad7616_sdz.c:113
charge_count_t::a_hr
uint32_t a_hr
Definition: ltc3337.h:119
ltc3337_get_and_clear_interrupts
int ltc3337_get_and_clear_interrupts(struct ltc3337_dev *dev, uint16_t *int_field, int16_t *temp_c)
Definition: ltc3337.c:441
LTC3337_CALC_SCALE
#define LTC3337_CALC_SCALE
Definition: ltc3337.h:87
ltc3337_set_prescaler
int ltc3337_set_prescaler(struct ltc3337_dev *dev, uint8_t prescale)
Definition: ltc3337.c:271
LTC3337_RA_SHTDN
#define LTC3337_RA_SHTDN
Definition: ltc3337.h:53
ltc3337_init_param::i2c_init
struct no_os_i2c_init_param i2c_init
Definition: ltc3337.h:111
ltc3337.h
Header file for the LTC3337 Driver.
ipeak_entry_t
Definition: ltc3337.c:43
ipeak_entry_t::ipeak_ma
uint8_t ipeak_ma
Definition: ltc3337.c:44
ltc3337_get_voltage_mv
int ltc3337_get_voltage_mv(struct ltc3337_dev *dev, enum ltc3337_voltage_src_t source, uint32_t *value)
Definition: ltc3337.c:153
ltc3337_init_param::prescale
uint8_t prescale
Definition: ltc3337.h:112
ltc3337_remove
int ltc3337_remove(struct ltc3337_dev *dev)
Definition: ltc3337.c:132
ltc3337_dev::latched_reg_a
uint16_t latched_reg_a
Definition: ltc3337.h:104
no_os_util.h
Header file of utility functions.
LTC3337_RA_PRESCALE_MSK
#define LTC3337_RA_PRESCALE_MSK
Definition: ltc3337.h:50
LTC3337_REG_F
#define LTC3337_REG_F
Definition: ltc3337.h:71
LTC3337_REG_D
#define LTC3337_REG_D
Definition: ltc3337.h:69
ltc3337_calculate_charge_register
int ltc3337_calculate_charge_register(struct ltc3337_dev *dev, struct charge_count_t *charge_a, uint16_t *reg_value)
Definition: ltc3337.c:479
BAT_IN_IPEAK_ON
@ BAT_IN_IPEAK_ON
Definition: ltc3337.h:127
LTC3337_TEMP_MIN_C
#define LTC3337_TEMP_MIN_C
Definition: ltc3337.h:81
errno.h
Error macro definition for ARM Compiler.
NO_OS_DIV_ROUND_CLOSEST
#define NO_OS_DIV_ROUND_CLOSEST(x, y)
Definition: no_os_util.h:54
LTC3337_CALC_TO_WHOLE
#define LTC3337_CALC_TO_WHOLE
Definition: ltc3337.h:88
LTC3337_RA_CLEAR_INT
#define LTC3337_RA_CLEAR_INT
Definition: ltc3337.h:51