cfg_clock_default.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 TriaGnoSys GmbH
3  * SPDX-FileCopyrightText: 2017 Alexander Kurth, Sören Tempel, Tristan Bruns
4  * SPDX-FileCopyrightText: 2020 Inria
5  * SPDX-License-Identifier: LGPL-2.1-only
6  */
7 
8 #pragma once
9 
26 #include "kernel_defines.h"
27 #include "macros/units.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
37 #if IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && (CONFIG_CLOCK_HSE < MHZ(4) || CONFIG_CLOCK_HSE > MHZ(32))
38 #error "HSE clock frequency must be between 4MHz and 32MHz"
39 #endif
40 
41 /* The following parameters configure:
42  - on F0: a 48MHz system clock with HSI (or default HSE) as input clock
43  On stm32f031x6 and stm32f042x6 lines, there's no HSE and PREDIV is
44  hard-wired to 2, so to reach 48MHz set PLL_PREDIV to 2 and PLL_MUL to 12 so
45  system clock = (HSI8 / 2) * 12 = 48MHz
46  - on F1/F3: a 72MHz system clock with HSE (8MHz or 16MHz) and HSI (8MHz) as input clock
47  On stm32f303x6, stm32f303x8, stm32f303xB, stm32f303xC, stm32f328x8 and
48  stm32f358xC lines, PREDIV is hard-wired to 2 (see RM0316 p.126 to p.128).
49  To reach the maximum possible system clock (64MHz) set PLL_PREDIV to 2 and
50  PLL_MUL to 16, so system clock = (HSI8 / 2) * 16 = 64MHz
51 */
52 #ifndef CONFIG_CLOCK_PLL_PREDIV
53 #if (IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && (CONFIG_CLOCK_HSE == MHZ(16))) || \
54  defined(CPU_LINE_STM32F303x6) || defined(CPU_LINE_STM32F303x8) || \
55  defined(CPU_LINE_STM32F303xB) || defined(CPU_LINE_STM32F303xC) || \
56  defined(CPU_LINE_STM32F328x8) || defined(CPU_LINE_STM32F358xC) || \
57  defined(CPU_LINE_STM32F031x6) || defined(CPU_LINE_STM32F042x6)
58 #define CONFIG_CLOCK_PLL_PREDIV (2)
59 #else
60 #define CONFIG_CLOCK_PLL_PREDIV (1)
61 #endif
62 #endif
63 #ifndef CONFIG_CLOCK_PLL_MUL
64 #ifdef CPU_FAM_STM32F0
65 #if defined(CPU_LINE_STM32F031x6) || defined(CPU_LINE_STM32F042x6)
66 #define CONFIG_CLOCK_PLL_MUL (12)
67 #else
68 #define CONFIG_CLOCK_PLL_MUL (6)
69 #endif
70 #else /* CPU_FAM_F1 || CPU_FAM_F3 */
71 #if defined(CPU_LINE_STM32F303x6) || defined(CPU_LINE_STM32F303x8) || \
72  defined(CPU_LINE_STM32F303xB) || defined(CPU_LINE_STM32F303xC) || \
73  defined(CPU_LINE_STM32F328x8) || defined(CPU_LINE_STM32F358xC)
74 #define CONFIG_CLOCK_PLL_MUL (16)
75 #else
76 #define CONFIG_CLOCK_PLL_MUL (9)
77 #endif
78 #endif /* CPU_FAM_STM32F0 */
79 #endif /* CONFIG_CLOCK_PLL_MUL */
80 
81 #if IS_ACTIVE(CONFIG_USE_CLOCK_HSI)
82 #define CLOCK_CORECLOCK (CONFIG_CLOCK_HSI)
83 
84 #elif IS_ACTIVE(CONFIG_USE_CLOCK_HSE)
85 #if !IS_ACTIVE(CONFIG_BOARD_HAS_HSE)
86 #error "The board doesn't provide an HSE oscillator"
87 #endif
88 #define CLOCK_CORECLOCK (CONFIG_CLOCK_HSE)
89 
90 #elif IS_ACTIVE(CONFIG_USE_CLOCK_PLL)
91 #if IS_ACTIVE(CONFIG_BOARD_HAS_HSE)
92 #define CLOCK_PLL_SRC (CONFIG_CLOCK_HSE)
93 #else /* CONFIG_CLOCK_HSI */
94 #define CLOCK_PLL_SRC (CONFIG_CLOCK_HSI)
95 #endif
96 /* PLL configuration: make sure your values are legit!
97  *
98  * compute by: CORECLOCK = ((PLL_IN / PLL_PREDIV) * PLL_MUL)
99  * with:
100  * PLL_IN: input clock is HSE if available or HSI otherwise
101  * PLL_PREDIV : pre-divider, allowed range: [1:16]
102  * PLL_MUL: multiplier, allowed range: [2:16]
103  * CORECLOCK -> 48MHz Max on F0, 72MHz MAX on F1/F3!
104  */
105 #define CLOCK_CORECLOCK ((CLOCK_PLL_SRC / CONFIG_CLOCK_PLL_PREDIV) * CONFIG_CLOCK_PLL_MUL)
106 #ifdef CPU_FAM_STM32F0
107 #if CLOCK_CORECLOCK > MHZ(48)
108 #error "SYSCLK cannot exceed 48MHz"
109 #endif
110 #else
111 #if CLOCK_CORECLOCK > MHZ(72)
112 #error "SYSCLK cannot exceed 72MHz"
113 #endif
114 #endif
115 #endif /* CONFIG_USE_CLOCK_PLL */
116 
117 #define CLOCK_AHB CLOCK_CORECLOCK /* HCLK, max: 48MHz (F0), 72MHz (F1/F3)*/
118 
119 #ifndef CONFIG_CLOCK_APB1_DIV
120 #ifdef CPU_FAM_STM32F0
121 #define CONFIG_CLOCK_APB1_DIV (1)
122 #else
123 #define CONFIG_CLOCK_APB1_DIV (2)
124 #endif
125 #endif
126 #define CLOCK_APB1 (CLOCK_AHB / CONFIG_CLOCK_APB1_DIV) /* PCLK1, max: 48MHz (F0), 36MHz (F1/F3)*/
127 #ifdef CPU_FAM_STM32F0
128 /* APB2 and APB1 are the same bus but configuration registers still follows the
129  * split between APB1 and APB2. Since it's the same bus, APB2 clock is equal to APB1 clock.
130  */
131 #define CLOCK_APB2 (CLOCK_APB1)
132 #else
133 #ifndef CONFIG_CLOCK_APB2_DIV
134 #define CONFIG_CLOCK_APB2_DIV (1)
135 #endif
136 #define CLOCK_APB2 (CLOCK_AHB / CONFIG_CLOCK_APB2_DIV) /* PCLK2, max: 72MHz */
137 #endif
140 #ifdef __cplusplus
141 }
142 #endif
143 
Base STM32Fx/Gx/MP1/C0 clock configuration.
Common macros and compiler attributes/pragmas configuration.
Unit helper macros.