2 * Copyright © 2018 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.
26 #if HAS_TICK || defined(AO_TIMER_HOOK)
29 volatile AO_TICK_TYPE ao_tick_count;
39 volatile uint8_t ao_data_interval = 1;
40 volatile uint8_t ao_data_count;
43 void stm_systick_isr(void)
45 ao_validate_cur_stack();
46 if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
50 ao_task_check_alarm();
52 if (++ao_data_count == ao_data_interval && ao_data_interval) {
55 if (ao_fake_flight_active)
56 ao_fake_flight_poll();
60 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
61 ao_wakeup((void *) &ao_data_count);
73 ao_timer_set_adc_interval(uint8_t interval)
76 ao_data_interval = interval;
82 #define SYSTICK_RELOAD ((AO_SYSTICK / 8) / 100 - 1)
87 stm_systick.rvr = SYSTICK_RELOAD;
89 stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
90 (1 << STM_SYSTICK_CSR_TICKINT) |
91 (STM_SYSTICK_CSR_CLKSOURCE_AHB_8 << STM_SYSTICK_CSR_CLKSOURCE));
92 stm_scb.shpr3 |= AO_STM_NVIC_CLOCK_PRIORITY << 24;
103 /* Switch to HSI while messing about */
104 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
105 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
108 stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
109 (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
111 /* wait for system to switch to HSI */
112 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
113 (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
116 /* reset everything but the HSI selection and status */
117 stm_rcc.cfgr &= (uint32_t)0x0000000f;
119 /* reset everything but HSI */
120 stm_rcc.cr &= 0x0000ffff;
122 /* Disable and clear all interrupts */
123 stm_rcc.cir = 0xffff0000;
127 stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
129 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
131 /* Enable HSE clock */
132 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
133 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
138 /* Set flash latency to tolerate SYSCLK */
140 #define FLASH_LATENCY ((AO_SYSCLK - 1) / 25000000)
142 /* Enable icache, dcache and prefetch. Set latency */
143 stm_flash.acr = ((1 << STM_FLASH_ACR_DCEN) |
144 (1 << STM_FLASH_ACR_ICEN) |
145 (1 << STM_FLASH_ACR_PRFTEN) |
146 (FLASH_LATENCY << STM_FLASH_ACR_LATENCY));
148 /* Enable power interface clock */
149 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
151 #if AO_SYSCLK <= 64000000
152 #define VOS_SCALE_MODE STM_PWR_CR_VOS_SCALE_MODE_1
153 #elif AO_SYSCLK <= 84000000
154 #define VOS_SCALE_MODE STM_PWR_CR_VOS_SCALE_MODE_2
156 #define VOS_SCALE_MODE STM_PWR_CR_VOS_SCALE_MODE_1
159 /* Set voltage scale mode */
160 stm_pwr.cr = ((stm_pwr.cr & ~(STM_PWR_CR_VOS_SCALE_MODE_MASK)) |
161 (VOS_SCALE_MODE << STM_PWR_CR_VOS));
165 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
166 cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
169 /* APB1 Prescaler = AO_APB1_PRESCALER */
171 cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
172 cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
175 /* APB2 Prescaler = AO_APB2_PRESCALER */
177 cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
178 cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
181 /* Clock configuration register DCKCFGR2; mostly make sure USB
182 * gets clocked from PLL_Q
184 stm_rcc.dckcfgr2 = ((STM_RCC_DCKCFGR2_LPTIMER1SEL_APB << STM_RCC_DCKCFGR2_LPTIMER1SEL) |
185 (STM_RCC_DCKCFGR2_SDIOSEL_CK_48MHZ << STM_RCC_DCKCFGR2_SDIOSEL) |
186 (STM_RCC_DCKCFGR2_CK48MSEL_PLL_Q << STM_RCC_DCKCFGR2_CK48MSEL) |
187 (STM_RCC_DCKCFGR2_I2CFMP1SEL_APB << STM_RCC_DCKCFGR2_I2CFMP1SEL));
189 /* Disable the PLL */
190 stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
191 while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
195 pllcfgr = stm_rcc.pllcfgr;
196 pllcfgr &= ~(STM_RCC_PLLCFGR_PLLM_MASK << STM_RCC_PLLCFGR_PLLM);
197 pllcfgr &= ~(STM_RCC_PLLCFGR_PLLN_MASK << STM_RCC_PLLCFGR_PLLN);
198 pllcfgr &= ~(STM_RCC_PLLCFGR_PLLP_MASK << STM_RCC_PLLCFGR_PLLP);
199 pllcfgr &= ~(STM_RCC_PLLCFGR_PLLQ_MASK << STM_RCC_PLLCFGR_PLLQ);
200 pllcfgr &= ~(STM_RCC_PLLCFGR_PLLR_MASK << STM_RCC_PLLCFGR_PLLR);
202 pllcfgr |= (AO_PLL_M << STM_RCC_PLLCFGR_PLLM);
203 pllcfgr |= (AO_PLL1_N << STM_RCC_PLLCFGR_PLLN);
205 #define AO_RCC_PLLCFGR_PLLP STM_RCC_PLLCFGR_PLLP_DIV_2
208 #define AO_RCC_PLLCFGR_PLLP STM_RCC_PLLCFGR_PLLP_DIV_4
211 #define AO_RCC_PLLCFGR_PLLP STM_RCC_PLLCFGR_PLLP_DIV_6
214 #define AO_RCC_PLLCFGR_PLLP STM_RCC_PLLCFGR_PLLP_DIV_8
216 pllcfgr |= (AO_RCC_PLLCFGR_PLLP << STM_RCC_PLLCFGR_PLLP);
217 pllcfgr |= (AO_PLL1_Q << STM_RCC_PLLCFGR_PLLQ);
218 pllcfgr |= (AO_PLL1_R << STM_RCC_PLLCFGR_PLLR);
220 pllcfgr &= ~(1 << STM_RCC_PLLCFGR_PLLSRC);
222 pllcfgr |= (STM_RCC_PLLCFGR_PLLSRC_HSI << STM_RCC_PLLCFGR_PLLSRC);
225 pllcfgr |= (STM_RCC_PLLCFGR_PLLSRC_HSE << STM_RCC_PLLCFGR_PLLSRC);
227 stm_rcc.pllcfgr = pllcfgr;
229 /* Enable the PLL and wait for it */
230 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
231 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
234 /* Switch to the PLL for the system clock */
237 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
238 cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
241 uint32_t c, part, mask, val;
244 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
245 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
252 /* Disable HSI clock */
253 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSION);
256 /* Clear reset flags */
257 stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
260 /* Output PLL clock on PA8 and SYCLK on PC9 for measurments */
262 ao_enable_port(&stm_gpioa);
263 stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
264 stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
265 stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_HIGH);
267 ao_enable_port(&stm_gpioc);
268 stm_afr_set(&stm_gpioc, 9, STM_AFR_AF0);
269 stm_moder_set(&stm_gpioc, 9, STM_MODER_ALTERNATE);
270 stm_ospeedr_set(&stm_gpioc, 9, STM_OSPEEDR_HIGH);
274 cfgr |= ((0 << STM_RCC_CFGR_MCO2) |
275 (6 << STM_RCC_CFGR_MCO2PRE) |
276 (6 << STM_RCC_CFGR_MCO1PRE) |
277 (2 << STM_RCC_CFGR_MCO1));