
/******************************************************************************
*@file  : app.c
*@brief : application
******************************************************************************/

#include "app.h" 


/******************************************************************************
* @brief : Set IO Wakeup.
* @param : None
* @return: None
******************************************************************************/
void Wakeup_IO_Init(void)
{
    //Wakeup2(PC13)高电平 
    PMU_WakeUpPinPolarityConfig(PMU_WAKEUP_PIN2, PMU_WAKEUPPIN_POLARITY_HIGH);
    
    //Wakeup2(PC13)管脚滤波使能
    PMU_WakeUpPinFilterCmd(PMU_WAKEUP_PIN2, ENABLE);
    
    //设置Standby唤醒等待时间
    PMU_SetSTANDBYWakeWaitTime(PMU_STANDBYWAKEWAIT_4RC32K);
    
    //Wakeup2(PC13)管脚唤醒功能使能
    PMU_WakeUpPinCmd(PMU_WAKEUP_PIN2, ENABLE);
}

/******************************************************************************
* @brief : Set RTC Wakeup.
* @param : None
* @return: None
******************************************************************************/
void Wakeup_RTC_Init(void)
{
    //RTC初始化
    RTC_Init(RTC_CLOCK_XTL);
    
    //开始时钟补偿
    RTC_CoarseCalibConfig(RTC_CALIBSIGN_POSITIVE, 5);
   
    //清除32秒中断标志
    RTC_ClearFlag(RTC_IT_FLAG_32S);    
    
    //使能32秒中断
    RTC_ITConfig(RTC_IT_FLAG_32S, ENABLE);
}

/******************************************************************************
* @brief : Enter Standby.
* @param : None
* @return: None
******************************************************************************/
void Standby_Mode(void)
{
    printfS("MCU enter standby mode \r\n");  
    SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;   // disable systick        
    __set_PRIMASK(1);    // disable interrupt 
    SCB->ICSR = BIT25;   // clear systick pending bit
    
    PMU_EnterSTANDBYMode(PMU_LOWPOWRENTRY_WFI);
    
    SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; /* Enable SysTick IRQ */               
    __set_PRIMASK(0);    // enable interrupt 
    while(1)
    {
        printfS("MCU never run here \r\n");
    }
}


/******************************************************************************
* @brief : Low power standby test.
* @param : mode: wakeup source select(IO or RTC) 
* @return: none
******************************************************************************/
void APP_LowPower_Standby_Mode_Test(uint8_t mode)
{
    FlagStatus result = RESET;
    
    PMU_Init();
    
    result = PMU_GetFlagStatus(PMU_FLAG_SB);
    
    if(result == SET)
    {
        printfS("MCU has entered standby mode!\n");
    }
    switch(mode)
    {
        /* standby模式下 IO唤醒 */
        case PMU_STANDBY_IO_WAKEUP:
        {
            printfS("MCU Standby wakeup by IO Test \r\n"); 
            
            result = PMU_GetFlagStatus(PMU_FLAG_WUPF2);
            if(result == SET)
            {
                printfS("WakeUp by PC13(WAKEUP2) \n");
                PMU_ClearFlag(PMU_SR_WUPFX_2);
            }
                
            DelayMs(5000);
            
            Wakeup_IO_Init();
            
            Standby_Mode();

        }
        /* standby模式下 RTC唤醒 */
        case PMU_STANDBY_RTC_WAKEUP:
        {	
            printfS("MCU Standby wakeup by RTC Test \r\n");

            PMU_WakeUpPinCmd(PMU_WAKEUP_PIN2, DISABLE);
            
            result = PMU_GetFlagStatus(PMU_FLAG_RTC);
            if(result == SET)
            {
                printfS("WakeUp by RTC \n");
                PMU_ClearFlag(PMU_FLAG_RTC);
            }
            
            DelayMs(5000); 

            Wakeup_RTC_Init();            

            Standby_Mode();
        }
        default: 
        printfS("The application is not supported\r\n");
        break; 
    }
    
}

