altos/stmf0: Remove ao_usb_free
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include <ao_task.h>
20 #if HAS_FAKE_FLIGHT
21 #include <ao_fake_flight.h>
22 #endif
23
24 #ifndef HAS_TICK
25 #define HAS_TICK 1
26 #endif
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
37 #if AO_DATA_ALL
38 volatile __data uint8_t ao_data_interval = 1;
39 volatile __data uint8_t ao_data_count;
40 #endif
41
42 void stm_systick_isr(void)
43 {
44         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
45                 ++ao_tick_count;
46 #if HAS_TASK_QUEUE
47                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
48                         ao_task_check_alarm((uint16_t) ao_tick_count);
49 #endif
50 #if AO_DATA_ALL
51                 if (++ao_data_count == ao_data_interval) {
52                         ao_data_count = 0;
53 #if HAS_ADC
54 #if HAS_FAKE_FLIGHT
55                         if (ao_fake_flight_active)
56                                 ao_fake_flight_poll();
57                         else
58 #endif
59                                 ao_adc_poll();
60 #endif
61 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
62                         ao_wakeup((void *) &ao_data_count);
63 #endif
64                 }
65 #endif
66 #ifdef AO_TIMER_HOOK
67                 AO_TIMER_HOOK;
68 #endif
69         }
70 }
71
72 #if HAS_ADC
73 void
74 ao_timer_set_adc_interval(uint8_t interval)
75 {
76         ao_arch_critical(
77                 ao_data_interval = interval;
78                 ao_data_count = 0;
79                 );
80 }
81 #endif
82
83 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
84
85 void
86 ao_timer_init(void)
87 {
88         stm_systick.rvr = SYSTICK_RELOAD;
89         stm_systick.cvr = 0;
90         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
91                            (1 << STM_SYSTICK_CSR_TICKINT) |
92                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
93 }
94
95 #endif
96
97 #if AO_HSI48
98 static void
99 ao_clock_enable_crs(void)
100 {
101         /* Enable crs interface clock */
102         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_CRSEN);
103
104         /* Disable error counter */
105         stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
106                       (32 << STM_CRS_CR_TRIM) |
107                       (0 << STM_CRS_CR_SWSYNC) |
108                       (0 << STM_CRS_CR_AUTOTRIMEN) |
109                       (0 << STM_CRS_CR_CEN) |
110                       (0 << STM_CRS_CR_ESYNCIE) |
111                       (0 << STM_CRS_CR_ERRIE) |
112                       (0 << STM_CRS_CR_SYNCWARNIE) |
113                       (0 << STM_CRS_CR_SYNCOKIE));
114
115         /* Configure for USB source */
116         stm_crs.cfgr = ((stm_crs.cfgr & ((1 << 30) | (1 << 27))) |
117                         (0 << STM_CRS_CFGR_SYNCPOL) |
118                         (STM_CRS_CFGR_SYNCSRC_USB << STM_CRS_CFGR_SYNCSRC) |
119                         (STM_CRS_CFGR_SYNCDIV_1 << STM_CRS_CFGR_SYNCDIV) |
120                         (0x22 << STM_CRS_CFGR_FELIM) |
121                         (((48000000 / 1000) - 1) << STM_CRS_CFGR_RELOAD));
122
123         /* Enable error counter, set auto trim */
124         stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
125                       (32 << STM_CRS_CR_TRIM) |
126                       (0 << STM_CRS_CR_SWSYNC) |
127                       (1 << STM_CRS_CR_AUTOTRIMEN) |
128                       (1 << STM_CRS_CR_CEN) |
129                       (0 << STM_CRS_CR_ESYNCIE) |
130                       (0 << STM_CRS_CR_ERRIE) |
131                       (0 << STM_CRS_CR_SYNCWARNIE) |
132                       (0 << STM_CRS_CR_SYNCOKIE));
133 }
134 #endif
135
136 static void
137 ao_clock_hsi(void)
138 {
139         stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
140         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
141                 ao_arch_nop();
142
143         stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
144                 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
145
146         /* wait for system to switch to HSI */
147         while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
148                (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
149                 ao_arch_nop();
150
151         /* reset the clock config, leaving us running on the HSI */
152         stm_rcc.cfgr &= (uint32_t)0x0000000f;
153
154         /* reset PLLON, CSSON, HSEBYP, HSEON */
155         stm_rcc.cr &= 0x0000ffff;
156 }
157
158 static void
159 ao_clock_normal_start(void)
160 {
161 #if AO_HSE
162         uint32_t        cfgr;
163 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_PLL
164 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_PLL
165 #define STM_PLLSRC                              AO_HSE
166 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        STM_RCC_CFGR_PLLSRC_HSE
167
168 #if AO_HSE_BYPASS
169         stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
170 #else
171         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
172 #endif
173         /* Enable HSE clock */
174         stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
175         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
176                 asm("nop");
177
178 #ifdef STM_PLLSRC
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         /* PLLVCO to 48MHz (for USB) -> PLLMUL = 3 */
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         /* Disable pre divider */
195         stm_rcc.cfgr2 = (STM_RCC_CFGR2_PREDIV_1 << 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 #endif
205
206
207 #if AO_HSI48
208 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSI48
209 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSI48
210
211         /* Turn HSI48 clock on */
212         stm_rcc.cr2 |= (1 << STM_RCC_CR2_HSI48ON);
213
214         /* Wait for clock to stabilize */
215         while ((stm_rcc.cr2 & (1 << STM_RCC_CR2_HSI48RDY)) == 0)
216                 ao_arch_nop();
217
218         ao_clock_enable_crs();
219 #endif
220
221 #ifndef STM_RCC_CFGR_SWS_TARGET_CLOCK
222 #define STM_HSI                                 16000000
223 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSI
224 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSI
225 #define STM_PLLSRC                              STM_HSI
226 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        0
227 #endif
228 }
229
230 static void
231 ao_clock_normal_switch(void)
232 {
233         uint32_t        cfgr;
234
235         cfgr = stm_rcc.cfgr;
236         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
237         cfgr |= (STM_RCC_CFGR_SW_TARGET_CLOCK << STM_RCC_CFGR_SW);
238         stm_rcc.cfgr = cfgr;
239         for (;;) {
240                 uint32_t        c, part, mask, val;
241
242                 c = stm_rcc.cfgr;
243                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
244                 val = (STM_RCC_CFGR_SWS_TARGET_CLOCK << STM_RCC_CFGR_SWS);
245                 part = c & mask;
246                 if (part == val)
247                         break;
248         }
249 #if !AO_HSI && !AO_NEED_HSI
250         /* Turn off the HSI clock */
251         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSION);
252 #endif
253 #ifdef STM_PLLSRC
254         /* USB PLL source */
255         stm_rcc.cfgr3 |= (1 << STM_RCC_CFGR3_USBSW);
256 #endif
257 }
258
259 void
260 ao_clock_init(void)
261 {
262         uint32_t        cfgr;
263
264         /* Switch to HSI while messing about */
265         ao_clock_hsi();
266
267         /* Disable all interrupts */
268         stm_rcc.cir = 0;
269
270         /* Start high speed clock */
271         ao_clock_normal_start();
272
273         /* Set flash latency to tolerate 48MHz SYSCLK  -> 1 wait state */
274
275         /* Enable prefetch */
276         stm_flash.acr |= (1 << STM_FLASH_ACR_PRFTBE);
277
278         /* Enable 1 wait state so the CPU can run at 48MHz */
279         stm_flash.acr |= (STM_FLASH_ACR_LATENCY_1 << STM_FLASH_ACR_LATENCY);
280
281         /* Enable power interface clock */
282         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
283
284         /* HCLK to 48MHz -> AHB prescaler = /1 */
285         cfgr = stm_rcc.cfgr;
286         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
287         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
288         stm_rcc.cfgr = cfgr;
289         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
290                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
291                 ao_arch_nop();
292
293         /* APB Prescaler = AO_APB_PRESCALER */
294         cfgr = stm_rcc.cfgr;
295         cfgr &= ~(STM_RCC_CFGR_PPRE_MASK << STM_RCC_CFGR_PPRE);
296         cfgr |= (AO_RCC_CFGR_PPRE_DIV << STM_RCC_CFGR_PPRE);
297         stm_rcc.cfgr = cfgr;
298
299         /* Switch to the desired system clock */
300         ao_clock_normal_switch();
301
302         /* Clear reset flags */
303         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
304
305 #if DEBUG_THE_CLOCK
306         /* Output SYSCLK on PA8 for measurments */
307
308         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
309
310         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
311         stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
312         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
313
314         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
315         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
316 #endif
317 }
318
319 #if AO_POWER_MANAGEMENT
320 void
321 ao_clock_suspend(void)
322 {
323         ao_clock_hsi();
324 }
325
326 void
327 ao_clock_resume(void)
328 {
329         ao_clock_normal_start();
330         ao_clock_normal_switch();
331 }
332 #endif