cancel
Showing results for 
Search instead for 
Did you mean: 

STM32Cube RTC Clock issues

gvigelette
Associate II
Posted on August 29, 2014 at 16:09

Hi Everyone, hopefully someone can shed some light on my issue that I keep seeing.  I set the RTC clock from the cellular chipset and get the time every minute with the following results.

Global Date: Aug 29, 2014  Time: 08:49 AM

Updated Time: 08:49 AM

Updated Time: 08:50 AM

Updated Time: 08:51 AM

Updated Time: 08:52 AM

Updated Time: 08:53 AM

Updated Time: 08:54 AM

Updated Time: 08:55 AM

Updated Time: 08:56 AM

Updated Time: 08:57 AM

Updated Time: 08:64 AM

DATA: AT+CCLK?

DATA: +CCLK: ''14/08/29,09:27:16-16''

Setting RTC Clock

Updated Time: 09:28 AM

Updated Time: 09:29 AM

Updated Time: 09:30 AM

Updated Time: 09:31 AM

Updated Time: 09:16 AM

Updated Time: 09:16 AM

Updated Time: 09:17 AM

Updated Time: 09:18 AM

Updated Time: 09:19 AM

Updated Time: 09:20 AM

Updated Time: 09:21 AM

As you can see for the first few minutes the clock updates as expected and then starts to have issues.

I am using STM32Cube with FreeRTOS on a STM32F415VG processor using HSE and LSI for the clock settings.  The following are the functions used to configure, set the time, and update my time string.

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

  RCC_OscInitTypeDef RCC_OscInitStruct;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.LSIState = RCC_LSI_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);

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;

  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

}

/* RTC init function */

void MX_RTC_Init(void)

{

  /**Initialize RTC and set the Time and Date 

  */

  hrtc.Instance = RTC;

  hrtc.Init.HourFormat = RTC_HOURFORMAT_12;

  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;

  HAL_RTC_Init(&hrtc);

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)

{

__HAL_RCC_RTC_ENABLE();

}

int8_t rtc_SetDateTime(char* dateTimeStr)

{

  RTC_TimeTypeDef sTime;

  RTC_DateTypeDef sDate;

uint8_t month;

uint8_t date;

uint8_t year;

uint8_t hour;

uint8_t minute;

uint8_t second;

uint8_t ssecond;

char* pData = NULL;

// parse string

pData = strstr(dateTimeStr, ''\'''');

if(pData != NULL)

{

pData++;

//sscanf(dateTimeStr, ''%hhu/%hhu/%hhu %hhu:%hhu:%hhu'', &sDate.Month, &sDate.Date, &sDate.Year, &sTime.Hours, &sTime.Minutes , &sTime.Seconds);

sscanf(pData, ''%hhu/%hhu/%hhu,%hhu:%hhu:%hhu-%hhu'', &year, &month, &date, &hour, &minute, &second, &ssecond);

if(hour>12)

{

sTime.Hours = hour-12;

sTime.TimeFormat = RTC_HOURFORMAT12_PM;

}

else

{

sTime.Hours = hour;

sTime.TimeFormat = RTC_HOURFORMAT12_AM;

}

sTime.Minutes = minute;

sTime.Seconds = second;

sTime.SubSeconds = ssecond;

sDate.Month = month;

sDate.Date = date;

sDate.Year = year;

sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

sTime.StoreOperation = RTC_STOREOPERATION_RESET;

sDate.WeekDay = RTC_WEEKDAY_FRIDAY;

if(HAL_RTC_SetDate(&hrtc,&sDate,FORMAT_BCD) != HAL_OK)

{

printf(''Error setting the Date\n\r'');

}

if(HAL_RTC_SetTime(&hrtc,&sTime,FORMAT_BCD) != HAL_OK)

{

printf(''Error setting the Time\n\r'');

}

return 1;

}

return 0;

}

int8_t rtc_UpdateTime(void)

{

  RTC_TimeTypeDef sTime;

  

memset(g_timeStr,0,9);

HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BCD);

sprintf(g_timeStr, ''%02hhu:%02hhu %s'', sTime.Hours, sTime.Minutes, sTime.TimeFormat==0x00?''AM'':''PM'');

printf(''Updated Time: %s\n\r'', g_timeStr);

return 1;

}

Any help would be greatly appreciated, and thank you in advance.

George

#whiskey-tango-foxtrot #stm32 #rtc #wakeup #timer
2 REPLIES 2
Posted on August 29, 2014 at 16:38

Updated Time: 08:57 AM

 

Updated Time: 08:64 AM

64!!!?

You are using BCD, this means Hexadecimal like presentation where the high/low nibbles hold 0-9 values, perhaps you want to use binary, 0..59 ?

57 -> 0x39

64 -> 0x40

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gvigelette
Associate II
Posted on August 29, 2014 at 16:49

Thanks Clive,

I just figured it out and re-formatted my functions to use BCD correctly.