altos: add ao_time_ns API
[fw/altos] / src / stm / 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 || defined(AO_TIMER_HOOK)
30
31 #if HAS_TICK
32 volatile AO_TICK_TYPE ao_tick_count;
33
34 AO_TICK_TYPE
35 ao_time(void)
36 {
37         return ao_tick_count;
38 }
39
40 uint64_t
41 ao_time_ns(void)
42 {
43         AO_TICK_TYPE    before, after;
44         uint32_t        cvr;
45
46         do {
47                 before = ao_tick_count;
48                 cvr = stm_systick.cvr;
49                 after = ao_tick_count;
50         } while (before != after);
51
52         return (uint64_t) after * (1000000000ULL / AO_HERTZ) +
53                 (uint64_t) cvr * (1000000000ULL / AO_SYSTICK);
54 }
55
56 #endif
57
58 #if AO_DATA_ALL
59 volatile uint8_t        ao_data_interval = 1;
60 volatile uint8_t        ao_data_count;
61 #endif
62
63 void stm_systick_isr(void)
64 {
65         ao_validate_cur_stack();
66         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
67 #if HAS_TICK
68                 ++ao_tick_count;
69 #endif
70 #if HAS_TASK_QUEUE
71                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
72                         ao_task_check_alarm((uint16_t) ao_tick_count);
73 #endif
74 #if AO_DATA_ALL
75                 if (++ao_data_count == ao_data_interval) {
76                         ao_data_count = 0;
77 #if HAS_FAKE_FLIGHT
78                         if (ao_fake_flight_active)
79                                 ao_fake_flight_poll();
80                         else
81 #endif
82                                 ao_adc_poll();
83 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
84                         ao_wakeup((void *) &ao_data_count);
85 #endif
86                 }
87 #endif
88 #ifdef AO_TIMER_HOOK
89                 AO_TIMER_HOOK;
90 #endif
91         }
92 }
93
94 #if HAS_ADC
95 void
96 ao_timer_set_adc_interval(uint8_t interval)
97 {
98         ao_arch_critical(
99                 ao_data_interval = interval;
100                 ao_data_count = 0;
101                 );
102 }
103 #endif
104
105 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
106
107 void
108 ao_timer_init(void)
109 {
110         stm_systick.rvr = SYSTICK_RELOAD;
111         stm_systick.cvr = 0;
112         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
113                            (1 << STM_SYSTICK_CSR_TICKINT) |
114                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
115         stm_nvic.shpr15_12 |= AO_STM_NVIC_CLOCK_PRIORITY << 24;
116 }
117
118 #endif
119
120 void
121 ao_clock_init(void)
122 {
123         uint32_t        cfgr;
124         uint32_t        cr;
125         
126         /* Switch to MSI while messing about */
127         stm_rcc.cr |= (1 << STM_RCC_CR_MSION);
128         while (!(stm_rcc.cr & (1 << STM_RCC_CR_MSIRDY)))
129                 ao_arch_nop();
130
131         stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
132                 (STM_RCC_CFGR_SW_MSI << STM_RCC_CFGR_SW);
133
134         /* wait for system to switch to MSI */
135         while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
136                (STM_RCC_CFGR_SWS_MSI << STM_RCC_CFGR_SWS))
137                 ao_arch_nop();
138
139         /* reset SW, HPRE, PPRE1, PPRE2, MCOSEL and MCOPRE */
140         stm_rcc.cfgr &= (uint32_t)0x88FFC00C;
141
142         /* reset HSION, HSEON, CSSON and PLLON bits */
143         stm_rcc.cr &= 0xeefefffe;
144         
145         /* reset PLLSRC, PLLMUL and PLLDIV bits */
146         stm_rcc.cfgr &= 0xff02ffff;
147         
148         /* Disable all interrupts */
149         stm_rcc.cir = 0;
150
151 #if AO_HSE
152 #if AO_HSE_BYPASS
153         stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
154 #else
155         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
156 #endif
157         /* Enable HSE clock */
158         stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
159         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
160                 asm("nop");
161
162 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
163 #define STM_RCC_CFGR_SW_TARGET_CLOCK            (STM_RCC_CFGR_SW_HSE)
164 #define STM_PLLSRC                              AO_HSE
165 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        (1 << STM_RCC_CFGR_PLLSRC)
166 #else
167 #define STM_HSI                                 16000000
168 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
169 #define STM_RCC_CFGR_SW_TARGET_CLOCK            (STM_RCC_CFGR_SW_HSI)
170 #define STM_PLLSRC                              STM_HSI
171 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        (0 << STM_RCC_CFGR_PLLSRC)
172 #endif
173
174 #if !AO_HSE || HAS_ADC || HAS_ADC_SINGLE
175         /* Enable HSI RC clock 16MHz */
176         stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
177         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
178                 asm("nop");
179 #endif
180
181         /* Set flash latency to tolerate 32MHz SYSCLK  -> 1 wait state */
182
183         /* Enable 64-bit access and prefetch */
184         stm_flash.acr |= (1 << STM_FLASH_ACR_ACC64);
185         stm_flash.acr |= (1 << STM_FLASH_ACR_PRFEN);
186
187         /* Enable 1 wait state so the CPU can run at 32MHz */
188         stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
189
190         /* Enable power interface clock */
191         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
192
193         /* Set voltage range to 1.8V */
194
195         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
196         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
197                 asm("nop");
198
199         /* Configure voltage scaling range */
200         cr = stm_pwr.cr;
201         cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
202         cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
203         stm_pwr.cr = cr;
204
205         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
206         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
207                 asm("nop");
208
209         /* HCLK to 16MHz -> AHB prescaler = /1 */
210         cfgr = stm_rcc.cfgr;
211         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
212         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
213         stm_rcc.cfgr = cfgr;
214         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
215                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
216                 asm ("nop");
217
218         /* APB1 Prescaler = AO_APB1_PRESCALER */
219         cfgr = stm_rcc.cfgr;
220         cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
221         cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
222         stm_rcc.cfgr = cfgr;
223
224         /* APB2 Prescaler = AO_APB2_PRESCALER */
225         cfgr = stm_rcc.cfgr;
226         cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
227         cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
228         stm_rcc.cfgr = cfgr;
229
230         /* Disable the PLL */
231         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
232         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
233                 asm("nop");
234         
235         /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
236         cfgr = stm_rcc.cfgr;
237         cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
238         cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
239
240         cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
241         cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
242
243         /* PLL source */
244         cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
245         cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
246
247         stm_rcc.cfgr = cfgr;
248
249         /* Enable the PLL and wait for it */
250         stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
251         while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
252                 asm("nop");
253
254         /* Switch to the PLL for the system clock */
255
256         cfgr = stm_rcc.cfgr;
257         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
258         cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
259         stm_rcc.cfgr = cfgr;
260         for (;;) {
261                 uint32_t        c, part, mask, val;
262
263                 c = stm_rcc.cfgr;
264                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
265                 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
266                 part = c & mask;
267                 if (part == val)
268                         break;
269         }
270
271 #if 0
272         stm_rcc.apb2rstr = 0xffff;
273         stm_rcc.apb1rstr = 0xffff;
274         stm_rcc.ahbrstr = 0x3f;
275         stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
276         stm_rcc.apb2enr = 0;
277         stm_rcc.apb1enr = 0;
278         stm_rcc.ahbrstr = 0;
279         stm_rcc.apb1rstr = 0;
280         stm_rcc.apb2rstr = 0;
281 #endif
282
283         /* Clear reset flags */
284         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
285
286
287 #if DEBUG_THE_CLOCK
288         /* Output SYSCLK on PA8 for measurments */
289
290         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
291         
292         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
293         stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
294         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
295
296         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
297         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
298 #endif
299 }