Skip to content

Commit f2067d0

Browse files
committed
pbio/battery: Fix initial battery value.
This restores an alternative for the hack that was dropped in b2fd58d and adds a suggestion to fix it properly after other upstream ADC changes have been merged.
1 parent 0b0dc49 commit f2067d0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
## [Unreleased]
66

7+
### Fixed
8+
- Fixed low-battery warning on boot ([pybricks-micropython#292]) when the
9+
voltage is not actually low.
10+
711
## [3.6.0b3] - 2025-02-14
812

913
### Added

lib/pbio/drv/battery/battery_adc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include <pbdrv/adc.h>
4545
#include <pbdrv/battery.h>
46+
#include <pbdrv/clock.h>
4647
#include <pbdrv/gpio.h>
4748
#include <pbio/error.h>
4849

@@ -52,13 +53,16 @@
5253
static pbdrv_battery_type_t pbdrv_battery_type;
5354
#endif
5455

56+
static uint32_t pbdrv_battery_start_time;
57+
5558
void pbdrv_battery_init(void) {
5659
#if PBDRV_CONFIG_BATTERY_ADC_TYPE == 3
5760
const pbdrv_battery_adc_platform_data_t *pdata = &pbdrv_battery_adc_platform_data;
5861
pbdrv_gpio_set_pull(&pdata->gpio, pdata->pull);
5962
pbdrv_battery_type = pbdrv_gpio_input(&pdata->gpio) ?
6063
PBDRV_BATTERY_TYPE_ALKALINE : PBDRV_BATTERY_TYPE_LIION;
6164
#endif
65+
pbdrv_battery_start_time = pbdrv_clock_get_ms();
6266
}
6367

6468
pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
@@ -89,6 +93,17 @@ pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
8993
}
9094

9195
pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) {
96+
97+
if (pbdrv_clock_get_ms() - pbdrv_battery_start_time < 100) {
98+
// The battery voltage is not reliable when the hub is just powered on.
99+
// This suppresses immediate low-battery warnings when the hub is
100+
// powered on. REVISIT: This can be integrated in the ADC driver to
101+
// hold the pbdrv init busy flag after currently pending driver changes
102+
// are merged.
103+
*value = 7500;
104+
return PBIO_SUCCESS;
105+
}
106+
92107
uint16_t raw;
93108
pbio_error_t err;
94109

0 commit comments

Comments
 (0)