diff options
Diffstat (limited to 'lib/chibios/os/hal/ports/STM32/STM32G4xx/hal_lld.c')
-rw-r--r-- | lib/chibios/os/hal/ports/STM32/STM32G4xx/hal_lld.c | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/lib/chibios/os/hal/ports/STM32/STM32G4xx/hal_lld.c b/lib/chibios/os/hal/ports/STM32/STM32G4xx/hal_lld.c new file mode 100644 index 000000000..454f7f401 --- /dev/null +++ b/lib/chibios/os/hal/ports/STM32/STM32G4xx/hal_lld.c | |||
@@ -0,0 +1,277 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | /** | ||
18 | * @file STM32G4xx/hal_lld.c | ||
19 | * @brief STM32G4xx HAL subsystem low level driver source. | ||
20 | * | ||
21 | * @addtogroup HAL | ||
22 | * @{ | ||
23 | */ | ||
24 | |||
25 | #include "hal.h" | ||
26 | |||
27 | /*===========================================================================*/ | ||
28 | /* Driver local definitions. */ | ||
29 | /*===========================================================================*/ | ||
30 | |||
31 | /*===========================================================================*/ | ||
32 | /* Driver exported variables. */ | ||
33 | /*===========================================================================*/ | ||
34 | |||
35 | /** | ||
36 | * @brief CMSIS system core clock variable. | ||
37 | * @note It is declared in system_stm32g4xx.h. | ||
38 | */ | ||
39 | uint32_t SystemCoreClock = STM32_HCLK; | ||
40 | |||
41 | /*===========================================================================*/ | ||
42 | /* Driver local variables and types. */ | ||
43 | /*===========================================================================*/ | ||
44 | |||
45 | /*===========================================================================*/ | ||
46 | /* Driver local functions. */ | ||
47 | /*===========================================================================*/ | ||
48 | |||
49 | /** | ||
50 | * @brief Initializes the backup domain. | ||
51 | * @note WARNING! Changing RTC clock source impossible without resetting | ||
52 | * of the whole BKP domain. | ||
53 | */ | ||
54 | static void hal_lld_backup_domain_init(void) { | ||
55 | |||
56 | /* Reset BKP domain if different clock source selected.*/ | ||
57 | if ((RCC->BDCR & STM32_RTCSEL_MASK) != STM32_RTCSEL) { | ||
58 | /* Backup domain reset.*/ | ||
59 | RCC->BDCR = RCC_BDCR_BDRST; | ||
60 | RCC->BDCR = 0; | ||
61 | } | ||
62 | |||
63 | #if STM32_LSE_ENABLED | ||
64 | /* LSE activation.*/ | ||
65 | #if defined(STM32_LSE_BYPASS) | ||
66 | /* LSE Bypass.*/ | ||
67 | RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP; | ||
68 | #else | ||
69 | /* No LSE Bypass.*/ | ||
70 | RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON; | ||
71 | #endif | ||
72 | while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0) | ||
73 | ; /* Wait until LSE is stable. */ | ||
74 | #endif | ||
75 | |||
76 | #if HAL_USE_RTC | ||
77 | /* If the backup domain hasn't been initialized yet then proceed with | ||
78 | initialization.*/ | ||
79 | if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) { | ||
80 | /* Selects clock source.*/ | ||
81 | RCC->BDCR |= STM32_RTCSEL; | ||
82 | |||
83 | /* RTC clock enabled.*/ | ||
84 | RCC->BDCR |= RCC_BDCR_RTCEN; | ||
85 | } | ||
86 | #endif /* HAL_USE_RTC */ | ||
87 | |||
88 | /* Low speed output mode.*/ | ||
89 | RCC->BDCR |= STM32_LSCOSEL; | ||
90 | } | ||
91 | |||
92 | /*===========================================================================*/ | ||
93 | /* Driver interrupt handlers. */ | ||
94 | /*===========================================================================*/ | ||
95 | |||
96 | /*===========================================================================*/ | ||
97 | /* Driver exported functions. */ | ||
98 | /*===========================================================================*/ | ||
99 | |||
100 | /** | ||
101 | * @brief Low level HAL driver initialization. | ||
102 | * | ||
103 | * @notapi | ||
104 | */ | ||
105 | void hal_lld_init(void) { | ||
106 | |||
107 | /* Initializes the backup domain.*/ | ||
108 | hal_lld_backup_domain_init(); | ||
109 | |||
110 | /* DMA subsystems initialization.*/ | ||
111 | #if defined(STM32_DMA_REQUIRED) | ||
112 | dmaInit(); | ||
113 | #endif | ||
114 | |||
115 | /* IRQ subsystem initialization.*/ | ||
116 | irqInit(); | ||
117 | |||
118 | /* Programmable voltage detector settings.*/ | ||
119 | PWR->CR2 = STM32_PWR_CR2; | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * @brief STM32L4xx clocks and PLL initialization. | ||
124 | * @note All the involved constants come from the file @p board.h. | ||
125 | * @note This function should be invoked just after the system reset. | ||
126 | * | ||
127 | * @special | ||
128 | */ | ||
129 | void stm32_clock_init(void) { | ||
130 | |||
131 | #if !STM32_NO_INIT | ||
132 | |||
133 | /* Reset of all peripherals. | ||
134 | Note, GPIOs are not reset because initialized before this point in | ||
135 | board files.*/ | ||
136 | rccResetAHB1(~0); | ||
137 | rccResetAHB2(~STM32_GPIO_EN_MASK); | ||
138 | rccResetAHB3(~0); | ||
139 | rccResetAPB1R1(~0); | ||
140 | rccResetAPB1R2(~0); | ||
141 | rccResetAPB2(~0); | ||
142 | |||
143 | /* PWR clock enable.*/ | ||
144 | #if (HAL_USE_RTC == TRUE) && defined(RCC_APBENR1_RTCAPBEN) | ||
145 | rccEnableAPB1R1(RCC_APB1ENR1_PWREN | RCC_APB1ENR1_RTCAPBEN, false) | ||
146 | #else | ||
147 | rccEnableAPB1R1(RCC_APB1ENR1_PWREN, false) | ||
148 | #endif | ||
149 | |||
150 | /* Core voltage setup.*/ | ||
151 | PWR->CR1 = STM32_VOS; | ||
152 | while ((PWR->SR2 & PWR_SR2_VOSF) != 0) /* Wait until regulator is */ | ||
153 | ; /* stable. */ | ||
154 | |||
155 | /* Additional PWR configurations.*/ | ||
156 | PWR->CR2 = STM32_PWR_CR2; | ||
157 | PWR->CR3 = STM32_PWR_CR3; | ||
158 | PWR->CR4 = STM32_PWR_CR4; | ||
159 | PWR->CR5 = STM32_CR5BITS; | ||
160 | PWR->PUCRA = STM32_PWR_PUCRA; | ||
161 | PWR->PDCRA = STM32_PWR_PDCRA; | ||
162 | PWR->PUCRB = STM32_PWR_PUCRB; | ||
163 | PWR->PDCRB = STM32_PWR_PDCRB; | ||
164 | PWR->PUCRC = STM32_PWR_PUCRC; | ||
165 | PWR->PDCRC = STM32_PWR_PDCRC; | ||
166 | PWR->PUCRD = STM32_PWR_PUCRD; | ||
167 | PWR->PDCRD = STM32_PWR_PDCRD; | ||
168 | PWR->PUCRE = STM32_PWR_PUCRE; | ||
169 | PWR->PDCRE = STM32_PWR_PDCRE; | ||
170 | PWR->PUCRF = STM32_PWR_PUCRF; | ||
171 | PWR->PDCRF = STM32_PWR_PDCRF; | ||
172 | PWR->PUCRG = STM32_PWR_PUCRG; | ||
173 | PWR->PDCRG = STM32_PWR_PDCRG; | ||
174 | |||
175 | #if STM32_HSI16_ENABLED | ||
176 | /* HSI activation.*/ | ||
177 | RCC->CR |= RCC_CR_HSION; | ||
178 | while ((RCC->CR & RCC_CR_HSIRDY) == 0) | ||
179 | ; /* Wait until HSI16 is stable. */ | ||
180 | #endif | ||
181 | |||
182 | #if STM32_HSI48_ENABLED | ||
183 | /* HSI activation.*/ | ||
184 | RCC->CRRCR |= RCC_CRRCR_HSI48ON; | ||
185 | while ((RCC->CRRCR & RCC_CRRCR_HSI48RDY) == 0) | ||
186 | ; /* Wait until HSI48 is stable. */ | ||
187 | #endif | ||
188 | |||
189 | #if STM32_HSE_ENABLED | ||
190 | #if defined(STM32_HSE_BYPASS) | ||
191 | /* HSE Bypass.*/ | ||
192 | RCC->CR |= RCC_CR_HSEON | RCC_CR_HSEBYP; | ||
193 | #endif | ||
194 | /* HSE activation.*/ | ||
195 | RCC->CR |= RCC_CR_HSEON; | ||
196 | while ((RCC->CR & RCC_CR_HSERDY) == 0) | ||
197 | ; /* Wait until HSE is stable. */ | ||
198 | #endif | ||
199 | |||
200 | #if STM32_LSI_ENABLED | ||
201 | /* LSI activation.*/ | ||
202 | RCC->CSR |= RCC_CSR_LSION; | ||
203 | while ((RCC->CSR & RCC_CSR_LSIRDY) == 0) | ||
204 | ; /* Wait until LSI is stable. */ | ||
205 | #endif | ||
206 | |||
207 | /* Backup domain access enabled and left open.*/ | ||
208 | PWR->CR1 |= PWR_CR1_DBP; | ||
209 | |||
210 | #if STM32_LSE_ENABLED | ||
211 | /* LSE activation.*/ | ||
212 | #if defined(STM32_LSE_BYPASS) | ||
213 | /* LSE Bypass.*/ | ||
214 | RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON | RCC_BDCR_LSEBYP; | ||
215 | #else | ||
216 | /* No LSE Bypass.*/ | ||
217 | RCC->BDCR |= STM32_LSEDRV | RCC_BDCR_LSEON; | ||
218 | #endif | ||
219 | while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0) | ||
220 | ; /* Wait until LSE is stable. */ | ||
221 | #endif | ||
222 | |||
223 | #if STM32_ACTIVATE_PLL | ||
224 | /* PLLM and PLLSRC are common to all PLLs.*/ | ||
225 | RCC->PLLCFGR = STM32_PLLPDIV | | ||
226 | STM32_PLLR | STM32_PLLREN | | ||
227 | STM32_PLLQ | STM32_PLLQEN | | ||
228 | STM32_PLLP | STM32_PLLPEN | | ||
229 | STM32_PLLN | STM32_PLLM | | ||
230 | STM32_PLLSRC; | ||
231 | #endif | ||
232 | |||
233 | #if STM32_ACTIVATE_PLL | ||
234 | /* PLL activation.*/ | ||
235 | RCC->CR |= RCC_CR_PLLON; | ||
236 | |||
237 | /* Waiting for PLL lock.*/ | ||
238 | while ((RCC->CR & RCC_CR_PLLRDY) == 0) | ||
239 | ; | ||
240 | #endif | ||
241 | |||
242 | /* Other clock-related settings (dividers, MCO etc).*/ | ||
243 | RCC->CFGR = STM32_MCOPRE | STM32_MCOSEL | STM32_PPRE2 | STM32_PPRE1 | | ||
244 | STM32_HPRE; | ||
245 | |||
246 | /* CCIPR registers initialization, note.*/ | ||
247 | RCC->CCIPR = STM32_ADC345SEL | STM32_ADC12SEL | STM32_CLK48SEL | | ||
248 | STM32_FDCANSEL | STM32_I2S23SEL | STM32_SAI1SEL | | ||
249 | STM32_LPTIM1SEL | STM32_I2C3SEL | STM32_I2C2SEL | | ||
250 | STM32_I2C1SEL | STM32_LPUART1SEL | STM32_UART5SEL | | ||
251 | STM32_UART4SEL | STM32_USART3SEL | STM32_USART2SEL | | ||
252 | STM32_USART1SEL; | ||
253 | RCC->CCIPR2 = STM32_QSPISEL | STM32_I2C4SEL; | ||
254 | |||
255 | /* Set flash WS's for SYSCLK source */ | ||
256 | FLASH->ACR = FLASH_ACR_DBG_SWEN | FLASH_ACR_DCEN | FLASH_ACR_ICEN | | ||
257 | FLASH_ACR_PRFTEN | STM32_FLASHBITS; | ||
258 | while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != | ||
259 | (STM32_FLASHBITS & FLASH_ACR_LATENCY_Msk)) { | ||
260 | } | ||
261 | |||
262 | /* Switching to the configured SYSCLK source if it is different from HSI16.*/ | ||
263 | #if STM32_SW != STM32_SW_HSI16 | ||
264 | RCC->CFGR |= STM32_SW; /* Switches on the selected clock source. */ | ||
265 | /* Wait until SYSCLK is stable.*/ | ||
266 | while ((RCC->CFGR & RCC_CFGR_SWS) != (STM32_SW << 2)) | ||
267 | ; | ||
268 | #endif | ||
269 | |||
270 | #endif /* STM32_NO_INIT */ | ||
271 | |||
272 | /* SYSCFG clock enabled here because it is a multi-functional unit shared | ||
273 | among multiple drivers.*/ | ||
274 | rccEnableAPB2(RCC_APB2ENR_SYSCFGEN, true); | ||
275 | } | ||
276 | |||
277 | /** @} */ | ||