Skip to content

Commit 9fc5dbb

Browse files
committed
pbio/drv/reset/reset_ev3: Implement software reset reason tracking
This helps determine whether the system was soft-rebooted, booted from cold, or reset due to a crash.
1 parent ef5bb95 commit 9fc5dbb

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lib/pbio/drv/reset/reset_ev3.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#if PBDRV_CONFIG_RESET_EV3
99

10+
#include <stdbool.h>
11+
1012
#include <pbdrv/reset.h>
1113
#include <pbdrv/gpio.h>
1214

@@ -19,6 +21,11 @@
1921

2022
#define BOOTLOADER_UPDATE_MODE_VALUE 0x5555AAAA
2123

24+
// 'Pybr'
25+
#define RESET_REASON_FLAG_WDT 0x50796272
26+
// 'rboo'
27+
#define RESET_REASON_FLAG_SOFT_RESET 0x72626f6f
28+
2229
typedef struct {
2330
// 0xffff1ff0
2431
uint32_t _dummy0;
@@ -39,10 +46,13 @@ typedef struct {
3946
// This lives at the very end of the ARM local RAM.
4047
extern volatile persistent_data_t ev3_persistent_data;
4148

49+
static uint32_t saved_reset_reason_flag;
50+
4251
static const pbdrv_gpio_t poweroff_pin = PBDRV_GPIO_EV3_PIN(13, 19, 16, 6, 11);
4352

4453
void pbdrv_reset_init(void) {
45-
pbdrv_gpio_out_high(&poweroff_pin);
54+
saved_reset_reason_flag = ev3_persistent_data.reset_reason_flag;
55+
ev3_persistent_data.reset_reason_flag = RESET_REASON_FLAG_WDT;
4656
}
4757

4858
void pbdrv_reset(pbdrv_reset_action_t action) {
@@ -57,6 +67,8 @@ void pbdrv_reset(pbdrv_reset_action_t action) {
5767
default:
5868
// PBDRV_RESET_ACTION_RESET
5969

70+
ev3_persistent_data.reset_reason_flag = RESET_REASON_FLAG_SOFT_RESET;
71+
6072
// Poke the watchdog timer with a bad value to immediately trigger it
6173
HWREG(SOC_TMR_1_REGS + TMR_WDTCR) = 0;
6274
break;
@@ -65,9 +77,19 @@ void pbdrv_reset(pbdrv_reset_action_t action) {
6577
}
6678

6779
pbdrv_reset_reason_t pbdrv_reset_get_reason(void) {
80+
if (saved_reset_reason_flag == RESET_REASON_FLAG_SOFT_RESET) {
81+
return PBDRV_RESET_REASON_SOFTWARE;
82+
}
83+
if (saved_reset_reason_flag == RESET_REASON_FLAG_WDT) {
84+
return PBDRV_RESET_REASON_WATCHDOG;
85+
}
6886
return PBDRV_RESET_REASON_NONE;
6987
}
7088

89+
void pbdrv_reset_ev3_early_init(void) {
90+
pbdrv_gpio_out_high(&poweroff_pin);
91+
}
92+
7193
void pbdrv_reset_power_off(void) {
7294
pbdrv_gpio_out_low(&poweroff_pin);
7395
}

lib/pbio/drv/reset/reset_ev3.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (c) 2025 The Pybricks Authors
3+
4+
// The EV3 requires a GPIO pin to be set in order to stay powered on.
5+
// We want to be able to do this as early as possible.
6+
7+
#ifndef _INTERNAL_PBDRV_RESET_EV3_H_
8+
#define _INTERNAL_PBDRV_RESET_EV3_H_
9+
10+
void pbdrv_reset_ev3_early_init(void);
11+
12+
#endif // _INTERNAL_PBDRV_RESET_EV3_H_

lib/pbio/platform/ev3/platform.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
#include "../../drv/led/led_dual.h"
6464
#include "../../drv/led/led_pwm.h"
6565
#include "../../drv/pwm/pwm_ev3.h"
66+
#include "../../drv/reset/reset_ev3.h"
6667
#include "../../drv/uart/uart_ev3.h"
67-
#include "../../drv/reset/reset.h"
6868

6969
enum {
7070
LED_DEV_0_STATUS,
@@ -668,7 +668,7 @@ void SystemInit(void) {
668668

669669
// Must set the power enable bin before disabling the pull up on the power
670670
// pin below, otherwise the hub will power off.
671-
pbdrv_reset_init();
671+
pbdrv_reset_ev3_early_init();
672672

673673
// Disable all pull-up/pull-down groups.
674674
HWREG(SOC_SYSCFG_1_REGS + SYSCFG1_PUPD_ENA) &= ~0xFFFFFFFF;

0 commit comments

Comments
 (0)