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

12 REPLIES 12

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

The sTimeSet is global, lines 49 and 50 of the snippet............. i have working reference code that i also mentioned in the original post, and i did use cubemx to generate this code, which i also mentioned in the original post..............

I got it working though, as you can see in the other thread we had going it was only working if i stepped through the code. The problem was I was running my set time function in my main while loop that I had forgotten to move after debugging so it was writing the time and writing over the read results before they could be used.

thanks for your input.

Got it working, my setTime function was in my main loop and overwriting the values before they could get used. Thanks for your advice.