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