I have to restart MCU to set RTC date in STM32F205ZG. I wrote a Set date and set time function for RTC. while Set time function updates the RTC time properly, set date function does not update the RTC date until I reset the MCU!
- April 6, 2023
- 6 replies
- 2386 views
I am using STM32F205ZG RTC with External clock; the RTC initalization, RTC set Date function in the following lines:
int16_t RTC_HAL_init(void)
{
RTC_InitTypeDef RTC_InitStruct;
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to the RTC */
PWR_BackupAccessCmd(ENABLE);
/* Enable the LSE clock */
RCC_LSEConfig(RCC_LSE_ON);
while ((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET))
{ } /* wait for LSI clock to be ready*/
/* Select the RTC clock source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable the RTC clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Configure the RTC */
RTC_InitStruct.RTC_AsynchPrediv = 127U;
RTC_InitStruct.RTC_SynchPrediv = 255U;
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStruct);
return NO_ERR;
}
int16_t RTC_set_date(uint16_t year, uint8_t month, uint8_t day)
{
RTC_DateTypeDef RTC_DateStruct;
uint16_t tempYear = 0u;
uint8_t tempMonth = 0u;
uint8_t tempDay = 0u;
RTC_DateStruct.RTC_WeekDay = day_of_week(year , month, day);
RTC_DateStruct.RTC_Date = day;
RTC_DateStruct.RTC_Month = month;
RTC_DateStruct.RTC_Year = (uint8_t)(year - 2000u);
printf( "RTC date Value is : %d : %d : %d !!\n",RTC_DateStruct.RTC_Year , RTC_DateStruct.RTC_Month, RTC_DateStruct.RTC_Date );
if (RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct)== ERROR)
{
return G_ERR;
}
return NO_ERR;
}
when I call the RTC_set_date function, it does not update the RTC date, while set time function works properly. I use the following line of the code to restart the MCU
if ((cmd =='Y') || (cmd=='y'))
{
SCB->AIRCR = 0x05FA0004;
}
in the attachment you can see the function output in different steps.
( the BSD library that I use is version V1.1.2 (05-March-2012) and BSD set date function is in below)
ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct)
{
uint32_t tmpreg = 0;
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_RTC_FORMAT(RTC_Format));
if ((RTC_Format == RTC_Format_BIN) && ((RTC_DateStruct->RTC_Month & 0x10) == 0x10))
{
RTC_DateStruct->RTC_Month = (RTC_DateStruct->RTC_Month & (uint32_t)~(0x10)) + 0x0A;
}
if (RTC_Format == RTC_Format_BIN)
{
assert_param(IS_RTC_YEAR(RTC_DateStruct->RTC_Year));
assert_param(IS_RTC_MONTH(RTC_DateStruct->RTC_Month));
assert_param(IS_RTC_DATE(RTC_DateStruct->RTC_Date));
}
else
{
assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(RTC_DateStruct->RTC_Year)));
tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Month);
assert_param(IS_RTC_MONTH(tmpreg));
tmpreg = RTC_Bcd2ToByte(RTC_DateStruct->RTC_Date);
assert_param(IS_RTC_DATE(tmpreg));
}
assert_param(IS_RTC_WEEKDAY(RTC_DateStruct->RTC_WeekDay));
/* Check the input parameters format */
if (RTC_Format != RTC_Format_BIN)
{
tmpreg = ((((uint32_t)RTC_DateStruct->RTC_Year) << 16) | \
(((uint32_t)RTC_DateStruct->RTC_Month) << 8) | \
((uint32_t)RTC_DateStruct->RTC_Date) | \
(((uint32_t)RTC_DateStruct->RTC_WeekDay) << 13));
}
else
{
tmpreg = (((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Year) << 16) | \
((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Month) << 8) | \
((uint32_t)RTC_ByteToBcd2(RTC_DateStruct->RTC_Date)) | \
((uint32_t)RTC_DateStruct->RTC_WeekDay << 13));
}
/* Disable the write protection for RTC registers */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
/* Set Initialization mode */
if (RTC_EnterInitMode() == ERROR)
{
status = ERROR;
return ERROR;
}
else
{
/* Set the RTC_DR register */
RTC->DR = (uint32_t)(tmpreg & RTC_DR_RESERVED_MASK);
/* Exit Initialization mode */
RTC_ExitInitMode();
if(RTC_WaitForSynchro() == ERROR)
{
status = ERROR;
return ERROR;
}
else
{
status = SUCCESS;
}
}
/* Enable the write protection for RTC registers */
RTC->WPR = 0xFF;
return status;
}
what could be the reason for this issue?
