Skip to main content
Varadharajan V
Associate III
June 13, 2018
Solved

STM32CubeMX 4.26.0 RTC Init Issue

  • June 13, 2018
  • 10 replies
  • 3670 views
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

);

    This topic has been closed for replies.
    Best answer by Mohamed Mansouri
    Posted on June 19, 2018 at 18:05

    Hi

    V.Varadharajan

    ,

    Thanks for your feedback,

    In fact, the idea is to let the user insert his own code using habitual USER TAGs when he doesn't want to re-initialize RTC peripheral.

    As you can see, in CubeMX 4.26 version, we have added USER TAGs in the MX_RTC_Init(void) where user can add his own code.

    static void MX_RTC_Init(void)

    {

    /* USER CODE BEGIN RTC_Init 0 */

    /* USER CODE END RTC_Init 0 */

    RTC_TimeTypeDef sTime;

    RTC_DateTypeDef sDate;

    /* USER CODE BEGIN RTC_Init 1 */

    /* USER CODE END RTC_Init 1 */

    /**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 = 0x0;

    sTime.Minutes = 0x0;

    sTime.Seconds = 0x0;

    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

    sTime.StoreOperation = RTC_STOREOPERATION_RESET;

    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != 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 = 0x1;

    sDate.Year = 0x0;

    if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

    {

    _Error_Handler(__FILE__, __LINE__);

    }

    /* USER CODE BEGIN RTC_Init 4 */

    /* USER CODE END RTC_Init 4 */

    }

    Also, in addition to the backup register, there are others solutions to remain the RTC initialization. It depends on user application. For example, you may also use RTC & PWR flags to determine whether RTC is already initialized or a HW/SW reset has been occurred. So, we can not force user to use backup register solution to remain

    RTC initialization.

    Hoping it helps you, please do not hesitate to contact me for further details.

    Best Regards,

    Mohamed.

    10 replies

    Khouloud GARSI
    ST Employee
    June 13, 2018
    Posted on June 13, 2018 at 11:01

    Hi

    V.Varadharajan

    ‌,

    Thanks for your feedback. This is now reported internally for further check.

    Could you please precise which STM32 MCU and which Cube firmware you're using.

    We will update you ASAP.

    Khouloud.

    Varadharajan V
    Associate III
    June 13, 2018
    Posted on June 13, 2018 at 11:49

    Dear Sir

    I'm using NUCLEO-446ZE Board

    Mohamed Mansouri
    Mohamed MansouriBest answer
    Visitor II
    June 19, 2018
    Posted on June 19, 2018 at 18:05

    Hi

    V.Varadharajan

    ,

    Thanks for your feedback,

    In fact, the idea is to let the user insert his own code using habitual USER TAGs when he doesn't want to re-initialize RTC peripheral.

    As you can see, in CubeMX 4.26 version, we have added USER TAGs in the MX_RTC_Init(void) where user can add his own code.

    static void MX_RTC_Init(void)

    {

    /* USER CODE BEGIN RTC_Init 0 */

    /* USER CODE END RTC_Init 0 */

    RTC_TimeTypeDef sTime;

    RTC_DateTypeDef sDate;

    /* USER CODE BEGIN RTC_Init 1 */

    /* USER CODE END RTC_Init 1 */

    /**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 = 0x0;

    sTime.Minutes = 0x0;

    sTime.Seconds = 0x0;

    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

    sTime.StoreOperation = RTC_STOREOPERATION_RESET;

    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != 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 = 0x1;

    sDate.Year = 0x0;

    if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)

    {

    _Error_Handler(__FILE__, __LINE__);

    }

    /* USER CODE BEGIN RTC_Init 4 */

    /* USER CODE END RTC_Init 4 */

    }

    Also, in addition to the backup register, there are others solutions to remain the RTC initialization. It depends on user application. For example, you may also use RTC & PWR flags to determine whether RTC is already initialized or a HW/SW reset has been occurred. So, we can not force user to use backup register solution to remain

    RTC initialization.

    Hoping it helps you, please do not hesitate to contact me for further details.

    Best Regards,

    Mohamed.

    kotesoft
    Visitor II
    September 20, 2018

    But in CubeMX 4.27.0 version void MX_RTC_Init(void) does not contain USER TAGs.

    Dhaval Patel
    Associate II
    September 20, 2018

    Hi,

    MX_RTC_Init(void) does contain USER tag with version 4.27.0​.

    static void MX_RTC_Init(void)
    {
     
     /* USER CODE BEGIN RTC_Init 0 */
     
     /* USER CODE END RTC_Init 0 */
     
     /* USER CODE BEGIN RTC_Init 1 */
     
     /* USER CODE END RTC_Init 1 */
     
     /**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__);
     }
     
    }

    kotesoft
    Visitor II
    September 22, 2018

    STM32CubeMX 4.27.0, STM32F205RETx, STM32F2 MCU Package v. 1.7.0

    File Src\rtc.c Lines 49-91

    /* RTC init function */
    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__);
     }
     
     /**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__);
     }
     
    }

    motla
    Associate II
    October 15, 2018

    Same problem here for STM32F107. ST please add USER tags in the next release !

    Stefano Ugolini
    Associate II
    December 20, 2018

    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.