cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4XX RTC calendar can not be configured

sanmingx
Associate
Posted on May 01, 2015 at 01:45

When system boots up, RTC is initialized and could be configured with success, but any calendar configuration afterward will always fail with ''  if (RTC_EnterInitMode() == ERROR)'' in RTC_SetTime() standard API.

When system boots up, below code is called

//-------------------------------------------------------------------------------

void Rtc::open()

{

 // Enable the Power Controller APB1

 // interface clock

 /* Enable PWR peripheral clock */ 

 uint32_t status;

 uint8_t stat = 1;

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ 

 PWR_BackupAccessCmd(ENABLE);

 status = RTC_ReadBackupRegister(RTC_BKP_DR19);

 

 // Power up and wait until LSI is ready

 RCC_LSICmd(ENABLE);

 while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)

 {

 }

 // Select the RTC Clock Source

 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

 // Enable the RTC Clock

 RCC_RTCCLKCmd(ENABLE);

 RTC_WaitForSynchro();

 //RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);

 RTC_WriteBackupRegister(RTC_BKP_DR19, 0x1234);

 RTC_WaitForSynchro();

 RTC_ClearITPendingBit(RTC_IT_WUT);

 EXTI_ClearITPendingBit(EXTI_Line22);

}

//------------------------------------ Configure calendar ---------------

void Rtc::write(TimeDateStruct idateTime)

{

     RTC_SetTime(RTC_Format_BIN, &idateTime.time);

}

as you can see, RTC_SetTime is standard API from lib as below, it stops on line ''if (RTC_EnterInitMode() == ERROR)'' of ''INITF'' flag timeout.

Please help this issue, thanks

ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct)

{

  uint32_t tmpreg = 0;

  ErrorStatus status = ERROR;

   

  /* Check the parameters */

  assert_param(IS_RTC_FORMAT(RTC_Format));

 

  if (RTC_Format == RTC_Format_BIN)

  {

    if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)

    {

      assert_param(IS_RTC_HOUR12(RTC_TimeStruct->RTC_Hours));

      assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));

    }

    else

    {

      RTC_TimeStruct->RTC_H12 = 0x00;

      assert_param(IS_RTC_HOUR24(RTC_TimeStruct->RTC_Hours));

    }

    assert_param(IS_RTC_MINUTES(RTC_TimeStruct->RTC_Minutes));

    assert_param(IS_RTC_SECONDS(RTC_TimeStruct->RTC_Seconds));

  }

  else

  {

    if ((RTC->CR & RTC_CR_FMT) != (uint32_t)RESET)

    {

      tmpreg = RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours);

      assert_param(IS_RTC_HOUR12(tmpreg));

      assert_param(IS_RTC_H12(RTC_TimeStruct->RTC_H12));

    }

    else

    {

      RTC_TimeStruct->RTC_H12 = 0x00;

      assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Hours)));

    }

    assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Minutes)));

    assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(RTC_TimeStruct->RTC_Seconds)));

  }

 

  /* Check the input parameters format */

  if (RTC_Format != RTC_Format_BIN)

  {

    tmpreg = (((uint32_t)(RTC_TimeStruct->RTC_Hours) << 16) | \

             ((uint32_t)(RTC_TimeStruct->RTC_Minutes) << 😎 | \

             ((uint32_t)RTC_TimeStruct->RTC_Seconds) | \

             ((uint32_t)(RTC_TimeStruct->RTC_H12) << 16));

  } 

  else

  {

    tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Hours) << 16) | \

                   ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Minutes) << 😎 | \

                   ((uint32_t)RTC_ByteToBcd2(RTC_TimeStruct->RTC_Seconds)) | \

                   (((uint32_t)RTC_TimeStruct->RTC_H12) << 16));

  } 

  /* Disable the write protection for RTC registers */

  RTC->WPR = 0xCA;

  RTC->WPR = 0x53;

  /* Set Initialization mode */

  if (RTC_EnterInitMode() == ERROR)

  {

    status = ERROR;

  }

  else

  {

    /* Set the RTC_TR register */

    RTC->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);

    /* Exit Initialization mode */

    RTC_ExitInitMode();

    /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */

    if ((RTC->CR & RTC_CR_BYPSHAD) == RESET)

    {

    if(RTC_WaitForSynchro() == ERROR)

    {

      status = ERROR;

    }

    else

    {

      status = SUCCESS;

    }

  }

    else

    {

      status = SUCCESS;

    }

  }

  /* Enable the write protection for RTC registers */

  RTC->WPR = 0xFF;

   

  return status;

}
4 REPLIES 4
Posted on May 01, 2015 at 03:22

Make sure you reset the RTC if it gets into an unknown state, and that clocks are enabled.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Need%20help%20initializing%20RTC%20on%20STM32F407&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=3345]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FNeed%20help%20initializing%20RTC%20on%20STM32F407&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=3345

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sanmingx
Associate
Posted on May 05, 2015 at 20:48

Hello, Clivel

I did try to reset the RTC by calling ''RCC_BackupResetCmd(ENABLE);

RCC_BackupResetCmd(DISABLE)'' followed when ''

RTC_EnterInitMode() == ERROR'' occurred, it seemed not fix my issue at all. Is it the right way to reset RTC?

the clock is sourced with LSI, with no external battery, the Vbatt is 3.3 V, I guess it is good.

engenharia2
Associate II
Posted on January 14, 2016 at 15:12

Hi xiang.sanming, I have the same problem using the HAL Library. Do you got a solution to fix this problem? Thank you!

Posted on January 14, 2016 at 19:19

I'm not sure someone who participated for 2 posts, 8 months ago, using the SPL, is going to be at all useful with your HAL issues.

See if there is anything useful you can gleen from the cited thread.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..