Files

76 lines
2.3 KiB
C
Raw Permalink Normal View History

2022-12-22 12:43:05 +08:00
#include "nrfx_power.h"
#include "app_timer.h"
#include "syssleep.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
2023-08-26 04:42:40 +02:00
APP_TIMER_DEF(m_app_sleep_timer); //The timer for equipment sleep
2022-12-22 12:43:05 +08:00
static volatile bool m_system_off_enter = false;
2023-08-23 03:55:48 +02:00
extern bool g_is_ble_connected; //Link to log in BLE
extern bool g_is_tag_emulating; //The status of the logo simulation card
2022-12-22 12:43:05 +08:00
2023-08-26 04:42:40 +02:00
/** @brief Equipment sleep timer event
2023-08-23 03:55:48 +02:00
* @param none
* @return none
2022-12-22 12:43:05 +08:00
*/
2023-08-23 00:22:22 +02:00
static void timer_sleep_event_handle(void *arg) {
2023-08-26 04:42:40 +02:00
// The sleep conditions are achieved, set the logo bit, so that the processing in the main
2022-12-22 12:43:05 +08:00
m_system_off_enter = true;
}
2023-08-23 03:55:48 +02:00
/**@brief Sleep soft timer initialization
2022-12-22 12:43:05 +08:00
*/
void sleep_timer_init(void) {
ret_code_t err_code;
2023-08-23 00:18:10 +02:00
2023-08-23 03:55:48 +02:00
// Create a soft timer and wait for a period of time to sleep
2022-12-22 12:43:05 +08:00
err_code = app_timer_create(&m_app_sleep_timer, APP_TIMER_MODE_SINGLE_SHOT, timer_sleep_event_handle);
APP_ERROR_CHECK(err_code);
}
2023-08-23 03:55:48 +02:00
/**@brief Sleeping soft timer stop
2022-12-22 12:43:05 +08:00
*/
void sleep_timer_stop() {
m_system_off_enter = false;
ret_code_t err_code = app_timer_stop(m_app_sleep_timer);
APP_ERROR_CHECK(err_code);
}
2023-08-23 03:55:48 +02:00
/**@brief Sleep soft timer startup
2022-12-22 12:43:05 +08:00
*/
void sleep_timer_start(uint32_t time_ms) {
2023-08-26 04:42:40 +02:00
// Close the previous sleep timer first
2022-12-22 12:43:05 +08:00
sleep_timer_stop();
2023-08-23 03:55:48 +02:00
// Non -USB power supply status
2022-12-22 12:43:05 +08:00
if (nrfx_power_usbstatus_get() == NRFX_POWER_USB_STATE_DISCONNECTED) {
2023-08-26 04:42:40 +02:00
// If Bluetooth is still connected, or is still in the state of simulation card, you don't need to start sleep
2022-12-22 12:43:05 +08:00
if (g_is_ble_connected == false && g_is_tag_emulating == false) {
2023-08-23 03:55:48 +02:00
// Start the timer
2022-12-22 12:43:05 +08:00
ret_code_t err_code = app_timer_start(m_app_sleep_timer, APP_TIMER_TICKS(time_ms), NULL);
APP_ERROR_CHECK(err_code);
}
}
}
/**
2023-08-26 04:42:40 +02:00
*@brief Specific implementation of sleep
*@param sysOff: The system off function implement.
2022-12-22 12:43:05 +08:00
*/
void sleep_system_run(void (*sysOffSleep)(), void (*sysOnSleep)()) {
// No task to process, sleep enter
if (m_system_off_enter) {
2023-05-30 11:45:54 +08:00
m_system_off_enter = false;
2022-12-22 12:43:05 +08:00
// Enter Sleep(System_OFF sleep mode) zzzzz.....
sysOffSleep();
} else {
// Enter sleep(System_ON sleep mode) zzzzz.....
// If no task can to process, we can sleep cpu sometime.
sysOnSleep();
}
}