jianghuai/HARDWARE/LOWPOWER/app_pwr_manage.c
2024-05-07 09:36:09 +08:00

570 lines
13 KiB
C

#include "led.h"
#include "chipid.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
#include "usart.h"
#include "can.h"
#include "adc.h"
#include "ADS1015.h"
#include "dc300.h"
#include "24cxx.h"
#include "sd.h"
#include "common.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_rtc.h"
#include "app_pwr_manage.h"
u8 wake_type = 0;
void RTCAlarm_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line17);
wake_type = 2;
}
void RTC_EXTI_INITIAL(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;//
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void RTC_Int_Configuration(void)
{
////u32 CountLowGetVal = 0;
printf("RTC_Int_Configuration\r\n");
RCC_APB1PeriphClockCmd (RCC_APB1Periph_PWR | RCC_APB1Periph_BKP,ENABLE );//
PWR_BackupAccessCmd(ENABLE);//
BKP_DeInit(); ///?????BKP???
RCC_LSEConfig(RCC_LSE_OFF);//??LSE
RCC_LSICmd(ENABLE);//??LSI?40khz
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);//?????????
{
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);////??LSI?RTC?? ? ??,LSI?40khz
RCC_RTCCLKCmd(ENABLE);;//??RTC
RTC_WaitForSynchro();//????
RTC_SetPrescaler(40000); //
RTC_WaitForLastTask();/* 1. ??RTOFF?,??RTOFF????1 */
// RTC_ITConfig(RTC_IT_ALR, ENABLE); //??????
// RTC_WaitForLastTask();
RTC_SetCounter(0);
}
void EnterSlpCfgRtc(void)
{
RTC_Int_Configuration();
}
void RTC_SET_ALARM(u32 sec)
{
RTC_SetAlarm(RTC_GetCounter()+sec);
RTC_WaitForLastTask();
RTC_ITConfig(RTC_FLAG_ALR,ENABLE); //
}
void EXTI15_10_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line10);
EXTI_ClearITPendingBit(EXTI_Line11);
EXTI_ClearITPendingBit(EXTI_Line12);
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line14);
EXTI_ClearITPendingBit(EXTI_Line15);
wake_type = 1;
}
void EXTI9_5_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line5);
EXTI_ClearITPendingBit(EXTI_Line6);
EXTI_ClearITPendingBit(EXTI_Line7);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_ClearITPendingBit(EXTI_Line9);
wake_type = 1;
}
void EXTI3_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI4_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line4);
//wake_type = 1;
}
void EXIT_Configuration(FunctionalState val)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_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_PinSource9);
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;////;
EXTI_InitStructure.EXTI_LineCmd = val;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line9);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = val;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;////;
EXTI_InitStructure.EXTI_LineCmd = val;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line8);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_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_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
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_Line13);
EXTI_ClearITPendingBit(EXTI_Line5);
EXTI_ClearITPendingBit(EXTI_Line6);
EXTI_ClearITPendingBit(EXTI_Line7);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_ClearITPendingBit(EXTI_Line9);
EXTI_ClearITPendingBit(EXTI_Line10);
EXTI_ClearITPendingBit(EXTI_Line11);
EXTI_ClearITPendingBit(EXTI_Line12);
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line14);
EXTI_ClearITPendingBit(EXTI_Line15);
#if 0
EXTI_ClearITPendingBit(EXTI_Line11);
EXTI_ClearITPendingBit(EXTI_Line12);
EXTI_ClearITPendingBit(EXTI_Line13);
EXTI_ClearITPendingBit(EXTI_Line14);
EXTI_ClearITPendingBit(EXTI_Line15);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = val;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = val;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line5);
EXTI_ClearITPendingBit(EXTI_Line6);
EXTI_ClearITPendingBit(EXTI_Line7);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_ClearITPendingBit(EXTI_Line9);
#endif
}
#if 0
void PwrManageInit(void)
{
//return;
EXIT_Configuration(DISABLE);
RTC_EXTI_INITIAL();
}
#endif
void SavePwrCfg(u8 type) //
{
GPIO_InitTypeDef GPIO_InitStructure;
//////EnterSlpCfgRtc();
#if 0
GPIO_ResetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 | GPIO_Pin_11);/// i2c
#else
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 | GPIO_Pin_11; // i2c,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
MCU_LED(1);
#if 0
USART_DeInit(USART1);
///RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,DISABLE); // debug uart
USART_Cmd(USART1, DISABLE);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;////GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;////GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
ADC_DeInit(ADC1);
ADC_Cmd(ADC1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | \
RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO, ENABLE);
#if 0
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , DISABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN2, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, DISABLE);
CAN_ITConfig(CAN1,CAN_IT_FMP0, DISABLE);
CAN_ITConfig(CAN2,CAN_IT_FMP0, DISABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13; // can2,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_11; // can1,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
USART_DeInit(USART2);
///RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,DISABLE); // uart2
USART_Cmd(USART2, DISABLE);
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
EXIT_Configuration(ENABLE);
#if 1
USART_DeInit(UART4); // uart3 printf uart
////RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,DISABLE);
USART_Cmd(UART4, DISABLE);
USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
USART_ITConfig(UART4, USART_IT_TC, DISABLE);
USART_ITConfig(UART4, USART_IT_IDLE, DISABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
#endif
USART_DeInit(UART5); //uart4
///RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,DISABLE);
USART_Cmd(UART5, DISABLE);
USART_ITConfig(UART5, USART_IT_RXNE, DISABLE);
USART_ITConfig(UART5, USART_IT_IDLE, DISABLE);//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
KEY_PWR_4G_OFF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_5; // ,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // ,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOA,GPIO_Pin_10); // pwr5v close
//GPIO_ResetBits(GPIOB,GPIO_Pin_9); // pwr3.3v close
GPIO_SetBits(GPIOA,GPIO_Pin_10); // pwr5v close
GPIO_SetBits(GPIOB,GPIO_Pin_9); // pwr3.3v close
GPIO_SetBits(GPIOB,GPIO_Pin_5); // can_en close
GPIO_ResetBits(GPIOB,GPIO_Pin_4); // 1043 sleep
GPIO_ResetBits(GPIOC,GPIO_Pin_5); // kc1-5
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
GPIO_ResetBits(GPIOB,GPIO_Pin_2);
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
#if 0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_14; // i2c,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
return;
GPIO_SetBits(GPIOC,GPIO_Pin_5); // kc1-5
GPIO_SetBits(GPIOB,GPIO_Pin_0);
GPIO_SetBits(GPIOB,GPIO_Pin_1);
GPIO_SetBits(GPIOB,GPIO_Pin_2);
GPIO_SetBits(GPIOB,GPIO_Pin_14);
#endif
}
void EnterSleepProcess(void)
{
u32 SetSlpT;
//RTC_SET_ALARM(SetSlpT);
IWDG_Feed();
EXIT_Configuration(ENABLE);
printf("EnterSleep00000Process\r\n");
Delay_MS(10);
IWDG_Feed();
RTC_EXTI_INITIAL();
SavePwrCfg(1);
RTC_SET_ALARM(0xffffff00);
IWDG_Feed();
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);
////PWR_EnterSTANDBYMode();
//WDTFeed();
//PWR_EnterSTANDBYMode();
//SystemInit();
SystemInit();
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000);
SysTick_Init();
//RCC_GetClocksFreq(&RCC_Clockstemp);
NVIC_Configuration();
IWDG_Init();
IWDG_Feed();
uart4_init(115200);
if(wake_type == 2)
printf("Rtc_wake-00--Rtc_wake!!!!!!\r\n");
else if(wake_type == 1)
printf("IO wake---IO wake!!!!!!\r\n");
if(wake_type == 2)
printf("Rtc_wake-11--Rtc_wake!!!!!!\r\n");
else if(wake_type == 1)
printf("IO wake---IO wake!!!!!!\r\n");
if(wake_type == 2)
printf("Rtc_wake---Rtc_wake!!!!!!\r\n");
else if(wake_type == 1)
printf("IO wake---IO wake!!!!!!\r\n");
IWDG_Feed();
while(1);
//NVIC_SystemReset();
}
static u8 EntSlpT = 0;
static u8 EntSlpTindex = 0;
static u8 stateA,stateAPre = 0;
static u8 stateAcc,stateAccPre = 0;
uint8_t NetworkManageFrameFlag =0;
void PwrManageWork(void) // second enter
{
stateA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8);
stateAcc = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9);
if((stateA == 1) && (stateAcc == 1)){
if(EntSlpTindex++%10==1)
{
printf("A+ %s\r\n", stateA==0?"wakeup":"sleep");
printf("ACC %s\r\n", stateAcc==0?"wakeup":"sleep");
}
if(ChkNetworkManageoff() !=0) //??????????
{
EntSlpT = 0;
return;
}
}
else
{
if(stateA != stateAPre){
stateAPre = stateA;
printf("A+ %s\r\n", stateA==0?"wakeup":"sleep");
}
if(stateAccPre != stateAcc){
stateAccPre = stateAcc;
printf("ACC %s\r\n", stateAcc==0?"wakeup":"sleep");
}
EntSlpT = 0;
return;
}
printf("Enter Sleep Time add--- %d --Max-8\r\n",EntSlpT);
EntSlpT++;
if(EntSlpT <= 8)
return;
EntSlpT = 0;
EnterSleepProcess();
}
void clr_EntSlpT(void)
{
EntSlpT = 0;
}
/**
* @breif: ????????????????? 8s?
*
* @param void
*
* @return int: 0 - ???????? < 0 ????????
*/
int ChkNetworkManageoff(void)
{
if(NetworkManageFrameFlag == 0)
{
return 0;
}
return -1;
}
/**
* @breif: ?????????????
*
* @param void
*
* @return int: 0 - ?? < 0 ???
*/
int ChkPwrManageAccoff(void)
{
stateA = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8);
stateAcc = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_9);
if((stateA == 1) && (stateAcc == 1))
{
return 0;
}
return -1;
}
/**
* @breif: ??????????????
*
* @param void
*
* @return int: 0 - ?? < 0 ???
*/
int ChkPwrManageAppDataMute(void)
{
if(EntSlpT > 3)
{
return 0;
}
return -1;
}