From e9ee2af0dcee175b0a7bf0380d754ae0e63041c8 Mon Sep 17 00:00:00 2001 From: Lucjan Bryndza Date: Wed, 22 Dec 2021 15:04:59 +0100 Subject: [PATCH] [EGD-8131] Add Reboot/Poweroff for _platform_exit() Add support for Reboot and Power off for the _platform_exit code. Add _platform_exit mode handle Signed-off-by: Lucjan Bryndza --- module-bsp/board/linux/board.cpp | 11 +++++ module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp | 5 ++- module-bsp/board/rt1051/common/board.cpp | 42 +++++++++++++++++++ .../rt1051/common/startup_mimxrt1052.cpp | 8 +++- module-bsp/bsp/bsp.hpp | 7 +++- module-os/board/rt1051/_exit.cpp | 3 +- .../SystemManager/SystemManagerCommon.cpp | 6 ++- 7 files changed, 74 insertions(+), 8 deletions(-) diff --git a/module-bsp/board/linux/board.cpp b/module-bsp/board/linux/board.cpp index 693451937..fac3cc782 100644 --- a/module-bsp/board/linux/board.cpp +++ b/module-bsp/board/linux/board.cpp @@ -7,6 +7,17 @@ namespace bsp { // dummy } + + void BoardPowerOff() + { + // dummy + } + + void BoardReboot() + { + // dummy + } + } // namespace bsp namespace bsp::bell_switches diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp index 35d9ea69a..a73f225ea 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp @@ -10,6 +10,7 @@ #include "bsp/watchdog/watchdog.hpp" #include #include +#include #include "ClockState.hpp" #include "Oscillator.hpp" #include "critical.hpp" @@ -44,7 +45,7 @@ namespace bsp int32_t RT1051LPM::PowerOff() { - gpio_1->WritePin(static_cast(BoardDefinitions::POWER_SWITCH_HOLD_BUTTON), 0); + bsp::BoardPowerOff(); return 0; } @@ -64,7 +65,7 @@ namespace bsp SNVS->LPGPR[0] = bsp::rebootCode::rebootNormalCode; break; } - NVIC_SystemReset(); + BoardReboot(); return 0; } diff --git a/module-bsp/board/rt1051/common/board.cpp b/module-bsp/board/rt1051/common/board.cpp index 30b09683f..eadee69f8 100644 --- a/module-bsp/board/rt1051/common/board.cpp +++ b/module-bsp/board/rt1051/common/board.cpp @@ -1,5 +1,7 @@ #include "board.h" +#include "fsl_gpio.h" +#include extern "C" { #include "fsl_common.h" @@ -25,6 +27,15 @@ extern std::uint32_t __sdram_cached_start[]; namespace bsp { + namespace { + enum class rebootState : uintptr_t { + none, + poweroff, + reboot + }; + volatile rebootState rebootProgress {rebootState::none}; + } + /* MPU configuration. */ static void BOARD_ConfigMPU(void) { @@ -165,4 +176,35 @@ namespace bsp clearAndPrintBootReason(); } + //! Board PowerOff function by cutdown power + void BoardPowerOff() + { + rebootProgress = rebootState::poweroff; + } + + //! Board reboot by the SVNC code + void BoardReboot() + { + rebootProgress = rebootState::reboot; + } + + extern "C" { + void _platform_exit(void) + { + static constexpr auto POWER_SWITCH_PIN = 7; + static const auto POWER_SWITCH_PORT = GPIO2; + switch(rebootProgress) + { + case rebootState::none: + break; + case rebootState::poweroff: + GPIO_PinWrite(POWER_SWITCH_PORT, POWER_SWITCH_PIN, 0); + break; + case rebootState::reboot: + NVIC_SystemReset(); + break; + } + } + } + } // namespace bsp diff --git a/module-bsp/board/rt1051/common/startup_mimxrt1052.cpp b/module-bsp/board/rt1051/common/startup_mimxrt1052.cpp index 453d73d9d..ea5db5573 100644 --- a/module-bsp/board/rt1051/common/startup_mimxrt1052.cpp +++ b/module-bsp/board/rt1051/common/startup_mimxrt1052.cpp @@ -71,7 +71,11 @@ extern "C" extern "C" { #endif - + /* Platform exit function it is a last function called after system + * deinitalization. It should be used to cutoff power + */ + WEAK void _platform_exit(void) + {} //***************************************************************************** // Variable to store CRP value in. Will be placed automatically // by the linker when "Enable Code Read Protect" selected. @@ -746,7 +750,7 @@ __attribute__((section(".after_vectors.reset"))) void ResetISR(void) #else main(); #endif - + _platform_exit(); // // main() shouldn't return, but if it does, we'll just enter an infinite loop // diff --git a/module-bsp/bsp/bsp.hpp b/module-bsp/bsp/bsp.hpp index ea0f807ec..eadeded43 100644 --- a/module-bsp/bsp/bsp.hpp +++ b/module-bsp/bsp/bsp.hpp @@ -3,9 +3,14 @@ namespace bsp { - + //! Board hardware init void BoardInit(); + // Board cuttoff power + void BoardPowerOff(); + + // Board system reset + void BoardReboot(); } diff --git a/module-os/board/rt1051/_exit.cpp b/module-os/board/rt1051/_exit.cpp index e02a3d524..3c5363f70 100644 --- a/module-os/board/rt1051/_exit.cpp +++ b/module-os/board/rt1051/_exit.cpp @@ -40,6 +40,7 @@ #include #include #include +#include static void __attribute__((noreturn)) stop_system(void) @@ -57,8 +58,8 @@ static void __attribute__((noreturn)) stop_system(void) } LOG_INFO("Restarting the system..."); haltIfDebugging(); + bsp::BoardReboot(); vTaskEndScheduler(); - NVIC_SystemReset(); // waiting for system reset while (1) { #ifndef DEBUG diff --git a/module-sys/SystemManager/SystemManagerCommon.cpp b/module-sys/SystemManager/SystemManagerCommon.cpp index 2338f62a0..9e2828e35 100644 --- a/module-sys/SystemManager/SystemManagerCommon.cpp +++ b/module-sys/SystemManager/SystemManagerCommon.cpp @@ -182,10 +182,12 @@ namespace sys systemDeinit(); } + // Power off request (pending) + PowerOff(); + + // End of scheduler and back to the main and poweroff EndScheduler(); - // Power off system - PowerOff(); } void SystemManagerCommon::initialize()