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