Skip to main content
lmill.1
Associate
January 20, 2021
Solved

STM32F042 RTC wont start

  • January 20, 2021
  • 12 replies
  • 3126 views

I seem to be unable to get the RTC to start. I can set the time, and read from it, but it doesn't increment.

I have tried using the external and internal oscillator, neither have been successful.

Everything has been set up with CubeMx, and I have used the RTC before on an STM32L0 micro with no issue, comparing it to my old code I cant see what I am doing differently.

I have attached the relevant parts of the code, if you know what I am missing please let me know.

Thanks,

Luke

static 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 = 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__);
 }
 
 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__);
 }
 
 HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
 }
 
}
/* USER CODE BEGIN PV */
RTC_TimeTypeDef sTimeGet;
RTC_TimeTypeDef sTimeSet;
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE END PV */
 
void updateTime(void){
	//RTC_TimeTypeDef sTimeGet;
	//RTC_TimeTypeDef sTimeSet;
	HAL_RTC_GetTime(&hrtc, &sTimeGet, RTC_FORMAT_BIN);
	seconds = sTimeGet.Seconds;
	minutes = sTimeGet.Minutes;
	hours = sTimeGet.Hours;
 
	if(hours>12){
		hours = 0;
		sTimeSet.Hours = hours;
		HAL_RTC_SetTime(&hrtc, &sTimeSet, RTC_FORMAT_BIN);
	}
}

This topic has been closed for replies.
Best answer by waclawek.jan

Have you visited the link I provided above?

Btw. this one is documented even in Cube/HAL.

JW

12 replies

waclawek.jan
Super User
January 20, 2021

Do you read date after reading time?

JW

lmill.1
lmill.1Author
Associate
January 20, 2021

No I haven't tried. I likely wont end up using that function.

waclawek.jan
waclawek.janBest answer
Super User
January 20, 2021

Have you visited the link I provided above?

Btw. this one is documented even in Cube/HAL.

JW

KnarfB
Super User
January 20, 2021

Call HAL_RTC_GetDate() afterwards even if you don't use it. There is a note about that in the source code. The topic pops up here frequently.

lmill.1
lmill.1Author
Associate
January 21, 2021

I currently am reading the date after the time. Strangely if i step my way through the time and date my time variables update, but if i run through like normal they don't, do you have any thoughts on this?

void updateTime(void){
	//RTC_TimeTypeDef sTimeGet;
	//RTC_TimeTypeDef sTimeSet;
	RTC_DateTypeDef sDateGet;
	HAL_RTC_GetTime(&hrtc, &sTimeGet, RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc, &sDateGet, RTC_FORMAT_BIN);
	seconds = sTimeGet.Seconds;
	minutes = sTimeGet.Minutes;
	hours = sTimeGet.Hours;
 
	if(hours>12){
		hours = 0;
		sTimeSet.Hours = hours;
		HAL_RTC_SetTime(&hrtc, &sTimeSet, RTC_FORMAT_BIN);
	}
}

KnarfB
Super User
January 21, 2021

You may use STM32CubeIDE to generate default code for init. Mine looks like (STM32L4, RTC running on LSI clock):

/**
 * @brief RTC Initialization Function
 * @param None
 * @retval None
 */
static void MX_RTC_Init(void)
{
 
 /* USER CODE BEGIN RTC_Init 0 */
 
 /* USER CODE END RTC_Init 0 */
 
 RTC_TimeTypeDef sTime = {0};
 RTC_DateTypeDef sDate = {0};
 
 /* USER CODE BEGIN RTC_Init 1 */
 
 /* USER CODE END RTC_Init 1 */
 /** Initialize RTC Only
 */
 hrtc.Instance = RTC;
 hrtc.Init.HourFormat = RTC_HOURFORMAT_12;
 hrtc.Init.AsynchPrediv = 127;
 hrtc.Init.SynchPrediv = 249;
 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
 hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 if (HAL_RTC_Init(&hrtc) != HAL_OK)
 {
 Error_Handler();
 }
 
 /* USER CODE BEGIN Check_RTC_BKUP */
 
 /* USER CODE END Check_RTC_BKUP */
 
 /** Initialize RTC and set the Time and Date
 */
 sTime.Hours = 1;
 sTime.Minutes = 0;
 sTime.Seconds = 0;
 sTime.TimeFormat = RTC_HOURFORMAT12_AM;
 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
 sTime.StoreOperation = RTC_STOREOPERATION_RESET;
 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
 {
 Error_Handler();
 }
 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();
 }
 /* USER CODE BEGIN RTC_Init 2 */
 
 /* USER CODE END RTC_Init 2 */
 
}

and reading

HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

in a loop is sufficient. The RTC can handle AM/PM if configured so.

hth

KnarfB

lmill.1
lmill.1Author
Associate
January 21, 2021

Sorry but I dont understand how this is much different than the init code I already have. What would this do differently?

I am calling my updateTime() function, which contains the getTime getDate, with a timer interrupt and it is being called regularly.

KnarfB
Super User
January 21, 2021

My code is only for your reference, there must be differences, othewise your code would work. Your code snippet does not define sTimeSet, must be defined elsewhere,...

waclawek.jan
Super User
January 21, 2021

When you step through code, is it the same binary as when running? Or, are you using a Debug/Release mechanism to compile with different optimization levels? If called from interior, is the variable where you store time tagged as volatile?

JW