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.
26 volatile AO_TICK_TYPE ao_tick_count;
35 volatile __data uint8_t ao_data_interval = 1;
36 volatile __data uint8_t ao_data_count;
39 void stm_systick_isr(void)
41 if (stm_systick.csr & (1 << STM_SYSTICK_CSR_COUNTFLAG)) {
44 if (ao_task_alarm_tick && (int16_t) (ao_tick_count - ao_task_alarm_tick) >= 0)
45 ao_task_check_alarm((uint16_t) ao_tick_count);
48 if (++ao_data_count == ao_data_interval) {
51 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
52 ao_wakeup((void *) &ao_data_count);
61 ao_timer_set_adc_interval(uint8_t interval)
64 ao_data_interval = interval;
70 #define SYSTICK_RELOAD (AO_SYSTICK / 100 - 1)
75 stm_systick.rvr = SYSTICK_RELOAD;
77 stm_systick.csr = ((1 << STM_SYSTICK_CSR_ENABLE) |
78 (1 << STM_SYSTICK_CSR_TICKINT) |
79 (STM_SYSTICK_CSR_CLKSOURCE_HCLK_8 << STM_SYSTICK_CSR_CLKSOURCE));
90 /* Switch to MSI while messing about */
91 stm_rcc.cr |= (1 << STM_RCC_CR_MSION);
92 while (!(stm_rcc.cr & (1 << STM_RCC_CR_MSIRDY)))
95 stm_rcc.cfgr = (stm_rcc.cfgr & ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW)) |
96 (STM_RCC_CFGR_SW_MSI << STM_RCC_CFGR_SW);
98 /* wait for system to switch to MSI */
99 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
100 (STM_RCC_CFGR_SWS_MSI << STM_RCC_CFGR_SWS))
103 /* reset SW, HPRE, PPRE1, PPRE2, MCOSEL and MCOPRE */
104 stm_rcc.cfgr &= (uint32_t)0x88FFC00C;
106 /* reset HSION, HSEON, CSSON and PLLON bits */
107 stm_rcc.cr &= 0xeefefffe;
109 /* reset PLLSRC, PLLMUL and PLLDIV bits */
110 stm_rcc.cfgr &= 0xff02ffff;
112 /* Disable all interrupts */
117 stm_rcc.cr |= (1 << STM_RCC_CR_HSEBYP);
119 stm_rcc.cr &= ~(1 << STM_RCC_CR_HSEBYP);
121 /* Enable HSE clock */
122 stm_rcc.cr |= (1 << STM_RCC_CR_HSEON);
123 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSERDY)))
126 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSE << STM_RCC_CFGR_SWS)
127 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSE)
128 #define STM_PLLSRC AO_HSE
129 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (1 << STM_RCC_CFGR_PLLSRC)
131 #define STM_HSI 16000000
132 #define STM_RCC_CFGR_SWS_TARGET_CLOCK (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)
133 #define STM_RCC_CFGR_SW_TARGET_CLOCK (STM_RCC_CFGR_SW_HSI)
134 #define STM_PLLSRC STM_HSI
135 #define STM_RCC_CFGR_PLLSRC_TARGET_CLOCK (0 << STM_RCC_CFGR_PLLSRC)
138 #if !AO_HSE || HAS_ADC
139 /* Enable HSI RC clock 16MHz */
140 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
141 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
145 /* Set flash latency to tolerate 32MHz SYSCLK -> 1 wait state */
147 /* Enable 64-bit access and prefetch */
148 stm_flash.acr |= (1 << STM_FLASH_ACR_ACC64);
149 stm_flash.acr |= (1 << STM_FLASH_ACR_PRFEN);
151 /* Enable 1 wait state so the CPU can run at 32MHz */
152 stm_flash.acr |= (1 << STM_FLASH_ACR_LATENCY);
154 /* Enable power interface clock */
155 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
157 /* Set voltage range to 1.8V */
159 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
160 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
163 /* Configure voltage scaling range */
165 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
166 cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
169 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
170 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
173 /* HCLK to 16MHz -> AHB prescaler = /1 */
175 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
176 cfgr |= (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE);
178 while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
179 (AO_RCC_CFGR_HPRE_DIV << STM_RCC_CFGR_HPRE))
182 /* APB1 Prescaler = AO_APB1_PRESCALER */
184 cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
185 cfgr |= (AO_RCC_CFGR_PPRE1_DIV << STM_RCC_CFGR_PPRE1);
188 /* APB2 Prescaler = AO_APB2_PRESCALER */
190 cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
191 cfgr |= (AO_RCC_CFGR_PPRE2_DIV << STM_RCC_CFGR_PPRE2);
194 /* Disable the PLL */
195 stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
196 while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
199 /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
201 cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
202 cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
204 cfgr |= (AO_RCC_CFGR_PLLMUL << STM_RCC_CFGR_PLLMUL);
205 cfgr |= (AO_RCC_CFGR_PLLDIV << STM_RCC_CFGR_PLLDIV);
208 cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
209 cfgr |= STM_RCC_CFGR_PLLSRC_TARGET_CLOCK;
213 /* Enable the PLL and wait for it */
214 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
215 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
218 /* Switch to the PLL for the system clock */
221 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
222 cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
225 uint32_t c, part, mask, val;
228 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
229 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
236 stm_rcc.apb2rstr = 0xffff;
237 stm_rcc.apb1rstr = 0xffff;
238 stm_rcc.ahbrstr = 0x3f;
239 stm_rcc.ahbenr = (1 << STM_RCC_AHBENR_FLITFEN);
243 stm_rcc.apb1rstr = 0;
244 stm_rcc.apb2rstr = 0;
247 /* Clear reset flags */
248 stm_rcc.csr |= (1 << STM_RCC_CSR_RMVF);
252 /* Output SYSCLK on PA8 for measurments */
254 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
256 stm_afr_set(&stm_gpioa, 8, STM_AFR_AF0);
257 stm_moder_set(&stm_gpioa, 8, STM_MODER_ALTERNATE);
258 stm_ospeedr_set(&stm_gpioa, 8, STM_OSPEEDR_40MHz);
260 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOPRE_DIV_1 << STM_RCC_CFGR_MCOPRE);
261 stm_rcc.cfgr |= (STM_RCC_CFGR_MCOSEL_HSE << STM_RCC_CFGR_MCOSEL);