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