From e9b517abbc1853eb7ae1a6d3d9672a9ca3245e7a Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Tue, 12 Aug 2025 18:47:09 +0200 Subject: [PATCH] STM32F4: rcc: added rcc_getPeriphClock function Added function to retrieve the clock frequency of the bus a peripheral is attached to, given its base address. The function is C++ only and relies on constexpr mechanism to have zero memory footprint. --- platform/mcu/STM32F4xx/drivers/rcc.h | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/platform/mcu/STM32F4xx/drivers/rcc.h b/platform/mcu/STM32F4xx/drivers/rcc.h index 246fdebd..1d2d46d0 100644 --- a/platform/mcu/STM32F4xx/drivers/rcc.h +++ b/platform/mcu/STM32F4xx/drivers/rcc.h @@ -22,6 +22,7 @@ #ifndef RCC_H #define RCC_H +#include #include #ifdef __cplusplus @@ -50,6 +51,34 @@ uint32_t rcc_getBusClock(const uint8_t bus); #ifdef __cplusplus } + +/** + * Get the clock frequency of the bus a peripheral is attached to. + * + * @param periph: peripheral base address + * @return bus clock frequency in Hz or zero in case of errors. + */ +static constexpr uint32_t rcc_getPeriphClock(const void *periph) +{ + uint32_t addr = reinterpret_cast(periph); + + switch(addr & 0xFFFF0000) { + case APB1PERIPH_BASE: + return rcc_getBusClock(PERIPH_BUS_APB1); + break; + + case APB2PERIPH_BASE: + return rcc_getBusClock(PERIPH_BUS_APB2); + break; + + case AHB1PERIPH_BASE: + case AHB2PERIPH_BASE: + return rcc_getBusClock(PERIPH_BUS_AHB); + break; + } + + return 0; +} #endif #endif /* RCC_H */