2024-12-09 11:41:44 +08:00
|
|
|
#include "bsp_key.h"
|
|
|
|
|
2025-02-22 17:22:49 +08:00
|
|
|
// #include "bsp_uart.h"
|
2024-12-14 13:31:23 +08:00
|
|
|
#include "log.h"
|
2024-12-09 11:41:44 +08:00
|
|
|
|
2024-12-14 16:00:58 +08:00
|
|
|
#include "SLEEP.h"
|
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
// https://www.cnblogs.com/iot-fan/p/14304943.html
|
2024-12-09 11:41:44 +08:00
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
#undef LOG_ENABLE
|
|
|
|
#define LOG_ENABLE 1
|
2024-12-09 11:41:44 +08:00
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
#undef LOG_TAG
|
|
|
|
#define LOG_TAG "key"
|
|
|
|
|
|
|
|
tmosTaskID key_task_id = INVALID_TASK_ID;
|
|
|
|
|
2024-12-14 16:00:58 +08:00
|
|
|
volatile uint8_t key_wakeup_flag = 0;
|
|
|
|
volatile uint8_t key_timeout_flag = 0;
|
2024-12-14 13:31:23 +08:00
|
|
|
|
2024-12-14 16:53:49 +08:00
|
|
|
static app_task_evt_handler_t p_handler = NULL;
|
|
|
|
|
|
|
|
static volatile uint8_t key_timeout_cnt = 0;
|
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
static void KEY_Task_ProcessTmosMsg(tmos_event_hdr_t *pMsg)
|
|
|
|
{
|
|
|
|
switch (pMsg->event)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
logDebug("pMsg->event %04x", pMsg->event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-14 16:53:49 +08:00
|
|
|
#if 1
|
2024-12-14 13:31:23 +08:00
|
|
|
static uint16_t KEY_Task_ProcessEvent(uint8_t task_id, uint16_t events)
|
|
|
|
{
|
|
|
|
if (events & SYS_EVENT_MSG)
|
|
|
|
{
|
|
|
|
uint8_t *pMsg;
|
|
|
|
if ((pMsg = tmos_msg_receive(key_task_id)) != NULL)
|
|
|
|
{
|
|
|
|
KEY_Task_ProcessTmosMsg((tmos_event_hdr_t *)pMsg);
|
|
|
|
// Release the TMOS message
|
|
|
|
tmos_msg_deallocate(pMsg);
|
|
|
|
}
|
|
|
|
// return unprocessed events
|
|
|
|
return (events ^ SYS_EVENT_MSG);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events & KEY_SCAN_EVT)
|
|
|
|
{
|
|
|
|
static volatile uint8_t key_vaild_times = 0;
|
|
|
|
static volatile bool key_vaild_for_long_press = false;
|
|
|
|
if (IS_KEY_Vaild())
|
|
|
|
{
|
2024-12-14 17:56:39 +08:00
|
|
|
if (key_vaild_times > (KEY_LONG_PRESS_MS / KEY_SACN_MS))
|
2024-12-14 13:31:23 +08:00
|
|
|
{ //> 20ms*100=2000ms
|
|
|
|
if (false == key_vaild_for_long_press)
|
|
|
|
{
|
|
|
|
if (NULL != p_handler)
|
|
|
|
{
|
|
|
|
p_handler(kKeyLong);
|
|
|
|
}
|
|
|
|
// PRINT("WE should power switch here\r\n");
|
|
|
|
key_vaild_for_long_press = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key_vaild_times++;
|
|
|
|
}
|
2024-12-17 14:51:12 +08:00
|
|
|
key_timeout_cnt = KEY_TIMOUT_MS / KEY_SACN_MS;
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // button release
|
|
|
|
if (key_vaild_times)
|
|
|
|
{
|
2024-12-14 17:56:39 +08:00
|
|
|
if ((key_vaild_times) < (KEY_LONG_PRESS_MS / KEY_SACN_MS))
|
2024-12-14 13:31:23 +08:00
|
|
|
{
|
|
|
|
p_handler(kKeyShort);
|
|
|
|
}
|
|
|
|
key_vaild_times = 0;
|
|
|
|
// PRINT("KEY VAILED\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
key_vaild_for_long_press = false;
|
|
|
|
}
|
|
|
|
if (key_timeout_cnt)
|
|
|
|
{
|
|
|
|
key_timeout_cnt--;
|
2024-12-17 14:51:12 +08:00
|
|
|
// logDebug("key_timeout_cnt %d", key_timeout_cnt);
|
2024-12-14 17:56:39 +08:00
|
|
|
tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(KEY_SACN_MS)); // 40ms
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p_handler(kKeyRelease);
|
|
|
|
}
|
|
|
|
return (events ^ KEY_SCAN_EVT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard unknown events
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
__HIGH_CODE
|
2024-12-14 13:31:23 +08:00
|
|
|
// 由按键中断唤醒后开启按键扫描
|
|
|
|
void BSP_KEY_EnterLowpower(void)
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
#if 1
|
2024-12-14 13:31:23 +08:00
|
|
|
// TODO:按键电平触发设置
|
|
|
|
// 下降沿触发
|
|
|
|
GPIOB_ITModeCfg(KEY_B_PIN, GPIO_ITMode_FallEdge);
|
|
|
|
|
|
|
|
// 开启GPIO的睡眠唤醒,如果需要的话
|
|
|
|
// PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
|
2025-02-22 17:22:49 +08:00
|
|
|
PWR_PeriphWakeUpCfg(ENABLE, RB_GPIO_WAKE_MODE | RB_SLP_GPIO_WAKE, Long_Delay);
|
2024-12-14 13:31:23 +08:00
|
|
|
|
|
|
|
// 开启GPIOB中断
|
|
|
|
PFIC_EnableIRQ(GPIO_B_IRQn);
|
2024-12-14 16:00:58 +08:00
|
|
|
|
|
|
|
BSP_RequestSleep();
|
2025-02-21 14:38:41 +08:00
|
|
|
#endif
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
__HIGH_CODE
|
2024-12-14 13:31:23 +08:00
|
|
|
void BSP_KEY_ExitLowpower(void)
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
#if 1
|
2024-12-14 16:00:58 +08:00
|
|
|
BSP_BlockSleep();
|
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
// 关闭GPIOB中断
|
|
|
|
PFIC_DisableIRQ(GPIO_B_IRQn);
|
2025-02-22 17:22:49 +08:00
|
|
|
PWR_PeriphWakeUpCfg(DISABLE, RB_GPIO_WAKE_MODE | RB_SLP_GPIO_WAKE, Long_Delay);
|
|
|
|
// PWR_PeriphWakeUpCfg(DISABLE, RB_SLP_GPIO_WAKE, Long_Delay);
|
2025-02-21 14:38:41 +08:00
|
|
|
|
|
|
|
#endif
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-14 16:53:49 +08:00
|
|
|
#if 0
|
2024-12-14 16:26:27 +08:00
|
|
|
uint8_t read_button_GPIO(uint8_t button_id)
|
2024-12-14 13:31:23 +08:00
|
|
|
{
|
2024-12-14 16:26:27 +08:00
|
|
|
// you can share the GPIO read function with multiple Buttons
|
|
|
|
switch(button_id)
|
|
|
|
{
|
|
|
|
case btn1_id:
|
|
|
|
return (uint8_t)GPIOB_ReadPortPin(KEY_B_PIN);
|
|
|
|
default:
|
|
|
|
// logAssert(0, while (1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2024-12-14 13:31:23 +08:00
|
|
|
|
2024-12-14 16:26:27 +08:00
|
|
|
void BTN1_PRESS_DOWN_Handler(void* btn)
|
|
|
|
{
|
|
|
|
logDebug("BTN1_PRESS_DOWN_Handler");
|
|
|
|
// BSP_KEY_EnterLowpower();
|
|
|
|
}
|
2024-12-14 13:31:23 +08:00
|
|
|
|
2024-12-14 16:26:27 +08:00
|
|
|
void BTN1_PRESS_UP_Handler(void* btn)
|
|
|
|
{
|
|
|
|
logDebug("BTN1_PRESS_UP_Handler");
|
|
|
|
tmos_stop_task(key_task_id, KEY_IDLE_TIMEOUT_EVT);
|
|
|
|
tmos_start_task(key_task_id, KEY_IDLE_TIMEOUT_EVT, MS1_TO_SYSTEM_TIME(1000 * 5));
|
|
|
|
logDebug("tmos_start_task KEY_IDLE_TIMEOUT_EVT");
|
|
|
|
// BSP_KEY_EnterLowpower();
|
|
|
|
}
|
2024-12-14 13:31:23 +08:00
|
|
|
|
2024-12-14 16:26:27 +08:00
|
|
|
void BTN1_SINGLE_Click_Handler(void* btn)
|
|
|
|
{
|
|
|
|
logDebug("BTN1_SINGLE_Click_Handler");
|
|
|
|
// BSP_KEY_EnterLowpower();
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-14 16:26:27 +08:00
|
|
|
void BTN1_DOUBLE_Click_Handler(void* btn)
|
2024-12-14 13:31:23 +08:00
|
|
|
{
|
2024-12-14 16:26:27 +08:00
|
|
|
logDebug("BTN1_DOUBLE_Click_Handler");
|
|
|
|
// BSP_KEY_EnterLowpower();
|
|
|
|
}
|
2024-12-14 13:31:23 +08:00
|
|
|
|
2024-12-14 16:26:27 +08:00
|
|
|
void BTN1_LONG_PRESS_START_Handler(void* btn)
|
|
|
|
{
|
|
|
|
logDebug("BTN1_LONG_PRESS_START_Handler");
|
|
|
|
// BSP_KEY_EnterLowpower();
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t KEY_Task_ProcessEvent(uint8_t task_id, uint16_t events)
|
|
|
|
{
|
|
|
|
if (events & SYS_EVENT_MSG)
|
|
|
|
{
|
|
|
|
uint8_t *pMsg;
|
|
|
|
if ((pMsg = tmos_msg_receive(key_task_id)) != NULL)
|
|
|
|
{
|
|
|
|
KEY_Task_ProcessTmosMsg((tmos_event_hdr_t *)pMsg);
|
|
|
|
// Release the TMOS message
|
|
|
|
tmos_msg_deallocate(pMsg);
|
|
|
|
}
|
|
|
|
// return unprocessed events
|
|
|
|
return (events ^ SYS_EVENT_MSG);
|
|
|
|
}
|
2024-12-14 16:00:58 +08:00
|
|
|
// if (events & KEY_WAKEUP_EVT)
|
|
|
|
// {
|
|
|
|
// logDebug("KEY_WAKEUP_EVT");
|
2024-12-14 13:31:23 +08:00
|
|
|
|
2024-12-14 16:00:58 +08:00
|
|
|
// BSP_KEY_ExitLowpower();
|
|
|
|
// return (events ^ KEY_WAKEUP_EVT);
|
|
|
|
// }
|
2024-12-14 13:31:23 +08:00
|
|
|
if (events & KEY_SCAN_EVT)
|
|
|
|
{
|
2024-12-14 16:26:27 +08:00
|
|
|
// 按键没抬起来就继续扫描
|
2024-12-14 16:00:58 +08:00
|
|
|
if (0 == key_timeout_flag)
|
|
|
|
{
|
2024-12-14 16:26:27 +08:00
|
|
|
tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(5));
|
2024-12-14 16:00:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logDebug("KEY_SCAN_EVT timeout");
|
|
|
|
BSP_KEY_EnterLowpower();
|
|
|
|
}
|
|
|
|
// logDebug("KEY_SCAN_EVT");
|
2024-12-14 16:26:27 +08:00
|
|
|
button_ticks();
|
2024-12-14 16:00:58 +08:00
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
return (events ^ KEY_SCAN_EVT);
|
|
|
|
}
|
2024-12-14 16:00:58 +08:00
|
|
|
if (events & KEY_IDLE_TIMEOUT_EVT)
|
|
|
|
{
|
2024-12-14 16:26:27 +08:00
|
|
|
tmos_stop_task(key_task_id, KEY_SCAN_EVT);
|
|
|
|
BSP_KEY_EnterLowpower();
|
2024-12-14 16:00:58 +08:00
|
|
|
logDebug("KEY_IDLE_TIMEOUT_EVT");
|
|
|
|
key_timeout_flag = 1;
|
|
|
|
return (events ^ KEY_IDLE_TIMEOUT_EVT);
|
|
|
|
}
|
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
|
|
|
|
// Discard unknown events
|
|
|
|
return 0;
|
|
|
|
}
|
2024-12-14 16:53:49 +08:00
|
|
|
#endif
|
2024-12-09 11:41:44 +08:00
|
|
|
|
2024-12-14 16:53:49 +08:00
|
|
|
void BSP_KEY_Init(app_task_evt_handler_t handler)
|
2024-12-09 11:41:44 +08:00
|
|
|
{
|
2024-12-14 16:53:49 +08:00
|
|
|
p_handler = handler;
|
|
|
|
key_task_id = TMOS_ProcessEventRegister(KEY_Task_ProcessEvent);
|
|
|
|
|
2025-02-22 17:22:49 +08:00
|
|
|
// // https://www.wch.cn/bbs/thread-98540-1.html
|
|
|
|
// R16_PIN_ALTERNATE |= RB_PIN_INTX;
|
|
|
|
|
2024-12-09 11:41:44 +08:00
|
|
|
// 由外部上拉电阻了
|
2024-12-14 13:31:23 +08:00
|
|
|
// 设置为浮空输入模式
|
2025-02-22 17:22:49 +08:00
|
|
|
|
|
|
|
GPIOB_ModeCfg(GPIO_Pin_16, GPIO_ModeIN_PU);
|
|
|
|
|
|
|
|
GPIOB_ModeCfg(GPIO_Pin_0, GPIO_ModeIN_Floating);
|
|
|
|
// GPIOB_ModeCfg(GPIO_Pin_1, GPIO_ModeIN_Floating);
|
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
#if 1
|
2024-12-14 16:00:58 +08:00
|
|
|
// 下降沿触发
|
2025-02-22 17:22:49 +08:00
|
|
|
GPIOB_ITModeCfg(GPIO_Pin_0, GPIO_ITMode_FallEdge);
|
|
|
|
// GPIOB_ITModeCfg(GPIO_Pin_1, GPIO_ITMode_FallEdge);
|
2024-12-14 16:00:58 +08:00
|
|
|
|
|
|
|
// 开启GPIO的睡眠唤醒,如果需要的话
|
|
|
|
// PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
|
2025-02-22 17:22:49 +08:00
|
|
|
PWR_PeriphWakeUpCfg(ENABLE, RB_GPIO_WAKE_MODE | RB_SLP_GPIO_WAKE, Long_Delay);
|
2024-12-14 16:00:58 +08:00
|
|
|
|
|
|
|
// 开启GPIOB中断
|
|
|
|
PFIC_EnableIRQ(GPIO_B_IRQn);
|
|
|
|
|
2024-12-14 17:56:39 +08:00
|
|
|
tmos_start_task(key_task_id, KEY_SCAN_EVT, MS1_TO_SYSTEM_TIME(KEY_SACN_MS));
|
2025-02-21 14:38:41 +08:00
|
|
|
#endif
|
2025-02-22 17:22:49 +08:00
|
|
|
DelayMs(2);
|
|
|
|
logDebug("BSP_KEY_Init=%d", IS_KEY_Vaild());
|
|
|
|
DelayMs(2);
|
|
|
|
logDebug("BSP_KEY_Init=%d", IS_KEY_Vaild());
|
2024-12-14 13:31:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-14 16:53:49 +08:00
|
|
|
__HIGH_CODE
|
|
|
|
__attribute__((noinline))
|
|
|
|
void KEY_ProcessLoop(void)
|
|
|
|
{
|
|
|
|
if (key_wakeup_flag)
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
// 阻止睡眠
|
|
|
|
BSP_BlockSleep();
|
|
|
|
// 关按键中断
|
|
|
|
BSP_KEY_ExitLowpower();
|
|
|
|
|
|
|
|
// PRINT("KEY IRQ\n");
|
|
|
|
|
|
|
|
// GPIOB_ClearITFlagBit(KEY_B_PIN);
|
|
|
|
|
2024-12-14 16:53:49 +08:00
|
|
|
key_wakeup_flag = 0;
|
|
|
|
tmos_set_event(key_task_id, KEY_SCAN_EVT);
|
|
|
|
logDebug("KEY_ProcessLoop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-14 13:31:23 +08:00
|
|
|
__INTERRUPT // 告诉编译器使用硬件压栈
|
2024-12-14 16:00:58 +08:00
|
|
|
__HIGH_CODE // 放到RAM里
|
2024-12-14 13:31:23 +08:00
|
|
|
void GPIOB_IRQHandler(void)
|
|
|
|
{
|
2025-02-21 14:38:41 +08:00
|
|
|
// // 阻止睡眠
|
|
|
|
// BSP_BlockSleep();
|
|
|
|
// // 关按键中断
|
|
|
|
// BSP_KEY_ExitLowpower();
|
2024-12-14 16:00:58 +08:00
|
|
|
|
2025-02-21 14:38:41 +08:00
|
|
|
// PRINT("KEY IRQ\n");
|
2024-12-14 16:00:58 +08:00
|
|
|
|
2025-02-22 17:22:49 +08:00
|
|
|
// GPIOB_ClearITFlagBit(KEY_B_PIN);
|
|
|
|
GPIOB_ClearITFlagBit(GPIO_Pin_0);
|
|
|
|
// GPIOB_ClearITFlagBit(GPIO_Pin_1);
|
2024-12-14 16:00:58 +08:00
|
|
|
|
2024-12-17 14:51:12 +08:00
|
|
|
// tmos_set_event(key_task_id, KEY_SCAN_EVT);
|
|
|
|
// logDebug("KEY_ProcessLoop");
|
2024-12-14 16:00:58 +08:00
|
|
|
|
2024-12-17 14:51:12 +08:00
|
|
|
key_wakeup_flag = 1;
|
2024-12-09 11:41:44 +08:00
|
|
|
}
|