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; version 2 of the License.
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.
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.
21 #include <ao_fake_flight.h>
29 volatile AO_TICK_TYPE ao_tick_count;
38 volatile __data uint8_t ao_data_interval = 1;
39 volatile __data uint8_t ao_data_count;
42 void stm_systick_isr(void)
44 if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
47 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
48 ao_task_check_alarm((uint16_t) ao_tick_count);
51 if (++ao_data_count == ao_data_interval) {
54 if (ao_fake_flight_active)
55 ao_fake_flight_poll();
59 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
60 ao_wakeup((void *) &ao_data_count);
72 ao_timer_set_adc_interval(uint8_t interval)
75 ao_data_interval = interval;
81 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
86 stm_systick.rvr = SYSTICK_RELOAD;
88 stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
89 (1 << STM_SYSTICK_CSR_TICKINT) |
90 (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
101 /* Switch to MSI while messing about */
102 stm_rcc.cr |= (1 << STM_RCC_CR_MSION);
103 while (!(stm_rcc.cr & (1 << STM_RCC_CR_MSIRDY)))
106 stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
107 (STM_RCC_CFGR_SW_MSI << STM_RCC_CFGR_SW);
109 /* wait for system to switch to MSI */
110 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
111 (STM_RCC_CFGR_SWS_MSI << STM_RCC_CFGR_SWS))
114 /* reset SW, HPRE, PPRE1, PPRE2, MCOSEL and MCOPRE */
115 stm_rcc.cfgr &= (uint32_t)0x88FFC00C;
117 /* reset HSION, HSEON, CSSON and PLLON bits */
118 stm_rcc.cr &= 0xeefefffe;
120 /* reset PLLSRC, PLLMUL and PLLDIV bits */
121 stm_rcc.cfgr &= 0xff02ffff;
123 /* Disable all interrupts */
128 stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
130 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
132 /* Enable HSE clock */
133 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
134 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
137 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
138 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSE)
139 #define STM_PLLSRC AO_HSE
140 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (1 << STM_RCC_CFGR_PLLSRC)
142 #define STM_HSI 16000000
143 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
144 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSI)
145 #define STM_PLLSRC STM_HSI
146 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (0 << STM_RCC_CFGR_PLLSRC)
149 #if !AO_HSE || HAS_ADC
150 /* Enable HSI RC clock 16MHz */
151 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
152 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
156 /* Set flash latency to tolerate 32MHz SYSCLK -> 1 wait state */
158 /* Enable 64-bit access and prefetch */
159 stm_flash.acr |= (1 << STM_FLASH_ACR_ACC64);
160 stm_flash.acr |= (1 << STM_FLASH_ACR_PRFEN);
162 /* Enable 1 wait state so the CPU can run at 32MHz */
163 stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
165 /* Enable power interface clock */
166 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
168 /* Set voltage range to 1.8V */
170 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
171 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
174 /* Configure voltage scaling range */
176 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
177 cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
180 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
181 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
184 /* HCLK to 16MHz -> AHB prescaler = /1 */
186 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
187 cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
189 while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
190 (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
193 /* APB1 Prescaler = AO_APB1_PRESCALER */
195 cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
196 cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
199 /* APB2 Prescaler = AO_APB2_PRESCALER */
201 cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
202 cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
205 /* Disable the PLL */
206 stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
207 while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
210 /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
212 cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
213 cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
215 cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
216 cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
219 cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
220 cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
224 /* Enable the PLL and wait for it */
225 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
226 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
229 /* Switch to the PLL for the system clock */
232 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
233 cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
236 uint32_t c, part, mask, val;
239 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
240 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
247 stm_rcc.apb2rstr = 0xffff;
248 stm_rcc.apb1rstr = 0xffff;
249 stm_rcc.ahbrstr = 0x3f;
250 stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
254 stm_rcc.apb1rstr = 0;
255 stm_rcc.apb2rstr = 0;
258 /* Clear reset flags */
259 stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
263 /* Output SYSCLK on PA8 for measurments */
265 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
267 stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
268 stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
269 stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
271 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
272 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);