2018-06-13 01:42 AM
STM32CubeMx 4.26.0 RTC Reset always during INIT
The generated Code
void
MX_RTC_Init
(
void
)
{
RTC_TimeTypeDef
sTime
;
RTC_DateTypeDef
sDate
;
/**Initialize RTC Only
*/
hrtc.
Instance
=
RTC;
hrtc.
Init
.
HourFormat
=
RTC_HOURFORMAT_24;
hrtc.
Init
.
AsynchPrediv
=
127
;
hrtc.
Init
.
SynchPrediv
=
255
;
hrtc.
Init
.
OutPut
=
RTC_OUTPUT_DISABLE;
hrtc.
Init
.
OutPutPolarity
=
RTC_OUTPUT_POLARITY_HIGH;
hrtc.
Init
.
OutPutType
=
RTC_OUTPUT_TYPE_OPENDRAIN;
if
(
HAL_RTC_Init
(
&
hrtc)
!=
HAL_OK)
{
_Error_Handler
(__FILE__, __LINE__);
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
/**Initialize RTC and set the Time and Date
*/
sTime
.
Hours
=
0
;
sTime
.
Minutes
=
0
;
sTime
.
Seconds
=
0
;
sTime
.
DayLightSaving
=
RTC_DAYLIGHTSAVING_NONE;
sTime
.
StoreOperation
=
RTC_STOREOPERATION_RESET;
if
(
HAL_RTC_SetTime
(
&
hrtc,
&
sTime
, RTC_FORMAT_BIN)
!=
HAL_OK)
{
_Error_Handler
(__FILE__, __LINE__);
}
/* USER CODE BEGIN RTC_Init 3 */
/* USER CODE END RTC_Init 3 */
sDate
.
WeekDay
=
RTC_WEEKDAY_MONDAY;
sDate
.
Month
=
RTC_MONTH_JANUARY;
sDate
.
Date
=
1
;
sDate
.
Year
=
0
;
if
(
HAL_RTC_SetDate
(
&
hrtc,
&
sDate
, RTC_FORMAT_BIN)
!=
HAL_OK)
{
_Error_Handler
(__FILE__, __LINE__);
}
/* USER CODE BEGIN RTC_Init 4 */
/* USER CODE END RTC_Init 4 */
}
But Older versions generated code (which was correct)
void
MX_RTC_Init
(
void
)
{
RTC_TimeTypeDef
sTime
;
RTC_DateTypeDef
sDate
;
/**Initialize RTC Only
*/
hrtc.
Instance
=
RTC;
if
(
HAL_RTCEx_BKUPRead
(
&
hrtc, RTC_BKP_DR0)
!=
0x32F2
){
hrtc.
Init
.
HourFormat
=
RTC_HOURFORMAT_24;
hrtc.
Init
.
AsynchPrediv
=
127
;
hrtc.
Init
.
SynchPrediv
=
255
;
hrtc.
Init
.
OutPut
=
RTC_OUTPUT_DISABLE;
hrtc.
Init
.
OutPutPolarity
=
RTC_OUTPUT_POLARITY_HIGH;
hrtc.
Init
.
OutPutType
=
RTC_OUTPUT_TYPE_OPENDRAIN;
if
(
HAL_RTC_Init
(
&
hrtc)
!=
HAL_OK)
{
_Error_Handler
(__FILE__, __LINE__);
}
/**Initialize RTC and set the Time and Date
*/
sTime
.
Hours
=
0
;
sTime
.
Minutes
=
0
;
sTime
.
Seconds
=
0
;
sTime
.
DayLightSaving
=
RTC_DAYLIGHTSAVING_NONE;
sTime
.
StoreOperation
=
RTC_STOREOPERATION_RESET;
if
(
HAL_RTC_SetTime
(
&
hrtc,
&
sTime
, RTC_FORMAT_BIN)
!=
HAL_OK)
{
_Error_Handler
(__FILE__, __LINE__);
}
sDate
.
WeekDay
=
RTC_WEEKDAY_MONDAY;
sDate
.
Month
=
RTC_MONTH_JANUARY;
sDate
.
Date
=
1
;
sDate
.
Year
=
0
;
if
(
HAL_RTC_SetDate
(
&
hrtc,
&
sDate
, RTC_FORMAT_BIN)
!=
HAL_OK)
{
_Error_Handler
(__FILE__, __LINE__);
}
HAL_RTCEx_BKUPWrite
(
&
hrtc,RTC_BKP_DR0,
0x32F2
);
}
}
these two lines are missed which always makes the RTC reset
if
(
HAL_RTCEx_BKUPRead
(
&
hrtc, RTC_BKP_DR0)
!=
0x32F2
){
HAL_RTCEx_BKUPWrite
(
&
hrtc,RTC_BKP_DR0,
0x32F2
);
Solved! Go to Solution.
2018-12-20 02:44 AM
Could you please remove set time and date from the init function?
I don't see any usecase for the RTC to reset to the same time every time the mcu is turned on.