altos: Make STM clock configuration per-product. Fix 32MHz CPU speed
[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 static volatile __data uint16_t 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 static __xdata uint8_t ao_forever;
32
33 void
34 ao_delay(uint16_t ticks)
35 {
36         ao_alarm(ticks);
37         ao_sleep(&ao_forever);
38 }
39
40 #if HAS_ADC
41 volatile __data uint8_t ao_adc_interval = 1;
42 volatile __data uint8_t ao_adc_count;
43 #endif
44
45 void
46 ao_debug_out(char c);
47
48
49 void stm_tim6_isr(void)
50 {
51         if (stm_tim6.sr & (1 << STM_TIM67_SR_UIF)) {
52                 stm_tim6.sr = 0;
53                 ++ao_tick_count;
54 #if HAS_ADC
55                 if (++ao_adc_count == ao_adc_interval) {
56                         ao_adc_count = 0;
57                         ao_adc_poll();
58                 }
59 #endif
60         }
61 }
62
63 #if HAS_ADC
64 void
65 ao_timer_set_adc_interval(uint8_t interval) __critical
66 {
67         ao_adc_interval = interval;
68         ao_adc_count = 0;
69 }
70 #endif
71
72 #define TIMER_10kHz     (AO_PCLK1 / 10000)
73
74 void
75 ao_timer_init(void)
76 {
77         stm_nvic_set_enable(STM_ISR_TIM6_POS);
78         stm_nvic_set_priority(STM_ISR_TIM6_POS, 1);
79
80         /* Turn on timer 6 */
81         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);
82
83         stm_tim6.psc = TIMER_10kHz;
84         stm_tim6.arr = 100;
85         stm_tim6.cnt = 0;
86
87         /* Enable update interrupt */
88         stm_tim6.dier = (1 << STM_TIM67_DIER_UIE);
89
90         /* Poke timer to reload values */
91         stm_tim6.egr |= (1 << STM_TIM67_EGR_UG);
92
93         stm_tim6.cr2 = (STM_TIM67_CR2_MMS_RESET << STM_TIM67_CR2_MMS);
94
95         /* And turn it on */
96         stm_tim6.cr1 = ((0 << STM_TIM67_CR1_ARPE) |
97                         (0 << STM_TIM67_CR1_OPM) |
98                         (1 << STM_TIM67_CR1_URS) |
99                         (0 << STM_TIM67_CR1_UDIS) |
100                         (1 << STM_TIM67_CR1_CEN));
101 }
102
103 void
104 ao_clock_init(void)
105 {
106         uint32_t        cfgr;
107         uint32_t        cr;
108         
109         /* Set flash latency to tolerate 32MHz SYSCLK  -> 1 wait state */
110
111         /* Enable 64-bit access and prefetch */
112         stm_flash.acr |= (1 << STM_FLASH_ACR_ACC64);
113         stm_flash.acr |= (1 << STM_FLASH_ACR_PRFEN);
114
115         /* Enable 1 wait state so the CPU can run at 32MHz */
116         /* (haven't managed to run the CPU at 32MHz yet, it's at 16MHz) */
117         stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
118
119
120         /* HCLK to 16MHz -> AHB prescaler = /1 */
121         cfgr = stm_rcc.cfgr;
122         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
123         cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
124         stm_rcc.cfgr = cfgr;
125         while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
126                (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
127                 asm ("nop");
128
129         /* APB1 Prescaler = AO_APB1_PRESCALER */
130         cfgr = stm_rcc.cfgr;
131         cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
132         cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
133         stm_rcc.cfgr = cfgr;
134
135         /* APB2 Prescaler = AO_APB2_PRESCALER */
136         cfgr = stm_rcc.cfgr;
137         cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
138         cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
139         stm_rcc.cfgr = cfgr;
140
141         /* Enable power interface clock */
142         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
143
144
145         /* Set voltage range to 1.8V */
146
147         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
148         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
149                 asm("nop");
150
151         /* Configure voltage scaling range */
152         cr = stm_pwr.cr;
153         cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
154         cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
155         stm_pwr.cr = cr;
156
157         /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
158         while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
159                 asm("nop");
160
161 #if AO_HSE
162         /* Enable HSE clock */
163         if (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY))) {
164                 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
165                 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
166                         asm("nop");
167         }
168 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
169 #define STM_RCC_CFGR_SW_TARGET_CLOCK            (STM_RCC_CFGR_SW_HSE)
170 #define STM_PLLSRC                              AO_HSE
171 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        (1 << STM_RCC_CFGR_PLLSRC)
172 #else
173 #define STM_HSI                                 16000000
174 #define STM_RCC_CFGR_SWS_TARGET_CLOCK           (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
175 #define STM_RCC_CFGR_SW_TARGET_CLOCK            (STM_RCC_CFGR_SW_HSI)
176 #define STM_PLLSRC                              STM_HSI
177 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK        (0 << STM_RCC_CFGR_PLLSRC)
178 #endif
179
180 #if !AO_HSE || HAS_ADC
181         /* Enable HSI RC clock 16MHz */
182         if (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY))) {
183                 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
184                 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
185                         asm("nop");
186         }
187 #endif
188         /* Switch to direct high speed clock for SYSCLK */
189         if ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
190             STM_RCC_CFGR_SWS_TARGET_CLOCK) {
191                 cfgr = stm_rcc.cfgr;
192                 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
193                 cfgr |= STM_RCC_CFGR_SW_TARGET_CLOCK;
194                 stm_rcc.cfgr = cfgr;
195                 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
196                        STM_RCC_CFGR_SWS_TARGET_CLOCK);
197                         asm("nop");
198         }
199
200         /* Disable the PLL */
201         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
202         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
203                 asm("nop");
204         
205         /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
206         cfgr = stm_rcc.cfgr;
207         cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
208         cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
209
210         cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
211         cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
212
213         /* PLL source */
214         cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
215         cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
216
217         stm_rcc.cfgr = cfgr;
218
219         /* Enable the PLL and wait for it */
220         stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
221         while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
222                 asm("nop");
223
224         /* Switch to the PLL for the system clock */
225
226         cfgr = stm_rcc.cfgr;
227         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
228         cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
229         stm_rcc.cfgr = cfgr;
230         for (;;) {
231                 uint32_t        c, part, mask, val;
232
233                 c = stm_rcc.cfgr;
234                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
235                 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
236                 part = c & mask;
237                 if (part == val)
238                         break;
239         }
240 }