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