cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042 RTC wont start

lmill.1
Associate II

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);
	}
}

1 ACCEPTED SOLUTION

Accepted Solutions

Have you visited the link I provided above?

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

JW

View solution in original post

12 REPLIES 12

Do you read date after reading time?

JW

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

KnarfB
Principal III

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.

Have you visited the link I provided above?

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

JW

No I had completely missed it. I checked it out now.

I tried both, together and independently and I am got the same result.

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);
	}
}

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
Principal III

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

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.

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,...