cfg_clock_default.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
22 #include "kernel_defines.h"
23 #include "macros/units.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
33 #if IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && (CONFIG_CLOCK_HSE < MHZ(4) || CONFIG_CLOCK_HSE > MHZ(48))
34 #error "HSE clock frequency must be between 4MHz and 48MHz"
35 #endif
36 
37 /* The following parameters configure a 80MHz system clock with PLL as input clock */
38 #ifndef CONFIG_CLOCK_PLL_SRC_MSI
39 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE) || IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSI) || \
40  IS_ACTIVE(CONFIG_BOARD_HAS_HSE)
41 #define CONFIG_CLOCK_PLL_SRC_MSI 0
42 #else
43 #define CONFIG_CLOCK_PLL_SRC_MSI 1 /* Use MSI as input clock by default */
44 #endif
45 #endif /* CONFIG_CLOCK_PLL_SRC_MSI */
46 #ifndef CONFIG_CLOCK_PLL_SRC_HSE
47 #if IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && \
48  !IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSI) && !IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_MSI)
49 #define CONFIG_CLOCK_PLL_SRC_HSE 1
50 #else
51 #define CONFIG_CLOCK_PLL_SRC_HSE 0
52 #endif
53 #endif
54 #ifndef CONFIG_CLOCK_PLL_SRC_HSI
55 #define CONFIG_CLOCK_PLL_SRC_HSI 0
56 #endif
57 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_MSI)
58 #define CLOCK_PLL_SRC (CONFIG_CLOCK_MSI)
59 #elif IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_HSE)
60 #define CLOCK_PLL_SRC (CONFIG_CLOCK_HSE)
61 #else /* CONFIG_CLOCK_PLL_SRC_ */
62 #define CLOCK_PLL_SRC (CONFIG_CLOCK_HSI)
63 #endif
64 #ifndef CONFIG_CLOCK_PLL_M
65 #if IS_ACTIVE(CONFIG_CLOCK_PLL_SRC_MSI)
66 #define CONFIG_CLOCK_PLL_M (6) /* MSI at 48MHz */
67 #else
68 #define CONFIG_CLOCK_PLL_M (2) /* HSI/HSE at 16MHz */
69 #endif
70 #endif
71 #ifndef CONFIG_CLOCK_PLL_N
72 #define CONFIG_CLOCK_PLL_N (40)
73 #endif
74 #ifndef CONFIG_CLOCK_PLL_Q
75 #define CONFIG_CLOCK_PLL_Q (2)
76 #endif
77 #ifndef CONFIG_CLOCK_PLL_R
78 #define CONFIG_CLOCK_PLL_R (2)
79 #endif
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 #define CLOCK_CORECLOCK (CONFIG_CLOCK_HSE)
86 
87 #elif IS_ACTIVE(CONFIG_USE_CLOCK_MSI)
88 #define CLOCK_CORECLOCK (CONFIG_CLOCK_MSI)
89 
90 #elif IS_ACTIVE(CONFIG_USE_CLOCK_PLL)
91 /* PLL configuration: make sure your values are legit!
92  *
93  * compute by: CORECLOCK = (((PLL_IN / M) * N) / R)
94  * with:
95  * PLL_IN: input clock, HSE or MSI
96  * M: pre-divider, allowed range: [1:8]
97  * N: multiplier, allowed range: [5:512]
98  * R: post-divider, allowed range: [2:8]
99  *
100  * Also the following constraints need to be met:
101  * (PLL_IN / M) -> [4MHz:16MHz]
102  * (PLL_IN / M) * N -> [64MHz:344MHz]
103  * CORECLOCK -> 64MHz, 80MHZ or 120MHz MAX!
104  */
105 #define CLOCK_CORECLOCK \
106  ((CLOCK_PLL_SRC / CONFIG_CLOCK_PLL_M) * CONFIG_CLOCK_PLL_N) / CONFIG_CLOCK_PLL_R
107 
108 /* Set max allowed sysclk */
109 #define CLOCK_CORECLOCK_MAX MHZ(160)
110 
111 #if CLOCK_CORECLOCK > CLOCK_CORECLOCK_MAX
112 #error "SYSCLK cannot exceed 160MHz"
113 #endif /* CLOCK_CORECLOCK > CLOCK_CORECLOCK_MAX */
114 #endif /* CONFIG_USE_CLOCK_PLL */
115 
116 #define CLOCK_AHB CLOCK_CORECLOCK /* HCLK, max: 160MHz */
117 
118 #ifndef CONFIG_CLOCK_APB1_DIV
119 #define CONFIG_CLOCK_APB1_DIV (4)
120 #endif
121 #define CLOCK_APB1 (CLOCK_AHB / CONFIG_CLOCK_APB1_DIV) /* PCLK1, max: 160MHz */
122 #ifndef CONFIG_CLOCK_APB2_DIV
123 #define CONFIG_CLOCK_APB2_DIV (2)
124 #endif
125 #define CLOCK_APB2 (CLOCK_AHB / CONFIG_CLOCK_APB2_DIV) /* PCLK1, max: 160MHz */
128 #ifdef __cplusplus
129 }
130 #endif
131 
Base STM32Lx/U5/Wx clock configuration.
Common macros and compiler attributes/pragmas configuration.
Unit helper macros.