altos/stm32f4: Fix clock configuration
[fw/altos] / src / stm32f4 / ao_timer.c
1 /*
2  * Copyright © 2018 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "ao.h"
20 #include <ao_task.h>
21
22 #ifndef HAS_TICK
23 #define HAS_TICK 1
24 #endif
25
26 #if HAS_TICK || defined(AO_TIMER_HOOK)
27
28 #if HAS_TICK
29 volatile AO_TICK_TYPE ao_tick_count;
30
31 AO_TICK_TYPE
32 ao_time(void)
33 {
34         return ao_tick_count;
35 }
36 #endif
37
38 #if AO_DATA_ALL
39 volatile uint8_t        ao_data_interval = 1;
40 volatile uint8_t        ao_data_count;
41 #endif
42
43 void stm_systick_isr(void)
44 {
45         ao_validate_cur_stack();
46         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
47 #if HAS_TICK
48                 ++ao_tick_count;
49 #endif
50 #if HAS_TASK_QUEUE
51                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
52                         ao_task_check_alarm((uint16_t) ao_tick_count);
53 #endif
54 #if AO_DATA_ALL
55                 if (++ao_data_count == ao_data_interval) {
56                         ao_data_count = 0;
57 #if HAS_FAKE_FLIGHT
58                         if (ao_fake_flight_active)
59                                 ao_fake_flight_poll();
60                         else
61 #endif
62                                 ao_adc_poll();
63 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
64                         ao_wakeup((void *) &ao_data_count);
65 #endif
66                 }
67 #endif
68 #ifdef AO_TIMER_HOOK
69                 AO_TIMER_HOOK;
70 #endif
71         }
72 }
73
74 #if HAS_ADC
75 void
76 ao_timer_set_adc_interval(uint8_t interval)
77 {
78         ao_arch_critical(
79                 ao_data_interval = interval;
80                 ao_data_count = 0;
81                 );
82 }
83 #endif
84
85 #define SYSTICK_RELOAD ((AO_SYSTICK / 8) / 100 - 1)
86
87 void
88 ao_timer_init(void)
89 {
90         stm_systick.rvr = SYSTICK_RELOAD;
91         stm_systick.cvr = 0;
92         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
93                            (1 << STM_SYSTICK_CSR_TICKINT) |
94                            (STM_SYSTICK_CSR_CLKSOURCE_AHB_8 << STM_SYSTICK_CSR_CLKSOURCE));
95         stm_scb.shpr3 |= AO_STM_NVIC_CLOCK_PRIORITY << 24;
96 }
97
98 #endif
99
100 void
101 ao_clock_init(void)
102 {
103         uint32_t        cfgr;
104         uint32_t        pllcfgr;
105
106         /* Switch to HSI while messing about */
107         stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
108         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
109                 ao_arch_nop();
110
111         stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
112                 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
113
114         /* wait for system to switch to HSI */
115         while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
116                (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
117                 ao_arch_nop();
118
119         /* reset everything but the HSI selection and status */
120         stm_rcc.cfgr &= (uint32_t)0x0000000f;
121
122         /* reset everything but HSI */
123         stm_rcc.cr &= 0x0000ffff;
124
125         /* Disable and clear all interrupts */
126         stm_rcc.cir = 0xffff0000;
127
128 #if AO_HSE
129 #if AO_HSE_BYPASS
130         stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
131 #else
132         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
133 #endif
134         /* Enable HSE clock */
135         stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
136         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
137                 asm("nop");
138
139 #endif
140
141         /* Set flash latency to tolerate SYSCLK */
142
143 #define FLASH_LATENCY   ((AO_SYSCLK - 1) / 25000000)
144
145         /* Enable icache, dcache and prefetch. Set latency */
146         stm_flash.acr = ((1 << STM_FLASH_ACR_DCEN) |
147                          (1 << STM_FLASH_ACR_ICEN) |
148                          (1 << STM_FLASH_ACR_PRFTEN) |
149                          (FLASH_LATENCY << STM_FLASH_ACR_LATENCY));
150
151         /* Enable power interface clock */
152         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
153
154 #if AO_SYSCLK <= 64000000
155 #define VOS_SCALE_MODE  STM_PWR_CR_VOS_SCALE_MODE_1
156 #elif AO_SYSCLK <= 84000000
157 #define VOS_SCALE_MODE  STM_PWR_CR_VOS_SCALE_MODE_2
158 #else
159 #define VOS_SCALE_MODE  STM_PWR_CR_VOS_SCALE_MODE_1
160 #endif
161
162         /* Set voltage scale mode */
163         stm_pwr.cr = ((stm_pwr.cr & ~(STM_PWR_CR_VOS_SCALE_MODE_MASK)) |
164                       (VOS_SCALE_MODE << STM_PWR_CR_VOS));
165
166         /* HCLK */
167         cfgr = stm_rcc.cfgr;
168         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
169         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
170         stm_rcc.cfgr = cfgr;
171
172         /* APB1 Prescaler = AO_APB1_PRESCALER */
173         cfgr = stm_rcc.cfgr;
174         cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
175         cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
176         stm_rcc.cfgr = cfgr;
177
178         /* APB2 Prescaler = AO_APB2_PRESCALER */
179         cfgr = stm_rcc.cfgr;
180         cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
181         cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
182         stm_rcc.cfgr = cfgr;
183
184         /* Disable the PLL */
185         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
186         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
187                 asm("nop");
188
189         /* PLL1VCO */
190         pllcfgr = stm_rcc.pllcfgr;
191         pllcfgr &= ~(STM_RCC_PLLCFGR_PLLM_MASK << STM_RCC_PLLCFGR_PLLM);
192         pllcfgr &= ~(STM_RCC_PLLCFGR_PLLN_MASK << STM_RCC_PLLCFGR_PLLN);
193         pllcfgr &= ~(STM_RCC_PLLCFGR_PLLP_MASK << STM_RCC_PLLCFGR_PLLP);
194         pllcfgr &= ~(STM_RCC_PLLCFGR_PLLQ_MASK << STM_RCC_PLLCFGR_PLLQ);
195         pllcfgr &= ~(STM_RCC_PLLCFGR_PLLR_MASK << STM_RCC_PLLCFGR_PLLR);
196
197         pllcfgr |= (AO_PLL_M << STM_RCC_PLLCFGR_PLLM);
198         pllcfgr |= (AO_PLL1_N << STM_RCC_PLLCFGR_PLLN);
199 #if AO_PLL1_P
200 #if AO_PLL1_P == 2
201 #define AO_RCC_PLLCFGR_PLLP     STM_RCC_PLLCFGR_PLLP_DIV_2
202 #endif
203 #if AO_PLL1_P == 4
204 #define AO_RCC_PLLCFGR_PLLP     STM_RCC_PLLCFGR_PLLP_DIV_4
205 #endif
206 #if AO_PLL1_P == 6
207 #define AO_RCC_PLLCFGR_PLLP     STM_RCC_PLLCFGR_PLLP_DIV_6
208 #endif
209 #if AO_PLL1_P == 8
210 #define AO_RCC_PLLCFGR_PLLP     STM_RCC_PLLCFGR_PLLP_DIV_8
211 #endif
212         pllcfgr |= (AO_RCC_PLLCFGR_PLLP << STM_RCC_PLLCFGR_PLLP);
213 #endif
214 #if AO_PLL1_Q
215         pllcfgr |= (AO_PLL1_Q << STM_RCC_PLLCFGR_PLLQ);
216 #endif
217 #if AO_PLL1_R
218         pllcfgr |= (AO_PLL1_R << STM_RCC_PLLCFGR_PLLR);
219 #endif
220         /* PLL source */
221         pllcfgr &= ~(1 << STM_RCC_PLLCFGR_PLLSRC);
222 #if AO_HSI
223         pllcfgr |= (STM_RCC_PLLCFGR_PLLSRC_HSI << STM_RCC_PLLCFGR_PLLSRC);
224 #endif
225 #if AO_HSE
226         pllcfgr |= (STM_RCC_PLLCFGR_PLLSRC_HSE << STM_RCC_PLLCFGR_PLLSRC);
227 #endif
228         stm_rcc.pllcfgr = pllcfgr;
229
230         /* Enable the PLL and wait for it */
231         stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
232         while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
233                 asm("nop");
234
235         /* Switch to the PLL for the system clock */
236
237         cfgr = stm_rcc.cfgr;
238         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
239         cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
240         stm_rcc.cfgr = cfgr;
241         for (;;) {
242                 uint32_t        c, part, mask, val;
243
244                 c = stm_rcc.cfgr;
245                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
246                 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
247                 part = c & mask;
248                 if (part == val)
249                         break;
250         }
251
252 #if AO_HSE
253         /* Disable HSI clock */
254         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSION);
255 #endif
256
257         /* Clear reset flags */
258         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
259
260 #if DEBUG_THE_CLOCK
261         /* Output PLL clock on PA8 and SYCLK on PC9 for measurments */
262
263         stm_rcc.ahb1enr |= ((1 << STM_RCC_AHB1ENR_IOPAEN) |
264                             (1 << STM_RCC_AHB1ENR_IOPCEN));
265
266         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
267         stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
268         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_HIGH);
269
270         stm_afr_set(&stm_gpioc, 9, STM_AFR_AF0);
271         stm_moder_set(&stm_gpioc, 9, STM_MODER_ALTERNATE);
272         stm_ospeedr_set(&stm_gpioc, 9, STM_OSPEEDR_HIGH);
273
274         cfgr = stm_rcc.cfgr;
275         cfgr &= 0x001fffff;
276         cfgr |= ((0 << STM_RCC_CFGR_MCO2) |
277                  (6 << STM_RCC_CFGR_MCO2PRE) |
278                  (6 << STM_RCC_CFGR_MCO1PRE) |
279                  (2 << STM_RCC_CFGR_MCO1));
280         stm_rcc.cfgr = cfgr;
281 #endif
282 }