WAKE UP

Dependencies:   WakeUp mbed

Fork of NucleoStandbyTest by Michael Walker

main.cpp

Committer:
MichaelW
Date:
2016-05-07
Revision:
0:1ecaa40f74d0
Child:
1:085a87258f10

File content as of revision 0:1ecaa40f74d0:

// Depending on the LED connections either the LED is off the 2 seconds
// the target spends in deepsleep(), and on for the other second. Or it is inverted


#include "mbed.h"
#include "WakeUp.h"
#include "stm32f0xx_hal.h"
#include "stm32f0xx_hal_rtc_ex.h"
#include "rtc_wakeup.h"

#define RTC_CR_WUTE   (1<<10)
#define RTC_CR_WUTIE    (1<<14)
#define EXTI_IMR_MR20 0x00100000


DigitalOut myled(LED1);
DigitalOut Buzz(A2);
DigitalOut RST(D0);

static RTC_HandleTypeDef RtcHandle;

void ClearWakeUp(void);

int main()
{
    ClearWakeUp();
    RST = 1;
    Buzz = 0;
    myled = 1;
    wait(0.2);
    myled = 0;
    wait(0.2);
    //print("Wake %i\r\n", TM_LOWPOWER_StandbyReset() );
    //The low-power oscillator can be quite inaccurate on some targets
    //this function calibrates it against the main clock
    WakeUp::calibrate();



    //RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, DISABLE);   //Need write access to DBP bit

    //__HAL_RCC_PWR_CLK_ENABLE();
    //PWR->CR |= PWR_CR_DBP;
    //SET_BIT(PWR->CR, PWR_CR_DBP);
    /* //Wait for Backup domain Write protection disable
    uint32_t tickstart = HAL_GetTick();


    while((PWR->CR & PWR_CR_DBP) == RESET)
      {
        if((HAL_GetTick() - tickstart) > RCC_DBP_TIMEOUT_VALUE)
        {
          return HAL_TIMEOUT;
        }
      }
    */
    /*
      HAL_PWR_EnableBkUpAccess();

      RtcHandle.Instance = RTC;
      __HAL_RTC_WRITEPROTECTION_DISABLE(&RtcHandle);

       __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
       __HAL_RTC_ALARM_CLEAR_FLAG(&RtcHandle, RTC_FLAG_ALRAF);
       __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF);
      //__HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG() ;

    // __HAL_RCC_BACKUPRESET_FORCE();
    //__HAL_RCC_BACKUPRESET_FORCE();

       __HAL_RTC_WRITEPROTECTION_ENABLE(&RtcHandle);

    */

    ///Direct register access version  ***************************************************

    // ClearWakeUp();

    //RCC_APB1PeriphCmd(RCC_APB1Periph_PWR, ENABLE);

    // __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
    //From Below works  ***************************************************

    WakeUp::set_ms(2000);

    //Enter deepsleep, the program won't go beyond this point until it is woken up
    //deepsleep();

    printf("Standby Mode\r\n");

    ClearWakeUp();

    HAL_PWR_EnterSTANDBYMode();

    //This will never be executed
    printf("After sleep\r\n");
}
void ClearWakeUp(void){
    PWR->CR |= PWR_CR_DBP;      //Enable power domain
    RTC->WPR = 0xCA;            //Disable RTC write protection
    RTC->WPR = 0x53;

    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);

    RTC->WPR = 0xFF;        //Enable RTC write protection
    PWR->CR &= ~PWR_CR_DBP; //Disable power domain
}

void ClearWakeUp2(void)
{
    ///Direct register access version  ***************************************************
    RtcHandle.Instance = RTC;

    __HAL_RTC_WAKEUPTIMER_DISABLE(&RtcHandle);
    __HAL_RTC_WAKEUPTIMER_DISABLE_IT(&RtcHandle,RTC_IT_WUT);

    PWR->CR |= PWR_CR_DBP;      //Enable power domain                           //*******************
    RTC->WPR = 0xCA;            //Disable RTC write protection
    RTC->WPR = 0x53;

    __HAL_RTC_WAKEUPTIMER_EXTI_DISABLE_IT() ;
    __HAL_RTC_WAKEUPTIMER_EXTI_DISABLE_EVENT() ;

    /* Disable the Alarm A interrupt */
    __HAL_RTC_ALARMA_DISABLE(&RtcHandle);

    /* Clear flag alarm A */
    __HAL_RTC_ALARM_CLEAR_FLAG(&RtcHandle, RTC_FLAG_ALRAF);

    __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
    __HAL_RTC_ALARM_CLEAR_FLAG(&RtcHandle, RTC_FLAG_ALRAF);
    __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(&RtcHandle, RTC_FLAG_WUTF);

    // #define __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG()            (EXTI->PR = RTC_EXTI_LINE_WAKEUPTIMER_EVENT)
    __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG() ;

    EXTI->PR = RTC_EXTI_LINE_WAKEUPTIMER_EVENT ;
    EXTI->PR = 0x007FFFFF;
    RTC->WPR = 0xFF;        //Enable RTC write protection
    PWR->CR &= ~PWR_CR_DBP; //Disable power domain

}