AnsweredAssumed Answered

STM32F4XX RTC calendar can not be configured

Question asked by xiang.sanming on May 1, 2015
Latest reply on Jan 14, 2016 by Clive One

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) << 8) | \
             ((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) << 8) | \
                   ((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;
}

Outcomes