cancel
Showing results for 
Search instead for 
Did you mean: 

STM32WB55: Trying to get the RTC running based on HSE_DIV32

apous.1
Associate II

Hi,

I have been trying to use the HSE_DIV32 clock as input for the RTC but without success.

I cannot find any example where this is done even though it's available in the datasheet and the register map.

I have been doing this after enabling the HSE clock to 32MHz:

void RTC_Init(void)
{
    RTC_TimeTypeDef sTime = {0};
    RTC_DateTypeDef sDate = {0};
 
    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;
    hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
    if (HAL_RTC_Init(&hrtc) != HAL_OK)  {
       Error_Handler();
    }
    if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2) {
        sTime.Hours = 0x6;
        sTime.Minutes = 0xa;
        sTime.Seconds = 0x0;
        sTime.SubSeconds = 0x0;
        sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
        sTime.StoreOperation = RTC_STOREOPERATION_RESET;
        if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) {
       Error_Handler();
        }
        sDate.WeekDay = RTC_WEEKDAY_MONDAY;
        sDate.Month = RTC_MONTH_APRIL;
        sDate.Date = 0x16;
        sDate.Year = 0x18;
 
        if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK) {
       Error_Handler();
        }
        HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
    }
    __HAL_RCC_CLEAR_RESET_FLAGS();
}

and

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
  if(hrtc->Instance==RTC)
  {
    RCC_OscInitTypeDef        RCC_OscInitStruct;
    RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
 
    /* Enables the PWR Clock and Enables access to the backup domain */
    /* To change the source clock of the RTC feature (LSE, LSI), You have to:
       - Enable the power clock using __HAL_RCC_PWR_CLK_ENABLE()
       - Enable write access using HAL_PWR_EnableBkUpAccess() function before to
         configure the RTC clock source (to be done once after reset).
       - Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and
         __HAL_RCC_BACKUPRESET_RELEASE().
       - Configure the needed RTc clock source */
    HAL_PWR_EnableBkUpAccess();
 
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
    PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV32;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)  {
        Error_Handler();
    } 
    __HAL_RCC_RTCAPB_CLK_ENABLE();
    __HAL_RCC_RTC_ENABLE();
    __HAL_RCC_RTCAPB_CLK_ENABLE();
 
  }
 
}

I expect the RTC to be a 1Hz clock based on HSE after that.

What I see is that the RTC counters are not changing.

The whole system runs directly on HSE so the clock is running fine.

Kind regards,

2 REPLIES 2
Foued_KH
ST Employee

Hello @apous.1​ 

You should perhaps export the HSE clock via the MCO/PA8 pin and measure the frequency with an accurate frequency counter.

Might want to check the loading on the crystal. If it's way off, perhaps you can tune it better.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Read out and check/post RTC and relevant RCC registers content.

> What I see is that the RTC counters are not changing.

Which countesr? How do you see that? Don't you just try to read RTC_TR without reading RTC_DR?

JW