Skip to content

Commit 7eb699f

Browse files
authored
Merge pull request #16 from tmobile/tmo-Fix-charger-related-issues
Fix charger-related issues
2 parents d2d3b10 + 62608a6 commit 7eb699f

File tree

10 files changed

+120
-138
lines changed

10 files changed

+120
-138
lines changed

samples/tmo_shell/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ target_sources(app PRIVATE src/led_test.c)
2626
target_sources(app PRIVATE src/misc_test.c)
2727
target_sources(app PRIVATE src/tmo_file.c)
2828
target_sources(app PRIVATE src/tmo_adc.c)
29-
target_sources(app PRIVATE src/tmo_batt_charger.c)
29+
target_sources(app PRIVATE src/tmo_bq24250.c)
3030
target_sources(app PRIVATE src/tmo_battery_ctrl.c)
3131
target_sources(app PRIVATE src/tmo_sntp.c)
3232
target_sources_ifdef(CONFIG_WIFI app PRIVATE src/tmo_wifi.c)

samples/tmo_shell/src/tmo_adc.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,30 @@ void initADC (void)
6969
}
7070

7171
/***************************************************************************
72-
* @brief This function writes the amount of battery charge remaing (to the
73-
* nearest 1%) in bv.
72+
* @brief Exponential filter for battery level
73+
***************************************************************************/
74+
static void apply_filter(float *bv)
75+
{
76+
static float s_filtered_capacity = -1;
77+
static bool s_battery_is_charging = false;
78+
bool battery_is_charging;
79+
80+
// If there has been a switch between charger and battery, reset the filter
81+
battery_is_charging = is_battery_charging();
82+
if (s_battery_is_charging != battery_is_charging) {
83+
s_battery_is_charging = battery_is_charging;
84+
s_filtered_capacity = -1;
85+
}
86+
87+
if (s_filtered_capacity < 0) {
88+
s_filtered_capacity = *bv;
89+
}
90+
*bv = s_filtered_capacity = s_filtered_capacity * 0.95 + (*bv) * 0.05;
91+
}
92+
93+
/***************************************************************************
94+
* @brief This function writes the amount of battery charge remaining
95+
* (to the nearest 1%) in bv.
7496
* It returns true if successful, or false if there is an issue
7597
***************************************************************************/
7698
bool millivolts_to_percent(uint32_t millivolts, uint8_t *percent) {
@@ -87,6 +109,7 @@ int read_battery_voltage(void)
87109
{
88110
uint32_t sample;
89111
uint32_t millivolts;
112+
float millivolts_f;
90113
// Start ADC conversion
91114
k_sem_take(&adc_sem, K_MSEC(500));
92115
ADC_Start(ADC0, adcStartSingle);
@@ -100,11 +123,11 @@ int read_battery_voltage(void)
100123
k_sem_give(&adc_sem);
101124

102125
// Calculate input voltage in mV
103-
millivolts = (sample * 2500) / 4096;
126+
millivolts_f = (sample * 2500.0) / 4096.0;
104127

105128
// On the 2nd generation dev edge, voltage on PA2 is
106129
// one third the actual battery voltage
107-
millivolts = 3 * millivolts;
130+
millivolts = (uint32_t) (3.0 * millivolts_f + 0.5);
108131

109132
return (millivolts);
110133
}

samples/tmo_shell/src/tmo_battery_ctrl.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
#include <stdint.h>
88
#include <stdbool.h>
99
#include <stdlib.h>
10+
#include <stdio.h>
11+
#include <devicetree.h>
12+
13+
#if DT_NODE_EXISTS(DT_NODELABEL(bq24250))
14+
#include "tmo_bq24250.h"
15+
#endif
1016

1117
#include "tmo_battery_ctrl.h"
1218

1319
#define TABLE_LEN 11
1420
#define TABLE_VOLTAGE 0
15-
#define TABLE_PERCENTAGE 1
16-
17-
/** According to NSEP-720 Jm-battery-modeling.xlsx **/
21+
#define TABLE_PERCENT 1
1822

1923
// 100% value based on actual new battery measurement
2024
static float battery_discharging_tbl [TABLE_LEN][2] = {
21-
{4.160, 100.0 },
25+
{4.145, 100.0 },
2226
{4.072, 90.0 },
2327
{3.992, 80.0 },
2428
{3.923, 70.0 },
@@ -31,9 +35,8 @@ static float battery_discharging_tbl [TABLE_LEN][2] = {
3135
{3.632, 0.0 }};
3236

3337
// 100% value based on actual new battery measurement
34-
// 90% value based on an old dev board only getting to 4.135V fully charged
3538
static float battery_charging_tbl [TABLE_LEN][2] = {
36-
{4.180, 100.0 },
39+
{4.175, 100.0 },
3740
{4.130, 90.0 },
3841
{4.071, 80.0 },
3942
{4.005, 70.0 },
@@ -45,8 +48,16 @@ static float battery_charging_tbl [TABLE_LEN][2] = {
4548
{3.741, 10.0 },
4649
{3.597, 0.0 }};
4750

48-
// extern bool battery_is_charging; The following line needs to be placed in the battery charging code
49-
bool battery_is_charging = false;
51+
bool is_battery_charging()
52+
{
53+
uint8_t charging;
54+
uint8_t vbus;
55+
uint8_t attached;
56+
uint8_t fault;
57+
58+
get_battery_charging_status(&charging, &vbus, &attached, &fault);
59+
return (vbus && charging);
60+
}
5061

5162
float get_remaining_capacity(float battery_voltage)
5263
{
@@ -55,7 +66,7 @@ float get_remaining_capacity(float battery_voltage)
5566
float low_voltage = 0.0;
5667
uint32_t i;
5768

58-
if (battery_is_charging)
69+
if (is_battery_charging())
5970
voltage_capacity_table = &battery_charging_tbl;
6071
else
6172
voltage_capacity_table = &battery_discharging_tbl;
@@ -85,26 +96,24 @@ float get_remaining_capacity(float battery_voltage)
8596
return 0.0;
8697
}
8798

88-
// make sure the averaging algorithm zeros the return value if
89-
// the voltage capacity is less than 10 percent
90-
91-
float pct_gap = (float) (*voltage_capacity_table)[i-1][TABLE_PERCENTAGE] - (*voltage_capacity_table)[i][TABLE_PERCENTAGE];
92-
return ((battery_voltage - low_voltage) / (high_voltage - low_voltage) * pct_gap) + (*voltage_capacity_table)[i][TABLE_PERCENTAGE];
99+
float pct_gap = (float) (*voltage_capacity_table)[i-1][TABLE_PERCENT] - (*voltage_capacity_table)[i][TABLE_PERCENT];
100+
return ((battery_voltage - low_voltage) / (high_voltage - low_voltage) * pct_gap) + (*voltage_capacity_table)[i][TABLE_PERCENT];
93101
}
94102

95-
void apply_filter(float *bv)
103+
#if DT_NODE_EXISTS(DT_NODELABEL(pmic))
104+
extern int get_pmic_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault, uint8_t *charge_status);
105+
#endif
106+
107+
int get_battery_charging_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault)
96108
{
97-
static float s_filtered_capacity = -1;
98-
static bool s_battery_is_charging = false;
109+
#if DT_NODE_EXISTS(DT_NODELABEL(bq24250))
110+
int status = get_bq24250_status(charging, vbus, attached, fault);
111+
#endif
99112

100-
// If there has been a switch between charger and battery, reset the filter
101-
if (s_battery_is_charging != battery_is_charging) {
102-
s_battery_is_charging = battery_is_charging;
103-
s_filtered_capacity = -1;
104-
}
113+
#if DT_NODE_EXISTS(DT_NODELABEL(pmic))
114+
uint8_t charge_status;
115+
int status = get_pmic_status(charging, vbus, attached, fault, &charge_status);
116+
#endif
105117

106-
if (s_filtered_capacity < 0) {
107-
s_filtered_capacity = *bv;
108-
}
109-
*bv = s_filtered_capacity = s_filtered_capacity * 0.95 + (*bv) * 0.05;
118+
return status;
110119
}

