7 years, 2 months ago.

How to use LSE for RTC on the Nucleo-L476RG

Hello, I am configuring RTC with LSE, the crystal is on-board (X2) but I can't see it oscillate with my scope. However the RTC is working well and I can get the time without problem. I don't understand what I am doing wrong. Any help would be appreciated.

Here is my code :

    __HAL_RCC_PWR_CLK_ENABLE();
    HAL_PWR_EnableBkUpAccess();
    
      // Reset Backup domain
    __HAL_RCC_BACKUPRESET_FORCE();
    __HAL_RCC_BACKUPRESET_RELEASE();
    
    RCC_OscInitTypeDef              handle_RCC_Osc;
    RCC_PeriphCLKInitTypeDef  handle_RCC_Clk;

    handle_RCC_Osc.OscillatorType = RCC_OSCILLATORTYPE_LSE;
    handle_RCC_Osc.PLL.PLLState = RCC_PLL_NONE;
    handle_RCC_Osc.LSEState = RCC_LSE_ON;
    handle_RCC_Osc.LSIState = RCC_LSI_OFF;
    if(HAL_RCC_OscConfig(&handle_RCC_Osc) != HAL_OK)
    { 
        pc.printf("Error HAL_RCC_OscConfig \r\n");   
    }
    
    handle_RCC_Clk.PeriphClockSelection = RCC_PERIPHCLK_RTC;
    handle_RCC_Clk.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
    if(HAL_RCCEx_PeriphCLKConfig(&handle_RCC_Clk) != HAL_OK)
    { 
        pc.printf("Error HAL_RCCEx_PeriphCLKConfig \r\n");   
    }
    
    // Enable RTC Clock
    __HAL_RCC_RTC_ENABLE(); 
    
    handleRTC.Instance = RTC;
    handleRTC.Init.HourFormat     = RTC_HOURFORMAT_24;
    handleRTC.Init.AsynchPrediv   = 0x7F;
    handleRTC.Init.SynchPrediv    = 0x00FF;
    handleRTC.Init.OutPut         = RTC_OUTPUT_DISABLE;
    handleRTC.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
    handleRTC.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
    
    if(HAL_RTC_Init(&handleRTC) != HAL_OK)
    {
        pc.printf("Error Hal_RTC_init \r\n");   
    }
      
    RTC_DateTypeDef date;
    date.WeekDay = RTC_WEEKDAY_WEDNESDAY;
    date.Month   = RTC_MONTH_AUGUST;
    date.Date    = 31;
    date.Year    = 16;
    
    RTC_TimeTypeDef time;
    time.Hours      = 15;
    time.Minutes    = 42;
    time.Seconds    = 0;
    time.TimeFormat = RTC_HOURFORMAT12_PM;

    
    if (HAL_RTC_SetDate(&handleRTC, &date, RTC_FORMAT_BIN) != HAL_OK)
        pc.printf("Error Set Date \r\n");
         
    if (HAL_RTC_SetTime(&handleRTC, &time, RTC_FORMAT_BIN) != HAL_OK)
        pc.printf("Error Set Time \r\n");     

Thanks.

1 Answer

7 years, 2 months ago.

Hi Jonathan, my understanding is that the LSE is turned on by default using mbed. I use the RTC on the 476 Nucleo, it works. You don't need to do anything with the HAL APIs. I use the mbed API set_time() to set the time and time()/localtime() to read the time. For Wakeups I use the "WakeUpRTC" library from Erik Olieman available here at mbed, his library calls “rtc_init()”

You will not see the crystal clock with a scope because the load of the scope is too much for the crystal.

Regards Helmut

Accepted Answer

Hi Helmut, thank you for your answer. Now it's working, I was focused on my scope and I didn't realize that LSE was turned on..

posted by Jonathan CONSTANT 25 Jan 2017