AnsweredAssumed Answered

RTC and reset problem

Question asked by Laga.Freya on Mar 31, 2016
Latest reply on Apr 1, 2016 by Laga.Freya
Hi to all,
I encounterd a problem using Real Time Clock.
After I initilaized it, everything seems to work, but when I reset MCU it stops and I can restart my board only powering it off and then on again.
Hereafter there is my code:

CLOCK INITIALIZATION
==========================================================
void SystemInit()
{
    /*-------------------------------- HSE Configuration -----------------------*/
    if(RCC->CR & RCC_CR_HSEBYP)    RCC->CR &= ~RCC_CR_HSEBYP;    // if HSE bypass, disable it
    if(!(RCC->CR & RCC_CR_HSEON))    // Check if HSE is on
    {
        RCC->CR |= RCC_CR_HSEON;    // if not, just enable it
        while((RCC->CR & RCC_CR_HSERDY) == 0);    // Wait for HSE being ready
    }
    
    /*-------------------------------- PLL Configuration -----------------------*/
    if(RCC->CR & RCC_CR_PLLON) RCC->CR &= ~RCC_CR_PLLON;    //Disable PLL
    while(RCC->CR & RCC_CR_PLLRDY);    // Wait for PLL disabled
    
    RCC->CFGR |= 4<<18 | 1<<16;    //PLLMUL6<<18 | PLLSRC<<16 (HSE)
    RCC->CFGR2 = 0x0000;    //RCC_CFGR2_PREDIV_DIV1;    //
    RCC->CR |= 0x01<<24;    //PLLON
    while(!(RCC->CR & RCC_CR_PLLRDY));    // Wait for PLL enabled
    
    if(!(RCC->CFGR & RCC_CFGR_SWS_PLL)) RCC->CFGR &= 0xFFFFFFFC;    // if PLL is not SystemClock, set PLL as SystemClock
    RCC->CFGR |= RCC_CFGR_SW_PLL;
    RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_PPRE);    //AHB and APB not divided
    
    /*-------------------------------- LSI Configuration -----------------------*/
    if(!(RCC->CSR & RCC_CSR_LSION)) RCC->CSR |= RCC_CSR_LSION;    //Enable LSI
    while(!(RCC->CSR & RCC_CSR_LSIRDY));    // Wait for LSI ready
    
    RCC->CIR = 0x00000000;                // Disable all interrupts
    return;
}
==========================================================


RTC INITIALIZATION
==========================================================
MYRTC::MYRTC()
{
    uint8_t i;
    uint32_t t;
    
    if((RCC->BDCR & RCC_BDCR_RTCSEL) != (RCC_BDCR_RTCSEL_LSI & RCC_BDCR_RTCSEL))    // If RTC Clock is not LSI, just change it to LSI
    {
        RCC->APB1ENR |= RCC_APB1ENR_PWREN;    // Enable power clock
        PWR->CR |= PWR_CR_DBP;    // Enable write access to Backup domain
        i=0;
        while(((PWR->CR & PWR_CR_DBP) == 0) && (i++<10)) Delay(1);    // Wait for write access enabled
        t = (RCC->BDCR & ~(RCC_BDCR_RTCSEL));    // Save BDCR value
        RCC->BDCR |= RCC_BDCR_BDRST;    // Reset
        RCC->BDCR &= ~RCC_BDCR_BDRST;    // End reset
        RCC->BDCR = t;    // Restore register
        RCC->BDCR |= RCC_BDCR_RTCSEL_LSI;     // Set LSI as RTC clock
    }
    
    if(!(RCC->BDCR & RCC_BDCR_RTCEN)) RCC->BDCR |= RCC_BDCR_RTCEN;        // Enable RTC clock
    RTC->WPR = 0xCA;
    RTC->WPR = 0x53; // Enable register writing
    RTC->ISR |= RTC_ISR_INIT; // Enable init phase
    i=0;
    while(((RTC->ISR & RTC_ISR_INITF) != RTC_ISR_INITF) && (i++<10000));    // Wait for system ready
    RTC->PRER = 0x007F0137; // Set prescaler for 1Hz from 40KHz (LSI)
    RTC->ISR &=~ RTC_ISR_INIT; // Disable init phase
    RTC->WPR = 0xFE;
    RTC->WPR = 0x64; // Disable write access for RTC registers    
    
    PWR->CR &= ~PWR_CR_DBP;    // Disable write access to Backup domain
}
==========================================================

I followed what I found in the user manual and in CubeMX code, so I do not understand why a reset crashes everything.

Do I forgot something?

Thank you
Freya


Outcomes