b61c21c648
Signed-off-by: lidun <1084178170@qq.com>
466 lines
10 KiB
C
466 lines
10 KiB
C
/*
|
||
* @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"
|
||
#include "kC_Features.h"
|
||
#include "SEN_DCUMessage.h"
|
||
|
||
void HSI_SystemClock_Config(void);
|
||
|
||
void Predormancy_treatment(void);//预休眠处理
|
||
static void EXIT_Configuration_plus(FunctionalState val);
|
||
|
||
enum MCU_Sleep_State MCU_PowerState = ActiveState;
|
||
void Wake_up_processing(void);
|
||
|
||
void MCU_Sleep_Fun(void);
|
||
|
||
struct Sleep_parame Sleep_Parame;
|
||
|
||
//返回true代表激活条件存在,返回false代表激活条件失效
|
||
bool ActiveState_Scan()
|
||
{
|
||
// uint8_t stateA,stateAcc;
|
||
|
||
// stateA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8);
|
||
// stateAcc = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9);
|
||
// if(RTC_GetCounter()>500)
|
||
|
||
// if( stateA==0 || stateAcc==0)//休眠检测条件
|
||
// {
|
||
// //RTC计数清0
|
||
//
|
||
// return true;
|
||
// }
|
||
|
||
if(Sleep_Parame.CAN_WakeUp_Flag != 0)//休眠信号
|
||
{
|
||
Sleep_Parame.SaveTimerFlag = 0;
|
||
return true;
|
||
}
|
||
else if(Sleep_Parame.SaveTimerFlag == 0)//无信号保存当前RTC计数值
|
||
{
|
||
Sleep_Parame.SaveTimerFlag = 1;
|
||
Sleep_Parame.Before_Sleep_Value = RTC_GetCounter();
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
uint8_t standby_condition_Scan()
|
||
{
|
||
if( ActiveState_Scan() == true)
|
||
{
|
||
Sleep_Parame.SC_State = 0;
|
||
return 1;
|
||
}
|
||
Sleep_Parame.SC_State = RTC_GetCounter();
|
||
// printf("Sleep Idle count %d MAX is %d\r\n",SC_State,Sleep_Parame.Before_Sleep_Value+MAX_Scount);
|
||
// SC_State = (SC_State+1)%MAX_Scount;
|
||
|
||
// if(SysInfo.This_State == STATE_AC_Charge || Get_Sampl_Data.WorkMode() == DC_Charging)
|
||
// Sleep_Parame.Sleep_Time=Sleep_Parame.Before_Sleep_Value+MAX_Scount;
|
||
// else
|
||
Sleep_Parame.Sleep_Time=Sleep_Parame.Before_Sleep_Value+MAX_Scount;
|
||
|
||
if(Sleep_Parame.SC_State >= Sleep_Parame.Sleep_Time)//进入假休眠时间
|
||
{
|
||
Sleep_Parame.insertion_State=SysInfo.This_State == STATE_AC_Charge || Get_Sampl_Data.WorkMode() == DC_Charging?1:0;
|
||
return 2;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
enum FalseSleep_WakeUp Gun_State = INIT_STATE;
|
||
void Insert_Gun_WakeUp()
|
||
{
|
||
switch(Gun_State)
|
||
{
|
||
case INIT_STATE:
|
||
Gun_State=Sleep_Parame.insertion_State?Insert_Gun_INIT:No_Gun_INIT;
|
||
break;
|
||
case No_Gun_INIT:
|
||
Gun_State=SysInfo.This_State == STATE_AC_Charge || Get_Sampl_Data.WorkMode() == DC_Charging?Insert_Gun:No_Gun_INIT;
|
||
break;
|
||
case Insert_Gun_INIT:
|
||
Gun_State=SysInfo.This_State == STATE_AC_Charge || Get_Sampl_Data.WorkMode() == DC_Charging?Insert_Gun_INIT:NO_Gun;
|
||
break;
|
||
case NO_Gun:
|
||
Gun_State=SysInfo.This_State == STATE_AC_Charge || Get_Sampl_Data.WorkMode() == DC_Charging?Insert_Gun:NO_Gun;
|
||
break;
|
||
case Insert_Gun:
|
||
OPEN_CC1_CP_RELAY();
|
||
Sleep_Parame.SC_State = 0;
|
||
Sleep_Parame.False_sleep_State = 0;
|
||
Sleep_Parame.Before_Sleep_Value = RTC_GetCounter();
|
||
Gun_State = INIT_STATE;
|
||
MCU_PowerState = ActiveState;
|
||
break;
|
||
default:break;
|
||
};
|
||
}
|
||
|
||
extern u8 CAN2_Mode_Init(void);
|
||
void PwrManageWorkPlus()
|
||
{
|
||
static uint32_t Count = 0;
|
||
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)
|
||
{
|
||
// CLOSE_CC1_CP_RELAY();
|
||
CLOSE_CC2_CC_RELAY();
|
||
ParseDCU_State_314();
|
||
Delay_MS(500);
|
||
Sleep_Parame.False_sleep_State = 1;
|
||
MCU_PowerState = Go_to_FalseSleep;
|
||
}
|
||
}
|
||
break;
|
||
case Go_to_FalseSleep:
|
||
Count++;
|
||
if(Sleep_Parame.Bus_Packet_Free != 1 && Count>=5)
|
||
{
|
||
Count = 0;
|
||
MCU_PowerState = Go_to_sleep;
|
||
}
|
||
else if(Count ==1)
|
||
{
|
||
CAN2_Mode_Init();
|
||
}
|
||
|
||
Insert_Gun_WakeUp();//有插枪动作
|
||
if(Sleep_Parame.CAN_WakeUp_Flag != 0)//休眠信号
|
||
{
|
||
OPEN_CC1_CP_RELAY();
|
||
Sleep_Parame.SC_State = 0;
|
||
Sleep_Parame.False_sleep_State = 0;
|
||
MCU_PowerState = ActiveState;
|
||
}
|
||
break;
|
||
case Go_to_sleep:
|
||
// Sleep_Parame.False_sleep_State = 0;
|
||
MCU_Sleep_Fun();
|
||
break;
|
||
|
||
default:
|
||
printf("ERR Sleep\r\n");
|
||
break;
|
||
}
|
||
|
||
|
||
}
|
||
|
||
static __INLINE void ClearAllInterruptFlags(void)
|
||
{
|
||
// 遍历所有中断,并清除其标志位
|
||
for (int i = 0; i < 8; i++)
|
||
{
|
||
NVIC->ICER[i] = 0xFFFFFFFF; // 清除所有中断标志位
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
void MCU_Sleep_Fun(void)
|
||
{
|
||
|
||
printf("Go to Sleep\r\n");
|
||
|
||
WWDG_DeInit();
|
||
Predormancy_treatment();//休眠前处理
|
||
|
||
|
||
// __WFE();
|
||
|
||
// PWR_EnterSTANDBYMode();//待机模式
|
||
|
||
|
||
// __WFI(); //WFI指令进入睡眠
|
||
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);
|
||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, DISABLE); //使能USART4时钟
|
||
// PWR_EnterSTANDBYMode();
|
||
Wake_up_processing();//唤醒处理
|
||
}
|
||
|
||
|
||
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); //使能GPIOD时钟
|
||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE); //使能USART2时钟
|
||
|
||
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, DISABLE); //使能GPIOD时钟
|
||
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, DISABLE); //使能GPIOD时钟
|
||
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, DISABLE); //使能GPIOD时钟
|
||
|
||
// CLOSE_CC1_CP_RELAY();
|
||
// CLOSE_CC2_CC_RELAY();
|
||
// ParseDCU_State_314();
|
||
// Delay_MS(500);
|
||
// ParseDCU_State_3E8();
|
||
// Delay_MS(500);
|
||
#include "adc.h"
|
||
|
||
KC_Set_GPIO(&GetDEVICE_Name(CONG_TX));
|
||
if(Sleep_Parame.insertion_State)
|
||
{
|
||
KC_Set_GPIO(&GetDEVICE_Name(CONG_RX));
|
||
}
|
||
else
|
||
{
|
||
KC_ReSet_GPIO(&GetDEVICE_Name(CONG_RX));
|
||
}
|
||
|
||
// while(1)
|
||
// {
|
||
Delay_MS(5000);
|
||
// }
|
||
NVIC_SystemReset();//不应该执行到这里
|
||
IWDG_Feed();
|
||
// Init_ADC_WK();//ADC唤醒测试
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
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)
|
||
{
|
||
// 启动HSI时钟
|
||
RCC->CR |= RCC_CR_HSION;
|
||
while ((RCC->CR & RCC_CR_HSIRDY) == 0)
|
||
{
|
||
// 等待HSI时钟就绪
|
||
}
|
||
|
||
// 选择HSI作为系统时钟
|
||
RCC->CFGR &= ~RCC_CFGR_SW;
|
||
RCC->CFGR |= RCC_CFGR_SW_HSI;
|
||
|
||
// 等待系统时钟稳定
|
||
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI)
|
||
{
|
||
// 等待
|
||
}
|
||
|
||
// 关闭HSE时钟
|
||
RCC->CR &= ~RCC_CR_HSEON;
|
||
|
||
}
|
||
|
||
void RTC_Init(void)
|
||
{
|
||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
|
||
|
||
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // 或 RCC_RTCCLKSource_LSE
|
||
RCC_RTCCLKCmd(ENABLE);
|
||
/* 允许访问 Backup 区域 */
|
||
PWR_BackupAccessCmd(ENABLE);
|
||
|
||
/* 复位 Backup 区域 */
|
||
BKP_DeInit();
|
||
/* 使能 LSI */
|
||
RCC_LSICmd(ENABLE);
|
||
|
||
/* 等待 LSI 准备好 */
|
||
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
|
||
{}
|
||
|
||
/* 选择 LSI 作为 RTC 时钟源 */
|
||
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
|
||
|
||
/* 使能 RTC 时钟 */
|
||
RCC_RTCCLKCmd(ENABLE);
|
||
|
||
/* 等待 RTC 寄存器 同步
|
||
* 因为RTC时钟是低速的,内环时钟是高速的,所以要同步
|
||
*/
|
||
RTC_WaitForSynchro();
|
||
/* 确保上一次 RTC 的操作完成 */
|
||
RTC_WaitForLastTask();
|
||
|
||
/* 设置 RTC 分频: 使 RTC 周期为1s ,LSI约为40KHz */
|
||
/* RTC period = RTCCLK/RTC_PR = (40 KHz)/(40000-1+1) = 1HZ */
|
||
RTC_SetPrescaler(40000-1);
|
||
|
||
/* 确保上一次 RTC 的操作完成 */
|
||
RTC_WaitForLastTask();
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|