[beken-72xx] Implement deep sleep (#140)
* Initial support code for Deep Sleep * Global functions * Remove unnecessary override * clang-format * Support for multiple pins * Fix math * Add a way to unset GPIOs * Clang format * Update brief --------- Co-authored-by: Kuba Szczodrzyński <kuba@szczodrzynski.pl>
This commit is contained in:
40
cores/beken-72xx/base/api/lt_sleep.c
Normal file
40
cores/beken-72xx/base/api/lt_sleep.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/* Copyright (c) Peter Sarkozi 2023-06-17. */
|
||||
|
||||
#include <libretiny.h>
|
||||
#include <sdk_private.h>
|
||||
|
||||
static PS_DEEP_CTRL_PARAM deep_sleep_param;
|
||||
|
||||
void lt_deep_sleep_config_gpio(uint32_t gpio_index_map, bool on_high) {
|
||||
deep_sleep_param.wake_up_way |= PS_DEEP_WAKEUP_GPIO;
|
||||
deep_sleep_param.gpio_index_map |= gpio_index_map;
|
||||
if (on_high) {
|
||||
deep_sleep_param.gpio_edge_map &= (~gpio_index_map);
|
||||
} else {
|
||||
deep_sleep_param.gpio_edge_map |= gpio_index_map;
|
||||
}
|
||||
}
|
||||
|
||||
void lt_deep_sleep_unset_gpio(uint32_t gpio_index_map) {
|
||||
deep_sleep_param.gpio_index_map &= (~gpio_index_map);
|
||||
}
|
||||
|
||||
void lt_deep_sleep_config_timer(uint32_t sleep_duration) {
|
||||
deep_sleep_param.wake_up_way |= PS_DEEP_WAKEUP_RTC;
|
||||
uint64_t duration_math = 32768 * sleep_duration;
|
||||
if (duration_math / 1000 > 0xFFFFFFFF) {
|
||||
// Sleep forever
|
||||
deep_sleep_param.sleep_time = 0xFFFFFFFF;
|
||||
} else {
|
||||
deep_sleep_param.sleep_time = (duration_math / 1000) & 0xFFFFFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
void lt_deep_sleep_enter() {
|
||||
bk_misc_update_set_type(RESET_SOURCE_DEEPPS_GPIO);
|
||||
GLOBAL_INT_DECLARATION();
|
||||
GLOBAL_INT_DISABLE();
|
||||
sctrl_enter_rtos_deep_sleep((PS_DEEP_CTRL_PARAM *)&deep_sleep_param);
|
||||
ps_delay(500);
|
||||
GLOBAL_INT_RESTORE();
|
||||
}
|
||||
@@ -18,6 +18,8 @@ uint32_t wdt_ctrl(uint32_t cmd, void *param);
|
||||
void bk_send_byte(uint8_t uport, uint8_t data);
|
||||
void uart_hw_set_change(uint8_t uport, bk_uart_config_t *uart_config);
|
||||
int uart_rx_callback_set(int uport, uart_callback callback, void *param);
|
||||
void sctrl_enter_rtos_deep_sleep(PS_DEEP_CTRL_PARAM *deep_param);
|
||||
void ps_delay(volatile UINT16 times);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
|
||||
@@ -14,6 +14,7 @@ extern "C" {
|
||||
#include <bk_timer_pub.h>
|
||||
#include <flash_pub.h>
|
||||
#include <gpio_pub.h>
|
||||
#include <manual_ps_pub.h>
|
||||
#include <param_config.h>
|
||||
#include <pwm_pub.h>
|
||||
#include <rtos_pub.h>
|
||||
|
||||
11
cores/common/base/api/lt_sleep.c
Normal file
11
cores/common/base/api/lt_sleep.c
Normal file
@@ -0,0 +1,11 @@
|
||||
/* Copyright (c) Peter Sarkozi 2023-06-17. */
|
||||
|
||||
#include "lt_sleep.h"
|
||||
|
||||
__attribute__((weak)) void lt_deep_sleep_config_gpio(uint32_t gpio_index_map, bool on_high);
|
||||
|
||||
__attribute__((weak)) void lt_deep_sleep_unset_gpio(uint32_t gpio_index_map);
|
||||
|
||||
__attribute__((weak)) void lt_deep_sleep_config_timer(uint32_t sleep_duration);
|
||||
|
||||
__attribute__((weak)) void lt_deep_sleep_enter();
|
||||
31
cores/common/base/api/lt_sleep.h
Normal file
31
cores/common/base/api/lt_sleep.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/* Copyright (c) Peter Sarkozi 2023-06-17. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libretiny.h>
|
||||
|
||||
/**
|
||||
* @brief Enable GPIO Wakeup from Deep Sleep.
|
||||
*
|
||||
* @param gpio_index_map bitMap of the pins we should wake up on
|
||||
* @param on_high whether to wake up on High or Low state
|
||||
*/
|
||||
void lt_deep_sleep_config_gpio(uint32_t gpio_index_map, bool on_high);
|
||||
|
||||
/**
|
||||
* @brief Disable GPIO Wakeup on given pins
|
||||
*
|
||||
* @param gpio_index_map bitMap of the pins we should disable wake up on
|
||||
*/
|
||||
void lt_deep_sleep_unset_gpio(uint32_t gpio_index_map);
|
||||
|
||||
/**
|
||||
* @brief Set a sleep timer to wake up the device
|
||||
* @param sleep_duration the time in seconds to sleep
|
||||
*/
|
||||
void lt_deep_sleep_config_timer(uint32_t sleep_duration);
|
||||
|
||||
/**
|
||||
* @brief Start deep sleep
|
||||
*/
|
||||
void lt_deep_sleep_enter();
|
||||
@@ -16,6 +16,7 @@ extern "C" {
|
||||
#include "api/lt_init.h"
|
||||
#include "api/lt_mem.h"
|
||||
#include "api/lt_ota.h"
|
||||
#include "api/lt_sleep.h"
|
||||
#include "api/lt_utils.h"
|
||||
#include "api/lt_wdt.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user