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