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