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