altos: Replace __critical usage with ao_arch_critical as needed
[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
20 volatile __data AO_TICK_TYPE ao_tick_count;
21
22 uint16_t ao_time(void)
23 {
24         uint16_t        v;
25         ao_arch_critical(
26                 v = ao_tick_count;
27                 );
28         return v;
29 }
30
31 #if AO_DATA_ALL
32 volatile __data uint8_t ao_data_interval = 1;
33 volatile __data uint8_t ao_data_count;
34 #endif
35
36 void
37 ao_debug_out(char c);
38
39
40 void stm_tim6_isr(void)
41 {
42         if (stm_tim6.sr & (1 << STM_TIM67_SR_UIF)) {
43                 stm_tim6.sr = 0;
44                 ++ao_tick_count;
45 #if AO_DATA_ALL
46                 if (++ao_data_count == ao_data_interval) {
47                         ao_data_count = 0;
48                         ao_adc_poll();
49 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
50                         ao_wakeup((void *) &ao_data_count);
51 #endif
52                 }
53 #endif
54         }
55 }
56
57 #if HAS_ADC
58 void
59 ao_timer_set_adc_interval(uint8_t interval)
60 {
61         ao_arch_critical(
62                 ao_data_interval = interval;
63                 ao_data_count = 0;
64                 );
65 }
66 #endif
67
68 /*
69  * According to the STM clock-configuration, timers run
70  * twice as fast as the APB1 clock *if* the APB1 prescaler
71  * is greater than 1.
72  */
73
74 #if AO_APB1_PRESCALER > 1
75 #define TIMER_23467_SCALER 2
76 #else
77 #define TIMER_23467_SCALER 1
78 #endif
79
80 #define TIMER_10kHz     ((AO_PCLK1 * TIMER_23467_SCALER) / 10000)
81
82 void
83 ao_timer_init(void)
84 {
85         stm_nvic_set_enable(STM_ISR_TIM6_POS);
86         stm_nvic_set_priority(STM_ISR_TIM6_POS, AO_STM_NVIC_CLOCK_PRIORITY);
87
88         /* Turn on timer 6 */
89         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);
90
91         stm_tim6.psc = TIMER_10kHz;
92         stm_tim6.arr = 99;
93         stm_tim6.cnt = 0;
94
95         /* Enable update interrupt */
96         stm_tim6.dier = (1 << STM_TIM67_DIER_UIE);
97
98         /* Poke timer to reload values */
99         stm_tim6.egr |= (1 << STM_TIM67_EGR_UG);
100
101         stm_tim6.cr2 = (STM_TIM67_CR2_MMS_RESET << STM_TIM67_CR2_MMS);
102
103         /* And turn it on */
104         stm_tim6.cr1 = ((0 << STM_TIM67_CR1_ARPE) |
105                         (0 << STM_TIM67_CR1_OPM) |
106                         (1 << STM_TIM67_CR1_URS) |
107                         (0 << STM_TIM67_CR1_UDIS) |
108                         (1 << STM_TIM67_CR1_CEN));
109 }
110
111 void
112 ao_clock_init(void)
113 {
114         uint32_t        cfgr;
115         uint32_t        cr;
116         
117         /* Switch to MSI while messing about */
118         stm_rcc.cr |= (1 << STM_RCC_CR_MSION);
119         while (!(stm_rcc.cr & (1 << STM_RCC_CR_MSIRDY)))
120                 asm("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
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         /* (haven't managed to run the CPU at 32MHz yet, it's at 16MHz) */
172         stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
173
174         /* Enable power interface clock */
175         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
176
177         /* Set voltage range to 1.8V */
178
179         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
180         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
181                 asm("nop");
182
183         /* Configure voltage scaling range */
184         cr = stm_pwr.cr;
185         cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
186         cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
187         stm_pwr.cr = cr;
188
189         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
190         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
191                 asm("nop");
192
193         /* HCLK to 16MHz -> AHB prescaler = /1 */
194         cfgr = stm_rcc.cfgr;
195         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
196         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
197         stm_rcc.cfgr = cfgr;
198         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
199                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
200                 asm ("nop");
201
202         /* APB1 Prescaler = AO_APB1_PRESCALER */
203         cfgr = stm_rcc.cfgr;
204         cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
205         cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
206         stm_rcc.cfgr = cfgr;
207
208         /* APB2 Prescaler = AO_APB2_PRESCALER */
209         cfgr = stm_rcc.cfgr;
210         cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
211         cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
212         stm_rcc.cfgr = cfgr;
213
214         /* Disable the PLL */
215         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
216         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
217                 asm("nop");
218         
219         /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
220         cfgr = stm_rcc.cfgr;
221         cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
222         cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
223
224         cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
225         cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
226
227         /* PLL source */
228         cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
229         cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
230
231         stm_rcc.cfgr = cfgr;
232
233         /* Enable the PLL and wait for it */
234         stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
235         while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
236                 asm("nop");
237
238         /* Switch to the PLL for the system clock */
239
240         cfgr = stm_rcc.cfgr;
241         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
242         cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
243         stm_rcc.cfgr = cfgr;
244         for (;;) {
245                 uint32_t        c, part, mask, val;
246
247                 c = stm_rcc.cfgr;
248                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
249                 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
250                 part = c & mask;
251                 if (part == val)
252                         break;
253         }
254
255 #if 0
256         stm_rcc.apb2rstr = 0xffff;
257         stm_rcc.apb1rstr = 0xffff;
258         stm_rcc.ahbrstr = 0x3f;
259         stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
260         stm_rcc.apb2enr = 0;
261         stm_rcc.apb1enr = 0;
262         stm_rcc.ahbrstr = 0;
263         stm_rcc.apb1rstr = 0;
264         stm_rcc.apb2rstr = 0;
265 #endif
266
267         /* Clear reset flags */
268         stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
269
270
271 #if DEBUG_THE_CLOCK
272         /* Output SYSCLK on PA8 for measurments */
273
274         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
275         
276         stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
277         stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
278         stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
279
280         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
281         stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
282 #endif
283 }