cancel
Showing results for 
Search instead for 
Did you mean: 

RTC and reset problem

freya17365
Associate II
Posted on March 31, 2016 at 14:20

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

2 REPLIES 2
Walid FTITI_O
Senior II
Posted on March 31, 2016 at 15:10

Hi Freya, 

It seems that your problem is coming from a bad switching between Power-ON and resuming from Standby/RESET states like this [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F437%20exiting%20standby%20incorrectly&currentviews=63]request

.

Take a look to my recommendation there.

-Hannibal-

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

freya17365
Associate II
Posted on April 01, 2016 at 17:25

Hi Hannibal,

indeed I do not want to put mcu in stanby or any other state. In my project mcu  is on and stay on.

I followed what I found to configure RTC.

Now I changed sligthy initialization routine and reset is wworking properly, but I encounterd another problem, that is TR register is updated discontinuously.

In other worlds, I read RTC_TR register more or less every second an I get the same values for several times and than the new value for some rondom times. For example:

7 for 10 times then

17 and it remains 17 for 20 times then

37 and so on.

Now I try to check registers as suggested in your hint, but I do not understand what's wrong.

Freya