altos: Add stack-guard code. Uses STM MPU to trap stack overflow.
[fw/altos] / src / stm-bringup / bringup.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <string.h>
19
20 #include <stdio.h>
21 #include "stm32l.h"
22
23 void delay(void);
24
25 static void
26 set_clock(void)
27 {
28         uint32_t        cfgr;
29         uint32_t        cr;
30         
31         /* Set flash latency to tolerate 32MHz SYSCLK  -> 1 wait state */
32         uint32_t        acr = stm_flash.acr;
33
34         /* Enable 64-bit access and prefetch */
35         acr |= (1 << STM_FLASH_ACR_ACC64) | (1 << STM_FLASH_ACR_PRFEN);
36         stm_flash.acr = acr;
37
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);
41         stm_flash.acr = acr;
42
43         /* HCLK to 16MHz -> AHB prescaler = /1 */
44         cfgr = stm_rcc.cfgr;
45         cfgr &= ~(STM_RCC_CFGR_HPRE_MASK << STM_RCC_CFGR_HPRE);
46         cfgr |= (STM_RCC_CFGR_HPRE_DIV_1 << STM_RCC_CFGR_HPRE);
47         stm_rcc.cfgr = cfgr;
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))
50                 asm ("nop");
51 #define STM_AHB_PRESCALER       1
52
53         /* PCLK1 to 16MHz -> APB1 Prescaler = 1 */
54         cfgr = stm_rcc.cfgr;
55         cfgr &= ~(STM_RCC_CFGR_PPRE1_MASK << STM_RCC_CFGR_PPRE1);
56         cfgr |= (STM_RCC_CFGR_PPRE1_DIV_1 << STM_RCC_CFGR_PPRE1);
57         stm_rcc.cfgr = cfgr;
58 #define STM_APB1_PRESCALER      1
59
60         /* PCLK2 to 16MHz -> APB2 Prescaler = 1 */
61         cfgr = stm_rcc.cfgr;
62         cfgr &= ~(STM_RCC_CFGR_PPRE2_MASK << STM_RCC_CFGR_PPRE2);
63         cfgr |= (STM_RCC_CFGR_PPRE2_DIV_1 << STM_RCC_CFGR_PPRE2);
64         stm_rcc.cfgr = cfgr;
65 #define STM_APB2_PRESCALER      1
66
67         /* Enable power interface clock */
68         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
69
70         /* Set voltage range to 1.8V */
71
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)
74                 asm("nop");
75
76         /* Configure voltage scaling range */
77         cr = stm_pwr.cr;
78         cr &= ~(STM_PWR_CR_VOS_MASK << STM_PWR_CR_VOS);
79         cr |= (STM_PWR_CR_VOS_1_8 << STM_PWR_CR_VOS);
80         stm_pwr.cr = cr;
81
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)
84                 asm("nop");
85
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)))
90                         asm("nop");
91         }
92 #define STM_HSI 16000000
93
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)) {
97                 cfgr = stm_rcc.cfgr;
98                 cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
99                 cfgr |= (STM_RCC_CFGR_SW_HSI << STM_RCC_CFGR_SW);
100                 stm_rcc.cfgr = cfgr;
101                 while ((stm_rcc.cfgr & (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS)) !=
102                        (STM_RCC_CFGR_SWS_HSI << STM_RCC_CFGR_SWS))
103                         asm("nop");
104         }
105
106         /* Disable the PLL */
107         stm_rcc.cr &= ~(1 << STM_RCC_CR_PLLON);
108         while (stm_rcc.cr & (1 << STM_RCC_CR_PLLRDY))
109                 asm("nop");
110         
111         /* PLLVCO to 96MHz (for USB) -> PLLMUL = 6, PLLDIV = 4 */
112         cfgr = stm_rcc.cfgr;
113         cfgr &= ~(STM_RCC_CFGR_PLLMUL_MASK << STM_RCC_CFGR_PLLMUL);
114         cfgr &= ~(STM_RCC_CFGR_PLLDIV_MASK << STM_RCC_CFGR_PLLDIV);
115
116 //      cfgr |= (STM_RCC_CFGR_PLLMUL_6 << STM_RCC_CFGR_PLLMUL);
117 //      cfgr |= (STM_RCC_CFGR_PLLDIV_3 << STM_RCC_CFGR_PLLDIV);
118
119         cfgr |= (STM_RCC_CFGR_PLLMUL_6 << STM_RCC_CFGR_PLLMUL);
120         cfgr |= (STM_RCC_CFGR_PLLDIV_4 << STM_RCC_CFGR_PLLDIV);
121
122 #define STM_PLLMUL      6
123 #define STM_PLLDIV      4
124
125         /* PLL source to HSI */
126         cfgr &= ~(1 << STM_RCC_CFGR_PLLSRC);
127
128 #define STM_PLLSRC      STM_HSI
129
130         stm_rcc.cfgr = cfgr;
131
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)))
135                 asm("nop");
136
137         /* Switch to the PLL for the system clock */
138
139         cfgr = stm_rcc.cfgr;
140         cfgr &= ~(STM_RCC_CFGR_SW_MASK << STM_RCC_CFGR_SW);
141         cfgr |= (STM_RCC_CFGR_SW_PLL << STM_RCC_CFGR_SW);
142         stm_rcc.cfgr = cfgr;
143         for (;;) {
144                 uint32_t        c, part, mask, val;
145
146                 c = stm_rcc.cfgr;
147                 mask = (STM_RCC_CFGR_SWS_MASK << STM_RCC_CFGR_SWS);
148                 val = (STM_RCC_CFGR_SWS_PLL << STM_RCC_CFGR_SWS);
149                 part = c & mask;
150                 if (part == val)
151                         break;
152         }
153 }
154
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)
160
161 #define BAUD_9600 (STM_APB2 / 9600)
162
163 void
164 set_serial()
165 {
166         uint32_t        moder, afr;
167
168         /* Enable GPIOA */
169         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOAEN);
170         
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);
176
177         /* Enable USART1 */
178         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_USART1EN);
179         
180         /* 9.6KBps. PCLK1 = 16MHz. OVER8 = 0 */
181
182         /* USARTDIV = PCLK1 / (16 * 9600) = 104.1{6}
183          * round to 104.1875 (1667 / 16)
184          *
185          * actual baud rate = 16e6 / (16 * 104.1875) = 9598Bps
186          */
187
188         stm_usart1.brr = BAUD_9600;
189
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));
205
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));
215
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));
228 }
229
230 void
231 outbyte(char c)
232 {
233         if (c == '\n')
234                 outbyte('\r');
235         while (!(stm_usart1.sr & (1 << STM_USART_SR_TXE)))
236                 ;
237         stm_usart1.dr = c;
238 }
239
240 int putc( int c, FILE * stream ) {
241         outbyte(c);
242 }
243
244 void
245 serial_string(char *string)
246 {
247         char    c;
248
249         while (c = *string++)
250                 outbyte(c);
251 }
252
253 volatile uint16_t       tick_count;
254
255 void
256 stm_tim6_isr(void)
257 {
258         if (stm_tim6.sr & (1 << STM_TIM67_SR_UIF)) {
259                 stm_tim6.sr = 0;
260                 ++tick_count;
261         }
262 }
263
264 #define TIMER_10kHz     (STM_APB1 / 10000)
265
266 void
267 set_timer6(void)
268 {
269         stm_nvic_set_enable(STM_ISR_TIM6_POS);
270         stm_nvic_set_priority(STM_ISR_TIM6_POS, 1);
271
272         /* Turn on timer 6 */
273         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);
274
275         stm_tim6.psc = TIMER_10kHz;
276         stm_tim6.arr = 100;
277         stm_tim6.cnt = 0;
278
279         /* Enable update interrupt */
280         stm_tim6.dier = (1 << STM_TIM67_DIER_UIE);
281
282         /* Poke timer to reload values */
283         stm_tim6.egr |= (1 << STM_TIM67_EGR_UG);
284
285         stm_tim6.cr2 = (STM_TIM67_CR2_MMS_RESET << STM_TIM67_CR2_MMS);
286
287         /* And turn it on */
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));
293 }
294
295 void
296 main (void)
297 {
298         set_clock();
299         set_serial();
300         set_timer6();
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);
304         for (;;) {
305                 stm_gpiob.odr = (1 << 7);
306                 printf ("hello, ");
307                 delay();
308                 stm_gpiob.odr = (1 << 6);
309                 printf ("world %d\n", tick_count);
310                 delay();
311         }
312 }
313
314 void
315 delay(void)
316 {
317         int i;
318         for (i = 0; i < 1000000; i++)
319                 __asm__ __volatile__ ("nop\n\t":::"memory");
320 }