samples/tmo_shell/src/tmo_battery_ctrl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
#define CUR_STAT_FAULT 40
3333

3434
bool getBatteryPercent(uint8_t *bv);
35-
void apply_filter(float *bv);
3635
float get_remaining_capacity(float battery_voltage);
36+
bool is_battery_charging(void);
37+
int get_battery_charging_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault);
3738

3839
#endif

samples/tmo_shell/src/tmo_ble_demo.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
#include "tmo_gnss.h"
3737
#include "tmo_smp.h"
3838
#include "tmo_shell.h"
39+
#include "tmo_battery_ctrl.h"
3940

4041
#define ON_CHARGER_POWER 0
4142
#define ON_BATTERY_POWER 1
4243

4344
static inline void strupper(char *p) { while (*p) *p++ &= 0xdf;}
4445
#define uuid128(...) BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(__VA_ARGS__))
45-
extern bool battery_is_charging;
4646

4747
extern struct bt_conn *get_acl_conn(int i);
4848
extern int get_active_le_conns();
@@ -195,15 +195,16 @@ static ssize_t battery_voltage_get(struct bt_conn *conn,
195195
uint8_t charging = 0;
196196
uint8_t vbus = 0;
197197
uint8_t fault = 0;
198-
set_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
198+
199+
get_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
199200

200201
/* flush the fault status out by reading again */
201202
if (fault) {
202-
set_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
203+
get_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
203204
}
204-
/* there can be 2 of these 2 flush */
205+
/* there can be 2 of these to flush */
205206
if (fault) {
206-
set_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
207+
get_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
207208
}
208209

209210
if (battery_attached) {
@@ -220,8 +221,8 @@ static ssize_t battery_power_source_get(struct bt_conn *conn,
220221
uint8_t power_source;
221222
uint8_t charging, vbus, battery_attached, fault;
222223

223-
set_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
224-
if (charging || !battery_attached) {
224+
get_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
225+
if (vbus) {
225226
power_source = ON_CHARGER_POWER;
226227
} else {
227228
power_source = ON_BATTERY_POWER;
@@ -235,7 +236,6 @@ static void dummy_ccc_cfg_changed(const struct bt_gatt_attr *attr,
235236
printf("CCC: %2x\n", value);
236237
}
237238

238-
239239
/**
240240
* @brief Acceleration CCC callback
241241
*
@@ -825,7 +825,7 @@ void ble_notif_thread(void *a, void *b, void *c)
825825
}
826826
uint8_t charging, vbus, battery_attached, fault, percent;
827827

828-
set_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
828+
get_battery_charging_status(&charging, &vbus, &battery_attached, &fault);
829829
if (battery_attached) {
830830
uint32_t millivolts = read_battery_voltage();
831831
millivolts_to_percent(millivolts, &percent);
@@ -837,7 +837,6 @@ void ble_notif_thread(void *a, void *b, void *c)
837837
}
838838
}
839839

840-
841840
#define BLE_NOTIF_THREAD_STACK_SIZE 2048
842841
#define BLE_NOTIF_THREAD_PRIORITY CONFIG_MAIN_THREAD_PRIORITY
843842

samples/tmo_shell/src/tmo_batt_charger.c renamed to samples/tmo_shell/src/tmo_bq24250.c

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <drivers/i2c.h>
1111
#include <drivers/gpio.h>
1212
#include <sys/byteorder.h>
13-
#include "tmo_batt_charger.h"
13+
#include "tmo_bq24250.h"
14+
15+
#if DT_NODE_HAS_STATUS(DT_NODELABEL(bq24250), okay)
1416

1517
#define BQ_GPIO_F_NAME "GPIO_F"
1618
#define BQ_CHARGE_INT 12 /* PF12 - configured here */
@@ -46,9 +48,9 @@ static const struct device *i2c1_dev = DEVICE_DT_GET(DT_NODELABEL(i2c1));
4648
static struct gpio_callback gpio_batt_chrg_cb;
4749
int bq_battery_charge_int_isr_count = 0;
4850
bool bq_battery_charge_int_state_change = false;
49-
5051
uint8_t i2c_read_data[20];
51-
static int i2c_read_to_buffer( uint8_t dev_addr, int reg_addr, uint8_t *buf, uint8_t buf_length)
52+
53+
static int i2c_read_to_buffer(uint8_t dev_addr, int reg_addr, uint8_t *buf, uint8_t buf_length)
5254
{
5355
const struct device *dev;
5456
uint8_t reg_addr_buf[MAX_BYTES_FOR_REGISTER_INDEX];
@@ -72,11 +74,10 @@ static int i2c_read_to_buffer( uint8_t dev_addr, int reg_addr, uint8_t *buf, uin
7274
printf("Failed to read from device: 0x%x", dev_addr);
7375
return -EIO;
7476
}
75-
7677
return 0;
7778
}
7879

79-
int get_bq24250_charger_vbus_status (uint8_t * charging, uint8_t * vbus, uint8_t * attached, uint8_t * fault)
80+
int get_bq24250_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault)
8081
{
8182
int ret = 0;
8283
uint8_t bq_addr = 0x6a;
@@ -93,40 +94,38 @@ int get_bq24250_charger_vbus_status (uint8_t * charging, uint8_t * vbus, uint8_t
9394
uint8_t charger_status = ((bq_reg_0x00_data & 0x30) >> 4);
9495
uint8_t charge_fault_status = (bq_reg_0x00_data & 0x0F);
9596

97+
*vbus = 0;
98+
*charging = 0;
99+
*attached = 0;
100+
*fault = 0;
96101

97-
*vbus = 0;
98-
*charging = 0;
99-
*attached = 0;
100-
*fault = 0;
101-
102-
if ((charger_status == 3) && (charge_fault_status > 0 )) {
103-
*vbus = 0;
104-
*charging = 0;
105-
if (charge_fault_status == 8) {
106-
*attached = 0;
107-
}
108-
else {
109-
*attached = 1;
110-
*fault = 1;
111-
}
112-
}
113-
else {
114-
if ((charger_status == 1) && (charge_fault_status == 0)) {
115-
*vbus = 1;
116-
*charging = 1;
117-
*attached = 1;
118-
}
119-
else {
120-
*vbus = 1;
121-
*charging = 0;
122-
*attached = 1;
123-
}
124-
}
125-
126-
return ret;
102+
if ((charger_status == 3) && (charge_fault_status > 0 )) {
103+
*vbus = 0;
104+
*charging = 0;
105+
if (charge_fault_status == 8) {
106+
*attached = 0;
107+
}
108+
else {
109+
*attached = 1;
110+
*fault = 1;
111+
}
112+
}
113+
else {
114+
if ((charger_status == 1) && (charge_fault_status == 0)) {
115+
*vbus = 1;
116+
*charging = 1;
117+
*attached = 1;
118+
}
119+
else {
120+
*vbus = 1;
121+
*charging = 0;
122+
*attached = 1;
123+
}
124+
}
125+
return ret;
127126
}
128127

129-
static int check_battery_charger_regs (void)
128+
static int check_battery_charger_regs(void)
130129
{
131130
int ret = 0;
132131

@@ -240,18 +239,17 @@ static int check_battery_charger_regs (void)
240239
break;
241240
}
242241
}
243-
244242
return ret;
245243
}
246244

247-
void bq_intr_callback(const struct device *port,
245+
static void bq_intr_callback(const struct device *port,
248246
struct gpio_callback *cb, uint32_t pins)
249247
{
250248
bq_battery_charge_int_isr_count++;
251249
bq_battery_charge_int_state_change = true;
252250
}
253251

254-
void bq_thread(void *a, void *b, void *c)
252+
static void bq_thread(void *a, void *b, void *c)
255253
{
256254
ARG_UNUSED(a);
257255
ARG_UNUSED(b);
@@ -330,7 +328,6 @@ void bq_thread(void *a, void *b, void *c)
330328
#define BQ_NOTIF_THREAD_STACK_SIZE 2048
331329
#define BQ_NOTIF_THREAD_PRIORITY CONFIG_MAIN_THREAD_PRIORITY
332330

333-
#if DT_NODE_HAS_STATUS(DT_NODELABEL(bq24250), okay)
334331
K_THREAD_DEFINE(bq_tid, BQ_NOTIF_THREAD_STACK_SIZE,
335332
bq_thread, NULL, NULL, NULL,
336333
BQ_NOTIF_THREAD_PRIORITY, 0, 0);

samples/tmo_shell/src/tmo_batt_charger.h renamed to samples/tmo_shell/src/tmo_bq24250.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
#ifndef TMO_BATT_CHARGER_H
88
#define TMO_BATT_CHARGER_H
99

10-
int get_bq24250_charger_vbus_status(uint8_t * charging , uint8_t * vbus , uint8_t * attached, uint8_t *fault);
10+
int get_bq24250_status(uint8_t *charging, uint8_t *vbus, uint8_t *attached, uint8_t *fault);
1111

1212
#endif

0 commit comments

Comments
 (0)