f86a51169293e78a4fb930f8e132f19557c378e7
[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
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         ao_validate_cur_stack();
46         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
47                 ++ao_tick_count;
48 #if HAS_TASK_QUEUE
49                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
50                         ao_task_check_alarm((uint16_t) ao_tick_count);
51 #endif
52 #if AO_DATA_ALL
53                 if (++ao_data_count == ao_data_interval) {
54                         ao_data_count = 0;
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 #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 void
98 ao_clock_init(void)
99 {
100         uint32_t        cfgr;
101         uint32_t        cr;
102         
103         /* Switch to MSI while messing about */
104         stm_rcc.cr |= (1 << STM_RCC_CR_MSION);
105         while (!(stm_rcc.cr & (1 << STM_RCC_CR_MSIRDY)))
106                 ao_arch_nop();
107
108         stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
109                 (STM_RCC_CFGR_SW_MSI << STM_RCC_CFGR_SW);
110
111         /* wait for system to switch to MSI */
112         while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
113                (STM_RCC_CFGR_SWS_MSI << STM_RCC_CFGR_SWS))
114                 ao_arch_nop();
115
116         /* reset SW, HPRE, PPRE1, PPRE2, MCOSEL and MCOPRE */
117         stm_rcc.cfgr &= (uint32_t)0x88FFC00C;
118
119         /* reset HSION, HSEON, CSSON and PLLON bits */
120         stm_rcc.cr &= 0xeefefffe;
121         
122         /* reset PLLSRC, PLLMUL and PLLDIV bits */
123         stm_rcc.cfgr &= 0xff02ffff;
124         
125         /* Disable all interrupts */
126         stm_rcc.cir = 0;
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 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
140 #define STM_RCC_CFGR_SW_TARGET_CLOCK            (STM_RCC_CFGR_SW_HSE)
141 #define STM_PLLSRC                              AO_HSE
142 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        (1 << STM_RCC_CFGR_PLLSRC)
143 #else
144 #define STM_HSI                                 16000000
145 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
146 #define STM_RCC_CFGR_SW_TARGET_CLOCK            (STM_RCC_CFGR_SW_HSI)
147 #define STM_PLLSRC                              STM_HSI
148 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        (0 << STM_RCC_CFGR_PLLSRC)
149 #endif
150
151 #if !AO_HSE || HAS_ADC
152         /* Enable HSI RC clock 16MHz */
153         stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
154         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
155                 asm("nop");
156 #endif
157
158         /* Set flash latency to tolerate 32MHz SYSCLK  -> 1 wait state */
159
160         /* Enable 64-bit access and prefetch */
161         stm_flash.acr |= (1 << STM_FLASH_ACR_ACC64);
162         stm_flash.acr |= (1 << STM_FLASH_ACR_PRFEN);
163
164         /* Enable 1 wait state so the CPU can run at 32MHz */
165         stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
166
167         /* Enable power interface clock */
168         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
169
170         /* Set voltage range to 1.8V */
171
172         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
173         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
174                 asm("nop");
175
176         /* Configure voltage scaling range */
177         cr = stm_pwr.cr;
178         cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
179         cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
180         stm_pwr.cr = cr;
181
182         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
183         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
184                 asm("nop");
185
186         /* HCLK to 16MHz -> AHB prescaler = /1 */
187         cfgr = stm_rcc.cfgr;
188         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
189         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
190         stm_rcc.cfgr = cfgr;
191         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
192                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
193                 asm ("nop");
194
195         /* APB1 Prescaler = AO_APB1_PRESCALER */
196         cfgr = stm_rcc.cfgr;
197         cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
198         cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
199         stm_rcc.cfgr = cfgr;
200
201         /* APB2 Prescaler = AO_APB2_PRESCALER */
202         cfgr = stm_rcc.cfgr;
203         cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
204         cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
205         stm_rcc.cfgr = cfgr;
206
207         /* Disable the PLL */
208         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
209         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
210                 asm("nop");
211         
212         /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
213         cfgr = stm_rcc.cfgr;
214         cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
215         cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
216
217         cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
218         cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
219
220         /* PLL source */
221         cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
222         cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
223
224         stm_rcc.cfgr = cfgr;
225
226         /* Enable the PLL and wait for it */
227         stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
228         while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
229                 asm("nop");
230
231         /* Switch to the PLL for the system clock */
232
233         cfgr = stm_rcc.cfgr;
234         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
235         cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
236         stm_rcc.cfgr = cfgr;
237         for (;;) {
238                 uint32_t        c, part, mask, val;
239
240                 c = stm_rcc.cfgr;
241                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
242                 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
243                 part = c & mask;
244                 if (part == val)
245                         break;
246         }
247
248 #if 0
249         stm_rcc.apb2rstr = 0xffff;
250         stm_rcc.apb1rstr = 0xffff;
251         stm_rcc.ahbrstr = 0x3f;
252         stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
253         stm_rcc.apb2enr = 0;
254         stm_rcc.apb1enr = 0;
255         stm_rcc.ahbrstr = 0;
256         stm_rcc.apb1rstr = 0;
257         stm_rcc.apb2rstr = 0;
258 #endif
259
260         /* Clear reset flags */
261         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
262
263
264 #if DEBUG_THE_CLOCK
265         /* Output SYSCLK on PA8 for measurments */
266
267         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
268         
269         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
270         stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
271         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
272
273         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
274         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
275 #endif
276 }