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