Skip to content

Commit

Permalink
syslink: Move shutdown to PM group
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasdn committed Sep 7, 2021
1 parent cce2f4a commit 1ce7cd9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 62 deletions.
7 changes: 7 additions & 0 deletions src/hal/interface/pm.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ typedef enum
USBWallAdapter,
} PMUSBPower;

typedef void (*graceful_shutdown_callback_t)();

void pmInit(void);

bool pmTest(void);
Expand Down Expand Up @@ -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_ */
8 changes: 4 additions & 4 deletions src/hal/interface/syslink.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@
#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
#define SYSLINK_OW_GETINFO 0x21
#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
{
Expand Down
49 changes: 49 additions & 0 deletions src/hal/src/pm_stm32f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/modules/interface/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include <stdbool.h>
#include <stdint.h>

typedef void (*graceful_shutdown_callback_t)();

void systemInit(void);
bool systemTest(void);

Expand All @@ -47,6 +45,4 @@ void systemRequestShutdown();
void systemRequestNRFVersion();
void systemSyslinkReceive();

bool systemRegisterGracefulShutdownCallback(graceful_shutdown_callback_t cb);

#endif //__SYSTEM_H__
55 changes: 1 addition & 54 deletions src/modules/src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,70 +352,17 @@ 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) {
len = sizeof(nrf_version) - 1;
}
memcpy(&nrf_version, &slp->data[0], len);
DEBUG_PRINT("NRF51 version: %s\n", nrf_version);
} break;

case SYSLINK_SYS_SHUTDOWN_REQUEST:
systemGracefulShutdown();
break;
}
}

Expand Down

0 comments on commit 1ce7cd9

Please sign in to comment.