Skip to content

Commit b851bc1

Browse files
committed
pbio/drv/battery/battery_ev3: implement EV3 battery type
Implement reading the EV3 battery type from the GP8_8 pin. Also make the remaining TODOs a bit more clear the the next person who works on this.
1 parent 1d10654 commit b851bc1

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/pbio/drv/battery/battery_ev3.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2024 The Pybricks Authors
2+
// Copyright (c) 2024-2025 The Pybricks Authors
33

44
#include <pbdrv/config.h>
55

@@ -9,28 +9,42 @@
99

1010
#include <pbdrv/battery.h>
1111
#include <pbio/error.h>
12+
#include <pbdrv/gpio.h>
13+
#include "../gpio/gpio_ev3.h"
14+
15+
#include <tiam1808/hw/hw_syscfg0_AM1808.h>
16+
17+
static const pbdrv_gpio_t battery_type_gpio = PBDRV_GPIO_EV3_PIN(19, 7, 4, 8, 8);
1218

1319
void pbdrv_battery_init(void) {
20+
pbdrv_gpio_alt(&battery_type_gpio, SYSCFG_PINMUX19_PINMUX19_7_4_GPIO8_8);
1421
}
1522

1623
pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) {
17-
// TODO. Return nominal value for now so we don't trigger low battery shutdown.
24+
// TODO. Calculate battery voltage based on ADC channel 4.
25+
// For now, return nominal value for now so we don't trigger low battery shutdown.
1826
*value = 7200;
1927
return PBIO_SUCCESS;
2028
}
2129

2230
pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
31+
// TODO. Calculate battery current based on ADC channel 3.
2332
*value = 0;
2433
return PBIO_ERROR_NOT_IMPLEMENTED;
2534
}
2635

2736
pbio_error_t pbdrv_battery_get_type(pbdrv_battery_type_t *value) {
28-
// TODO
29-
*value = PBDRV_BATTERY_TYPE_UNKNOWN;
30-
return PBIO_ERROR_NOT_IMPLEMENTED;
37+
if (pbdrv_gpio_input(&battery_type_gpio)) {
38+
*value = PBDRV_BATTERY_TYPE_ALKALINE;
39+
} else {
40+
*value = PBDRV_BATTERY_TYPE_LIION;
41+
}
42+
43+
return PBIO_SUCCESS;
3144
}
3245

3346
pbio_error_t pbdrv_battery_get_temperature(uint32_t *value) {
47+
// EV3 does not have a way to read the battery temperature.
3448
*value = 0;
3549
return PBIO_ERROR_NOT_SUPPORTED;
3650
}

0 commit comments

Comments
 (0)