cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 RTC Problem

malcolm2
Associate II
Posted on September 01, 2014 at 13:25

I have a STM32F405RG with external 16MHz crystal which is also used, divided by 16 as the RTC clock source.  The project has been built using VisualGDB.

The problem is that the RTC appears to stop after it is read.  I have reduced the code down to a minimum to investigate this problem.  If  put a breakpoint on the first read then wait a known length of time the clock reads OK and the elapsed time is correct.  Subsequent reads show the same time.

The code is shown below.  Any ideas what is happening?

RTC_TimeTypeDef sTime;

RTC_DateTypeDef sDate;

RTC_HandleTypeDef hrtc;

&sharpifdef __cplusplus

extern ''C''

&sharpendif

void SysTick_Handler(void)

{

    HAL_IncTick();

}

int main(void)

{

    HAL_Init();

    init_clocks();

    init_Gpio();

    HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BCD);

    while (1)

    {

        HAL_GPIO_TogglePin(EVENT_PORT, EVENT_LED_PIN);

        HAL_Delay(500);

        HAL_RTC_GetTime(&hrtc, &sTime, FORMAT_BCD);

    }

}

void init_clocks(void)

{

    RCC_ClkInitTypeDef RCC_ClkInitStruct;

    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

    RCC_OscInitTypeDef RCC_OscInitStruct;

    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;

    PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV16;

    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

    __HAL_RCC_RTC_ENABLE();

    __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 = 16;

    RCC_OscInitStruct.PLL.PLLN = 320;

    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

    RCC_OscInitStruct.PLL.PLLQ = 4;

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

    hrtc.Instance = RTC;

    hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

    hrtc.Init.AsynchPrediv = 124;

    hrtc.Init.SynchPrediv = 7999;

    hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

    hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

    hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

    HAL_RTC_Init(&hrtc);

    sTime.Hours = 1;

    sTime.Minutes = 1;

    sTime.Seconds = 1;

    sTime.SubSeconds = 0;

    sTime.TimeFormat = RTC_HOURFORMAT_24;

    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

    sTime.StoreOperation = RTC_STOREOPERATION_RESET;

    HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);

    sDate.WeekDay = RTC_WEEKDAY_MONDAY;

    sDate.Month = RTC_MONTH_JANUARY;

    sDate.Date = 1;

    sDate.Year = 0;

    HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);

}

void init_Gpio(void)

{

    GPIO_InitTypeDef GPIO_InitStruct;

    __GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = EVENT_LED_PIN | EVENT1_LED_PIN;

    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

#stm32f4 #rtc #visualgdb
3 REPLIES 3
malcolm2
Associate II
Posted on September 01, 2014 at 14:16

Gets ever more peculiar.

I have got the clock updating OK now.

I added a HAL_RTC_GetDate(&hrtc, &sDate, FORMAT_BCD) command after the time read.

It looks as though it is a bug!

Not a major issue for me as most of the time I do need to read both but it should not be necessary as it wastes processor time.

davidkind9
Associate II
Posted on March 18, 2015 at 17:32

Thanks for the post, had the exact same problem and was pulling my hair out.

Reading the date immediately afterwards does indeed fix the problem.

Posted on March 18, 2015 at 18:25

It all boils down to Reading of The Fine Manual.

[STM32Cube_FW_F4_V1.4.0]\Drivers\STM32F4xx_HAL_Driver\STM32F417xx_User_Manual.chm , description of () and also :

Note:

You must call after to unlock the values in the higher-order calendar shadow registers to ensure consistency between the time and date values. Reading RTC current time locks the values in calendar shadow registers until Current date is read.

Return values: