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>
29 #if HAS_TICK || defined(AO_TIMER_HOOK)
32 volatile AO_TICK_TYPE ao_tick_count;
42 volatile __data uint8_t ao_data_interval = 1;
43 volatile __data uint8_t ao_data_count;
46 void stm_systick_isr(void)
48 ao_validate_cur_stack();
49 if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
54 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
55 ao_task_check_alarm((uint16_t) ao_tick_count);
58 if (++ao_data_count == ao_data_interval) {
61 if (ao_fake_flight_active)
62 ao_fake_flight_poll();
66 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
67 ao_wakeup((void *) &ao_data_count);
79 ao_timer_set_adc_interval(uint8_t interval)
82 ao_data_interval = interval;
88 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
93 stm_systick.rvr = SYSTICK_RELOAD;
95 stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
96 (1 << STM_SYSTICK_CSR_TICKINT) |
97 (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
98 stm_nvic.shpr15_12 |= AO_STM_NVIC_CLOCK_PRIORITY << 24;
109 /* Switch to MSI while messing about */
110 stm_rcc.cr |= (1 << STM_RCC_CR_MSION);
111 while (!(stm_rcc.cr & (1 << STM_RCC_CR_MSIRDY)))
114 stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
115 (STM_RCC_CFGR_SW_MSI << STM_RCC_CFGR_SW);
117 /* wait for system to switch to MSI */
118 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
119 (STM_RCC_CFGR_SWS_MSI << STM_RCC_CFGR_SWS))
122 /* reset SW, HPRE, PPRE1, PPRE2, MCOSEL and MCOPRE */
123 stm_rcc.cfgr &= (uint32_t)0x88FFC00C;
125 /* reset HSION, HSEON, CSSON and PLLON bits */
126 stm_rcc.cr &= 0xeefefffe;
128 /* reset PLLSRC, PLLMUL and PLLDIV bits */
129 stm_rcc.cfgr &= 0xff02ffff;
131 /* Disable all interrupts */
136 stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
138 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
140 /* Enable HSE clock */
141 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
142 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
145 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
146 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSE)
147 #define STM_PLLSRC AO_HSE
148 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (1 << STM_RCC_CFGR_PLLSRC)
150 #define STM_HSI 16000000
151 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
152 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSI)
153 #define STM_PLLSRC STM_HSI
154 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (0 << STM_RCC_CFGR_PLLSRC)
157 #if !AO_HSE || HAS_ADC || HAS_ADC_SINGLE
158 /* Enable HSI RC clock 16MHz */
159 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
160 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
164 /* Set flash latency to tolerate 32MHz SYSCLK -> 1 wait state */
166 /* Enable 64-bit access and prefetch */
167 stm_flash.acr |= (1 << STM_FLASH_ACR_ACC64);
168 stm_flash.acr |= (1 << STM_FLASH_ACR_PRFEN);
170 /* Enable 1 wait state so the CPU can run at 32MHz */
171 stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
173 /* Enable power interface clock */
174 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
176 /* Set voltage range to 1.8V */
178 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
179 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
182 /* Configure voltage scaling range */
184 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
185 cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
188 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
189 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
192 /* HCLK to 16MHz -> AHB prescaler = /1 */
194 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
195 cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
197 while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
198 (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
201 /* APB1 Prescaler = AO_APB1_PRESCALER */
203 cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
204 cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
207 /* APB2 Prescaler = AO_APB2_PRESCALER */
209 cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
210 cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
213 /* Disable the PLL */
214 stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
215 while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
218 /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
220 cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
221 cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
223 cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
224 cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
227 cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
228 cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
232 /* Enable the PLL and wait for it */
233 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
234 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
237 /* Switch to the PLL for the system clock */
240 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
241 cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
244 uint32_t c, part, mask, val;
247 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
248 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
255 stm_rcc.apb2rstr = 0xffff;
256 stm_rcc.apb1rstr = 0xffff;
257 stm_rcc.ahbrstr = 0x3f;
258 stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
262 stm_rcc.apb1rstr = 0;
263 stm_rcc.apb2rstr = 0;
266 /* Clear reset flags */
267 stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
271 /* Output SYSCLK on PA8 for measurments */
273 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
275 stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
276 stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
277 stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
279 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
280 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);