2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #include <ao_fake_flight.h>
30 volatile AO_TICK_TYPE ao_tick_count;
41 AO_TICK_TYPE before, after;
45 before = ao_tick_count;
46 cvr = stm_systick.cvr;
47 after = ao_tick_count;
48 } while (before != after);
50 return (uint64_t) after * (1000000000ULL / AO_HERTZ) +
51 (uint64_t) cvr * (1000000000ULL / AO_SYSTICK);
55 volatile uint8_t ao_data_interval = 1;
56 volatile uint8_t ao_data_count;
59 void stm_systick_isr(void)
61 if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
64 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
65 ao_task_check_alarm((uint16_t) ao_tick_count);
68 if (++ao_data_count == ao_data_interval) {
72 if (ao_fake_flight_active)
73 ao_fake_flight_poll();
78 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
79 ao_wakeup((void *) &ao_data_count);
91 ao_timer_set_adc_interval(uint8_t interval)
94 ao_data_interval = interval;
100 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
106 stm_systick.rvr = SYSTICK_RELOAD;
108 stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
109 (1 << STM_SYSTICK_CSR_TICKINT) |
110 (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
117 ao_clock_enable_crs(void)
119 /* Enable crs interface clock */
120 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_CRSEN);
122 /* Disable error counter */
123 stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
124 (32 << STM_CRS_CR_TRIM) |
125 (0 << STM_CRS_CR_SWSYNC) |
126 (0 << STM_CRS_CR_AUTOTRIMEN) |
127 (0 << STM_CRS_CR_CEN) |
128 (0 << STM_CRS_CR_ESYNCIE) |
129 (0 << STM_CRS_CR_ERRIE) |
130 (0 << STM_CRS_CR_SYNCWARNIE) |
131 (0 << STM_CRS_CR_SYNCOKIE));
133 /* Configure for USB source */
134 stm_crs.cfgr = ((stm_crs.cfgr & ((1 << 30) | (1 << 27))) |
135 (0 << STM_CRS_CFGR_SYNCPOL) |
136 (STM_CRS_CFGR_SYNCSRC_USB << STM_CRS_CFGR_SYNCSRC) |
137 (STM_CRS_CFGR_SYNCDIV_1 << STM_CRS_CFGR_SYNCDIV) |
138 (0x22 << STM_CRS_CFGR_FELIM) |
139 (((48000000 / 1000) - 1) << STM_CRS_CFGR_RELOAD));
141 /* Enable error counter, set auto trim */
142 stm_crs.cr = ((stm_crs.cr & (1 << 4)) |
143 (32 << STM_CRS_CR_TRIM) |
144 (0 << STM_CRS_CR_SWSYNC) |
145 (1 << STM_CRS_CR_AUTOTRIMEN) |
146 (1 << STM_CRS_CR_CEN) |
147 (0 << STM_CRS_CR_ESYNCIE) |
148 (0 << STM_CRS_CR_ERRIE) |
149 (0 << STM_CRS_CR_SYNCWARNIE) |
150 (0 << STM_CRS_CR_SYNCOKIE));
157 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
158 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
161 stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
162 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
164 /* wait for system to switch to HSI */
165 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
166 (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
169 /* reset the clock config, leaving us running on the HSI */
170 stm_rcc.cfgr &= (uint32_t)0x0000000f;
172 /* reset PLLON, CSSON, HSEBYP, HSEON */
173 stm_rcc.cr &= 0x0000ffff;
177 ao_clock_normal_start(void)
181 #define STM_RCC_CFGR_SWS_TARGET_CLOCK STM_RCC_CFGR_SWS_PLL
182 #define STM_RCC_CFGR_SW_TARGET_CLOCK STM_RCC_CFGR_SW_PLL
183 #define STM_PLLSRC AO_HSE
184 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK STM_RCC_CFGR_PLLSRC_HSE
187 stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
189 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
191 /* Enable HSE clock */
192 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
193 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
196 /* Disable the PLL */
197 stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
198 while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
203 cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
204 cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
207 cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
208 cfgr |= (STM_RCC_CFGR_PLLSRC_TARGET_CLOCK << STM_RCC_CFGR_PLLSRC);
211 /* Set pre divider */
212 stm_rcc.cfgr2 = (AO_RCC_CFGR2_PLLDIV << STM_RCC_CFGR2_PREDIV);
214 /* Enable the PLL and wait for it */
215 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
216 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
222 #define STM_RCC_CFGR_SWS_TARGET_CLOCK STM_RCC_CFGR_SWS_HSI48
223 #define STM_RCC_CFGR_SW_TARGET_CLOCK STM_RCC_CFGR_SW_HSI48
225 /* Turn HSI48 clock on */
226 stm_rcc.cr2 |= (1 << STM_RCC_CR2_HSI48ON);
228 /* Wait for clock to stabilize */
229 while ((stm_rcc.cr2 & (1 << STM_RCC_CR2_HSI48RDY)) == 0)
232 ao_clock_enable_crs();
235 #ifndef STM_RCC_CFGR_SWS_TARGET_CLOCK
236 #define STM_HSI 16000000
237 #define STM_RCC_CFGR_SWS_TARGET_CLOCK STM_RCC_CFGR_SWS_HSI
238 #define STM_RCC_CFGR_SW_TARGET_CLOCK STM_RCC_CFGR_SW_HSI
239 #define STM_PLLSRC STM_HSI
240 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK 0
247 ao_clock_normal_switch(void)
252 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
253 cfgr |= (STM_RCC_CFGR_SW_TARGET_CLOCK << STM_RCC_CFGR_SW);
256 uint32_t c, part, mask, val;
259 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
260 val = (STM_RCC_CFGR_SWS_TARGET_CLOCK << STM_RCC_CFGR_SWS);
265 #if !AO_HSI && !AO_NEED_HSI
266 /* Turn off the HSI clock */
267 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSION);
271 stm_rcc.cfgr3 |= (1 << STM_RCC_CFGR3_USBSW);
280 /* Switch to HSI while messing about */
283 /* Disable all interrupts */
286 /* Start high speed clock */
287 ao_clock_normal_start();
289 /* Set flash latency to tolerate 48MHz SYSCLK -> 1 wait state */
291 /* Enable prefetch */
292 stm_flash.acr |= (1 << STM_FLASH_ACR_PRFTBE);
294 /* Enable 1 wait state so the CPU can run at 48MHz */
295 stm_flash.acr |= (STM_FLASH_ACR_LATENCY_1 << STM_FLASH_ACR_LATENCY);
297 /* HCLK to 48MHz -> AHB prescaler = /1 */
299 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
300 cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
302 while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
303 (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
306 /* APB Prescaler = AO_APB_PRESCALER */
308 cfgr &= ~(STM_RCC_CFGR_PPRE_MASK << STM_RCC_CFGR_PPRE);
309 cfgr |= (AO_RCC_CFGR_PPRE_DIV << STM_RCC_CFGR_PPRE);
312 /* Switch to the desired system clock */
313 ao_clock_normal_switch();
315 /* Clear reset flags */
316 stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
321 /* Send PLL clock to MCO */
322 cfgr &= ~(STM_RCC_CFGR_MCO_MASK << STM_RCC_CFGR_MCO);
323 cfgr |= (STM_RCC_CFGR_MCO_PLLCLK << STM_RCC_CFGR_MCO);
326 cfgr &= ~(STM_RCC_CFGR_MCOPRE_DIV_MASK << STM_RCC_CFGR_MCOPRE);
327 cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
329 /* Don't divide PLL */
330 cfgr |= (1 << STM_RCC_CFGR_PLL_NODIV);
334 ao_enable_port(AO_MCO_PORT);
335 stm_ospeedr_set(AO_MCO_PORT, AO_MCO_PIN, STM_OSPEEDR_HIGH);
336 stm_afr_set(AO_MCO_PORT, AO_MCO_PIN, AO_MCO_AF);
340 /* Output SYSCLK on PA8 for measurments */
342 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
344 stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
345 stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_HIGH);
347 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
348 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);
352 #if AO_POWER_MANAGEMENT
354 ao_clock_suspend(void)
360 ao_clock_resume(void)
362 ao_clock_normal_start();
363 ao_clock_normal_switch();