HAL.h增加:
#define LED_BLINK_EVENT 0x0001
#define HAL_KEY_EVENT 0x0002
#define HAL_REG_INIT_EVENT 0x2000
#define HAL_TEST_EVENT 0x4000
#define HAL_SLEEP_EVENT 0x4003
#define HAL_ADV_START_EVENT 0x4004
MCU.c增加:
if(events & HAL_TEST_EVENT)
{
tmos_start_task(halTaskID, HAL_SLEEP_EVENT, MS1_TO_SYSTEM_TIME(2000)); // 模拟一定时间后sleep休眠
uint8_t initial_advertising_enable = FALSE; //关广播
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
tmos_stop_task(halTaskID, HAL_REG_INIT_EVENT); //默认2分钟一次的校准先暂停
//为了更好掌握代码逻辑,建议所有TMOS事件在此暂停,唤醒后再安排事件。
return events ^ HAL_TEST_EVENT;
}
if(events & HAL_SLEEP_EVENT) //MCU在此事件中休眠和唤醒,唤醒后立即校准RF,再开蓝牙广播
{
sys_safe_access_enable();
R8_CK32K_CONFIG &= ~(RB_CLK_INT32K_PON | RB_CLK_XT32K_PON); //关闭32K晶振电源,更省电
sys_safe_access_disable();
LowPower_Sleep(RB_PWR_RAM30K | RB_PWR_RAM2K); //保留24+2K的SRAM的供电
sys_safe_access_enable();
R8_CK32K_CONFIG |= RB_CLK_INT32K_PON; //32K电源打开,供TMOS系统使用
sys_safe_access_disable();
HSECFG_Current(HSE_RCur_100); // 降为额定电流(低功耗函数中提升了HSE偏置电流)
DelayMs(5); //这里建议给一定的延时,延时足够到下方的打印能正常就好
PRINT("wake.. \n");
tmos_set_event(halTaskID, HAL_REG_INIT_EVENT); //先校准RF,再开广播
tmos_start_task(halTaskID, HAL_ADV_START_EVENT, 80);
return events ^ HAL_SLEEP_EVENT;
}
主程序:
int flag_gpio_int = 0;
__HIGH_CODE
__attribute__((noinline))
void Main_Circulation()
{
while(1)
{
{
TMOS_SystemProcess();
if(flag_gpio_int){ //根据GPIO中置的标志变量来安排事件
flag_gpio_int = 0;
tmos_start_task(halTaskID, HAL_TEST_EVENT, 160);
PRINT("main_flag\n");
}
}
}
}
/*********************************************************************
* @fn main
*
* @brief 主函数
*
* @return none
*/
int main(void)
{
GPIOA_ModeCfg(GPIO_Pin_14, GPIO_ModeIN_PU);
GPIOB_ITModeCfg(GPIO_Pin_14, GPIO_ITMode_FallEdge); // 下降沿唤醒
PFIC_EnableIRQ(GPIO_A_IRQn);
PWR_PeriphWakeUpCfg(ENABLE, RB_SLP_GPIO_WAKE, Long_Delay);
#if(defined(DCDC_ENABLE)) && (DCDC_ENABLE == TRUE)
PWR_DCDCCfg(ENABLE);
#endif
SetSysClock(CLK_SOURCE_PLL_60MHz);
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
GPIOA_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PU);
GPIOB_ModeCfg(GPIO_Pin_All, GPIO_ModeIN_PU);
#endif
#ifdef DEBUG
GPIOA_SetBits(bTXD1);
GPIOA_ModeCfg(bTXD1, GPIO_ModeOut_PP_5mA);
UART1_DefInit();
#endif
PRINT("%s\n", VER_LIB);
CH58X_BLEInit();
MyBsp_Init();
HAL_Init();
GAPRole_PeripheralInit();
HidDev_Init();
HidEmu_Init();
Main_Circulation();
}
__INTERRUPT
__HIGH_CODE
void GPIOA_IRQHandler(void)
{
static uint8_t int_num = 0; //按一下PB4休眠,再按一下唤醒
GPIOA_ClearITFlagBit(GPIO_Pin_14);
if(!int_num){
flag_gpio_int = 1; //置标志,主循环中查标志并安排事件
int_num = 1;
}
else {
int_num = 0;
}
}