5c05e4f187c09017876b1dfd476ed6a9259e7e9d
[fw/altos] / src / stmf0 / ao_timer.c
1 /*
2  * Copyright © 2012 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 #if HAS_FAKE_FLIGHT
22 #include <ao_fake_flight.h>
23 #endif
24
25 #ifndef HAS_TICK
26 #define HAS_TICK 1
27 #endif
28
29 #if HAS_TICK
30 volatile AO_TICK_TYPE ao_tick_count;
31
32 AO_TICK_TYPE
33 ao_time(void)
34 {
35         return ao_tick_count;
36 }
37
38 #if AO_DATA_ALL
39 volatile __data uint8_t ao_data_interval = 1;
40 volatile __data uint8_t ao_data_count;
41 #endif
42
43 void stm_systick_isr(void)
44 {
45         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
46                 ++ao_tick_count;
47 #if HAS_TASK_QUEUE
48                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
49                         ao_task_check_alarm((uint16_t) ao_tick_count);
50 #endif
51 #if AO_DATA_ALL
52                 if (++ao_data_count == ao_data_interval) {
53                         ao_data_count = 0;
54 #if HAS_ADC
55 #if HAS_FAKE_FLIGHT
56                         if (ao_fake_flight_active)
57                                 ao_fake_flight_poll();
58                         else
59 #endif
60                                 ao_adc_poll();
61 #endif
62 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
63                         ao_wakeup((void *) &ao_data_count);
64 #endif
65                 }
66 #endif
67 #ifdef AO_TIMER_HOOK
68                 AO_TIMER_HOOK;
69 #endif
70         }
71 }
72
73 #if HAS_ADC
74 void
75 ao_timer_set_adc_interval(uint8_t interval)
76 {
77         ao_arch_critical(
78                 ao_data_interval = interval;
79                 ao_data_count = 0;
80                 );
81 }
82 #endif
83
84 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
85
86 void
87 ao_timer_init(void)
88 {
89         stm_systick.rvr = SYSTICK_RELOAD;
90         stm_systick.cvr = 0;
91         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
92                            (1 << STM_SYSTICK_CSR_TICKINT) |
93                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
94 }
95
96 #endif
97
98 #if AO_HSI48
99 static void
100 ao_clock_enable_crs(void)
101 {
102         /* Enable crs interface clock */
103         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_CRSEN);
104
105         /* Disable error counter */
106         stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
107                       (32 << STM_CRS_CR_TRIM) |
108                       (0 << STM_CRS_CR_SWSYNC) |
109                       (0 << STM_CRS_CR_AUTOTRIMEN) |
110                       (0 << STM_CRS_CR_CEN) |
111                       (0 << STM_CRS_CR_ESYNCIE) |
112                       (0 << STM_CRS_CR_ERRIE) |
113                       (0 << STM_CRS_CR_SYNCWARNIE) |
114                       (0 << STM_CRS_CR_SYNCOKIE));
115
116         /* Configure for USB source */
117         stm_crs.cfgr = ((stm_crs.cfgr & ((1 << 30) | (1 << 27))) |
118                         (0 << STM_CRS_CFGR_SYNCPOL) |
119                         (STM_CRS_CFGR_SYNCSRC_USB << STM_CRS_CFGR_SYNCSRC) |
120                         (STM_CRS_CFGR_SYNCDIV_1 << STM_CRS_CFGR_SYNCDIV) |
121                         (0x22 << STM_CRS_CFGR_FELIM) |
122                         (((48000000 / 1000) - 1) << STM_CRS_CFGR_RELOAD));
123
124         /* Enable error counter, set auto trim */
125         stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
126                       (32 << STM_CRS_CR_TRIM) |
127                       (0 << STM_CRS_CR_SWSYNC) |
128                       (1 << STM_CRS_CR_AUTOTRIMEN) |
129                       (1 << STM_CRS_CR_CEN) |
130                       (0 << STM_CRS_CR_ESYNCIE) |
131                       (0 << STM_CRS_CR_ERRIE) |
132                       (0 << STM_CRS_CR_SYNCWARNIE) |
133                       (0 << STM_CRS_CR_SYNCOKIE));
134 }
135 #endif
136
137 static void
138 ao_clock_hsi(void)
139 {
140         stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
141         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
142                 ao_arch_nop();
143
144         stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
145                 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
146
147         /* wait for system to switch to HSI */
148         while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
149                (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
150                 ao_arch_nop();
151
152         /* reset the clock config, leaving us running on the HSI */
153         stm_rcc.cfgr &= (uint32_t)0x0000000f;
154
155         /* reset PLLON, CSSON, HSEBYP, HSEON */
156         stm_rcc.cr &= 0x0000ffff;
157 }
158
159 static void
160 ao_clock_normal_start(void)
161 {
162 #if AO_HSE
163         uint32_t        cfgr;
164 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_PLL
165 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_PLL
166 #define STM_PLLSRC                              AO_HSE
167 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        STM_RCC_CFGR_PLLSRC_HSE
168
169 #if AO_HSE_BYPASS
170         stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
171 #else
172         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
173 #endif
174         /* Enable HSE clock */
175         stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
176         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
177                 asm("nop");
178
179         /* Disable the PLL */
180         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
181         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
182                 asm("nop");
183
184         /* Set multiplier */
185         cfgr = stm_rcc.cfgr;
186         cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
187         cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
188
189         /* PLL source */
190         cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
191         cfgr |= (STM_RCC_CFGR_PLLSRC_TARGET_CLOCK  << STM_RCC_CFGR_PLLSRC);
192         stm_rcc.cfgr = cfgr;
193
194         /* Set pre divider */
195         stm_rcc.cfgr2 = (AO_RCC_CFGR2_PLLDIV << STM_RCC_CFGR2_PREDIV);
196
197         /* Enable the PLL and wait for it */
198         stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
199         while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
200                 asm("nop");
201
202 #endif
203
204 #if AO_HSI48
205 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSI48
206 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSI48
207
208         /* Turn HSI48 clock on */
209         stm_rcc.cr2 |= (1 << STM_RCC_CR2_HSI48ON);
210
211         /* Wait for clock to stabilize */
212         while ((stm_rcc.cr2 & (1 << STM_RCC_CR2_HSI48RDY)) == 0)
213                 ao_arch_nop();
214
215         ao_clock_enable_crs();
216 #endif
217
218 #ifndef STM_RCC_CFGR_SWS_TARGET_CLOCK
219 #define STM_HSI                                 16000000
220 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSI
221 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSI
222 #define STM_PLLSRC                              STM_HSI
223 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        0
224 #endif
225
226
227 }
228
229 static void
230 ao_clock_normal_switch(void)
231 {
232         uint32_t        cfgr;
233
234         cfgr = stm_rcc.cfgr;
235         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
236         cfgr |= (STM_RCC_CFGR_SW_TARGET_CLOCK << STM_RCC_CFGR_SW);
237         stm_rcc.cfgr = cfgr;
238         for (;;) {
239                 uint32_t        c, part, mask, val;
240
241                 c = stm_rcc.cfgr;
242                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
243                 val = (STM_RCC_CFGR_SWS_TARGET_CLOCK << STM_RCC_CFGR_SWS);
244                 part = c & mask;
245                 if (part == val)
246                         break;
247         }
248 #if !AO_HSI && !AO_NEED_HSI
249         /* Turn off the HSI clock */
250         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSION);
251 #endif
252 #ifdef STM_PLLSRC
253         /* USB PLL source */
254         stm_rcc.cfgr3 |= (1 << STM_RCC_CFGR3_USBSW);
255 #endif
256 }
257
258 void
259 ao_clock_init(void)
260 {
261         uint32_t        cfgr;
262
263         /* Switch to HSI while messing about */
264         ao_clock_hsi();
265
266         /* Disable all interrupts */
267         stm_rcc.cir = 0;
268
269         /* Start high speed clock */
270         ao_clock_normal_start();
271
272         /* Set flash latency to tolerate 48MHz SYSCLK  -> 1 wait state */
273
274         /* Enable prefetch */
275         stm_flash.acr |= (1 << STM_FLASH_ACR_PRFTBE);
276
277         /* Enable 1 wait state so the CPU can run at 48MHz */
278         stm_flash.acr |= (STM_FLASH_ACR_LATENCY_1 << STM_FLASH_ACR_LATENCY);
279
280         /* Enable power interface clock */
281         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
282
283         /* HCLK to 48MHz -> AHB prescaler = /1 */
284         cfgr = stm_rcc.cfgr;
285         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
286         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
287         stm_rcc.cfgr = cfgr;
288         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
289                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
290                 ao_arch_nop();
291
292         /* APB Prescaler = AO_APB_PRESCALER */
293         cfgr = stm_rcc.cfgr;
294         cfgr &= ~(STM_RCC_CFGR_PPRE_MASK << STM_RCC_CFGR_PPRE);
295         cfgr |= (AO_RCC_CFGR_PPRE_DIV << STM_RCC_CFGR_PPRE);
296         stm_rcc.cfgr = cfgr;
297
298         /* Switch to the desired system clock */
299         ao_clock_normal_switch();
300
301         /* Clear reset flags */
302         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
303
304 #ifdef AO_MCO_PORT
305         cfgr = stm_rcc.cfgr;
306
307         /* Send PLL clock to MCO */
308         cfgr &= ~(STM_RCC_CFGR_MCO_MASK << STM_RCC_CFGR_MCO);
309         cfgr |= (STM_RCC_CFGR_MCO_PLLCLK << STM_RCC_CFGR_MCO);
310
311         /* Divide by 1 */
312         cfgr &= ~(STM_RCC_CFGR_MCOPRE_DIV_MASK << STM_RCC_CFGR_MCOPRE);
313         cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
314
315         /* Don't divide PLL */
316         cfgr |= (1 << STM_RCC_CFGR_PLL_NODIV);
317
318         stm_rcc.cfgr = cfgr;
319
320         ao_enable_port(AO_MCO_PORT);
321         stm_ospeedr_set(AO_MCO_PORT, AO_MCO_PIN, STM_OSPEEDR_HIGH);
322         stm_afr_set(AO_MCO_PORT, AO_MCO_PIN, AO_MCO_AF);
323 #endif
324
325 #if DEBUG_THE_CLOCK
326         /* Output SYSCLK on PA8 for measurments */
327
328         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
329
330         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
331         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_HIGH);
332
333         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
334         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
335 #endif
336 }
337
338 #if AO_POWER_MANAGEMENT
339 void
340 ao_clock_suspend(void)
341 {
342         ao_clock_hsi();
343 }
344
345 void
346 ao_clock_resume(void)
347 {
348         ao_clock_normal_start();
349         ao_clock_normal_switch();
350 }
351 #endif