cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX 4.26.0 RTC Init Issue

Varadharajan V
Associate III
Posted on June 13, 2018 at 10:42

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

);

10 REPLIES 10
Stefano Ugolini
Associate III

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.