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.
31 /* Set flash latency to tolerate 32MHz SYSCLK -> 1 wait state */
32 uint32_t acr = stm_flash.acr;
34 /* Enable 64-bit access and prefetch */
35 acr |= (1 << STM_FLASH_ACR_ACC64) | (1 << STM_FLASH_ACR_PRFEN);
38 /* Enable 1 wait state so the CPU can run at 32MHz */
39 /* (haven't managed to run the CPU at 32MHz yet, it's at 16MHz) */
40 acr |= (1 << STM_FLASH_ACR_LATENCY);
43 /* HCLK to 16MHz -> AHB prescaler = /1 */
45 cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
46 cfgr |= (STM_RCC_CFGR_HPRE_DIV_1 << STM_RCC_CFGR_HPRE);
48 while ((stm_rcc.cfgr & (STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE)) !=
49 (STM_RCC_CFGR_HPRE_DIV_1 << STM_RCC_CFGR_HPRE))
51 #define STM_AHB_PRESCALER 1
53 /* PCLK1 to 16MHz -> APB1 Prescaler = 1 */
55 cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
56 cfgr |= (STM_RCC_CFGR_PPRE1_DIV_1 << STM_RCC_CFGR_PPRE1);
58 #define STM_APB1_PRESCALER 1
60 /* PCLK2 to 16MHz -> APB2 Prescaler = 1 */
62 cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
63 cfgr |= (STM_RCC_CFGR_PPRE2_DIV_1 << STM_RCC_CFGR_PPRE2);
65 #define STM_APB2_PRESCALER 1
67 /* Enable power interface clock */
68 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
70 /* Set voltage range to 1.8V */
72 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
73 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
76 /* Configure voltage scaling range */
78 cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
79 cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
82 /* poll VOSF bit in PWR_CSR. Wait until it is reset to 0 */
83 while ((stm_pwr.csr & (1 << STM_PWR_CSR_VOSF)) != 0)
86 /* Enable HSI RC clock 16MHz */
87 if (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY))) {
88 stm_rcc.cr |= (1 << STM_RCC_CR_HSION);
89 while (!(stm_rcc.cr & (1 << STM_RCC_CR_HSIRDY)))
92 #define STM_HSI 16000000
94 /* Switch to direct HSI for SYSCLK */
95 if ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
96 (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS)) {
98 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
99 cfgr |= (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
101 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
102 (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
106 /* Disable the PLL */
107 stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
108 while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
111 /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
113 cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
114 cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
116 // cfgr |= (STM_RCC_CFGR_PLLMUL_6 << STM_RCC_CFGR_PLLMUL);
117 // cfgr |= (STM_RCC_CFGR_PLLDIV_3 << STM_RCC_CFGR_PLLDIV);
119 cfgr |= (STM_RCC_CFGR_PLLMUL_6 << STM_RCC_CFGR_PLLMUL);
120 cfgr |= (STM_RCC_CFGR_PLLDIV_4 << STM_RCC_CFGR_PLLDIV);
125 /* PLL source to HSI */
126 cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
128 #define STM_PLLSRC STM_HSI
132 /* Enable the PLL and wait for it */
133 stm_rcc.cr |= (1 << STM_RCC_CR_PLLON);
134 while (!(stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY)))
137 /* Switch to the PLL for the system clock */
140 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
141 cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
144 uint32_t c, part, mask, val;
147 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
148 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
155 #define STM_PLLVCO (STM_PLLSRC * STM_PLLMUL)
156 #define STM_SYSCLK (STM_PLLVCO / STM_PLLDIV)
157 #define STM_HCLK (STM_SYSCLK / STM_AHB_PRESCALER)
158 #define STM_APB1 (STM_HCLK / STM_APB1_PRESCALER)
159 #define STM_APB2 (STM_HCLK / STM_APB2_PRESCALER)
161 #define BAUD_9600 (STM_APB2 / 9600)
169 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
171 /* Hook PA9, PA10 to USART1 (AFIO7) */
172 stm_moder_set(&stm_gpioa, 9, STM_MODER_ALTERNATE);
173 stm_moder_set(&stm_gpioa, 10, STM_MODER_ALTERNATE);
174 stm_afr_set(&stm_gpioa, 9, STM_AFR_AF7);
175 stm_afr_set(&stm_gpioa, 10, STM_AFR_AF7);
178 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_USART1EN);
180 /* 9.6KBps. PCLK1 = 16MHz. OVER8 = 0 */
182 /* USARTDIV = PCLK1 / (16 * 9600) = 104.1{6}
183 * round to 104.1875 (1667 / 16)
185 * actual baud rate = 16e6 / (16 * 104.1875) = 9598Bps
188 stm_usart1.brr = BAUD_9600;
190 stm_usart1.cr1 = ((0 << STM_USART_CR1_OVER8) |
191 (1 << STM_USART_CR1_UE) |
192 (0 << STM_USART_CR1_M) |
193 (0 << STM_USART_CR1_WAKE) |
194 (0 << STM_USART_CR1_PCE) |
195 (0 << STM_USART_CR1_PS) |
196 (0 << STM_USART_CR1_PEIE) |
197 (0 << STM_USART_CR1_TXEIE) |
198 (0 << STM_USART_CR1_TCIE) |
199 (0 << STM_USART_CR1_RXNEIE) |
200 (0 << STM_USART_CR1_IDLEIE) |
201 (1 << STM_USART_CR1_TE) |
202 (1 << STM_USART_CR1_RE) |
203 (0 << STM_USART_CR1_RWU) |
204 (0 << STM_USART_CR1_SBK));
206 stm_usart1.cr2 = ((0 << STM_USART_CR2_LINEN) |
207 (STM_USART_CR2_STOP_1 << STM_USART_CR2_STOP) |
208 (0 << STM_USART_CR2_CLKEN) |
209 (0 << STM_USART_CR2_CPOL) |
210 (0 << STM_USART_CR2_CPHA) |
211 (0 << STM_USART_CR2_LBCL) |
212 (0 << STM_USART_CR2_LBDIE) |
213 (0 << STM_USART_CR2_LBDL) |
214 (0 << STM_USART_CR2_ADD));
216 stm_usart1.cr3 = ((0 << STM_USART_CR3_ONEBITE) |
217 (0 << STM_USART_CR3_CTSIE) |
218 (0 << STM_USART_CR3_CTSE) |
219 (0 << STM_USART_CR3_RTSE) |
220 (0 << STM_USART_CR3_DMAT) |
221 (0 << STM_USART_CR3_DMAR) |
222 (0 << STM_USART_CR3_SCEN) |
223 (0 << STM_USART_CR3_NACK) |
224 (0 << STM_USART_CR3_HDSEL) |
225 (0 << STM_USART_CR3_IRLP) |
226 (0 << STM_USART_CR3_IREN) |
227 (0 << STM_USART_CR3_EIE));
235 while (!(stm_usart1.sr & (1 << STM_USART_SR_TXE)))
240 int putc( int c, FILE * stream ) {
245 serial_string(char *string)
249 while (c = *string++)
253 volatile uint16_t tick_count;
258 if (stm_tim6.sr & (1 << STM_TIM67_SR_UIF)) {
264 #define TIMER_10kHz (STM_APB1 / 10000)
269 stm_nvic_set_enable(STM_ISR_TIM6_POS);
270 stm_nvic_set_priority(STM_ISR_TIM6_POS, 1);
272 /* Turn on timer 6 */
273 stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);
275 stm_tim6.psc = TIMER_10kHz;
279 /* Enable update interrupt */
280 stm_tim6.dier = (1 << STM_TIM67_DIER_UIE);
282 /* Poke timer to reload values */
283 stm_tim6.egr |= (1 << STM_TIM67_EGR_UG);
285 stm_tim6.cr2 = (STM_TIM67_CR2_MMS_RESET << STM_TIM67_CR2_MMS);
288 stm_tim6.cr1 = ((0 << STM_TIM67_CR1_ARPE) |
289 (0 << STM_TIM67_CR1_OPM) |
290 (1 << STM_TIM67_CR1_URS) |
291 (0 << STM_TIM67_CR1_UDIS) |
292 (1 << STM_TIM67_CR1_CEN));
301 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOBEN);
302 stm_moder_set(&stm_gpiob, 7, STM_MODER_OUTPUT);
303 stm_moder_set(&stm_gpiob, 6, STM_MODER_OUTPUT);
305 stm_gpiob.odr = (1 << 7);
308 stm_gpiob.odr = (1 << 6);
309 printf ("world %d\n", tick_count);
318 for (i = 0; i < 1000000; i++)
319 __asm__ __volatile__ ("nop\n\t":::"memory");