cfg_clock_default.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017 Freie Universität Berlin
3  * SPDX-FileCopyrightText: 2019 Inria
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
21 #include "kernel_defines.h"
22 #include "macros/units.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
32 #if IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && (CONFIG_CLOCK_HSE < MHZ(4) || CONFIG_CLOCK_HSE > MHZ(48))
33 #error "HSE clock frequency must be between 4MHz and 48MHz"
34 #endif
35 
36 /* The following parameters configure a 80MHz system clock with PLL as input clock */
37 #ifndef CONFIG_CLOCK_PLL_SRC_MSI
38 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) || IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSI) || \
39  IS_ACTIVE(CONFIG_BOARD_HAS_HSE)
40 #define CONFIG_CLOCK_PLL_SRC_MSI 0
41 #else
42 #define CONFIG_CLOCK_PLL_SRC_MSI 1 /* Use MSI as input clock by default */
43 #endif
44 #endif /* CONFIG_CLOCK_PLL_SRC_MSI */
45 #ifndef CONFIG_CLOCK_PLL_SRC_HSE
46 #if IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && \
47  !IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSI) && !IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_MSI)
48 #define CONFIG_CLOCK_PLL_SRC_HSE 1
49 #else
50 #define CONFIG_CLOCK_PLL_SRC_HSE 0
51 #endif
52 #endif
53 #ifndef CONFIG_CLOCK_PLL_SRC_HSI
54 #define CONFIG_CLOCK_PLL_SRC_HSI 0
55 #endif
56 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_MSI)
57 #define CLOCK_PLL_SRC (CONFIG_CLOCK_MSI)
58 #elif IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE)
59 #define CLOCK_PLL_SRC (CONFIG_CLOCK_HSE)
60 #else /* CONFIG_CLOCK_PLL_SRC_ */
61 #define CLOCK_PLL_SRC (CONFIG_CLOCK_HSI)
62 #endif
63 #ifndef CONFIG_CLOCK_PLL_M
64 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_MSI)
65 #define CONFIG_CLOCK_PLL_M (6) /* MSI at 48MHz */
66 #elif IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) && (CONFIG_CLOCK_HSE == MHZ(8))
67 #define CONFIG_CLOCK_PLL_M (1) /* HSE at 8MHz */
68 #elif IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) && (CONFIG_CLOCK_HSE == MHZ(32))
69 #define CONFIG_CLOCK_PLL_M (4) /* HSE at 32MHz */
70 #else
71 #define CONFIG_CLOCK_PLL_M (2) /* HSI at 16MHz */
72 #endif
73 #endif
74 #ifndef CONFIG_CLOCK_PLL_N
75 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) && (CONFIG_CLOCK_HSE == MHZ(32))
76 /* For STM32WL, VCO output frequency ((PLL input clock frequency / PLLM ) x PLLN )
77  must be between 96 and 344 MHz. PLLN can have values <=127 & >=6 */
78 #if IS_ACTIVE(CPU_FAM_STM32WL)
79 #define CONFIG_CLOCK_PLL_N (12)
80 #else
81 #define CONFIG_CLOCK_PLL_N (16)
82 #endif /* CPU_FAM_STM32WL */
83 #elif IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSI) || \
84  (IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) && (CONFIG_CLOCK_HSE == MHZ(16)))
85 #define CONFIG_CLOCK_PLL_N (32)
86 #else
87 #if defined(CPU_LINE_STM32L4A6xx) || defined(CPU_LINE_STM32L4P5xx) || \
88  defined(CPU_LINE_STM32L4Q5xx) || defined(CPU_LINE_STM32L4R5xx) || \
89  defined(CPU_LINE_STM32L4R7xx) || defined(CPU_LINE_STM32L4R9xx) || \
90  defined(CPU_LINE_STM32L4S5xx) || defined(CPU_LINE_STM32L4S7xx) || \
91  defined(CPU_LINE_STM32L4S9xx)
92 #define CONFIG_CLOCK_PLL_N (30)
93 #elif defined(CPU_FAM_STM32L5)
94 #define CONFIG_CLOCK_PLL_N (27)
95 #else
96 #define CONFIG_CLOCK_PLL_N (20)
97 #endif
98 #endif
99 #endif
100 #ifndef CONFIG_CLOCK_PLL_Q
101 #define CONFIG_CLOCK_PLL_Q (2)
102 #endif
103 #ifndef CONFIG_CLOCK_PLL_R
104 #define CONFIG_CLOCK_PLL_R (2)
105 #endif
106 
107 #if IS_ACTIVE(CONFIG_USE_CLOCK_HSI)
108 #define CLOCK_CORECLOCK (CONFIG_CLOCK_HSI)
109 
110 #elif IS_ACTIVE(CONFIG_USE_CLOCK_HSE)
111 #define CLOCK_CORECLOCK (CONFIG_CLOCK_HSE)
112 
113 #elif IS_ACTIVE(CONFIG_USE_CLOCK_MSI)
114 #define CLOCK_CORECLOCK (CONFIG_CLOCK_MSI)
115 
116 #elif IS_ACTIVE(CONFIG_USE_CLOCK_PLL)
117 /* PLL configuration: make sure your values are legit!
118  *
119  * compute by: CORECLOCK = (((PLL_IN / M) * N) / R)
120  * with:
121  * PLL_IN: input clock, HSE or MSI
122  * M: pre-divider, allowed range: [1:8]
123  * N: multiplier, allowed range: [8:86]
124  * R: post-divider, allowed range: [2:8]
125  *
126  * Also the following constraints need to be met:
127  * (PLL_IN / M) -> [4MHz:16MHz]
128  * (PLL_IN / M) * N -> [64MHz:344MHz]
129  * CORECLOCK -> 64MHz, 80MHZ or 120MHz MAX!
130  */
131 #define CLOCK_CORECLOCK \
132  ((CLOCK_PLL_SRC / CONFIG_CLOCK_PLL_M) * CONFIG_CLOCK_PLL_N) / CONFIG_CLOCK_PLL_R
133 
134 /* Set max allowed sysclk */
135 #if defined(CPU_FAM_STM32WL)
136 #define CLOCK_CORECLOCK_MAX MHZ(48)
137 #elif defined(CPU_FAM_STM32WB)
138 #define CLOCK_CORECLOCK_MAX MHZ(64)
139 #elif defined(CPU_FAM_STM32L5)
140 #define CLOCK_CORECLOCK_MAX MHZ(110)
141 #elif defined(CPU_LINE_STM32L4A6xx) || defined(CPU_LINE_STM32L4P5xx) || \
142  defined(CPU_LINE_STM32L4Q5xx) || defined(CPU_LINE_STM32L4R5xx) || \
143  defined(CPU_LINE_STM32L4R7xx) || defined(CPU_LINE_STM32L4R9xx) || \
144  defined(CPU_LINE_STM32L4S5xx) || defined(CPU_LINE_STM32L4S7xx) || \
145  defined(CPU_LINE_STM32L4S9xx)
146 #define CLOCK_CORECLOCK_MAX MHZ(120)
147 #else /* all the other L4 */
148 #define CLOCK_CORECLOCK_MAX MHZ(80)
149 #endif
150 
151 #if CLOCK_CORECLOCK > CLOCK_CORECLOCK_MAX
152 #if CLOCK_CORECLOCK_MAX == MHZ(48)
153 #error "SYSCLK cannot exceed 48MHz"
154 #elif CLOCK_CORECLOCK_MAX == MHZ(64)
155 #error "SYSCLK cannot exceed 64MHz"
156 #elif CLOCK_CORECLOCK_MAX == MHZ(80)
157 #error "SYSCLK cannot exceed 80MHz"
158 #elif CLOCK_CORECLOCK_MAX == MHZ(110)
159 #error "SYSCLK cannot exceed 110MHz"
160 #elif CLOCK_CORECLOCK_MAX == MHZ(120)
161 #error "SYSCLK cannot exceed 120MHz"
162 #else
163 #error "invalid SYSCLK"
164 #endif
165 #endif /* CLOCK_CORECLOCK > CLOCK_CORECLOCK_MAX */
166 #endif /* CONFIG_USE_CLOCK_PLL */
167 
168 #define CLOCK_AHB CLOCK_CORECLOCK /* HCLK, max: 48/64/80/120MHz */
169 
170 #ifndef CONFIG_CLOCK_APB1_DIV
171 #define CONFIG_CLOCK_APB1_DIV (4)
172 #endif
173 #define CLOCK_APB1 (CLOCK_AHB / CONFIG_CLOCK_APB1_DIV) /* PCLK1, max: 48/64/80/120MHz */
174 #ifndef CONFIG_CLOCK_APB2_DIV
175 #define CONFIG_CLOCK_APB2_DIV (2)
176 #endif
177 #define CLOCK_APB2 (CLOCK_AHB / CONFIG_CLOCK_APB2_DIV) /* PCLK1, max: 48/64/80/120MHz */
180 #ifdef __cplusplus
181 }
182 #endif
183 
Base STM32Lx/U5/Wx clock configuration.
Common macros and compiler attributes/pragmas configuration.
Unit helper macros.