2 * Copyright © 2023 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 /* Switch to HSI while messing about */
27 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
28 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
31 stm_rcc.cfgr = (stm_rcc.cfgr & ~(uint32_t) (STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
32 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
34 /* wait for system to switch to HSI */
35 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
36 (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
39 /* Disable all interrupts */
44 stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
46 stm_rcc.cr &= ~(uint32_t) (1 << STM_RCC_CR_HSEBYP);
48 /* Enable HSE clock */
49 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
50 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
53 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
54 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSE)
55 #define STM_PLLSRC AO_HSE
56 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (STM_RCC_CFGR_PLLSRC_HSE << STM_RCC_CFGR_PLLSRC)
58 #define STM_HSI 16000000
59 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
60 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSI)
61 #define STM_PLLSRC (STM_HSI/2)
62 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (STM_RCC_CFGR_PLLSRC_HSI_2 << STM_RCC_CFGR_PLLSRC)
65 #if !AO_HSE || HAS_ADC || HAS_ADC_SINGLE
66 /* Enable HSI RC clock 16MHz */
67 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
68 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
72 /* Set flash latency to tolerate 72MHz SYSCLK -> 2 wait states */
74 /* Enable 64-bit access and prefetch */
75 stm_flash.acr = ((1 << STM_FLASH_ACR_PRFTBE) |
76 (0 << STM_FLASH_ACR_HLFCYA) |
77 (STM_FLASH_ACR_LATENCY_2 << STM_FLASH_ACR_LATENCY));
79 /* Enable power interface clock */
80 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
83 /* Set voltage range to 1.8V */
85 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
86 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
89 /* Configure voltage scaling range */
91 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
92 cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
95 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
96 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
102 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
103 cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
105 while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
106 (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
109 /* APB1 Prescaler = AO_APB1_PRESCALER */
111 cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
112 cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
115 /* APB2 Prescaler = AO_APB2_PRESCALER */
117 cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
118 cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
123 cfgr &= ~(STM_RCC_CFGR_ADCPRE_MASK << STM_RCC_CFGR_ADCPRE);
124 cfgr |= (AO_RCC_CFGR_ADCPRE << STM_RCC_CFGR_ADCPRE);
127 /* Disable the PLL */
128 stm_rcc.cr &= ~(1UL << STM_RCC_CR_PLLON);
129 while (stm_rcc.cr & (1UL << STM_RCC_CR_PLLRDY))
134 cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
135 cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
138 cfgr &= ~(STM_RCC_CFGR_PLLXTPRE_MASK << STM_RCC_CFGR_PLLXTPRE);
139 cfgr |= (AO_RCC_CFGR_PLLXTPRE << STM_RCC_CFGR_PLLXTPRE);
142 cfgr &= ~(1UL << STM_RCC_CFGR_PLLSRC);
143 cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
147 /* Enable the PLL and wait for it */
148 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
149 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
152 /* Switch to the PLL for the system clock */
155 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
156 cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
159 uint32_t c, part, mask, val;
162 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
163 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
170 stm_rcc.apb2rstr = 0xffff;
171 stm_rcc.apb1rstr = 0xffff;
172 stm_rcc.ahbrstr = 0x3f;
173 stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
177 stm_rcc.apb1rstr = 0;
178 stm_rcc.apb2rstr = 0;
181 /* Clear reset flags */
182 stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
185 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_AFIOEN);
187 /* Release PB3, PA15 and PB4 from JTAG use */
188 stm_afio.mapr = (stm_afio.mapr &
189 ~(STM_AFIO_MAPR_SWJ_CFG_MASK << STM_AFIO_MAPR_SWJ_CFG)) |
190 STM_AFIO_MAPR_SWJ_CFG_SW_DP << STM_AFIO_MAPR_SWJ_CFG;
193 /* Output SYSCLK on PA8 for measurments */
195 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
197 stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
198 stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
199 stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
201 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
202 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);