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

);

1 ACCEPTED SOLUTION

Accepted Solutions
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.

View solution in original post

10 REPLIES 10
Khouloud GARSI
Lead II
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.

Posted on June 13, 2018 at 11:49

Dear Sir

I'm using NUCLEO-446ZE Board

Posted on June 13, 2018 at 11:50

The Cube Firmware version STM32Cube_FW_F4_V1.21.0

Posted on June 13, 2018 at 12:14

Thanks for the details. This will help us when reviewing the reported point.

Khouloud.

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
Associate II

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

Dhaval Patel
Associate II

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
Associate II

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

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