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