cpu: boards: smt32l4: rework clock configuration
This commit is contained in:
parent
95057a8aef
commit
0745cc4a99
@ -12,14 +12,14 @@
|
|||||||
* @{
|
* @{
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
* @brief Configure STM32L4 clock using 80MHz core clock and LSE (32.768kHz)
|
* @brief Default STM32L4 clock configuration
|
||||||
*
|
*
|
||||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||||
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef L4_CFG_CLOCK_80_1_H
|
#ifndef L4_CFG_CLOCK_DEFAULT_H
|
||||||
#define L4_CFG_CLOCK_80_1_H
|
#define L4_CFG_CLOCK_DEFAULT_H
|
||||||
|
|
||||||
#include "periph_cpu.h"
|
#include "periph_cpu.h"
|
||||||
|
|
||||||
@ -31,55 +31,153 @@ extern "C" {
|
|||||||
* @name Clock system configuration
|
* @name Clock system configuration
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
/* 0: no external high speed crystal available
|
#ifndef CONFIG_USE_CLOCK_PLL
|
||||||
* else: actual crystal frequency [in Hz] */
|
#if IS_ACTIVE(CONFIG_USE_CLOCK_HSE) || IS_ACTIVE(CONFIG_USE_CLOCK_HSI) || \
|
||||||
#define CLOCK_HSE (0)
|
IS_ACTIVE(CONFIG_USE_CLOCK_MSI)
|
||||||
|
#define CONFIG_USE_CLOCK_PLL (0)
|
||||||
|
#else
|
||||||
|
#define CONFIG_USE_CLOCK_PLL (1) /* Use PLL by default */
|
||||||
|
#endif
|
||||||
|
#endif /* CONFIG_USE_CLOCK_PLL */
|
||||||
|
|
||||||
/* 0: no external low speed crystal available,
|
#ifndef CONFIG_USE_CLOCK_MSI
|
||||||
* 1: external crystal available (always 32.768kHz)
|
#define CONFIG_USE_CLOCK_MSI (0)
|
||||||
*/
|
#endif /* CONFIG_USE_CLOCK_MSI */
|
||||||
#define CLOCK_LSE (1)
|
|
||||||
|
|
||||||
/* 0: enable MSI only if HSE isn't available
|
#ifndef CONFIG_USE_CLOCK_HSE
|
||||||
* 1: always enable MSI (e.g. if USB or RNG is used)*/
|
#define CONFIG_USE_CLOCK_HSE (0)
|
||||||
#define CLOCK_MSI_ENABLE (1)
|
#endif /* CONFIG_USE_CLOCK_HSE */
|
||||||
|
|
||||||
/* 0: disable Hardware auto calibration with LSE
|
#ifndef CONFIG_USE_CLOCK_HSI
|
||||||
* 1: enable Hardware auto calibration with LSE (PLL-mode)
|
#define CONFIG_USE_CLOCK_HSI (0)
|
||||||
* LSE is mandatory for MSI/LSE-trimming to work */
|
#endif /* CONFIG_USE_CLOCK_HSI */
|
||||||
#define CLOCK_MSI_LSE_PLL (1)
|
|
||||||
|
|
||||||
/* give the target core clock (HCLK) frequency [in Hz], maximum: 80MHz */
|
#if CONFIG_USE_CLOCK_PLL && \
|
||||||
#define CLOCK_CORECLOCK (80000000U)
|
(CONFIG_USE_CLOCK_MSI || CONFIG_USE_CLOCK_HSE || CONFIG_USE_CLOCK_HSI)
|
||||||
/* PLL configuration: make sure your values are legit!
|
#error "Cannot use PLL as clock source with other clock configurations"
|
||||||
*
|
#endif
|
||||||
* compute by: CORECLOCK = (((PLL_IN / M) * N) / R)
|
|
||||||
* with:
|
#if CONFIG_USE_CLOCK_MSI && \
|
||||||
* PLL_IN: input clock, HSE or MSI @ 48MHz
|
(CONFIG_USE_CLOCK_PLL || CONFIG_USE_CLOCK_HSI || CONFIG_USE_CLOCK_HSE)
|
||||||
* M: pre-divider, allowed range: [1:8]
|
#error "Cannot use MSI as clock source with other clock configurations"
|
||||||
* N: multiplier, allowed range: [8:86]
|
#endif
|
||||||
* R: post-divider, allowed range: [2,4,6,8]
|
|
||||||
*
|
#if CONFIG_USE_CLOCK_HSE && \
|
||||||
* Also the following constraints need to be met:
|
(CONFIG_USE_CLOCK_PLL || CONFIG_USE_CLOCK_MSI || CONFIG_USE_CLOCK_HSI)
|
||||||
* (PLL_IN / M) -> [4MHz:16MHz]
|
#error "Cannot use HSE as clock source with other clock configurations"
|
||||||
* (PLL_IN / M) * N -> [64MHz:344MHz]
|
#endif
|
||||||
* CORECLOCK -> 80MHz MAX!
|
|
||||||
*/
|
#if CONFIG_USE_CLOCK_HSI && \
|
||||||
#define CLOCK_PLL_M (6)
|
(CONFIG_USE_CLOCK_PLL || CONFIG_USE_CLOCK_MSI || CONFIG_USE_CLOCK_HSE)
|
||||||
#define CLOCK_PLL_N (20)
|
#error "Cannot use HSI as clock source with other clock configurations"
|
||||||
#define CLOCK_PLL_R (2)
|
#endif
|
||||||
/* peripheral clock setup */
|
|
||||||
#define CLOCK_AHB_DIV RCC_CFGR_HPRE_DIV1
|
#ifndef CONFIG_BOARD_HAS_HSE
|
||||||
#define CLOCK_AHB (CLOCK_CORECLOCK / 1)
|
#define CONFIG_BOARD_HAS_HSE (0)
|
||||||
#define CLOCK_APB1_DIV RCC_CFGR_PPRE1_DIV4
|
#endif
|
||||||
#define CLOCK_APB1 (CLOCK_CORECLOCK / 4)
|
#ifndef CLOCK_HSE
|
||||||
#define CLOCK_APB2_DIV RCC_CFGR_PPRE2_DIV2
|
#define CLOCK_HSE MHZ(8)
|
||||||
#define CLOCK_APB2 (CLOCK_CORECLOCK / 2)
|
#endif
|
||||||
/** @} */
|
#if CONFIG_BOARD_HAS_HSE && (CLOCK_HSE < MHZ(4) || CLOCK_HSE > MHZ(48))
|
||||||
|
#error "HSE clock frequency must be between 4MHz and 48MHz"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_BOARD_HAS_LSE
|
||||||
|
#define CONFIG_BOARD_HAS_LSE (0)
|
||||||
|
#endif
|
||||||
|
#if CONFIG_BOARD_HAS_LSE
|
||||||
|
#define CLOCK_LSE (1)
|
||||||
|
#else
|
||||||
|
#define CLOCK_LSE (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CLOCK_HSI MHZ(16)
|
||||||
|
|
||||||
|
#ifndef CONFIG_CLOCK_MSI
|
||||||
|
#define CONFIG_CLOCK_MSI MHZ(48)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The following parameters configure a 80MHz system clock with PLL as input clock */
|
||||||
|
#ifndef CONFIG_CLOCK_PLL_SRC_MSI
|
||||||
|
#if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) || IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSI) || \
|
||||||
|
CONFIG_BOARD_HAS_HSE
|
||||||
|
#define CONFIG_CLOCK_PLL_SRC_MSI (0)
|
||||||
|
#else
|
||||||
|
#define CONFIG_CLOCK_PLL_SRC_MSI (1) /* Use MSI an input clock by default */
|
||||||
|
#endif
|
||||||
|
#endif /* CONFIG_CLOCK_PLL_SRC_MSI */
|
||||||
|
#ifndef CONFIG_CLOCK_PLL_SRC_HSE
|
||||||
|
#if CONFIG_BOARD_HAS_HSE
|
||||||
|
#define CONFIG_CLOCK_PLL_SRC_HSE (1)
|
||||||
|
#else
|
||||||
|
#define CONFIG_CLOCK_PLL_SRC_HSE (0)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#ifndef CONFIG_CLOCK_PLL_SRC_HSI
|
||||||
|
#define CONFIG_CLOCK_PLL_SRC_HSI (0)
|
||||||
|
#endif
|
||||||
|
#ifndef CONFIG_CLOCK_PLL_M
|
||||||
|
#define CONFIG_CLOCK_PLL_M (6)
|
||||||
|
#endif
|
||||||
|
#ifndef CONFIG_CLOCK_PLL_N
|
||||||
|
#define CONFIG_CLOCK_PLL_N (20)
|
||||||
|
#endif
|
||||||
|
#ifndef CONFIG_CLOCK_PLL_R
|
||||||
|
#define CONFIG_CLOCK_PLL_R (2)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_USE_CLOCK_HSI
|
||||||
|
#define CLOCK_CORECLOCK (CLOCK_HSI)
|
||||||
|
|
||||||
|
#elif CONFIG_USE_CLOCK_HSE
|
||||||
|
#if CONFIG_BOARD_HAS_HSE == 0
|
||||||
|
#error "The board doesn't provide an HSE oscillator"
|
||||||
|
#endif
|
||||||
|
#define CLOCK_CORECLOCK (CLOCK_HSE)
|
||||||
|
|
||||||
|
#elif CONFIG_USE_CLOCK_MSI
|
||||||
|
#define CLOCK_CORECLOCK (CONFIG_CLOCK_MSI)
|
||||||
|
|
||||||
|
#elif CONFIG_USE_CLOCK_PLL
|
||||||
|
#if CONFIG_CLOCK_PLL_SRC_MSI
|
||||||
|
#define CLOCK_PLL_SRC (CONFIG_CLOCK_MSI)
|
||||||
|
#elif CONFIG_CLOCK_PLL_SRC_HSE
|
||||||
|
#define CLOCK_PLL_SRC (CLOCK_HSE)
|
||||||
|
#else /* CONFIG_CLOCK_PLL_SRC_ */
|
||||||
|
#define CLOCK_PLL_SRC (CLOCK_HSI)
|
||||||
|
#endif
|
||||||
|
#define CLOCK_CORECLOCK \
|
||||||
|
((CLOCK_PLL_SRC / CONFIG_CLOCK_PLL_M) * CONFIG_CLOCK_PLL_N) / CONFIG_CLOCK_PLL_R
|
||||||
|
#ifndef CLOCK_CORECLOCK_MAX
|
||||||
|
#define CLOCK_CORECLOCK_MAX MHZ(80)
|
||||||
|
#endif
|
||||||
|
#if CLOCK_CORECLOCK > CLOCK_CORECLOCK_MAX
|
||||||
|
#if CLOCK_CORECLOCK_MAX == MHZ(64)
|
||||||
|
#error "SYSCLK cannot exceed 64MHz"
|
||||||
|
#elif CLOCK_CORECLOCK_MAX == MHZ(80)
|
||||||
|
#error "SYSCLK cannot exceed 80MHz"
|
||||||
|
#elif CLOCK_CORECLOCK_MAX == MHZ(120)
|
||||||
|
#error "SYSCLK cannot exceed 120MHz"
|
||||||
|
#else
|
||||||
|
#error "invalid SYSCLK"
|
||||||
|
#endif
|
||||||
|
#endif /* CLOCK_CORECLOCK > CLOCK_CORECLOCK_MAX */
|
||||||
|
#endif /* CONFIG_USE_CLOCK_PLL */
|
||||||
|
|
||||||
|
#define CLOCK_AHB CLOCK_CORECLOCK /* max: 80MHz */
|
||||||
|
|
||||||
|
#ifndef CONFIG_CLOCK_APB1_DIV
|
||||||
|
#define CONFIG_CLOCK_APB1_DIV (4)
|
||||||
|
#endif
|
||||||
|
#define CLOCK_APB1 (CLOCK_CORECLOCK / CONFIG_CLOCK_APB1_DIV) /* max: 80MHz */
|
||||||
|
#ifndef CONFIG_CLOCK_APB2_DIV
|
||||||
|
#define CONFIG_CLOCK_APB2_DIV (2)
|
||||||
|
#endif
|
||||||
|
#define CLOCK_APB2 (CLOCK_CORECLOCK / CONFIG_CLOCK_APB2_DIV) /* max: 80MHz */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* L4_CFG_CLOCK_80_1_H */
|
#endif /* L4_CFG_CLOCK_DEFAULT_H */
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|||||||
@ -27,17 +27,6 @@
|
|||||||
#include "stmclk.h"
|
#include "stmclk.h"
|
||||||
#include "periph_conf.h"
|
#include "periph_conf.h"
|
||||||
|
|
||||||
/* make sure we have all needed information about the clock configuration */
|
|
||||||
#ifndef CLOCK_HSE
|
|
||||||
#error "Please provide CLOCK_HSE in your board's perhip_conf.h"
|
|
||||||
#endif
|
|
||||||
#ifndef CLOCK_LSE
|
|
||||||
#error "Please provide CLOCK_LSE in your board's periph_conf.h"
|
|
||||||
#endif
|
|
||||||
#if !defined(CLOCK_PLL_M) || !defined(CLOCK_PLL_N) || !defined(CLOCK_PLL_R)
|
|
||||||
#error "Please provide the PLL configuration in your board's periph_conf.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* map CMSIS defines not present in stm32wb55xx.h */
|
/* map CMSIS defines not present in stm32wb55xx.h */
|
||||||
#if defined(CPU_FAM_STM32WB)
|
#if defined(CPU_FAM_STM32WB)
|
||||||
#define RCC_PLLCFGR_PLLSRC_HSE (RCC_PLLCFGR_PLLSRC_0 | RCC_PLLCFGR_PLLSRC_1)
|
#define RCC_PLLCFGR_PLLSRC_HSE (RCC_PLLCFGR_PLLSRC_0 | RCC_PLLCFGR_PLLSRC_1)
|
||||||
@ -57,39 +46,39 @@
|
|||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
/* figure out which input to use */
|
/* figure out which input to use */
|
||||||
#if (CLOCK_HSE)
|
#if CONFIG_CLOCK_PLL_SRC_MSI
|
||||||
#define PLL_IN CLOCK_HSE
|
#define PLL_SRC RCC_PLLCFGR_PLLSRC_MSI
|
||||||
|
#elif CONFIG_CLOCK_PLL_SRC_HSE && CONFIG_BOARD_HAS_HSE
|
||||||
#define PLL_SRC RCC_PLLCFGR_PLLSRC_HSE
|
#define PLL_SRC RCC_PLLCFGR_PLLSRC_HSE
|
||||||
#else
|
#else
|
||||||
#define PLL_IN (48000000) /* MSI @ 48MHz */
|
#define PLL_SRC RCC_PLLCFGR_PLLSRC_HSI
|
||||||
#define PLL_SRC RCC_PLLCFGR_PLLSRC_MSI
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**check configuration and get the corresponding bitfields */
|
/**check configuration and get the corresponding bitfields */
|
||||||
#if (CLOCK_PLL_M < 1 || CLOCK_PLL_M > 8)
|
#if (CONFIG_CLOCK_PLL_M < 1 || CONFIG_CLOCK_PLL_M > 8)
|
||||||
#error "PLL configuration: PLL M value is out of range"
|
#error "PLL configuration: PLL M value is out of range"
|
||||||
#endif
|
#endif
|
||||||
#define PLL_M ((CLOCK_PLL_M - 1) << RCC_PLLCFGR_PLLM_Pos)
|
#define PLL_M ((CONFIG_CLOCK_PLL_M - 1) << RCC_PLLCFGR_PLLM_Pos)
|
||||||
|
|
||||||
#if (CLOCK_PLL_N < 8 || CLOCK_PLL_N > 86)
|
#if (CONFIG_CLOCK_PLL_N < 8 || CONFIG_CLOCK_PLL_N > 86)
|
||||||
#error "PLL configuration: PLL N value is out of range"
|
#error "PLL configuration: PLL N value is out of range"
|
||||||
#endif
|
#endif
|
||||||
#define PLL_N (CLOCK_PLL_N << RCC_PLLCFGR_PLLN_Pos)
|
#define PLL_N (CONFIG_CLOCK_PLL_N << RCC_PLLCFGR_PLLN_Pos)
|
||||||
|
|
||||||
#if defined(CPU_FAM_STM32WB)
|
#if defined(CPU_FAM_STM32WB)
|
||||||
#if (CLOCK_PLL_R < 1 || CLOCK_PLL_R > 8)
|
#if (CONFIG_CLOCK_PLL_R < 1 || CONFIG_CLOCK_PLL_R > 8)
|
||||||
#error "PLL configuration: PLL R value is invalid"
|
#error "PLL configuration: PLL R value is invalid"
|
||||||
#else
|
#else
|
||||||
#define PLL_R ((CLOCK_PLL_R - 1)<< RCC_PLLCFGR_PLLR_Pos)
|
#define PLL_R ((CONFIG_CLOCK_PLL_R - 1)<< RCC_PLLCFGR_PLLR_Pos)
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
#if (CLOCK_PLL_R == 2)
|
#if (CONFIG_CLOCK_PLL_R == 2)
|
||||||
#define PLL_R (0)
|
#define PLL_R (0)
|
||||||
#elif (CLOCK_PLL_R == 4)
|
#elif (CONFIG_CLOCK_PLL_R == 4)
|
||||||
#define PLL_R (RCC_PLLCFGR_PLLR_0)
|
#define PLL_R (RCC_PLLCFGR_PLLR_0)
|
||||||
#elif (CLOCK_PLL_R == 6)
|
#elif (CONFIG_CLOCK_PLL_R == 6)
|
||||||
#define PLL_R (RCC_PLLCFGR_PLLR_1)
|
#define PLL_R (RCC_PLLCFGR_PLLR_1)
|
||||||
#elif (CLOCK_PLL_R == 8)
|
#elif (CONFIG_CLOCK_PLL_R == 8)
|
||||||
#define PLL_R (RCC_PLLCFGR_PLLR_0 | RCC_PLLCFGR_PLLR_1)
|
#define PLL_R (RCC_PLLCFGR_PLLR_0 | RCC_PLLCFGR_PLLR_1)
|
||||||
#else
|
#else
|
||||||
#error "PLL configuration: PLL R value is invalid"
|
#error "PLL configuration: PLL R value is invalid"
|
||||||
@ -97,6 +86,88 @@
|
|||||||
#endif
|
#endif
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
|
#if CONFIG_CLOCK_MSI == KHZ(100)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_0)
|
||||||
|
#elif CONFIG_CLOCK_MSI == KHZ(200)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_1)
|
||||||
|
#elif CONFIG_CLOCK_MSI == KHZ(400)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_2)
|
||||||
|
#elif CONFIG_CLOCK_MSI == KHZ(800)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_3)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(1)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_4)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(2)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_5)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(4)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_6)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(8)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_7)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(16)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_8)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(24)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_9)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(32)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_10)
|
||||||
|
#elif CONFIG_CLOCK_MSI == MHZ(48)
|
||||||
|
#define CLOCK_MSIRANGE (RCC_CR_MSIRANGE_11)
|
||||||
|
#else
|
||||||
|
#error "Invalid MSI clock"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(CPU_FAM_STM32WB)
|
||||||
|
#define CLOCK_AHB_DIV (0)
|
||||||
|
|
||||||
|
#if CONFIG_CLOCK_APB1_DIV == 1
|
||||||
|
#define CLOCK_APB1_DIV (0)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 2
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_2)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 4
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_2 | RCC_CFGR_PPRE1_0)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 8
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_2 | RCC_CFGR_PPRE1_1)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 16
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_2 | RCC_CFGR_PPRE1_1 | RCC_CFGR_PPRE1_0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_CLOCK_APB2_DIV == 1
|
||||||
|
#define CLOCK_APB2_DIV (0)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 2
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_2)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 4
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_2 | RCC_CFGR_PPRE2_0)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 8
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_2 | RCC_CFGR_PPRE2_1)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 16
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_2 | RCC_CFGR_PPRE2_1 | RCC_CFGR_PPRE2_0)
|
||||||
|
#endif
|
||||||
|
#else /* CPU_FAM_STM32L4 */
|
||||||
|
#define CLOCK_AHB_DIV (RCC_CFGR_HPRE_DIV1)
|
||||||
|
|
||||||
|
#if CONFIG_CLOCK_APB1_DIV == 1
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_DIV1)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 2
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_DIV1)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 4
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_DIV4)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 8
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_DIV8)
|
||||||
|
#elif CONFIG_CLOCK_APB1_DIV == 16
|
||||||
|
#define CLOCK_APB1_DIV (RCC_CFGR_PPRE1_DIV16)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_CLOCK_APB2_DIV == 1
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_DIV1)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 2
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_DIV1)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 4
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_DIV4)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 8
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_DIV8)
|
||||||
|
#elif CONFIG_CLOCK_APB2_DIV == 16
|
||||||
|
#define CLOCK_APB2_DIV (RCC_CFGR_PPRE2_DIV16)
|
||||||
|
#endif
|
||||||
|
#endif /* CPU_FAM_STM32WB */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @name Deduct the needed flash wait states from the core clock frequency
|
* @name Deduct the needed flash wait states from the core clock frequency
|
||||||
* @{
|
* @{
|
||||||
@ -124,9 +195,9 @@ void stmclk_init_sysclk(void)
|
|||||||
stmclk_enable_hsi();
|
stmclk_enable_hsi();
|
||||||
|
|
||||||
/* use HSI as system clock while we do any further configuration and
|
/* use HSI as system clock while we do any further configuration and
|
||||||
* configure the AHB and APB clock dividers as configure by the board */
|
* configure the AHB and APB clock dividers as configured by the board */
|
||||||
RCC->CFGR = (RCC_CFGR_SW_HSI | CLOCK_AHB_DIV |
|
RCC->CFGR = (RCC_CFGR_SW_HSI | CLOCK_AHB_DIV | CLOCK_APB1_DIV | CLOCK_APB2_DIV);
|
||||||
CLOCK_APB1_DIV | CLOCK_APB2_DIV);
|
|
||||||
#if defined(CPU_FAM_STM32WB)
|
#if defined(CPU_FAM_STM32WB)
|
||||||
/* Use HSE/2 for radios systems */
|
/* Use HSE/2 for radios systems */
|
||||||
RCC->EXTCFGR = (RCC_EXTCFGR_RFCSS | CLOCK_EXTAHB_DIV);
|
RCC->EXTCFGR = (RCC_EXTCFGR_RFCSS | CLOCK_EXTAHB_DIV);
|
||||||
@ -151,50 +222,80 @@ void stmclk_init_sysclk(void)
|
|||||||
(instead of MSIRANGE in the RCC_CR) */
|
(instead of MSIRANGE in the RCC_CR) */
|
||||||
RCC->CR = (RCC_CR_HSION);
|
RCC->CR = (RCC_CR_HSION);
|
||||||
|
|
||||||
#if (CLOCK_HSE)
|
if (CONFIG_USE_CLOCK_HSE) {
|
||||||
/* if configured, we need to enable the HSE clock now */
|
RCC->CR |= (RCC_CR_HSEON);
|
||||||
RCC->CR |= (RCC_CR_HSEON);
|
while (!(RCC->CR & RCC_CR_HSERDY)) {}
|
||||||
while (!(RCC->CR & RCC_CR_HSERDY)) {}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if ((CLOCK_HSE == 0) || CLOCK_MSI_ENABLE)
|
/* Select HSE as system clock and configure the different prescalers */
|
||||||
/* reset clock to MSI with 48MHz, disables all other clocks */
|
RCC->CFGR &= ~RCC_CFGR_SW;
|
||||||
|
RCC->CFGR |= RCC_CFGR_SW_HSE;
|
||||||
|
}
|
||||||
|
else if (CONFIG_USE_CLOCK_MSI) {
|
||||||
#if defined(CPU_FAM_STM32WB)
|
#if defined(CPU_FAM_STM32WB)
|
||||||
RCC->CR |= (RCC_CR_MSIRANGE_11 | RCC_CR_MSION);
|
RCC->CR |= (CLOCK_MSIRANGE | RCC_CR_MSION);
|
||||||
#else
|
#else
|
||||||
RCC->CR |= (RCC_CR_MSIRANGE_11 | RCC_CR_MSION | RCC_CR_MSIRGSEL);
|
RCC->CR |= (CLOCK_MSIRANGE | RCC_CR_MSION | RCC_CR_MSIRGSEL);
|
||||||
#endif
|
#endif
|
||||||
while (!(RCC->CR & RCC_CR_MSIRDY)) {}
|
while (!(RCC->CR & RCC_CR_MSIRDY)) {}
|
||||||
/* select the MSI clock for the 48MHz clock tree (USB, RNG) */
|
|
||||||
RCC->CCIPR = (RCC_CCIPR_CLK48SEL_0 | RCC_CCIPR_CLK48SEL_1);
|
|
||||||
#if (CLOCK_MSI_LSE_PLL && CLOCK_LSE)
|
|
||||||
/* configure the low speed clock domain */
|
|
||||||
stmclk_enable_lfclk();
|
|
||||||
/* now we can enable the MSI PLL mode to enhance accuracy of the MSI*/
|
|
||||||
RCC->CR |= RCC_CR_MSIPLLEN;
|
|
||||||
while (!(RCC->CR & RCC_CR_MSIRDY)) {}
|
|
||||||
#endif /* (CLOCK_MSI_LSE_PLL && CLOCK_LSE) */
|
|
||||||
#endif /* ((CLOCK_HSE == 0) || CLOCK_MSI_ENABLE) */
|
|
||||||
|
|
||||||
/* now we can safely configure and start the PLL */
|
if (CONFIG_CLOCK_MSI == MHZ(48)) {
|
||||||
RCC->PLLCFGR = (PLL_SRC | PLL_M | PLL_N | PLL_R | RCC_PLLCFGR_PLLREN);
|
/* select the MSI clock for the 48MHz clock tree (USB, RNG) */
|
||||||
RCC->CR |= (RCC_CR_PLLON);
|
RCC->CCIPR = (RCC_CCIPR_CLK48SEL_0 | RCC_CCIPR_CLK48SEL_1);
|
||||||
while (!(RCC->CR & RCC_CR_PLLRDY)) {}
|
}
|
||||||
|
/* Select MSI as system clock and configure the different prescalers */
|
||||||
|
RCC->CFGR = (RCC_CFGR_SW_MSI | CLOCK_AHB_DIV | CLOCK_APB1_DIV | CLOCK_APB2_DIV);
|
||||||
|
}
|
||||||
|
else if (CONFIG_USE_CLOCK_PLL) {
|
||||||
|
if (CONFIG_BOARD_HAS_HSE && CONFIG_CLOCK_PLL_SRC_HSE) {
|
||||||
|
/* if configured, we need to enable the HSE clock now */
|
||||||
|
RCC->CR |= (RCC_CR_HSEON);
|
||||||
|
while (!(RCC->CR & RCC_CR_HSERDY)) {}
|
||||||
|
}
|
||||||
|
|
||||||
/* now that the PLL is running, we use it as system clock */
|
if (CONFIG_CLOCK_PLL_SRC_MSI) {
|
||||||
RCC->CFGR |= RCC_CFGR_SW_PLL;
|
/* reset clock to MSI with 48MHz, disables all other clocks */
|
||||||
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {}
|
#if defined(CPU_FAM_STM32WB)
|
||||||
|
RCC->CR |= (CLOCK_MSIRANGE | RCC_CR_MSION);
|
||||||
|
#else
|
||||||
|
RCC->CR |= (CLOCK_MSIRANGE | RCC_CR_MSION | RCC_CR_MSIRGSEL);
|
||||||
|
#endif
|
||||||
|
while (!(RCC->CR & RCC_CR_MSIRDY)) {}
|
||||||
|
|
||||||
|
if (CONFIG_BOARD_HAS_LSE) {
|
||||||
|
/* configure the low speed clock domain */
|
||||||
|
stmclk_enable_lfclk();
|
||||||
|
/* now we can enable the MSI PLL mode to enhance accuracy of the MSI */
|
||||||
|
RCC->CR |= RCC_CR_MSIPLLEN;
|
||||||
|
while (!(RCC->CR & RCC_CR_MSIRDY)) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CONFIG_CLOCK_MSI == MHZ(48)) {
|
||||||
|
/* select the MSI clock for the 48MHz clock tree (USB, RNG) */
|
||||||
|
RCC->CCIPR = (RCC_CCIPR_CLK48SEL_0 | RCC_CCIPR_CLK48SEL_1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* now we can safely configure and start the PLL */
|
||||||
|
RCC->PLLCFGR = (PLL_SRC | PLL_M | PLL_N | PLL_R | RCC_PLLCFGR_PLLREN);
|
||||||
|
RCC->CR |= (RCC_CR_PLLON);
|
||||||
|
while (!(RCC->CR & RCC_CR_PLLRDY)) {}
|
||||||
|
|
||||||
|
/* now that the PLL is running, we use it as system clock */
|
||||||
|
RCC->CFGR |= RCC_CFGR_SW_PLL;
|
||||||
|
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {}
|
||||||
|
}
|
||||||
|
|
||||||
stmclk_disable_hsi();
|
stmclk_disable_hsi();
|
||||||
irq_restore(is);
|
irq_restore(is);
|
||||||
|
|
||||||
#ifdef MODULE_PERIPH_RTT
|
if (IS_USED(MODULE_PERIPH_RTT)) {
|
||||||
/* Ensure LPTIM1 clock source (LSI or LSE) is correctly reset when initializing
|
/* Ensure LPTIM1 clock source (LSI or LSE) is correctly reset when initializing
|
||||||
the clock, this is particularly useful after waking up from deep sleep */
|
the clock, this is particularly useful after waking up from deep sleep */
|
||||||
#if CLOCK_LSE
|
if (CONFIG_BOARD_HAS_LSE) {
|
||||||
RCC->CCIPR |= RCC_CCIPR_LPTIM1SEL_0 | RCC_CCIPR_LPTIM1SEL_1;
|
RCC->CCIPR |= RCC_CCIPR_LPTIM1SEL_0 | RCC_CCIPR_LPTIM1SEL_1;
|
||||||
#else
|
}
|
||||||
RCC->CCIPR |= RCC_CCIPR_LPTIM1SEL_0;
|
else {
|
||||||
#endif /* CLOCK_LSE */
|
RCC->CCIPR |= RCC_CCIPR_LPTIM1SEL_0;
|
||||||
#endif /* MODULE_PERIPH_RTT */
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user