diff --git a/firmware/reggen/stm32h743.regs b/firmware/reggen/stm32h743.regs index c6c25cdf4a..df8d7235ae 100644 --- a/firmware/reggen/stm32h743.regs +++ b/firmware/reggen/stm32h743.regs @@ -587,6 +587,10 @@ RTC @ 0x58004000 : block { 14 00 PREDIV_S } + WUTR @ 0x14 : reg { + 15 00 VALUE + } + WPR @ 0x24 : reg { 07 00 KEY : { 0xCA = KEY1; 0x53 = KEY2 } } diff --git a/firmware/target/arm/stm32/echoplayer/power-echoplayer.c b/firmware/target/arm/stm32/echoplayer/power-echoplayer.c index 667cd99731..63bf173d33 100644 --- a/firmware/target/arm/stm32/echoplayer/power-echoplayer.c +++ b/firmware/target/arm/stm32/echoplayer/power-echoplayer.c @@ -21,6 +21,8 @@ #include "power.h" #include "mutex.h" #include "gpio-stm32h7.h" +#include "system-echoplayer.h" +#include "regs/cortex-m/cm_scb.h" static struct mutex power_1v8_lock; static int power_1v8_refcount; @@ -68,23 +70,34 @@ void power_init(void) void power_off(void) { + /* + * Disable power and reset to the bootloader immediately. + * The system can't really be powered off as long as USB + * is plugged or the power button is pressed -- it's the + * bootloader's job to monitor those inputs and decide + * when the system needs to "really" power on. + */ gpio_set_level(GPIO_CPU_POWER_ON, 0); - /* TODO: reset to bootloader if USB is plugged in */ - while (1) - core_idle(); + reg_writef(CM_SCB_AIRCR, VECTKEY_V(KEY), SYSRESETREQ(1)); + while (1); } void system_reboot(void) { /* - * TODO: support reboot - * - * For R1-Rev1 PCBs doing a CPU reset will cut power when - * running on battery (because cpu_power_on is no longer - * being driven high). The RTC alarm could be used to wake - * the system instead. + * Disable IRQs to ensure an errant panic can't disturb + * the RTC reconfig. */ + disable_irq(); + + /* + * Configure RTC_OUT pin to keep power enabled during + * reset and then do a normal power off. If this fails + * to reset back to the bootloader then the RTC_OUT + * pin will go low automatically after some timeout. + */ + echoplayer_set_rtcout_mode(ECHOPLAYER_RTCOUT_REBOOT); power_off(); } diff --git a/firmware/target/arm/stm32/echoplayer/system-echoplayer.c b/firmware/target/arm/stm32/echoplayer/system-echoplayer.c index 6a980f983e..3686ffb55b 100644 --- a/firmware/target/arm/stm32/echoplayer/system-echoplayer.c +++ b/firmware/target/arm/stm32/echoplayer/system-echoplayer.c @@ -22,8 +22,11 @@ #include "button.h" #include "gpio-stm32h7.h" #include "clock-echoplayer.h" +#include "system-echoplayer.h" #include "regs/stm32h743/fmc.h" +#include "regs/stm32h743/pwr.h" #include "regs/stm32h743/rcc.h" +#include "regs/stm32h743/rtc.h" #include "regs/cortex-m/cm_scb.h" #define F_INPUT GPIOF_INPUT(GPIO_PULL_DISABLED) @@ -207,9 +210,47 @@ void system_init(void) /* Configure GPIOs and start FMC */ gpio_init(); fmc_init(); + + /* Disable RTC_OUT pin */ + echoplayer_set_rtcout_mode(ECHOPLAYER_RTCOUT_DISABLED); } void system_exception_wait(void) { while (button_read_device() != (BUTTON_POWER | BUTTON_START)); } + +void echoplayer_set_rtcout_mode(enum echoplayer_rtcout_mode mode) +{ + reg_writef(RCC_APB4ENR, RTCAPBEN(1)); + reg_writef(PWR_CR1, DBP(1)); + + reg_writef(RTC_WPR, KEY_V(KEY1)); + reg_writef(RTC_WPR, KEY_V(KEY2)); + + reg_writef(RTC_OR, OUT_RMP(0), ALARM_TYPE_V(PUSH_PULL)); + + switch (mode) + { + case ECHOPLAYER_RTCOUT_REBOOT: + /* + * Use the inverted wakeup timer output to keep power + * enabled during reset. If, somehow, the system does + * not reset properly then the wakeup timer will drive + * the RTC_OUT pin low after 1 second and cut power. + */ + while (!reg_readf(RTC_ISR, WUTWF)); + reg_writef(RTC_ISR, WUTF(0)); + reg_writef(RTC_WUTR, VALUE(STM32_LSE_FREQ / 8)); + reg_writef(RTC_CR, OSEL_V(WAKEUP), POL(1), WUCKSEL(0), WUTE(1)); + break; + + case ECHOPLAYER_RTCOUT_DISABLED: + default: + reg_writef(RTC_CR, OSEL_V(DISABLED), POL(0), WUTE(0)); + break; + } + + reg_writef(RTC_WPR, KEY(0)); + reg_writef(PWR_CR1, DBP(0)); +} diff --git a/firmware/target/arm/stm32/echoplayer/system-echoplayer.h b/firmware/target/arm/stm32/echoplayer/system-echoplayer.h new file mode 100644 index 0000000000..3f93d84e30 --- /dev/null +++ b/firmware/target/arm/stm32/echoplayer/system-echoplayer.h @@ -0,0 +1,32 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2026 Aidan MacDonald + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef __SYSTEM_ECHOPLAYER_H__ +#define __SYSTEM_ECHOPLAYER_H__ + +enum echoplayer_rtcout_mode +{ + ECHOPLAYER_RTCOUT_DISABLED, + ECHOPLAYER_RTCOUT_REBOOT, +}; + +void echoplayer_set_rtcout_mode(enum echoplayer_rtcout_mode mode); + +#endif /* __SYSTEM_ECHOPLAYER_H__ */