Skip to main content
askari.nima
Associate III
May 17, 2015
Question

Stm32f1 cube and rtc problem

  • May 17, 2015
  • 7 replies
  • 1366 views
Posted on May 17, 2015 at 18:38

When I set RTC date , rtc counter do not change.

when turn on again my board   date to 2000/1/1

Stm32f103zc + cube
    This topic has been closed for replies.

    7 replies

    Tesla DeLorean
    Guru
    May 17, 2015
    Posted on May 17, 2015 at 19:03

    I guess you'd need to review the code and how things shutdown, and the response when reinitializing.

    The LSI is not powered in the low-power domain, only the LSE. The STM32's really like you to perform an orderly shutdown, not just pull the primary supply and hope.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    askari.nima
    Associate III
    May 18, 2015
    Posted on May 18, 2015 at 10:42

    I don't underestand  you.

    /* RTC init function */

    void MX_RTC_Init(void)

    {

      RTC_TimeTypeDef sTime;

      RTC_DateTypeDef DateToUpdate;

                /**Initialize RTC and set the Time and Date

                */

                hrtc.Instance = RTC;

                hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;

                hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;

                hrtc.DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;

                hrtc.DateToUpdate.Month = RTC_MONTH_JANUARY;

                hrtc.DateToUpdate.Date = 1;

                hrtc.DateToUpdate.Year = 0;

                HAL_RTC_Init(&hrtc);

        

                sTime.Hours = 0;

                sTime.Minutes = 0;

                sTime.Seconds = 0;

                HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);

            

                HAL_RTC_SetDate(&hrtc, &DateToUpdate, FORMAT_BIN);

    }

    in this code always date set to 2000/01/01

    and in   HAL_RTC_Init function repeat again.

    I have battery backup . when i turn on board again date  set to 2000/01/01

    I remove it , but did not solve.

    when I call hal_rtcSetDate function . RTC counter must be change to higher value;

    this year is 2015. but do not change.

    askari.nima
    Associate III
    May 19, 2015
    Posted on May 19, 2015 at 17:09

    I'm waiting for answer Clive1.

    thanks

    askari.nima
    Associate III
    May 20, 2015
    Posted on May 20, 2015 at 17:13

    I can not set date .. please help me.

    qwer.asdf
    Senior
    May 20, 2015
    Posted on May 20, 2015 at 18:25

    I don't have STM32F103 to test, so this code is taken and simplified from the examples provided by ST from STM32CubeF1. I don't know if this works or not, try to adapt to your code:

    #include ''stm32f1xx_hal.h''
    #include <stdio.h>
    RTC_HandleTypeDef RtcHandle;
    void
    SystemClock_Config(
    void
    );
    static
    void
    RTC_CalendarConfig(
    void
    );
    void
    Error_Handler(
    void
    );
    int
    main(
    void
    )
    {
    /* HAL library initialization:
    - Configure the Flash prefetch
    - Systick timer is configured by default as source of time base, but user 
    can eventually implement his proper time base source (a general purpose 
    timer for example or other time source), keeping in mind that Time base 
    duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
    handled in milliseconds basis.
    - Set NVIC Group Priority to 4
    - Low Level Initialization
    */
    HAL_Init();
    /* Configure the system clock to 72 MHz */
    SystemClock_Config();
    /*##-1- Configure the RTC peripheral #######################################*/
    /* Configure RTC prescaler and RTC data registers */
    /* RTC configured as follow:
    - Asynch Prediv = Calculated automatically by HAL */
    RtcHandle.Instance = RTC; 
    RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
    if
    (HAL_RTC_Init(&RtcHandle) != HAL_OK)
    {
    /* Initialization Error */
    Error_Handler();
    }
    /*##-2- Check if Data stored in BackUp register1: No Need to reconfigure RTC#*/
    /* Read the Back Up Register 1 Data */
    if
    (HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR1) != 0x32F2)
    {
    /* Configure RTC Calendar */
    RTC_CalendarConfig();
    }
    else
    {
    /* Check if the Power On Reset flag is set */
    if
    (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
    {
    /* Power on reset occured */
    }
    /* Check if Pin Reset flag is set */
    if
    (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
    {
    /* External reset occured */
    }
    /* Clear source Reset Flag */
    __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    /* Infinite loop */
    while
    (1)
    {
    }
    }
    /**
    * @brief System Clock Configuration
    * The system Clock is configured as follow : 
    * System Clock source = PLL (HSE)
    * SYSCLK(Hz) = 72000000
    * HCLK(Hz) = 72000000
    * AHB Prescaler = 1
    * APB1 Prescaler = 2
    * APB2 Prescaler = 1
    * HSE Frequency(Hz) = 8000000
    * HSE PREDIV1 = 1
    * PLLMUL = 9
    * Flash Latency(WS) = 2
    * @param None
    * @retval None
    */
    void
    SystemClock_Config(
    void
    )
    {
    RCC_ClkInitTypeDef clkinitstruct = {0};
    RCC_OscInitTypeDef oscinitstruct = {0};
    /* Enable HSE Oscillator and activate PLL with HSE as source */
    oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    oscinitstruct.HSEState = RCC_HSE_ON;
    oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    oscinitstruct.PLL.PLLState = RCC_PLL_ON;
    oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL9;
    if
    (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)
    {
    /* Initialization Error */
    while
    (1);
    }
    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 
    clocks dividers */
    clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
    clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2; 
    if
    (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)
    {
    /* Initialization Error */
    while
    (1);
    }
    }
    /**
    * @brief This function is executed in case of error occurrence.
    * @param None
    * @retval None
    */
    void
    Error_Handler(
    void
    )
    {
    while
    (1)
    {
    }
    }
    /**
    * @brief Configure the current time and date.
    * @param None
    * @retval None
    */
    static
    void
    RTC_CalendarConfig(
    void
    )
    {
    RTC_DateTypeDef sdatestructure;
    RTC_TimeTypeDef stimestructure;
    /*##-1- Configure the Date #################################################*/
    /* Set Date: Tuesday February 18th 2014 */
    sdatestructure.Year = 0x14;
    sdatestructure.Month = RTC_MONTH_FEBRUARY;
    sdatestructure.Date = 0x18;
    sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
    if
    (HAL_RTC_SetDate(&RtcHandle,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK)
    {
    /* Initialization Error */
    Error_Handler();
    }
    /*##-2- Configure the Time #################################################*/
    /* Set Time: 02:00:00 */
    stimestructure.Hours = 0x02;
    stimestructure.Minutes = 0x00;
    stimestructure.Seconds = 0x00;
    if
    (HAL_RTC_SetTime(&RtcHandle, &stimestructure, RTC_FORMAT_BCD) != HAL_OK)
    {
    /* Initialization Error */
    Error_Handler();
    }
    /*##-3- Writes a data in a RTC Backup data Register1 #######################*/
    HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_DR1, 0x32F2);
    }
    #ifdef USE_FULL_ASSERT
    /**
    * @brief Reports the name of the source file and the source line number
    * where the assert_param error has occurred.
    * @param file: pointer to the source file name
    * @param line: assert_param error line source number
    * @retval None
    */
    void
    assert_failed(uint8_t *file, uint32_t line)
    {
    /* User can add his own implementation to report the file name and line number,
    ex: printf(''Wrong parameters value: file %s on line %d
    
    '', file, line) */
    /* Infinite loop */
    while
    (1)
    {
    }
    }
    #endif

    askari.nima
    Associate III
    May 20, 2015
    Posted on May 20, 2015 at 20:21

    I test it. But not work.

    Date reset after power up.

    When I call hal_rtc_setdate function , my RTC counter register , do not change.

    I do it with old stm library and work it. but this new cube library dose not work.

    I2c library has a problem too.
    justas23
    Associate II
    August 2, 2015
    Posted on August 02, 2015 at 22:31

    Maybe you haven't read this (in ..hal_rtc.c):

        (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.

            User should implement a way to save date before entering in low power mode (an

            example is provided with firmware package based on backup registers)