Skip to main content
a60141
Associate
January 17, 2016
Question

STM32F4-Discovery RTC only counting in debug

  • January 17, 2016
  • 4 replies
  • 1264 views
Posted on January 17, 2016 at 22:04

Hello. I am having a bit of a problem using the RTC in the STM32F4-DISCOVERY board. I am using the HAL libraries, and have configured the RTC using the LSI based on a number of examples I found online. Basically I am sending some information wirelessly to another system, which contains the time that information was generated. However the RTC only seems to work in debug mode. When I get out of debug mode, it seems that the RTC is stopped and the remote system receives always the same date. Any ideas on why this is happening? Here is the code I use to configure the clocks and the RTC:

void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
void RTC_ClockConfig(){
RCC_OscInitTypeDef osc_init;
RCC_PeriphCLKInitTypeDef perif_init;
osc_init.PLL.PLLState = RCC_PLL_NONE;
osc_init.OscillatorType = RCC_OSCILLATORTYPE_LSI;
osc_init.LSIState = RCC_LSI_ON;
perif_init.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
HAL_RCC_OscConfig(&osc_init);
HAL_RCCEx_PeriphCLKConfig(&perif_init);
__HAL_RCC_RTC_ENABLE();
}
void RTC_Init(void){
rtc_handle.Instance = RTC;
rtc_handle.Init.HourFormat = RTC_HOURFORMAT_24;
rtc_handle.Init.AsynchPrediv = RTC_ASYNC_PREDIV;
rtc_handle.Init.SynchPrediv = RTC_SYNC_PREDIV;
rtc_handle.Init.OutPut = RTC_OUTPUT_DISABLE;
rtc_handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
rtc_handle.Init.OutPutType = RTC_OUTPUT_TYPE_PUSHPULL;
__HAL_RCC_PWR_CLK_ENABLE(); 
HAL_PWR_EnableBkUpAccess();
RTC_ClockConfig();
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
__HAL_RCC_RTC_ENABLE();
HAL_RTC_Init(&rtc_handle);
}

#stm32f4-rtc-rcc-lsi
This topic has been closed for replies.

4 replies

a60141
a60141Author
Associate
January 18, 2016
Posted on January 18, 2016 at 15:02

A little update. Only when I am watching the registers in the KEIL debug peripherals viewer it works. Otherwise the RTC stops.

a60141
a60141Author
Associate
January 18, 2016
Posted on January 18, 2016 at 15:18

Solved this, but didn't really understand how. I enabled the BYPSHAD bit in the RTC CR to access the registers directly, not through the shadow registers and it started working. I think the shadow registers were not being updated, but somehow watching the registers in the debug mode, made those registers being updated. If someone really understands how this works, please post here.

waclawek.jan
Super User
January 18, 2016
Posted on January 18, 2016 at 15:21

And when you connect with the debugger to a board which previously run without the debugger (i.e. ''clock not running''), what is the time?

JW

waclawek.jan
Super User
January 18, 2016
Posted on January 18, 2016 at 15:26

> If someone really understands how this works, please post here.

It's described in details in RM0090 rev.11, ch.26.3.6.

JW