altos: remove blank line in stmf0/ao_timer.c
[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; 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         if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
45                 ++ao_tick_count;
46 #if HAS_TASK_QUEUE
47                 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
48                         ao_task_check_alarm((uint16_t) ao_tick_count);
49 #endif
50 #if AO_DATA_ALL
51                 if (++ao_data_count == ao_data_interval) {
52                         ao_data_count = 0;
53 #if HAS_FAKE_FLIGHT
54                         if (ao_fake_flight_active)
55                                 ao_fake_flight_poll();
56                         else
57 #endif
58                                 ao_adc_poll();
59 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
60                         ao_wakeup((void *) &ao_data_count);
61 #endif
62                 }
63 #endif
64 #ifdef AO_TIMER_HOOK
65                 AO_TIMER_HOOK;
66 #endif
67         }
68 }
69
70 #if HAS_ADC
71 void
72 ao_timer_set_adc_interval(uint8_t interval)
73 {
74         ao_arch_critical(
75                 ao_data_interval = interval;
76                 ao_data_count = 0;
77                 );
78 }
79 #endif
80
81 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
82
83 void
84 ao_timer_init(void)
85 {
86         stm_systick.rvr = SYSTICK_RELOAD;
87         stm_systick.cvr = 0;
88         stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
89                            (1 << STM_SYSTICK_CSR_TICKINT) |
90                            (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
91 }
92
93 #endif
94
95 static void
96 ao_clock_enable_crs(void)
97 {
98         /* Enable crs interface clock */
99         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_CRSEN);
100
101         /* Disable error counter */
102         stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
103                       (32 << STM_CRS_CR_TRIM) |
104                       (0 << STM_CRS_CR_SWSYNC) |
105                       (0 << STM_CRS_CR_AUTOTRIMEN) |
106                       (0 << STM_CRS_CR_CEN) |
107                       (0 << STM_CRS_CR_ESYNCIE) |
108                       (0 << STM_CRS_CR_ERRIE) |
109                       (0 << STM_CRS_CR_SYNCWARNIE) |
110                       (0 << STM_CRS_CR_SYNCOKIE));
111
112         /* Configure for USB source */
113         stm_crs.cfgr = ((stm_crs.cfgr & ((1 << 30) | (1 << 27))) |
114                         (0 << STM_CRS_CFGR_SYNCPOL) |
115                         (STM_CRS_CFGR_SYNCSRC_USB << STM_CRS_CFGR_SYNCSRC) |
116                         (STM_CRS_CFGR_SYNCDIV_1 << STM_CRS_CFGR_SYNCDIV) |
117                         (0x22 << STM_CRS_CFGR_FELIM) |
118                         (((48000000 / 1000) - 1) << STM_CRS_CFGR_RELOAD));
119
120         /* Enable error counter, set auto trim */
121         stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
122                       (32 << STM_CRS_CR_TRIM) |
123                       (0 << STM_CRS_CR_SWSYNC) |
124                       (1 << STM_CRS_CR_AUTOTRIMEN) |
125                       (1 << STM_CRS_CR_CEN) |
126                       (0 << STM_CRS_CR_ESYNCIE) |
127                       (0 << STM_CRS_CR_ERRIE) |
128                       (0 << STM_CRS_CR_SYNCWARNIE) |
129                       (0 << STM_CRS_CR_SYNCOKIE));
130 }
131
132 void
133 ao_clock_init(void)
134 {
135         uint32_t        cfgr;
136
137         /* Switch to HSI while messing about */
138         stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
139         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
140                 ao_arch_nop();
141
142         stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
143                 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
144
145         /* wait for system to switch to HSI */
146         while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
147                (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
148                 ao_arch_nop();
149
150         /* reset the clock config, leaving us running on the HSI */
151         stm_rcc.cfgr &= (uint32_t)0x0000000f;
152
153         /* reset PLLON, CSSON, HSEBYP, HSEON */
154         stm_rcc.cr &= 0x0000ffff;
155
156         /* Disable all interrupts */
157         stm_rcc.cir = 0;
158
159 #if AO_HSE
160 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSE
161 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSE
162 #define STM_PLLSRC                              AO_HSE
163 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        1
164
165 #if AO_HSE_BYPASS
166         stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
167 #else
168         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
169 #endif
170         /* Enable HSE clock */
171         stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
172         while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
173                 asm("nop");
174 #endif
175
176
177 #if AO_HSI48
178 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSI48
179 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSI48
180
181         /* Turn HSI48 clock on */
182         stm_rcc.cr2 |= (1 << STM_RCC_CR2_HSI48ON);
183
184         /* Wait for clock to stabilize */
185         while ((stm_rcc.cr2 & (1 << STM_RCC_CR2_HSI48RDY)) == 0)
186                 ao_arch_nop();
187
188         ao_clock_enable_crs();
189 #endif
190
191 #ifndef STM_RCC_CFGR_SWS_TARGET_CLOCK
192 #define STM_HSI                                 16000000
193 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           STM_RCC_CFGR_SWS_HSI
194 #define STM_RCC_CFGR_SW_TARGET_CLOCK            STM_RCC_CFGR_SW_HSI
195 #define STM_PLLSRC                              STM_HSI
196 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        0
197 #endif
198
199 #ifdef STM_PLLSRC
200 #error No code for PLL initialization yet
201 #endif
202
203         /* Set flash latency to tolerate 48MHz SYSCLK  -> 1 wait state */
204
205         /* Enable prefetch */
206         stm_flash.acr |= (1 << STM_FLASH_ACR_PRFTBE);
207
208         /* Enable 1 wait state so the CPU can run at 48MHz */
209         stm_flash.acr |= (STM_FLASH_ACR_LATENCY_1 << STM_FLASH_ACR_LATENCY);
210
211         /* Enable power interface clock */
212         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
213
214         /* HCLK to 48MHz -> AHB prescaler = /1 */
215         cfgr = stm_rcc.cfgr;
216         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
217         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
218         stm_rcc.cfgr = cfgr;
219         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
220                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
221                 ao_arch_nop();
222
223         /* APB Prescaler = AO_APB_PRESCALER */
224         cfgr = stm_rcc.cfgr;
225         cfgr &= ~(STM_RCC_CFGR_PPRE_MASK << STM_RCC_CFGR_PPRE);
226         cfgr |= (AO_RCC_CFGR_PPRE_DIV << STM_RCC_CFGR_PPRE);
227         stm_rcc.cfgr = cfgr;
228
229         /* Switch to the desired system clock */
230
231         cfgr = stm_rcc.cfgr;
232         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
233         cfgr |= (STM_RCC_CFGR_SW_TARGET_CLOCK << STM_RCC_CFGR_SW);
234         stm_rcc.cfgr = cfgr;
235         for (;;) {
236                 uint32_t        c, part, mask, val;
237
238                 c = stm_rcc.cfgr;
239                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
240                 val = (STM_RCC_CFGR_SWS_TARGET_CLOCK << STM_RCC_CFGR_SWS);
241                 part = c & mask;
242                 if (part == val)
243                         break;
244         }
245
246         /* Clear reset flags */
247         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
248
249 #if !AO_HSI && !AO_NEED_HSI
250         /* Turn off the HSI clock */
251         stm_rcc.cr &= ~(1 << STM_RCC_CR_HSION);
252 #endif
253 #if DEBUG_THE_CLOCK
254         /* Output SYSCLK on PA8 for measurments */
255
256         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
257
258         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
259         stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
260         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
261
262         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
263         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
264 #endif
265 }