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