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.
This commit is contained in:
Silvano Seva 2025-08-12 18:47:09 +02:00
parent 2230c48d5d
commit e9b517abbc
1 changed files with 29 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#ifndef RCC_H
#define RCC_H
#include <hwconfig.h>
#include <stdint.h>
#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<uint32_t>(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 */