uint16_t SleepTime = 0;
_Bool b_StartSleep = 0;
void Gpio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
__SYSCTRL_GPIOB_CLK_ENABLE();
PB04_AFx_GPIO();
GPIO_InitStructure.Pins = GPIO_PIN_4;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP; //SDA/SCL设置为下拉
GPIO_InitStructure.IT = GPIO_IT_RISING; //SDA/SCL上升沿唤醒,表示设备开机,总线从低电平跳转到高电平(I2C中断不能唤醒深度睡眠)
GPIO_Init(CW_GPIOB,&GPIO_InitStructure);
//清除中断标志并使能NVIC
GPIOB_INTFLAG_CLR(GPIOx_ICR_PIN4_Msk);
}
void SleepOff(void)
{
SYSCTRL_HSI_Enable(SYSCTRL_HSIOSC_DIV12); //睡眠前,时钟源要降低到不大于4M
BTIM_Cmd(CW_BTIM1, DISABLE);
// //打开FLASH时钟
// REGBITS_SET( CW_SYSCTRL->AHBEN, SYSCTRL_AHBEN_FLASH_Msk);
// REGBITS_SET(CW_FLASH->CR1, FLASH_CR1_STANDBY_Msk); //打开FLASH低功耗使能控制
// //关闭FLASH时钟
// REGBITS_CLR( CW_SYSCTRL->AHBEN, SYSCTRL_AHBEN_FLASH_Msk);
// __RCC_I2C_CLK_DISABLE(); //关闭不需要的时钟(关不关功耗都一样的)
// __RCC_GPIOA_CLK_DISABLE();
// __RCC_BTIM_CLK_DISABLE();
PA00_SETHIGH(); //关闭SDQ
PB03_SETLOW(); //关闭IO口输出,66断电(降低功耗)
PB04_SETLOW();
SCL_OutLow; //关闭和66通信的I2C主机
SDA_OutLow;
// Gpio_Init(); //IO口中断配置
IIC_Slave_Sleep_Entry(1, GPIO_MODE_INPUT_PULLUP, GPIO_IT_RISING | GPIO_IT_FALLING);
}
void SleepOpen(void)
{
FLASH_SetLatency(FLASH_Latency_2); //唤醒后,时钟恢复到初始化状态
SYSCTRL_HSI_Enable(SYSCTRL_HSIOSC_DIV1);
// __RCC_GPIOA_CLK_ENABLE();
// __RCC_BTIM_CLK_ENABLE();
IIC_Slave_WakeUp_Handle();
I2c_Init();
BTIM_Cmd(CW_BTIM1, ENABLE);
Power_Init();
}
void GoToSleep(void)
{
if (Get_IIC_Slave_RxTxFlag())
{
Set_IIC_Slave_RxTxFlag(0);
SleepTime = 0;
}
if(((SDA_READ == GPIO_Pin_RESET)&&(SCK_READ == GPIO_Pin_RESET))||((SDA_READ == GPIO_Pin_SET)&&(SCK_READ == GPIO_Pin_SET)))
{
if(SleepTime == 0)
{
SleepTime = 1;
}
}
if(SleepTime > 0)
{
SleepTime++;
}
if(SleepTime > 2500)
{
SleepTime = 0;
SleepOff(); //睡眠前关闭外设功能
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; //设置深度睡眠模式
__WFI(); //进入睡眠
SleepOpen(); //睡眠唤醒后打开外设功能
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_50707044/article/details/141261160
|