@@ -94,6 +94,9 @@ struct rtc_stm32_config {
94
94
struct counter_config_info counter_info ;
95
95
LL_RTC_InitTypeDef ll_rtc_config ;
96
96
const struct stm32_pclken * pclken ;
97
+ #if DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_HSE
98
+ uint32_t hse_prescaler ;
99
+ #endif
97
100
};
98
101
99
102
struct rtc_stm32_data {
@@ -593,6 +596,11 @@ static int rtc_stm32_init(const struct device *dev)
593
596
}
594
597
#endif
595
598
599
+ #if DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_HSE
600
+ /* Must be configured before selecting the RTC clock source */
601
+ LL_RCC_SetRTC_HSEPrescaler (cfg -> hse_prescaler );
602
+ #endif
603
+
596
604
if (LL_RTC_Init (RTC , ((LL_RTC_InitTypeDef * )
597
605
& cfg -> ll_rtc_config )) != SUCCESS ) {
598
606
return - EIO ;
@@ -623,28 +631,65 @@ static struct rtc_stm32_data rtc_data;
623
631
624
632
static const struct stm32_pclken rtc_clk [] = STM32_DT_INST_CLOCKS (0 );
625
633
634
+ #if DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_HSE
635
+ #if STM32_HSE_FREQ % MHZ (1 ) != 0
636
+ #error RTC clock source HSE frequency should be whole MHz
637
+ #elif STM32_HSE_FREQ < MHZ (16 ) && defined(LL_RCC_RTC_HSE_DIV_16 )
638
+ #define RTC_HSE_PRESCALER LL_RCC_RTC_HSE_DIV_16
639
+ #define RTC_HSE_FREQUENCY (STM32_HSE_FREQ / 16)
640
+ #elif STM32_HSE_FREQ < MHZ (32 ) && defined(LL_RCC_RTC_HSE_DIV_32 )
641
+ #define RTC_HSE_PRESCALER LL_RCC_RTC_HSE_DIV_32
642
+ #define RTC_HSE_FREQUENCY (STM32_HSE_FREQ / 32)
643
+ #elif STM32_HSE_FREQ < MHZ (64 ) && defined(LL_RCC_RTC_HSE_DIV_64 )
644
+ #define RTC_HSE_PRESCALER LL_RCC_RTC_HSE_DIV_64
645
+ #define RTC_HSE_FREQUENCY (STM32_HSE_FREQ / 64)
646
+ #else
647
+ #error RTC does not support HSE frequency
648
+ #endif
649
+ #define RTC_HSE_ASYNC_PRESCALER 125
650
+ #define RTC_HSE_SYNC_PRESCALER (RTC_HSE_FREQUENCY / RTC_HSE_ASYNC_PRESCALER)
651
+ #endif /* DT_INST_CLOCKS_CELL_BY_IDX(0, 1, bus) == STM32_SRC_HSE */
652
+
626
653
static const struct rtc_stm32_config rtc_config = {
627
654
.counter_info = {
628
655
.max_top_value = UINT32_MAX ,
629
656
#ifndef CONFIG_COUNTER_RTC_STM32_SUBSECONDS
630
657
/* freq = 1Hz for not subsec based driver */
631
658
.freq = RTCCLK_FREQ / ((RTC_ASYNCPRE + 1 ) * (RTC_SYNCPRE + 1 )),
632
- #else /* ! CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
659
+ #else /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
633
660
.freq = RTCCLK_FREQ / (RTC_ASYNCPRE + 1 ),
634
- #endif /* CONFIG_COUNTER_RTC_STM32_SUBSECONDS */
661
+ #endif
635
662
.flags = COUNTER_CONFIG_INFO_COUNT_UP ,
636
663
.channels = 1 ,
637
664
},
638
665
.ll_rtc_config = {
639
- .AsynchPrescaler = RTC_ASYNCPRE ,
666
+ #if DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_LSI || \
667
+ DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_LSE
668
+ .AsynchPrescaler = DT_INST_PROP_OR (0 , async_prescaler , RTC_ASYNCPRE ),
640
669
#if !defined (CONFIG_SOC_SERIES_STM32F1X )
641
670
.HourFormat = LL_RTC_HOURFORMAT_24HOUR ,
642
- .SynchPrescaler = RTC_SYNCPRE ,
643
- #else /* CONFIG_SOC_SERIES_STM32F1X */
671
+ .SynchPrescaler = DT_INST_PROP_OR ( 0 , sync_prescaler , RTC_SYNCPRE ) ,
672
+ #else /* CONFIG_SOC_SERIES_STM32F1X */
644
673
.OutPutSource = LL_RTC_CALIB_OUTPUT_NONE ,
645
- #endif /* CONFIG_SOC_SERIES_STM32F1X */
674
+ #endif /* !CONFIG_SOC_SERIES_STM32F1X */
675
+ #elif DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_HSE
676
+ .AsynchPrescaler =
677
+ DT_INST_PROP_OR (0 , async_prescaler , _HSE_ASYNC_PRESCALER - 1 ),
678
+ #if !defined (CONFIG_SOC_SERIES_STM32F1X )
679
+ .HourFormat = LL_RTC_HOURFORMAT_24HOUR ,
680
+ .SynchPrescaler =
681
+ DT_INST_PROP_OR (0 , hse_prescaler , RTC_HSE_SYNC_PRESCALER - 1 ),
682
+ #else /* CONFIG_SOC_SERIES_STM32F1X */
683
+ .OutPutSource = LL_RTC_CALIB_OUTPUT_NONE ,
684
+ #endif /* !CONFIG_SOC_SERIES_STM32F1X */
685
+ #else
686
+ #error Invalid RTC SRC
687
+ #endif
646
688
},
647
689
.pclken = rtc_clk ,
690
+ #if DT_INST_CLOCKS_CELL_BY_IDX (0 , 1 , bus ) == STM32_SRC_HSE
691
+ .hse_prescaler = DT_INST_PROP_OR (0 , hse_prescaler , RTC_HSE_PRESCALER ),
692
+ #endif
648
693
};
649
694
650
695
#ifdef CONFIG_PM_DEVICE
0 commit comments