Skip to main content
shybiss
Associate
January 15, 2019
Question

How to turn on backup RTC registers on STM32F051R8T6

  • January 15, 2019
  • 0 replies
  • 770 views

Hello, i have probem with backup registers, which are powered from VBAT pin on Dicsovery board.

First problem was CubeMX init, where i can't choose LSE source, because the value of clock box, was not active. I tried differend things like change txt code for CubeMX project, and after code generation, the value changed again.

So I left CubeMX, and change code, first i comment 2 lines with LSI :

// in
void SystemClock_Config(void)
{
...
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; // | RCC_OSCILLATORTYPE_LSI;
 // RCC_OscInitStruct.LSIState = RCC_LSI_ON;
...
}

next i add my init :

void LSE_test(void){
 
	if (HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0) != BKP_VALUE)
	{
		LSE_init();
		MX_RTC_Init();
 
	}else{
 
		 __HAL_RCC_PWR_CLK_ENABLE();
		 HAL_PWR_EnableBkUpAccess();
		 HAL_RTC_WaitForSynchro(&hrtc);
 
		 assert_param(IS_RTC_CLEAR_FLAG(RTC_ISR_ALRAF));
 
		 /* Clear the Flags in the RTC_ISR register */
		 RTC->ISR = (uint32_t)((uint32_t)(~((RTC_ISR_ALRAF | RTC_ISR_INIT)& 0x0001FFFF) | (uint32_t)(RTC->ISR & RTC_ISR_INIT)));
	}
}
 
void LSE_init(void)
{
 
	 RCC_OscInitTypeDef RCC_OscInitStruct;
	 RCC_PeriphCLKInitTypeDef PeriphClkInit;
 
	 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
	 RCC_OscInitStruct.LSEState= RCC_LSE_ON;
	 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
	 {
	 _Error_Handler(__FILE__, __LINE__);
	 }
 
	 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
	 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
	 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
	 {
	 _Error_Handler(__FILE__, __LINE__);
	 }
}
 
// and cubemx function :
void MX_RTC_Init(void)
{
 RTC_TimeTypeDef sTime;
 RTC_DateTypeDef sDate;
 
 // Enable Power clock
 __HAL_RCC_PWR_CLK_ENABLE();
 // Enable access to Backup domain
 HAL_PWR_EnableBkUpAccess();
 
// // Reset Backup domain
// __HAL_RCC_BACKUPRESET_FORCE();
// __HAL_RCC_BACKUPRESET_RELEASE();
 
 /**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 = 0x10;
 sTime.Minutes = 0x15;
 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_FRIDAY;
 sDate.Month = RTC_MONTH_JANUARY;
 sDate.Date = 0x4;
 sDate.Year = 0x19;
 
 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 
 
 /* USER CODE BEGIN RTC_Init 4 */
 
 __HAL_RCC_RTC_ENABLE();
 
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,BKP_VALUE);
 
 /* USER CODE END RTC_Init 4 */
 
}

As you can see, I tried to place the code in different places, because I found various configurations in forum posts.

I modeled on the StandardPheriph library to reproduce the code.

In the reference manual there is no recipe for the use of backup registers. I find only that in std_lib for f0 :

 ##### Backup Domain Access #####
 ===============================================================================
 [..] After reset, the backup domain (RTC registers and RTC backup data 
 registers) is protected against possible unwanted write accesses. 
 [..] To enable access to the Backup Domain and RTC registers, proceed as follows:
 (#) Enable the Power Controller (PWR) APB1 interface clock using the
 RCC_APB1PeriphClockCmd() function.
 (#) Enable access to Backup domain using the PWR_BackupAccessCmd() function.
 (#) Select the RTC clock source using the RCC_RTCCLKConfig() function.
 (#) Enable RTC Clock using the RCC_RTCCLKCmd() function.

If you have some idea, what should I change, I will be grateful. Thanks!!

This topic has been closed for replies.