diff --git a/src/hal/interface/pm.h b/src/hal/interface/pm.h index 707af90ca9..89d4b0a5ef 100644 --- a/src/hal/interface/pm.h +++ b/src/hal/interface/pm.h @@ -101,6 +101,8 @@ typedef enum USBWallAdapter, } PMUSBPower; +typedef void (*graceful_shutdown_callback_t)(); + void pmInit(void); bool pmTest(void); @@ -175,4 +177,9 @@ void pmEnableExtBatteryCurrMeasuring(const deckPin_t pin, float ampPerVolt); */ float pmMeasureExtBatteryCurrent(void); +/** + * Register a callback to be run when the NRF51 signals shutdown + */ +bool pmRegisterGracefulShutdownCallback(graceful_shutdown_callback_t cb); + #endif /* PM_H_ */ diff --git a/src/hal/interface/syslink.h b/src/hal/interface/syslink.h index 96b14fbd4a..6bf1abab38 100644 --- a/src/hal/interface/syslink.h +++ b/src/hal/interface/syslink.h @@ -57,6 +57,8 @@ #define SYSLINK_PM_BATTERY_VOLTAGE 0x12 #define SYSLINK_PM_BATTERY_STATE 0x13 #define SYSLINK_PM_BATTERY_AUTOUPDATE 0x14 +#define SYSLINK_PM_SHUTDOWN_REQUEST 0x15 +#define SYSLINK_PM_SHUTDOWN_ACK 0x16 #define SYSLINK_OW_GROUP 0x20 #define SYSLINK_OW_SCAN 0x20 @@ -64,10 +66,8 @@ #define SYSLINK_OW_READ 0x22 #define SYSLINK_OW_WRITE 0x23 -#define SYSLINK_SYS_GROUP 0x30 -#define SYSLINK_SYS_NRF_VERSION 0x30 -#define SYSLINK_SYS_SHUTDOWN_REQUEST 0x31 -#define SYSLINK_SYS_SHUTDOWN_ACK 0x32 +#define SYSLINK_SYS_GROUP 0x30 +#define SYSLINK_SYS_NRF_VERSION 0x30 typedef struct _SyslinkPacket { diff --git a/src/hal/src/pm_stm32f4.c b/src/hal/src/pm_stm32f4.c index 9c184be698..43db699fbd 100644 --- a/src/hal/src/pm_stm32f4.c +++ b/src/hal/src/pm_stm32f4.c @@ -193,6 +193,53 @@ float pmGetBatteryVoltageMax(void) return batteryVoltageMax; } +/* + * When a module wants to register a callback to be called on shutdown they + * call pmRegisterGracefulShutdownCallback(graceful_shutdown_callback_t), + * with a function they which to be run at shutdown. We currently support + * GRACEFUL_SHUTDOWN_MAX_CALLBACKS number of callbacks to be registred. + */ +#define GRACEFUL_SHUTDOWN_MAX_CALLBACKS 5 +static uint8_t graceful_shutdown_callbacks_index; +static graceful_shutdown_callback_t graceful_shutdown_callbacks[GRACEFUL_SHUTDOWN_MAX_CALLBACKS]; + +/* + * Please take care in your callback, do not take to long time the nrf + * will not wait for you, it will shutdown. + */ +bool pmRegisterGracefulShutdownCallback(graceful_shutdown_callback_t cb) +{ + // To many registered allready! Increase limit if you think you are important + // enough! + if (graceful_shutdown_callbacks_index >= GRACEFUL_SHUTDOWN_MAX_CALLBACKS) { + return false; + } + + graceful_shutdown_callbacks[graceful_shutdown_callbacks_index] = cb; + graceful_shutdown_callbacks_index += 1; + + return true; +} + +/* + * Iterate through all registered shutdown callbacks and call them one after + * the other, when all is done, send the ACK back to nrf to allow power off. + */ +static void pmGracefulShutdown() +{ + for (int i = 0; i < graceful_shutdown_callbacks_index; i++) { + graceful_shutdown_callback_t callback = graceful_shutdown_callbacks[i]; + + callback(); + } + + SyslinkPacket slp = { + .type = SYSLINK_PM_SHUTDOWN_ACK, + }; + + syslinkSendPacket(&slp); +} + void pmSyslinkUpdate(SyslinkPacket *slp) { if (slp->type == SYSLINK_PM_BATTERY_STATE) { @@ -201,6 +248,8 @@ void pmSyslinkUpdate(SyslinkPacket *slp) #ifdef PM_SYSTLINK_INLCUDE_TEMP temp = pmSyslinkInfo.temp; #endif + } else if (slp->type == SYSLINK_PM_SHUTDOWN_REQUEST) { + pmGracefulShutdown(); } } diff --git a/src/modules/interface/system.h b/src/modules/interface/system.h index 50463da5cc..8fd3cc92c2 100644 --- a/src/modules/interface/system.h +++ b/src/modules/interface/system.h @@ -30,8 +30,6 @@ #include #include -typedef void (*graceful_shutdown_callback_t)(); - void systemInit(void); bool systemTest(void); @@ -47,6 +45,4 @@ void systemRequestShutdown(); void systemRequestNRFVersion(); void systemSyslinkReceive(); -bool systemRegisterGracefulShutdownCallback(graceful_shutdown_callback_t cb); - #endif //__SYSTEM_H__ diff --git a/src/modules/src/system.c b/src/modules/src/system.c index d0ea533983..c6de303900 100644 --- a/src/modules/src/system.c +++ b/src/modules/src/system.c @@ -352,58 +352,10 @@ void systemRequestNRFVersion() syslinkSendPacket(&slp); } -/* - * When a module wants to register a callback to be called on shutdown they - * call systemRegisterGracefulShutdownCallback(graceful_shutdown_callback_t), - * with a function they which to be run at shutdown. We currently support - * GRACEFUL_SHUTDOWN_MAX_CALLBACKS number of callbacks to be registred. - */ -#define GRACEFUL_SHUTDOWN_MAX_CALLBACKS 5 -static uint8_t graceful_shutdown_callbacks_index; -static graceful_shutdown_callback_t graceful_shutdown_callbacks[GRACEFUL_SHUTDOWN_MAX_CALLBACKS]; - -/* - * Please take care in your callback, do not take to long time the nrf - * will not wait for you, it will shutdown. - */ -bool systemRegisterGracefulShutdownCallback(graceful_shutdown_callback_t cb) -{ - // To many registered allready! Increase limit if you think you are important - // enough! - if (graceful_shutdown_callbacks_index >= GRACEFUL_SHUTDOWN_MAX_CALLBACKS) { - return false; - } - - graceful_shutdown_callbacks[graceful_shutdown_callbacks_index] = cb; - graceful_shutdown_callbacks_index += 1; - - return true; -} - -/* - * Iterate through all registered shutdown callbacks and call them one after - * the other, when all is done, send the ACK back to nrf to allow power off. - */ -void systemGracefulShutdown() -{ - for (int i = 0; i < graceful_shutdown_callbacks_index; i++) { - graceful_shutdown_callback_t callback = graceful_shutdown_callbacks[i]; - - callback(); - } - - SyslinkPacket slp = { - .type = SYSLINK_SYS_SHUTDOWN_ACK, - }; - - syslinkSendPacket(&slp); -} - void systemSyslinkReceive(SyslinkPacket *slp) { - switch (slp->type) + if (slp->type == SYSLINK_SYS_NRF_VERSION) { - case SYSLINK_SYS_NRF_VERSION:{ size_t len = slp->length - 2; if (sizeof(nrf_version) - 1 <= len) { @@ -411,11 +363,6 @@ void systemSyslinkReceive(SyslinkPacket *slp) } memcpy(&nrf_version, &slp->data[0], len); DEBUG_PRINT("NRF51 version: %s\n", nrf_version); - } break; - - case SYSLINK_SYS_SHUTDOWN_REQUEST: - systemGracefulShutdown(); - break; } }