Skip to main content
anhhuy0701
Associate III
May 28, 2014
Question

REAL TIME CLOCK (RTC)

  • May 28, 2014
  • 2 replies
  • 876 views
Posted on May 28, 2014 at 06:33

hello everyone

today I try to use RTC in stm32f4 cube library and some errors occur.

So now I will show my code for all of you see:

void

dtime_bsp_open (const dtime_time_format_t fmt)

{

    /* Configure the RTC peripheral */

    g_h_rtc.Instance = RTC;

    if (fmt == DTIME_FORMAT_12H)

    {

        g_h_rtc.Init.HourFormat = RTC_HOURFORMAT_12;

    }

    else

    {

        g_h_rtc.Init.HourFormat = RTC_HOURFORMAT_24;

    }

    g_h_rtc.Init.AsynchPrediv = 0x007F; // datasheet rm_0090_stm32f4 22.3.9 page 544

    g_h_rtc.Init.SynchPrediv = 0x00FF;  // 22.6.5 page 559

    g_h_rtc.Init.OutPut = RTC_OUTPUT_DISABLE;

    g_h_rtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH ;

    g_h_rtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

    /*Initialization*/

   

if (HAL_RTC_Init(&g_h_rtc)!= HAL_OK)

    {

        while(1);

    }

}

and program was loop in red part. Can anyones help me about my configure is wrong??

#lse
    This topic has been closed for replies.

    2 replies

    francescatodiego
    Associate III
    May 29, 2014
    Posted on May 29, 2014 at 10:29

    I reply but I don't know the cube library

    With this init code RTC work on my F407 device

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    static void RTC_RCC_config()
    {
    /* Enable the LSE OSC */
    RCC_LSEConfig(RCC_LSE_ON);
    /* Wait till LSE is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) ;
    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void RTC_Drv_Init()
    {
    /* Allow access to RTC */
    PWR_BackupAccessCmd(ENABLE);
    RTC_RCC_config() ;
    RTC_InitTypeDef RTC_InitStructure;
    RTC_DateTypeDef RTC_DateStructure;
    RTC_TimeTypeDef RTC_TimeStructure;
    if (RTC_ReadBackupRegister(RTC_BKP_DR0) != RTC_BACKUP_ID)
    {
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
    /* Configure the RTC data register and RTC prescaler */
    RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
    RTC_InitStructure.RTC_SynchPrediv = 0xFF;
    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
    RTC_Init(&RTC_InitStructure);
    RTC_DateStructure.RTC_Year = RTC_CFG_YROFFS;
    RTC_DateStructure.RTC_Month = RTC_CFG_MONTH+1 ;
    RTC_DateStructure.RTC_Date = RTC_CFG_DAY ;
    RTC_DateStructure.RTC_WeekDay = RTC_CFG_DAYOFW;
    RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure);
    RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
    RTC_TimeStructure.RTC_Hours = RTC_CFG_HOUR;
    RTC_TimeStructure.RTC_Minutes = RTC_CFG_MIN ;
    RTC_TimeStructure.RTC_Seconds = RTC_CFG_SEC ;
    RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
    /* Indicator for the RTC configuration */
    RTC_WriteBackupRegister(RTC_BKP_DR0, RTC_BACKUP_ID);
    }
    else
    {
    RTC_hw_read (&RTC_DateStructure, &RTC_TimeStructure) ;
    }
    }
     static void RTC_hw_read (RTC_DateTypeDef* ldate, RTC_TimeTypeDef* ltime)
     {
     RTC_GetTime(RTC_Format_BIN, ltime);
     RTC_GetDate(RTC_Format_BIN, ldate);
     }
    

    jpeacock2399
    Associate III
    May 29, 2014
    Posted on May 29, 2014 at 15:57

    I don't use the cube library but the obvious question is if your LSE oscillator works.  My guess is your program hangs trying to start the LSE.  Are the caps correctly matched to the crystal?  ST has an app note on how to configure crystal oscillators for STM32.

      Jack Peacock