CELIS/APPLICATION/Sleep/Sleep.c

373 lines
7.5 KiB
C
Raw Normal View History

/*
* @Description:
* @Version:
* @Author: Arnold
* @Date: 2023-11-24 18:49:35
* @LastEditTime: 2023-11-24 18:49:37
*/
#include "Sleep.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x.h"
#include <stdint.h>
#include "stm32f10x_rtc.h"
//#include "ADC_WAKE.h"
#include "stm32f10x_wwdg.h"
#include "KC.h"
#include "stm32f10x_bkp.h"
#include "stm32f10x_rcc.h"
void HSI_SystemClock_Config(void);
void Predormancy_treatment(void);//Ԥ<><D4A4><EFBFBD>ߴ<EFBFBD><DFB4><EFBFBD>
static void EXIT_Configuration_plus(FunctionalState val);
enum MCU_Sleep_State MCU_PowerState = ActiveState;
void Wake_up_processing(void);
void MCU_Sleep_Fun(void);
//<2F><><EFBFBD><EFBFBD>true<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>false<73><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧЧ
bool ActiveState_Scan()
{
uint8_t stateA,stateAcc;
stateA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8);
stateAcc = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9);
if( stateA==0 || stateAcc==0)//<2F><><EFBFBD>߼<EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
//RTC<54><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
return true;
}
return false;
}
uint8_t standby_condition_Scan()
{
static uint32_t SC_State = 0;
if( ActiveState_Scan() == true)
{
SC_State = 0;
return 1;
}
SC_State = RTC_GetCounter();
printf("Sleep Idle count %d MAX is %d\r\n",SC_State,MAX_Scount);
// SC_State = (SC_State+1)%MAX_Scount;
if(SC_State == MAX_Scount)
{
return 2;
}
return 0;
}
void PwrManageWorkPlus()
{
switch (MCU_PowerState)
{
case ActiveState:
MCU_PowerState = (ActiveState_Scan() == true) ? ActiveState:standby_condition;
break;
case standby_condition:
{
uint8_t data = standby_condition_Scan();
if(data == 0)
{
MCU_PowerState = standby_condition;
}
else if (data == 1)
{
MCU_PowerState = ActiveState;
}else if (data == 2)
{
MCU_PowerState = Go_to_sleep;
}
}
break;
case Go_to_sleep:
MCU_Sleep_Fun();
break;
default:
printf("ERR Sleep\r\n");
break;
}
}
static __INLINE void ClearAllInterruptFlags(void)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ
for (int i = 0; i < 8; i++)
{
NVIC->ICER[i] = 0xFFFFFFFF; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
}
}
void MCU_Sleep_Fun(void)
{
printf("Go to Sleep\r\n");
WWDG_DeInit();
Predormancy_treatment();//<2F><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
// __WFE();
// PWR_EnterSTANDBYMode();//<2F><><EFBFBD><EFBFBD>ģʽ
// __WFI(); //WFIָ<49><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˯<EFBFBD><CBAF>
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, DISABLE); //ʹ<><CAB9>USART4ʱ<34><CAB1>
// PWR_EnterSTANDBYMode();
Wake_up_processing();//<2F><><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD>
}
void Predormancy_treatment(void)
{
// GPIO_SetBits(GPIOB,GPIO_Pin_9);
//
//
// EXIT_Configuration_plus(ENABLE);
// NVIC_DeInit();
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
// NVIC_DisableIRQ(SysTick_IRQn);
// NVIC_DisableIRQ(WWDG_IRQn);
// NVIC_DisableIRQ(PVD_IRQn);
NVIC_DisableIRQ(TAMPER_IRQn);
NVIC_DisableIRQ(RTC_IRQn);
NVIC_DisableIRQ(FLASH_IRQn);
NVIC_DisableIRQ(RCC_IRQn);
NVIC_DisableIRQ(EXTI0_IRQn);
NVIC_DisableIRQ(EXTI1_IRQn);
NVIC_DisableIRQ(EXTI2_IRQn);
NVIC_DisableIRQ(EXTI3_IRQn);
NVIC_DisableIRQ(EXTI4_IRQn);
NVIC_DisableIRQ(DMA1_Channel1_IRQn);
NVIC_DisableIRQ(DMA1_Channel2_IRQn);
NVIC_DisableIRQ(DMA1_Channel3_IRQn);
NVIC_DisableIRQ(DMA1_Channel4_IRQn);
NVIC_DisableIRQ(DMA1_Channel5_IRQn);
NVIC_DisableIRQ(DMA1_Channel6_IRQn);
NVIC_DisableIRQ(DMA1_Channel7_IRQn);
NVIC_DisableIRQ(CAN1_TX_IRQn);
NVIC_DisableIRQ(CAN1_RX0_IRQn);
NVIC_DisableIRQ(CAN1_RX1_IRQn);
NVIC_DisableIRQ(CAN1_SCE_IRQn);
NVIC_DisableIRQ(EXTI9_5_IRQn);
NVIC_DisableIRQ(TIM1_BRK_IRQn);
NVIC_DisableIRQ(TIM1_UP_IRQn);
NVIC_DisableIRQ(TIM1_TRG_COM_IRQn);
NVIC_DisableIRQ(TIM1_CC_IRQn);
NVIC_DisableIRQ(TIM2_IRQn);
NVIC_DisableIRQ(TIM3_IRQn);
NVIC_DisableIRQ(TIM4_IRQn);
NVIC_DisableIRQ(I2C1_EV_IRQn);
NVIC_DisableIRQ(I2C1_ER_IRQn);
NVIC_DisableIRQ(I2C2_EV_IRQn);
NVIC_DisableIRQ(I2C2_ER_IRQn);
NVIC_DisableIRQ(SPI1_IRQn);
NVIC_DisableIRQ(SPI2_IRQn);
NVIC_DisableIRQ(USART1_IRQn);
NVIC_DisableIRQ(USART2_IRQn);
NVIC_DisableIRQ(USART3_IRQn);
IWDG_Feed();
NVIC_DisableIRQ(EXTI15_10_IRQn);
NVIC_DisableIRQ(RTCAlarm_IRQn);
NVIC_DisableIRQ(OTG_FS_WKUP_IRQn);
NVIC_DisableIRQ(TIM5_IRQn);
NVIC_DisableIRQ(SPI3_IRQn);
NVIC_DisableIRQ(UART4_IRQn);
NVIC_DisableIRQ(UART5_IRQn);
NVIC_DisableIRQ(TIM6_IRQn);
NVIC_DisableIRQ(TIM7_IRQn);
NVIC_DisableIRQ(DMA2_Channel1_IRQn);
NVIC_DisableIRQ(DMA2_Channel2_IRQn);
NVIC_DisableIRQ(DMA2_Channel3_IRQn);
NVIC_DisableIRQ(DMA2_Channel4_IRQn);
NVIC_DisableIRQ(DMA2_Channel5_IRQn);
NVIC_DisableIRQ(ETH_IRQn);
NVIC_DisableIRQ(ETH_WKUP_IRQn);
NVIC_DisableIRQ(CAN2_TX_IRQn);
NVIC_DisableIRQ(CAN2_RX0_IRQn);
NVIC_DisableIRQ(CAN2_RX1_IRQn);
NVIC_DisableIRQ(CAN2_SCE_IRQn);
NVIC_DisableIRQ(OTG_FS_IRQn);
ClearAllInterruptFlags();
PWR_ClearFlag(PWR_FLAG_WU);
IWDG_Feed();
KC_Set_GPIO(&GetDEVICE_Name(PW3_3V));
HSI_SystemClock_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, DISABLE); //ʹ<><CAB9>GPIODʱ<44><CAB1>
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE); //ʹ<><CAB9>USART2ʱ<32><CAB1>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, DISABLE); //ʹ<><CAB9>GPIODʱ<44><CAB1>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, DISABLE); //ʹ<><CAB9>GPIODʱ<44><CAB1>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, DISABLE); //ʹ<><CAB9>GPIODʱ<44><CAB1>
IWDG_Feed();
// Init_ADC_WK();//ADC<44><43><EFBFBD>Ѳ<EFBFBD><D1B2><EFBFBD>
}
void Wake_up_processing(void)
{
SystemInit();
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000);
SysTick_Init();
NVIC_Configuration();
IWDG_Init();
IWDG_Feed();
uart4_init(115200);
printf("IO wale up\r\n");
IWDG_Feed();
NVIC_SystemReset();
while(1);
}
static void EXIT_Configuration_plus(FunctionalState val)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = val;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;////;
EXTI_InitStructure.EXTI_LineCmd = val;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line0);
}
void HSI_SystemClock_Config(void)
{
// <20><><EFBFBD><EFBFBD>HSIʱ<49><CAB1>
RCC->CR |= RCC_CR_HSION;
while ((RCC->CR & RCC_CR_HSIRDY) == 0)
{
// <20>ȴ<EFBFBD>HSIʱ<49>Ӿ<EFBFBD><D3BE><EFBFBD>
}
// ѡ<><D1A1>HSI<53><49>Ϊϵͳʱ<CDB3><CAB1>
RCC->CFGR &= ~RCC_CFGR_SW;
RCC->CFGR |= RCC_CFGR_SW_HSI;
// <20>ȴ<EFBFBD>ϵͳʱ<CDB3><CAB1><EFBFBD>ȶ<EFBFBD>
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
{
// <20>ȴ<EFBFBD>
}
// <20>ر<EFBFBD>HSEʱ<45><CAB1>
RCC->CR &= ~RCC_CR_HSEON;
}
void RTC_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // <20><> RCC_RTCCLKSource_LSE
RCC_RTCCLKCmd(ENABLE);
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Backup <20><><EFBFBD><EFBFBD> */
PWR_BackupAccessCmd(ENABLE);
/* <20><>λ Backup <20><><EFBFBD><EFBFBD> */
BKP_DeInit();
/* ʹ<><CAB9> LSI */
RCC_LSICmd(ENABLE);
/* <20>ȴ<EFBFBD> LSI ׼<><D7BC><EFBFBD><EFBFBD> */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* ѡ<><D1A1> LSI <20><>Ϊ RTC ʱ<><CAB1>Դ */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* ʹ<><CAB9> RTC ʱ<><CAB1> */
RCC_RTCCLKCmd(ENABLE);
/* <20>ȴ<EFBFBD> RTC <20>Ĵ<EFBFBD><C4B4><EFBFBD> ͬ<><CDAC>
* <EFBFBD><EFBFBD>ΪRTCʱ<EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><EFBFBD>ٵģ<EFBFBD><EFBFBD>ڻ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD><EFBFBD>ٵģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫͬ<EFBFBD><EFBFBD>
*/
RTC_WaitForSynchro();
/* ȷ<><C8B7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> RTC <20>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
RTC_WaitForLastTask();
/* <20><><EFBFBD><EFBFBD> RTC <20><>Ƶ: ʹ RTC <20><><EFBFBD><EFBFBD>Ϊ1s ,LSIԼΪ40KHz */
/* RTC period = RTCCLK/RTC_PR = (40 KHz)/(40000-1+1) = 1HZ */
RTC_SetPrescaler(40000-1);
/* ȷ<><C8B7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> RTC <20>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
RTC_WaitForLastTask();
}