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