battmon3 2.0.0.0
battmon3.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef BATTMON3_H
29#define BATTMON3_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52
73#define BATTMON3_REG_CONTROL 0x00
74#define BATTMON3_REG_ACCUMULATED_CAPACITY 0x02
75#define BATTMON3_REG_TEMPERATURE 0x06
76#define BATTMON3_REG_VOLTAGE 0x08
77#define BATTMON3_REG_BATTERY_STATUS 0x0A
78#define BATTMON3_REG_BATTERY_ALERT 0x0B
79#define BATTMON3_REG_CURRENT 0x0C
80#define BATTMON3_REG_SCALED_R 0x16
81#define BATTMON3_REG_MEASURED_Z 0x22
82#define BATTMON3_REG_INTERNAL_TEMPERATURE 0x28
83#define BATTMON3_REG_STATE_OF_HEALTH 0x2E
84#define BATTMON3_REG_DESIGN_CAPACITY 0x3C
85#define BATTMON3_REG_MAC 0x3E
86#define BATTMON3_REG_MAC_DATA 0x40
87#define BATTMON3_REG_MAC_DATA_SUM 0x60
88#define BATTMON3_REG_MAC_DATA_LEN 0x61
89#define BATTMON3_REG_MAC_DATA_CONTROL 0x62
90#define BATTMON3_REG_CAL_COUNT 0x79
91#define BATTMON3_REG_CAL_CURRENT 0x7A
92#define BATTMON3_REG_CAL_VOLTAGE 0x7C
93#define BATTMON3_REG_CAL_TEMPERATURE 0x7E
94
99#define BATTMON3_CONTROL_STATUS 0x0000
100#define BATTMON3_CONTROL_DEVICE_TYPE 0x0001
101#define BATTMON3_CONTROL_FW_VERSION 0x0002
102#define BATTMON3_CONTROL_HW_VERSION 0x0003
103#define BATTMON3_CONTROL_STATIC_CHEM_CHKSUM 0x0005
104#define BATTMON3_CONTROL_CHEM_ID 0x0006
105#define BATTMON3_CONTROL_PREV_MACWRITE 0x0007
106#define BATTMON3_CONTROL_BOARD_OFFSET 0x0009
107#define BATTMON3_CONTROL_CC_OFFSET 0x000A
108#define BATTMON3_CONTROL_CC_OFFSET_SAVE 0x000B
109#define BATTMON3_CONTROL_DF_VERSION 0x000C
110#define BATTMON3_CONTROL_GAUGE_START 0x0011
111#define BATTMON3_CONTROL_GAUGE_STOP 0x0012
112#define BATTMON3_CONTROL_SEALED 0x0020
113#define BATTMON3_CONTROL_CAL_ENABLE 0x002D
114#define BATTMON3_CONTROL_LT_ENABLE 0x002E
115#define BATTMON3_CONTROL_RESET 0x0041
116#define BATTMON3_CONTROL_EXIT_CAL 0x0080
117#define BATTMON3_CONTROL_ENTER_CAL 0x0081
118#define BATTMON3_CONTROL_NEW_BATTERY 0xA613
119
125#define BATTMON3_DF_CC_GAIN 0x4000
126#define BATTMON3_DF_CC_DELTA 0x4004
127#define BATTMON3_DF_CC_OFFSET 0x4008
128#define BATTMON3_DF_BOARD_OFFSET 0x400C
129#define BATTMON3_DF_INT_TEMP_OFFSET 0x400D
130#define BATTMON3_DF_EXT_TEMP_OFFSET 0x400E
131#define BATTMON3_DF_PACK_V_OFFSET 0x400F
132#define BATTMON3_DF_OPERATION_CONFIG_A 0x41B1
133#define BATTMON3_DF_FULL_UNSEAL_STEP_1 0x41D0
134#define BATTMON3_DF_CELL_DESIGN_CAPACITY 0x41FE
135
136 // battmon3_reg
137
152#define BATTMON3_CHEM_ID 0x0606
153
158#define BATTMON3_SEAL_CODES_DEFAULT 0x04143672ul
159
164#define BATTMON3_STATUS_GA_BIT_MASK 0x0001
165#define BATTMON3_STATUS_G_DONE_BIT_MASK 0x0040
166#define BATTMON3_STATUS_INIT_COMP_BIT_MASK 0x0080
167#define BATTMON3_STATUS_CCA_BIT_MASK 0x0400
168#define BATTMON3_STATUS_BCA_BIT_MASK 0x0800
169#define BATTMON3_STATUS_CAL_MODE_BIT_MASK 0x1000
170#define BATTMON3_STATUS_FLASHF_BIT_MASK 0x8000
171
176#define BATTMON3_OP_CONFIG_A_TEMPS_BIT_MASK 0x80
177#define BATTMON3_OP_CONFIG_A_EXTVCELL_BIT_MASK 0x40
178#define BATTMON3_OP_CONFIG_A_WRTEMP_BIT_MASK 0x20
179#define BATTMON3_OP_CONFIG_A_LF_EN_BIT_MASK 0x10
180#define BATTMON3_OP_CONFIG_A_GNDSEL_BIT_MASK 0x04
181#define BATTMON3_OP_CONFIG_A_GMSEL_BIT_MASK 0x03
182
188#define BATTMON3_DEVICE_ADDRESS 0x55
189
190 // battmon3_set
191
206#define BATTMON3_MAP_MIKROBUS( cfg, mikrobus ) \
207 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
208 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
209 cfg.en = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
210 cfg.alr = MIKROBUS( mikrobus, MIKROBUS_INT )
211
212 // battmon3_map
213 // battmon3
214
219typedef struct
220{
221 // Output pins
222 digital_out_t en;
224 // Input pins
225 digital_in_t alr;
227 // Modules
228 i2c_master_t i2c;
230 // I2C slave address
235 bool enabled;
237} battmon3_t;
238
243typedef struct
244{
245 pin_name_t scl;
246 pin_name_t sda;
248 pin_name_t en;
249 pin_name_t alr;
251 uint32_t i2c_speed;
252 uint8_t i2c_address;
255
260typedef enum
261{
263 BATTMON3_ERROR = -1
264
266
279
292
309
324
338
353err_t battmon3_generic_write ( battmon3_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
354
369err_t battmon3_generic_read ( battmon3_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
370
384err_t battmon3_write_two_bytes ( battmon3_t *ctx, uint8_t reg, uint16_t data_in );
385
399err_t battmon3_read_two_bytes ( battmon3_t *ctx, uint8_t reg, uint16_t *data_out );
400
412err_t battmon3_write_control ( battmon3_t *ctx, uint16_t control );
413
426err_t battmon3_read_control ( battmon3_t *ctx, uint16_t control, uint16_t *data_out );
427
442err_t battmon3_write_data_flash ( battmon3_t *ctx, uint16_t address, uint8_t *tx_buf, uint8_t tx_len );
443
458err_t battmon3_read_data_flash ( battmon3_t *ctx, uint16_t address, uint8_t *rx_buf, uint8_t rx_len );
459
471
483err_t battmon3_read_status ( battmon3_t *ctx, uint16_t *status );
484
497err_t battmon3_wait_for_status ( battmon3_t *ctx, uint16_t mask, uint16_t status );
498
508
518
528
542
557
570
584err_t battmon3_set_gauge_mode ( battmon3_t *ctx, uint8_t gauge_mode );
585
597
609
623err_t battmon3_set_security_mode ( battmon3_t *ctx, uint8_t security_mode );
624
636err_t battmon3_new_battery ( battmon3_t *ctx, uint16_t des_capacity );
637
649err_t battmon3_set_design_capacity ( battmon3_t *ctx, uint16_t des_capacity );
650
662err_t battmon3_read_design_capacity ( battmon3_t *ctx, uint16_t *des_capacity );
663
675err_t battmon3_read_used_capacity ( battmon3_t *ctx, float *used_capacity );
676
689err_t battmon3_read_remaining_capacity ( battmon3_t *ctx, float *rem_capacity );
690
703err_t battmon3_read_remaining_percentage ( battmon3_t *ctx, float *rem_percentage );
704
716
728
740err_t battmon3_read_temperature ( battmon3_t *ctx, float *temperature );
741
753err_t battmon3_read_internal_temperature ( battmon3_t *ctx, float *temperature );
754
766err_t battmon3_read_voltage ( battmon3_t *ctx, uint16_t *voltage );
767
779err_t battmon3_read_current ( battmon3_t *ctx, int16_t *current );
780
793err_t battmon3_read_scaled_r ( battmon3_t *ctx, uint16_t *scaled_r );
794
807err_t battmon3_read_measured_z ( battmon3_t *ctx, uint16_t *measured_z );
808
820err_t battmon3_read_state_of_health ( battmon3_t *ctx, uint8_t *state_of_health );
821
833err_t battmon3_read_battery_status ( battmon3_t *ctx, uint8_t *batt_status );
834
846err_t battmon3_read_battery_alert ( battmon3_t *ctx, uint8_t *batt_alert );
847
859
871
884err_t battmon3_read_raw_calibration_data ( battmon3_t *ctx, uint8_t reg, uint16_t *cal_data );
885
897
909
921err_t battmon3_calibrate_voltage ( battmon3_t *ctx, uint16_t voltage );
922
934err_t battmon3_calibrate_current ( battmon3_t *ctx, int16_t current );
935
949err_t battmon3_calibrate_temperature ( battmon3_t *ctx, float temperature );
950
951#ifdef __cplusplus
952}
953#endif
954#endif // BATTMON3_H
955
956 // battmon3
957
958// ------------------------------------------------------------------------ END
battmon3_security_mode_t
BATT-MON 3 the security mode of the BQ35100 chip.
Definition battmon3.h:272
@ BATTMON3_SECURITY_MODE_SEALED
Normal operating mode, prevents accidental writes.
Definition battmon3.h:276
@ BATTMON3_SECURITY_MODE_UNKNOWN
Definition battmon3.h:273
@ BATTMON3_SECURITY_MODE_FULL_ACCESS
Allows writes to all of memory.
Definition battmon3.h:274
@ BATTMON3_SECURITY_MODE_UNSEALED
Allows writes to all of memory apart from the security codes area.
Definition battmon3.h:275
battmon3_gauge_mode_t
BATT-MON 3 the gauge mode of the BQ35100 chip.
Definition battmon3.h:285
@ BATTMON3_GAUGE_MODE_SOH
Definition battmon3.h:287
@ BATTMON3_GAUGE_MODE_ACCUMULATOR
Definition battmon3.h:286
@ BATTMON3_GAUGE_MODE_EOS
Definition battmon3.h:288
@ BATTMON3_GAUGE_MODE_UNKNOWN
Definition battmon3.h:289
battmon3_return_value_t
BATT-MON 3 Click return value data.
Definition battmon3.h:261
@ BATTMON3_ERROR
Definition battmon3.h:263
@ BATTMON3_OK
Definition battmon3.h:262
err_t battmon3_read_remaining_capacity(battmon3_t *ctx, float *rem_capacity)
BATT-MON 3 read remaining capacity function.
err_t battmon3_read_used_capacity(battmon3_t *ctx, float *used_capacity)
BATT-MON 3 read used capacity function.
err_t battmon3_set_security_mode(battmon3_t *ctx, uint8_t security_mode)
BATT-MON 3 set security mode function.
err_t battmon3_calibrate_voltage(battmon3_t *ctx, uint16_t voltage)
BATT-MON 3 calibrate voltage function.
err_t battmon3_perform_board_offset(battmon3_t *ctx)
BATT-MON 3 perform board offset function.
err_t battmon3_read_status(battmon3_t *ctx, uint16_t *status)
BATT-MON 3 read status function.
err_t battmon3_generic_read(battmon3_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
BATT-MON 3 I2C reading function.
err_t battmon3_wait_for_status(battmon3_t *ctx, uint16_t mask, uint16_t status)
BATT-MON 3 wait for status function.
err_t battmon3_read_battery_status(battmon3_t *ctx, uint8_t *batt_status)
BATT-MON 3 read battery status function.
err_t battmon3_write_data_flash(battmon3_t *ctx, uint16_t address, uint8_t *tx_buf, uint8_t tx_len)
BATT-MON 3 write data flash function.
err_t battmon3_read_remaining_percentage(battmon3_t *ctx, float *rem_percentage)
BATT-MON 3 read remaining percentage function.
err_t battmon3_exit_calibration_mode(battmon3_t *ctx)
BATT-MON 3 exit calibration mode function.
err_t battmon3_read_state_of_health(battmon3_t *ctx, uint8_t *state_of_health)
BATT-MON 3 read state of health function.
err_t battmon3_use_internal_temperature(battmon3_t *ctx)
BATT-MON 3 use internal temperature function.
err_t battmon3_read_internal_temperature(battmon3_t *ctx, float *temperature)
BATT-MON 3 read internal temperature function.
err_t battmon3_read_two_bytes(battmon3_t *ctx, uint8_t reg, uint16_t *data_out)
BATT-MON 3 read two bytes function.
err_t battmon3_enter_calibration_mode(battmon3_t *ctx)
BATT-MON 3 enter calibration mode function.
err_t battmon3_set_gauge_mode(battmon3_t *ctx, uint8_t gauge_mode)
BATT-MON 3 set gauge mode function.
err_t battmon3_write_control(battmon3_t *ctx, uint16_t control)
BATT-MON 3 write control function.
err_t battmon3_read_scaled_r(battmon3_t *ctx, uint16_t *scaled_r)
BATT-MON 3 read scaled R function.
err_t battmon3_read_battery_alert(battmon3_t *ctx, uint8_t *batt_alert)
BATT-MON 3 read battery alert function.
err_t battmon3_read_voltage(battmon3_t *ctx, uint16_t *voltage)
BATT-MON 3 read voltage function.
err_t battmon3_stop_gauge(battmon3_t *ctx)
BATT-MON 3 stop gauge function.
err_t battmon3_reset(battmon3_t *ctx)
BATT-MON 3 reset function.
err_t battmon3_read_design_capacity(battmon3_t *ctx, uint16_t *des_capacity)
BATT-MON 3 read design capacity function.
err_t battmon3_start_gauge(battmon3_t *ctx)
BATT-MON 3 start gauge function.
err_t battmon3_read_data_flash(battmon3_t *ctx, uint16_t address, uint8_t *rx_buf, uint8_t rx_len)
BATT-MON 3 read data flash function.
err_t battmon3_check_communication(battmon3_t *ctx)
BATT-MON 3 check communication function.
err_t battmon3_use_external_temperature(battmon3_t *ctx)
BATT-MON 3 use external temperature function.
err_t battmon3_new_battery(battmon3_t *ctx, uint16_t des_capacity)
BATT-MON 3 new battery function.
void battmon3_set_en_pin_high(battmon3_t *ctx)
BATT-MON 3 set en pin high function.
void battmon3_cfg_setup(battmon3_cfg_t *cfg)
BATT-MON 3 configuration object setup function.
err_t battmon3_read_security_mode(battmon3_t *ctx)
BATT-MON 3 read security mode function.
err_t battmon3_perform_cc_offset(battmon3_t *ctx)
BATT-MON 3 perform CC offset function.
err_t battmon3_read_current(battmon3_t *ctx, int16_t *current)
BATT-MON 3 read current function.
err_t battmon3_calibrate_current(battmon3_t *ctx, int16_t current)
BATT-MON 3 calibrate current function.
err_t battmon3_read_measured_z(battmon3_t *ctx, uint16_t *measured_z)
BATT-MON 3 read measured Z function.
err_t battmon3_generic_write(battmon3_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
BATT-MON 3 I2C writing function.
void battmon3_set_en_pin_low(battmon3_t *ctx)
BATT-MON 3 set en pin low function.
err_t battmon3_read_control(battmon3_t *ctx, uint16_t control, uint16_t *data_out)
BATT-MON 3 read control function.
err_t battmon3_read_raw_calibration_data(battmon3_t *ctx, uint8_t reg, uint16_t *cal_data)
BATT-MON 3 read raw calibration data function.
err_t battmon3_set_design_capacity(battmon3_t *ctx, uint16_t des_capacity)
BATT-MON 3 set design capacity function.
err_t battmon3_read_temperature(battmon3_t *ctx, float *temperature)
BATT-MON 3 read temperature function.
err_t battmon3_disable_gauge(battmon3_t *ctx)
BATT-MON 3 disable gauge function.
err_t battmon3_init(battmon3_t *ctx, battmon3_cfg_t *cfg)
BATT-MON 3 initialization function.
err_t battmon3_default_cfg(battmon3_t *ctx)
BATT-MON 3 default configuration function.
err_t battmon3_write_two_bytes(battmon3_t *ctx, uint8_t reg, uint16_t data_in)
BATT-MON 3 write two bytes function.
err_t battmon3_calibrate_temperature(battmon3_t *ctx, float temperature)
BATT-MON 3 calibrate temperature function.
uint8_t battmon3_get_alert_pin(battmon3_t *ctx)
BATT-MON 3 get alert pin function.
BATT-MON 3 Click configuration object.
Definition battmon3.h:244
pin_name_t alr
Definition battmon3.h:249
uint32_t i2c_speed
Definition battmon3.h:251
pin_name_t scl
Definition battmon3.h:245
pin_name_t en
Definition battmon3.h:248
pin_name_t sda
Definition battmon3.h:246
uint8_t i2c_address
Definition battmon3.h:252
BATT-MON 3 Click context object.
Definition battmon3.h:220
digital_in_t alr
Definition battmon3.h:225
uint8_t security_mode
Definition battmon3.h:233
bool enabled
Definition battmon3.h:235
i2c_master_t i2c
Definition battmon3.h:228
digital_out_t en
Definition battmon3.h:222
uint8_t slave_address
Definition battmon3.h:231