AnsweredAssumed Answered

STM32F4-Discovery RTC only counting in debug

Question asked by martins.jose.001 on Jan 17, 2016
Latest reply on Jan 18, 2016 by waclawek.jan
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);
     
}

Outcomes