altos/stm32f1: More stm32f103 work
[fw/altos] / src / stm32f1 / stm32f1.h
1 /*
2  * Copyright © 2023 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, either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #ifndef _STM32F1_H_
20 #define _STM32F1_H_
21
22 #include <stdint.h>
23
24 typedef volatile uint32_t       vuint32_t;
25 typedef volatile uint16_t       vuint16_t;
26 typedef volatile void *         vvoid_t;
27
28 struct stm_rcc {
29         vuint32_t       cr;
30         vuint32_t       cfgr;
31         vuint32_t       cir;
32         vuint32_t       apb2rstr;
33
34         vuint32_t       apb1rstr;
35         vuint32_t       ahbenr;
36         vuint32_t       apb2enr;
37         vuint32_t       apb1enr;
38
39         vuint32_t       bdcr;
40         vuint32_t       csr;
41         vuint32_t       ahbstr;
42         vuint32_t       cfgr2;
43 };
44
45 extern struct stm_rcc stm_rcc;
46
47 //#define stm_rcc    (*((struct stm_rcc *) 0x40021000))
48
49 #define STM_RCC_CR_RTCPRE       (29)
50 #define  STM_RCC_CR_RTCPRE_HSE_DIV_2    0
51 #define  STM_RCC_CR_RTCPRE_HSE_DIV_4    1
52 #define  STM_RCC_CR_RTCPRE_HSE_DIV_8    2
53 #define  STM_RCC_CR_RTCPRE_HSE_DIV_16   3
54 #define  STM_RCC_CR_RTCPRE_HSE_MASK     3UL
55
56 #define STM_RCC_CR_PLL3RDY      (29)
57 #define STM_RCC_CR_PLL3ON       (28)
58 #define STM_RCC_CR_PLL2RDY      (27)
59 #define STM_RCC_CR_PLL2ON       (26)
60 #define STM_RCC_CR_PLLRDY       (25)
61 #define STM_RCC_CR_PLLON        (24)
62 #define STM_RCC_CR_CSSON        (19)
63 #define STM_RCC_CR_HSEBYP       (18)
64 #define STM_RCC_CR_HSERDY       (17)
65 #define STM_RCC_CR_HSEON        (16)
66 #define STM_RCC_CR_HSICAL       (8)
67 #define STM_RCC_CR_HSITRIM      (3)
68 #define STM_RCC_CR_HSIRDY       (1)
69 #define STM_RCC_CR_HSION        (0)
70
71 #define STM_RCC_CFGR_MCOPRE     (28)
72 #define  STM_RCC_CFGR_MCOPRE_DIV_1      0
73 #define  STM_RCC_CFGR_MCOPRE_DIV_2      1
74 #define  STM_RCC_CFGR_MCOPRE_DIV_4      2
75 #define  STM_RCC_CFGR_MCOPRE_DIV_8      3
76 #define  STM_RCC_CFGR_MCOPRE_DIV_16     4
77 #define  STM_RCC_CFGR_MCOPRE_MASK       7UL
78
79 #define STM_RCC_CFGR_MCO        (24)
80 #define  STM_RCC_CFGR_MCO_DISABLE       0
81 #define  STM_RCC_CFGR_MCO_SYSCLK        4
82 #define  STM_RCC_CFGR_MCO_HSI   5
83 #define  STM_RCC_CFGR_MCO_HSE   6
84 #define  STM_RCC_CFGR_MCO_PLL_2 7
85 #define  STM_RCC_CFGR_MCO_MASK  7UL
86
87 #define STM_RCC_CFGR_USBPRE     (22)
88 #define  STM_RCC_CFGR_USBPRE_1_5        0
89 #define  STM_RCC_CFGR_USBPRE_1          1
90
91 #define STM_RCC_CFGR_PLLMUL     (18)
92 #define  STM_RCC_CFGR_PLLMUL_2          0
93 #define  STM_RCC_CFGR_PLLMUL_3          1
94 #define  STM_RCC_CFGR_PLLMUL_4          2
95 #define  STM_RCC_CFGR_PLLMUL_5          3
96 #define  STM_RCC_CFGR_PLLMUL_6          4
97 #define  STM_RCC_CFGR_PLLMUL_7          5
98 #define  STM_RCC_CFGR_PLLMUL_8          6
99 #define  STM_RCC_CFGR_PLLMUL_9          7
100 #define  STM_RCC_CFGR_PLLMUL_10         8
101 #define  STM_RCC_CFGR_PLLMUL_11         9
102 #define  STM_RCC_CFGR_PLLMUL_12         10
103 #define  STM_RCC_CFGR_PLLMUL_13         11
104 #define  STM_RCC_CFGR_PLLMUL_14         12
105 #define  STM_RCC_CFGR_PLLMUL_15         13
106 #define  STM_RCC_CFGR_PLLMUL_16         14
107 #define  STM_RCC_CFGR_PLLMUL_MASK       0xfUL
108
109 #define STM_RCC_CFGR_PLLXTPRE   (17)
110 #define  STM_RCC_CFGR_PLLXTPRE_1        0
111 #define  STM_RCC_CFGR_PLLXTPRE_2        1
112
113 #define STM_RCC_CFGR_PLLSRC     (16)
114 #define  STM_RCC_CFGR_PLLSRC_HSI_2      0
115 #define  STM_RCC_CFGR_PLLSRC_HSE        1
116
117 #define STM_RCC_CFGR_ADCPRE     (14)
118 #define  STM_RCC_CFGR_ADCPRE_2          0
119 #define  STM_RCC_CFGR_ADCPRE_4          1
120 #define  STM_RCC_CFGR_ADCPRE_6          2
121 #define  STM_RCC_CFGR_ADCPRE_8          3
122
123 #define STM_RCC_CFGR_PPRE2      (11)
124 #define  STM_RCC_CFGR_PPRE2_DIV_1       0
125 #define  STM_RCC_CFGR_PPRE2_DIV_2       4
126 #define  STM_RCC_CFGR_PPRE2_DIV_4       5
127 #define  STM_RCC_CFGR_PPRE2_DIV_8       6
128 #define  STM_RCC_CFGR_PPRE2_DIV_16      7
129 #define  STM_RCC_CFGR_PPRE2_MASK        7UL
130
131 #define STM_RCC_CFGR_PPRE1      (8)
132 #define  STM_RCC_CFGR_PPRE1_DIV_1       0
133 #define  STM_RCC_CFGR_PPRE1_DIV_2       4
134 #define  STM_RCC_CFGR_PPRE1_DIV_4       5
135 #define  STM_RCC_CFGR_PPRE1_DIV_8       6
136 #define  STM_RCC_CFGR_PPRE1_DIV_16      7
137 #define  STM_RCC_CFGR_PPRE1_MASK        7UL
138
139 #define STM_RCC_CFGR_HPRE       (4)
140 #define  STM_RCC_CFGR_HPRE_DIV_1        0
141 #define  STM_RCC_CFGR_HPRE_DIV_2        8
142 #define  STM_RCC_CFGR_HPRE_DIV_4        9
143 #define  STM_RCC_CFGR_HPRE_DIV_8        0xa
144 #define  STM_RCC_CFGR_HPRE_DIV_16       0xb
145 #define  STM_RCC_CFGR_HPRE_DIV_64       0xc
146 #define  STM_RCC_CFGR_HPRE_DIV_128      0xd
147 #define  STM_RCC_CFGR_HPRE_DIV_256      0xe
148 #define  STM_RCC_CFGR_HPRE_DIV_512      0xf
149 #define  STM_RCC_CFGR_HPRE_MASK         0xfUL
150
151 #define STM_RCC_CFGR_SWS        (2)
152 #define  STM_RCC_CFGR_SWS_HSI           0
153 #define  STM_RCC_CFGR_SWS_HSE           1
154 #define  STM_RCC_CFGR_SWS_PLL           2
155 #define  STM_RCC_CFGR_SWS_MASK          3UL
156
157 #define STM_RCC_CFGR_SW         (0)
158 #define  STM_RCC_CFGR_SW_HSI            0
159 #define  STM_RCC_CFGR_SW_HSE            1
160 #define  STM_RCC_CFGR_SW_PLL            2
161 #define  STM_RCC_CFGR_SW_MASK           3UL
162
163 #define STM_RCC_AHBENR_CRCEN    6
164 #define STM_RCC_AHBENR_FLITFEN  4
165 #define STM_RCC_AHBENR_SRAMEN   2
166 #define STM_RCC_AHBENR_DMA2EN   1
167 #define STM_RCC_AHBENR_DMA1EN   0
168
169
170 #define STM_RCC_APB2ENR_USART1EN        14
171 #define STM_RCC_APB2ENR_SPI1EN          12
172 #define STM_RCC_APB2ENR_TIM1EN          11
173 #define STM_RCC_APB2ENR_ADC2EN          10
174 #define STM_RCC_APB2ENR_ADC1EN          9
175 #define STM_RCC_APB2ENR_IOPEEN          6
176 #define STM_RCC_APB2ENR_IOPDEN          5
177 #define STM_RCC_APB2ENR_IOPCEN          4
178 #define STM_RCC_APB2ENR_IOPBEN          3
179 #define STM_RCC_APB2ENR_IOPAEN          2
180 #define STM_RCC_APB2ENR_AFIOEN          0
181
182 #define STM_RCC_APB1ENR_DACEN           29
183 #define STM_RCC_APB1ENR_PWREN           28
184 #define STM_RCC_APB1ENR_BKPEN           27
185 #define STM_RCC_APB1ENR_CANEN           26
186 #define STM_RCC_APB1ENR_USBEN           23
187 #define STM_RCC_APB1ENR_I2C2EN          22
188 #define STM_RCC_APB1ENR_I2C1EN          21
189 #define STM_RCC_APB1ENR_UART5EN         20
190 #define STM_RCC_APB1ENR_UART4EN         19
191 #define STM_RCC_APB1ENR_USART3EN        18
192 #define STM_RCC_APB1ENR_USART2EN        17
193 #define STM_RCC_APB1ENR_SPI3EN          15
194 #define STM_RCC_APB1ENR_SPI2EN          14
195 #define STM_RCC_APB1ENR_WWDGEN          11
196 #define STM_RCC_APB1ENR_TIM14EN         8
197 #define STM_RCC_APB1ENR_TIM13EN         7
198 #define STM_RCC_APB1ENR_TIM12EN         6
199 #define STM_RCC_APB1ENR_TIM7EN          5
200 #define STM_RCC_APB1ENR_TIM6EN          4
201 #define STM_RCC_APB1ENR_TIM5EN          3
202 #define STM_RCC_APB1ENR_TIM4EN          2
203 #define STM_RCC_APB1ENR_TIM3EN          1
204 #define STM_RCC_APB1ENR_TIM2EN          0
205
206 #define STM_RCC_CSR_LPWRRSTF            (31)
207 #define STM_RCC_CSR_WWDGRSTF            (30)
208 #define STM_RCC_CSR_IWDGRSTF            (29)
209 #define STM_RCC_CSR_SFTRSTF             (28)
210 #define STM_RCC_CSR_PORRSTF             (27)
211 #define STM_RCC_CSR_PINRSTF             (26)
212 #define STM_RCC_CSR_RMVF                (24)
213 #define STM_RCC_CSR_LSIRDY              (1)
214 #define STM_RCC_CSR_LSION               (0)
215
216 struct stm_systick {
217         vuint32_t       ctrl;
218         vuint32_t       load;
219         vuint32_t       val;
220         vuint32_t       calib;
221 };
222
223 extern struct stm_systick stm_systick;
224
225 //#define stm_systick   (*((struct stm_systick *) 0xe000e010))
226
227 #define STM_SYSTICK_CTRL_ENABLE         0
228 #define STM_SYSTICK_CTRL_TICKINT        1
229 #define STM_SYSTICK_CTRL_CLKSOURCE      2
230 #define  STM_SYSTICK_CTRL_CLKSOURCE_HCLK_8              0
231 #define  STM_SYSTICK_CTRL_CLKSOURCE_HCLK                1
232 #define STM_SYSTICK_CTRL_COUNTFLAG      16
233
234 /* The NVIC starts at 0xe000e100, so add that to the offsets to find the absolute address */
235
236 struct stm_nvic {
237         vuint32_t       iser[3];        /* 0x000 0xe000e100 Set Enable Register */
238
239         uint8_t         _unused00c[0x080 - 0x00c];
240
241         vuint32_t       icer[3];        /* 0x080 0xe000e180 Clear Enable Register */
242
243         uint8_t         _unused08c[0x100 - 0x08c];
244
245         vuint32_t       ispr[3];        /* 0x100 0xe000e200 Set Pending Register */
246
247         uint8_t         _unused10c[0x180 - 0x10c];
248
249         vuint32_t       icpr[3];        /* 0x180 0xe000e280 Clear Pending Register */
250
251         uint8_t         _unused18c[0x200 - 0x18c];
252
253         vuint32_t       iabr[3];        /* 0x200 0xe000e300 Active Bit Register */
254
255         uint8_t         _unused20c[0x300 - 0x20c];
256
257         vuint32_t       ipr[31];        /* 0x300 0xe000e400 Priority Register */
258
259         uint8_t         _unused37c[0xe00 - 0x37c];      /* covers SCB */
260
261         vuint32_t       stir;           /* 0xe00 0xe000ee00 Software Trigger Interrupt Register */
262 };
263
264 extern struct stm_nvic stm_nvic;
265
266 //#define stm_nvic (*((struct stm_nvic *) 0xe000e100))
267
268 #define IRQ_REG(irq)    ((irq) >> 5)
269 #define IRQ_BIT(irq)    ((irq) & 0x1f)
270 #define IRQ_MASK(irq)   (1 << IRQ_BIT(irq))
271 #define IRQ_BOOL(v,irq) (((v) >> IRQ_BIT(irq)) & 1)
272
273 static inline void
274 stm_nvic_set_enable(int irq) {
275         stm_nvic.iser[IRQ_REG(irq)] = IRQ_MASK(irq);
276 }
277
278 static inline void
279 stm_nvic_clear_enable(int irq) {
280         stm_nvic.icer[IRQ_REG(irq)] = IRQ_MASK(irq);
281 }
282
283 static inline int
284 stm_nvic_enabled(int irq) {
285         return IRQ_BOOL(stm_nvic.iser[IRQ_REG(irq)], irq);
286 }
287
288 static inline void
289 stm_nvic_set_pending(int irq) {
290         stm_nvic.ispr[IRQ_REG(irq)] = IRQ_MASK(irq);
291 }
292
293 static inline void
294 stm_nvic_clear_pending(int irq) {
295         stm_nvic.icpr[IRQ_REG(irq)] = IRQ_MASK(irq);
296 }
297
298 static inline int
299 stm_nvic_pending(int irq) {
300         return IRQ_BOOL(stm_nvic.ispr[IRQ_REG(irq)], irq);
301 }
302
303 static inline int
304 stm_nvic_active(int irq) {
305         return IRQ_BOOL(stm_nvic.iabr[IRQ_REG(irq)], irq);
306 }
307
308 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
309 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
310 #define IRQ_PRIO_MASK(irq)      (0xff << IRQ_PRIO_BIT(irq))
311
312 static inline void
313 stm_nvic_set_priority(int irq, uint8_t prio) {
314         int             n = IRQ_PRIO_REG(irq);
315         uint32_t        v;
316
317         v = stm_nvic.ipr[n];
318         v &= (uint32_t) ~IRQ_PRIO_MASK(irq);
319         v |= (prio) << IRQ_PRIO_BIT(irq);
320         stm_nvic.ipr[n] = v;
321 }
322
323 static inline uint8_t
324 stm_nvic_get_priority(int irq) {
325         return (stm_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
326 }
327
328 struct stm_scb {
329         vuint32_t       cpuid;
330         vuint32_t       icsr;
331         vuint32_t       vtor;
332         vuint32_t       aircr;
333
334         vuint32_t       scr;
335         vuint32_t       ccr;
336         vuint32_t       shpr1;
337         vuint32_t       shpr2;
338
339         vuint32_t       shpr3;
340         vuint32_t       shcrs;
341         vuint32_t       cfsr;
342         vuint32_t       hfsr;
343
344         uint32_t        unused_30;
345         vuint32_t       mmar;
346         vuint32_t       bfar;
347 };
348
349 extern struct stm_scb stm_scb;
350
351 #define STM_SCB_AIRCR_VECTKEY           16
352 #define  STM_SCB_AIRCR_VECTKEY_KEY              0x05fa
353 #define STM_SCB_AIRCR_PRIGROUP          8
354 #define STM_SCB_AIRCR_SYSRESETREQ       2
355 #define STM_SCB_AIRCR_VECTCLRACTIVE     1
356 #define STM_SCB_AIRCR_VECTRESET         0
357
358 struct stm_flash {
359         vuint32_t       acr;
360         vuint32_t       keyr;
361         vuint32_t       optkeyr;
362         vuint32_t       sr;
363
364         vuint32_t       cr;
365         vuint32_t       ar;
366         uint32_t        _unused018;
367         vuint32_t       obr;
368
369         vuint32_t       wrpr;
370 };
371
372 extern struct stm_flash stm_flash;
373
374 //#define stm_flash (*((struct stm_flash *) 0x40022000))
375
376 #define STM_FLASH_ACR_PRFTBS    5
377 #define STM_FLASH_ACR_PRFTBE    4
378 #define STM_FLASH_ACR_HLFCYA    3
379 #define STM_FLASH_ACR_LATENCY   0
380 #define  STM_FLASH_ACR_LATENCY_0        0
381 #define  STM_FLASH_ACR_LATENCY_1        1
382 #define  STM_FLASH_ACR_LATENCY_2        2
383
384 #define STM_FLASH_RDPRT_KEY     0x00A5
385 #define STM_FLASH_KEY1          0x45670123
386 #define STM_FLASH_KEY2          0xCDEF89AB
387
388
389 struct stm_flash_data {
390         vuint16_t       f_size;
391         vuint16_t       unused02;
392         vuint32_t       unused04;
393         vuint32_t       device_id[3];
394 };
395
396 extern struct stm_flash_data    stm_flash_data;
397
398 static inline uint32_t stm_flash_size(void) { return (uint32_t) stm_flash_data.f_size * 1024; }
399
400 //#define stm_flash_data        (*((struct stm_flash_data *) 0x1ffff7e0))
401
402 struct stm_gpio {
403         vuint32_t       cr[2];
404         vuint32_t       idr;
405         vuint32_t       odr;
406
407         vuint32_t       bsrr;
408         vuint32_t       brr;
409         vuint32_t       lckr;
410 };
411
412 #define STM_GPIO_CR(y)          ((uint8_t) (y) >> 3)
413 #define STM_GPIO_CR_CNF(y)      ((((uint8_t) (y) & 7) << 2) + 2)
414 #define  STM_GPIO_CR_CNF_INPUT_ANALOG           0
415 #define  STM_GPIO_CR_CNF_INPUT_FLOATING         1
416 #define  STM_GPIO_CR_CNF_INPUT_PULL             2
417 #define  STM_GPIO_CR_CNF_OUTPUT_PUSH_PULL       0
418 #define  STM_GPIO_CR_CNF_OUTPUT_OPEN_DRAIN      1
419 #define  STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL    2
420 #define  STM_GPIO_CR_CNF_OUTPUT_AF_OPEN_DRAIN   3
421 #define  STM_GPIO_CR_CNF_MASK                   3U
422 #define STM_GPIO_CR_MODE(y)     ((((y) & 7) << 2))
423 #define  STM_GPIO_CR_MODE_INPUT                 0
424 #define  STM_GPIO_CR_MODE_OUTPUT_10MHZ          1
425 #define  STM_GPIO_CR_MODE_OUTPUT_2MHZ           2
426 #define  STM_GPIO_CR_MODE_OUTPUT_50MHZ          3
427 #define  STM_GPIO_CR_MODE_MASK                  3U
428
429 static inline void
430 stm_gpio_conf(struct stm_gpio *gpio, int pin, uint8_t mode, uint8_t cnf)
431 {
432         uint8_t         cr = STM_GPIO_CR(pin);
433         uint32_t        v = gpio->cr[cr];
434
435         v &= ~((STM_GPIO_CR_CNF_MASK << STM_GPIO_CR_CNF(pin)) |
436                (STM_GPIO_CR_MODE_MASK << STM_GPIO_CR_MODE(pin)));
437         v |= (mode << STM_GPIO_CR_MODE(pin)) | (cnf << STM_GPIO_CR_CNF(pin));
438         gpio->cr[cr] = v;
439 }
440
441 static inline void
442 stm_gpio_set(struct stm_gpio *gpio, int pin, uint8_t value) {
443         /* Use the bit set/reset register to do this atomically */
444         gpio->bsrr = ((uint32_t) (value ^ 1) << (pin + 16)) | ((uint32_t) value << pin);
445 }
446
447 static inline void
448 stm_gpio_set_mask(struct stm_gpio *gpio, uint16_t bits, uint16_t mask) {
449         /* Use the bit set/reset register to do this atomically */
450         gpio->bsrr = ((uint32_t) (~bits & mask) << 16) | ((uint32_t) (bits & mask));
451 }
452
453 static inline void
454 stm_gpio_set_bits(struct stm_gpio *gpio, uint16_t bits) {
455         gpio->bsrr = bits;
456 }
457
458 static inline void
459 stm_gpio_clr_bits(struct stm_gpio *gpio, uint16_t bits) {
460         gpio->bsrr = ((uint32_t) bits) << 16;
461 }
462
463 static inline uint8_t
464 stm_gpio_get(struct stm_gpio *gpio, int pin) {
465         return (gpio->idr >> pin) & 1;
466 }
467
468 static inline uint16_t
469 stm_gpio_get_all(struct stm_gpio *gpio) {
470         return (uint16_t) gpio->idr;
471 }
472
473 extern struct stm_gpio stm_gpioa;
474 extern struct stm_gpio stm_gpiob;
475 extern struct stm_gpio stm_gpioc;
476 extern struct stm_gpio stm_gpiod;
477 extern struct stm_gpio stm_gpioe;
478
479 //#define stm_gpioe  (*((struct stm_gpio *) 0x40011800))
480 //#define stm_gpiod  (*((struct stm_gpio *) 0x40011400))
481 //#define stm_gpioc  (*((struct stm_gpio *) 0x40011000))
482 //#define stm_gpiob  (*((struct stm_gpio *) 0x40010c00))
483 //#define stm_gpioa  (*((struct stm_gpio *) 0x40010800))
484
485 struct stm_afio {
486         vuint32_t       evcr;
487         vuint32_t       mapr;
488         vuint32_t       exticr[4];
489         vuint32_t       mapr2;
490 };
491
492 extern struct stm_afio stm_afio;
493
494 //#define stm_afio      (*((struct stm_afio *) 0x40010000))
495
496 #define STM_AFIO_MAPR_ADC2_ETRGREG_REMAP        20
497 #define STM_AFIO_MAPR_ADC2_ETRGINJ_REMAP        19
498 #define STM_AFIO_MAPR_ADC1_ETRGREG_REMAP        18
499 #define STM_AFIO_MAPR_ADC1_ETRGINJ_REMAP        17
500 #define STM_AFIO_MAPR_TIM5CH4_IREMAP            16
501 #define STM_AFIO_MAPR_PD01_REMAP                15
502 #define STM_AFIO_MAPR_CAN_REMAP                 13
503 #define  STM_AFIO_MAPR_CAN_REMAP_PA11_PA12              0
504 #define  STM_AFIO_MAPR_CAN_REMAP_PB8_PB9                2
505 #define  STM_AFIO_MAPR_CAN_REMAP_PD0_PD1                3
506 #define  STM_AFIO_MAPR_CAN_REMAP_MASK                   3
507 #define STM_AFIO_MAPR_TIM4_REMAP                12
508 #define STM_AFIO_MAPR_TIM3_REMAP                10
509 #define  STM_AFIO_MAPR_TIM3_REMAP_PA6_PA7_PB0_PB1       0
510 #define  STM_AFIO_MAPR_TIM3_REMAP_PB4_PB5_PB0_PB1       2
511 #define  STM_AFIO_MAPR_TIM3_REMAP_PC6_PC7_PC8_PC9       3
512 #define  STM_AFIO_MAPR_TIM3_REMAP_MASK                  3
513 #define STM_AFIO_MAPR_TIM2_REMAP                8
514 #define  STM_AFIO_MAPR_TIM2_REMAP_PA0_PA1_PA2_PA3       0
515 #define  STM_AFIO_MAPR_TIM2_REMAP_PA15_PB3_PA2_PA3      1
516 #define  STM_AFIO_MAPR_TIM2_REMAP_PA0_PA1_PB10_PB11     2
517 #define  STM_AFIO_MAPR_TIM2_REMAP_PA15_PB3_PB10_PB11    3
518 #define  STM_AFIO_MAPR_TIM2_REMAP_MASK                  3
519 #define STM_AFIO_MAPR_TIM1_REMAP                6
520 #define  STM_AFIO_MAPR_TIM1_REMAP_PA12_PA8_PA9_PA10_PA11_PB12_PB13_PB14_PB15    0
521 #define  STM_AFIO_MAPR_TIM1_REMAP_PA12_PA8_PA9_PA10_PA11_PA6_PA7_PB0_PB1        1
522 #define  STM_AFIO_MAPR_TIM1_REMAP_PE7_PE9_PE11_PE13_PE14_PE15_PE8_PE10_PE12     3
523 #define  STM_AFIO_MAPR_TIM1_REMAP_MASK                                          3
524 #define STM_AFIO_MAPR_USART3_REMAP              4
525 #define  STM_AFIO_MAPR_USART3_REMAP_PB10_PB11_PB12_PB13_PB14    0
526 #define  STM_AFIO_MAPR_USART3_REMAP_PC10_PC11_PC12_PB13_PB14    1
527 #define  STM_AFIO_MAPR_USART3_REMAP_PD8_PD9_PD10_PD11_PD12      3
528 #define  STM_AFIO_MAPR_USART3_REMAP_MASK                        3
529 #define STM_AFIO_MAPR_USART2_REMAP              3
530 #define  STM_AFIO_MAPR_USART2_REMAP_PA0_PA1_PA2_PA3_PA4 0
531 #define  STM_AFIO_MAPR_USART2_REMAP_PD3_PD4_PD5_PD6_PD7 1
532 #define  STM_AFIO_MAPR_USART2_REMAP_MASK                1
533 #define STM_AFIO_MAPR_USART1_REMAP              2
534 #define  STM_AFIO_MAPR_USART1_REMAP_PA9_PA10            0
535 #define  STM_AFIO_MAPR_USART1_REMAP_PB6_PB7             1
536 #define  STM_AFIO_MAPR_USART1_REMAP_MASK                1
537 #define STM_AFIO_MAPR_I2C1_REMAP                1
538 #define  STM_AFIO_MAPR_I2C1_REMAP_PB6_PB7               0
539 #define  STM_AFIO_MAPR_I2C1_REMAP_PB8_PB9               1
540 #define  STM_AFIO_MAPR_I2C1_REMAP_MASK                  1
541 #define STM_AFIO_MAPR_SPI1_REMAP                0
542 #define  STM_AFIO_MAPR_SPI1_REMAP_PA4_PA5_PA6_PA7       0
543 #define  STM_AFIO_MAPR_SPI1_REMAP_PA15_PB3_PB4_PB5      1
544 #define  STM_AFIO_MAPR_SPI1_REMAP_MASK                  1
545
546 static inline void
547 stm_set_afio_mapr(uint8_t bit, uint32_t val, uint32_t mask) {
548         uint32_t        mapr = stm_afio.mapr;
549
550         mapr &= ~(mask << bit);
551         mapr |= (val << bit);
552         stm_afio.mapr = mapr;
553 }
554
555 struct stm_usart {
556         vuint32_t       sr;     /* status register */
557         vuint32_t       dr;     /* data register */
558         vuint32_t       brr;    /* baud rate register */
559         vuint32_t       cr1;    /* control register 1 */
560
561         vuint32_t       cr2;    /* control register 2 */
562         vuint32_t       cr3;    /* control register 3 */
563         vuint32_t       gtpr;   /* guard time and prescaler */
564 };
565
566 extern struct stm_usart stm_usart1;
567 extern struct stm_usart stm_usart2;
568 extern struct stm_usart stm_usart3;
569
570 //#define stm_usart1    (*((struct stm_usart *) 0x40013800))
571 //#define stm_usart2    (*((struct stm_usart *) 0x40004800))
572 //#define stm_usart3    (*((struct stm_usart *) 0x40004400))
573
574 #define STM_USART_SR_CTS        (9)     /* CTS flag */
575 #define STM_USART_SR_LBD        (8)     /* LIN break detection flag */
576 #define STM_USART_SR_TXE        (7)     /* Transmit data register empty */
577 #define STM_USART_SR_TC         (6)     /* Transmission complete */
578 #define STM_USART_SR_RXNE       (5)     /* Read data register not empty */
579 #define STM_USART_SR_IDLE       (4)     /* IDLE line detected */
580 #define STM_USART_SR_ORE        (3)     /* Overrun error */
581 #define STM_USART_SR_NE         (2)     /* Noise detected flag */
582 #define STM_USART_SR_FE         (1)     /* Framing error */
583 #define STM_USART_SR_PE         (0)     /* Parity error */
584
585 #define STM_USART_BRR_DIV_MANTISSA      (4)
586 #define STM_USART_BRR_DIV_FRACTION      (0)
587
588 #define STM_USART_CR1_UE        (13)    /* USART enable */
589 #define STM_USART_CR1_M         (12)    /* Word length */
590 #define STM_USART_CR1_WAKE      (11)    /* Wakeup method */
591 #define STM_USART_CR1_PCE       (10)    /* Parity control enable */
592 #define STM_USART_CR1_PS        (9)     /* Parity selection */
593 #define STM_USART_CR1_PEIE      (8)     /* PE interrupt enable */
594 #define STM_USART_CR1_TXEIE     (7)     /* TXE interrupt enable */
595 #define STM_USART_CR1_TCIE      (6)     /* Transmission complete interrupt enable */
596 #define STM_USART_CR1_RXNEIE    (5)     /* RXNE interrupt enable */
597 #define STM_USART_CR1_IDLEIE    (4)     /* IDLE interrupt enable */
598 #define STM_USART_CR1_TE        (3)     /* Transmitter enable */
599 #define STM_USART_CR1_RE        (2)     /* Receiver enable */
600 #define STM_USART_CR1_RWU       (1)     /* Receiver wakeup */
601 #define STM_USART_CR1_SBK       (0)     /* Send break */
602
603 #define STM_USART_CR2_LINEN     (14)    /* LIN mode enable */
604 #define STM_USART_CR2_STOP      (12)    /* STOP bits */
605 #define STM_USART_CR2_STOP_MASK 3UL
606 #define STM_USART_CR2_STOP_1    0
607 #define STM_USART_CR2_STOP_0_5  1
608 #define STM_USART_CR2_STOP_2    2
609 #define STM_USART_CR2_STOP_1_5  3
610
611 #define STM_USART_CR2_CLKEN     (11)    /* Clock enable */
612 #define STM_USART_CR2_CPOL      (10)    /* Clock polarity */
613 #define STM_USART_CR2_CPHA      (9)     /* Clock phase */
614 #define STM_USART_CR2_LBCL      (8)     /* Last bit clock pulse */
615 #define STM_USART_CR2_LBDIE     (6)     /* LIN break detection interrupt enable */
616 #define STM_USART_CR2_LBDL      (5)     /* lin break detection length */
617 #define STM_USART_CR2_ADD       (0)
618 #define STM_USART_CR2_ADD_MASK  0xfUL
619
620 #define STM_USART_CR3_CTSIE     (10)    /* CTS interrupt enable */
621 #define STM_USART_CR3_CTSE      (9)     /* CTS enable */
622 #define STM_USART_CR3_RTSE      (8)     /* RTS enable */
623 #define STM_USART_CR3_DMAT      (7)     /* DMA enable transmitter */
624 #define STM_USART_CR3_DMAR      (6)     /* DMA enable receiver */
625 #define STM_USART_CR3_SCEN      (5)     /* Smartcard mode enable */
626 #define STM_USART_CR3_NACK      (4)     /* Smartcard NACK enable */
627 #define STM_USART_CR3_HDSEL     (3)     /* Half-duplex selection */
628 #define STM_USART_CR3_IRLP      (2)     /* IrDA low-power */
629 #define STM_USART_CR3_IREN      (1)     /* IrDA mode enable */
630 #define STM_USART_CR3_EIE       (0)     /* Error interrupt enable */
631
632 struct stm_usb {
633         vuint32_t       epr[8];
634         uint8_t         reserved_20[0x40 - 0x20];
635         vuint32_t       cntr;
636         vuint32_t       istr;
637         vuint32_t       fnr;
638         vuint32_t       daddr;
639         vuint32_t       btable;
640 };
641
642 /*
643  * USB DM: PA11
644  * USB DP: PA12
645  *
646  * Need a pull-up on a separate GPIO
647  */
648 #define STM_USB_EPR_CTR_RX      15
649 #define  STM_USB_EPR_CTR_RX_WRITE_INVARIANT             1
650 #define STM_USB_EPR_DTOG_RX     14
651 #define STM_USB_EPR_DTOG_RX_WRITE_INVARIANT             0
652 #define STM_USB_EPR_STAT_RX     12
653 #define  STM_USB_EPR_STAT_RX_DISABLED                   0
654 #define  STM_USB_EPR_STAT_RX_STALL                      1
655 #define  STM_USB_EPR_STAT_RX_NAK                        2
656 #define  STM_USB_EPR_STAT_RX_VALID                      3
657 #define  STM_USB_EPR_STAT_RX_MASK                       3UL
658 #define  STM_USB_EPR_STAT_RX_WRITE_INVARIANT            0
659 #define STM_USB_EPR_SETUP       11
660 #define STM_USB_EPR_EP_TYPE     9
661 #define  STM_USB_EPR_EP_TYPE_BULK                       0
662 #define  STM_USB_EPR_EP_TYPE_CONTROL                    1
663 #define  STM_USB_EPR_EP_TYPE_ISO                        2
664 #define  STM_USB_EPR_EP_TYPE_INTERRUPT                  3
665 #define  STM_USB_EPR_EP_TYPE_MASK                       3UL
666 #define STM_USB_EPR_EP_KIND     8
667 #define  STM_USB_EPR_EP_KIND_DBL_BUF                    1       /* Bulk */
668 #define  STM_USB_EPR_EP_KIND_STATUS_OUT                 1       /* Control */
669 #define STM_USB_EPR_CTR_TX      7
670 #define  STM_USB_CTR_TX_WRITE_INVARIANT                 1
671 #define STM_USB_EPR_DTOG_TX     6
672 #define  STM_USB_EPR_DTOG_TX_WRITE_INVARIANT            0
673 #define STM_USB_EPR_STAT_TX     4
674 #define  STM_USB_EPR_STAT_TX_DISABLED                   0
675 #define  STM_USB_EPR_STAT_TX_STALL                      1
676 #define  STM_USB_EPR_STAT_TX_NAK                        2
677 #define  STM_USB_EPR_STAT_TX_VALID                      3
678 #define  STM_USB_EPR_STAT_TX_WRITE_INVARIANT            0
679 #define  STM_USB_EPR_STAT_TX_MASK                       3UL
680 #define STM_USB_EPR_EA          0
681 #define  STM_USB_EPR_EA_MASK                            0xfUL
682
683 #define STM_USB_CNTR_CTRM       15
684 #define STM_USB_CNTR_PMAOVRM    14
685 #define STM_USB_CNTR_ERRM       13
686 #define STM_USB_CNTR_WKUPM      12
687 #define STM_USB_CNTR_SUSPM      11
688 #define STM_USB_CNTR_RESETM     10
689 #define STM_USB_CNTR_SOFM       9
690 #define STM_USB_CNTR_ESOFM      8
691 #define STM_USB_CNTR_RESUME     4
692 #define STM_USB_CNTR_FSUSP      3
693 #define STM_USB_CNTR_LP_MODE    2
694 #define STM_USB_CNTR_PDWN       1
695 #define STM_USB_CNTR_FRES       0
696
697 #define STM_USB_ISTR_CTR        15
698 #define STM_USB_ISTR_PMAOVR     14
699 #define STM_USB_ISTR_ERR        13
700 #define STM_USB_ISTR_WKUP       12
701 #define STM_USB_ISTR_SUSP       11
702 #define STM_USB_ISTR_RESET      10
703 #define STM_USB_ISTR_SOF        9
704 #define STM_USB_ISTR_ESOF       8
705 #define STM_USB_ISTR_DIR        4
706 #define STM_USB_ISTR_EP_ID      0
707 #define  STM_USB_ISTR_EP_ID_MASK                0xfUL
708
709 #define STM_USB_FNR_RXDP        15
710 #define STM_USB_FNR_RXDM        14
711 #define STM_USB_FNR_LCK         13
712 #define STM_USB_FNR_LSOF        11
713 #define  STM_USB_FNR_LSOF_MASK                  0x3UL
714 #define STM_USB_FNR_FN          0
715 #define  STM_USB_FNR_FN_MASK                    0x7ffUL
716
717 #define STM_USB_DADDR_EF        7
718 #define STM_USB_DADDR_ADD       0
719 #define  STM_USB_DADDR_ADD_MASK                 0x7fUL
720
721 extern struct stm_usb stm_usb;
722
723
724 //#define stm_usb (*((struct stm_usb *) 0x40005c00))
725
726 union stm_usb_bdt {
727         struct {
728                 vuint32_t       addr_tx;
729                 vuint32_t       count_tx;
730                 vuint32_t       addr_rx;
731                 vuint32_t       count_rx;
732         } single;
733         struct {
734                 vuint32_t       addr;
735                 vuint32_t       count;
736         } double_tx[2];
737         struct {
738                 vuint32_t       addr;
739                 vuint32_t       count;
740         } double_rx[2];
741 };
742
743 #define STM_USB_BDT_COUNT_RX_BL_SIZE    15
744 #define STM_USB_BDT_COUNT_RX_NUM_BLOCK  10
745 #define  STM_USB_BDT_COUNT_RX_NUM_BLOCK_MASK    0x1fUL
746 #define STM_USB_BDT_COUNT_RX_COUNT_RX   0
747 #define  STM_USB_BDT_COUNT_RX_COUNT_RX_MASK     0x3ffUL
748
749 #define STM_USB_BDT_SIZE        8
750
751 extern uint8_t stm_usb_sram[] __attribute__ ((aligned(4)));
752
753 //#define stm_usb_sram ((uint8_t *)0x40006000);
754
755 #define isr_decl(name) void stm_ ## name ## _isr(void)
756
757 isr_decl(halt);
758 isr_decl(ignore);
759
760 isr_decl(nmi);
761 isr_decl(hardfault);
762 isr_decl(memmanage);
763 isr_decl(busfault);
764 isr_decl(usagefault);
765 isr_decl(svc);
766 isr_decl(debugmon);
767 isr_decl(pendsv);
768 isr_decl(systick);
769 isr_decl(wwdg);
770 isr_decl(pvd);
771 isr_decl(tamper_stamp);
772 isr_decl(rtc_wkup);
773 isr_decl(flash);
774 isr_decl(rcc);
775 isr_decl(exti0);
776 isr_decl(exti1);
777 isr_decl(exti2);
778 isr_decl(exti3);
779 isr_decl(exti4);
780 isr_decl(dma1_channel1);
781 isr_decl(dma1_channel2);
782 isr_decl(dma1_channel3);
783 isr_decl(dma1_channel4);
784 isr_decl(dma1_channel5);
785 isr_decl(dma1_channel6);
786 isr_decl(dma1_channel7);
787 isr_decl(adc1_2);
788 isr_decl(usb_hp);
789 isr_decl(usb_lp);
790 isr_decl(can_rx1);
791 isr_decl(can_sce);
792 isr_decl(exti9_5);
793 isr_decl(tim1_brk);
794 isr_decl(tim1_up);
795 isr_decl(tim1_trg_com);
796 isr_decl(tim1_cc);
797 isr_decl(tim2);
798 isr_decl(tim3);
799 isr_decl(tim4);
800 isr_decl(i2c1_ev);
801 isr_decl(i2c1_er);
802 isr_decl(i2c2_ev);
803 isr_decl(i2c2_er);
804 isr_decl(spi1);
805 isr_decl(spi2);
806 isr_decl(usart1);
807 isr_decl(usart2);
808 isr_decl(usart3);
809 isr_decl(exti15_10);
810 isr_decl(rtc_alarm);
811 isr_decl(usb_wakeup);
812 isr_decl(tim8_brk);
813 isr_decl(tim8_up);
814 isr_decl(tim8_trg_com);
815 isr_decl(tim8_cc);
816 isr_decl(adc3);
817 isr_decl(fsmc);
818 isr_decl(sdio);
819 isr_decl(tim5);
820 isr_decl(spi3);
821 isr_decl(uart4);
822 isr_decl(uart5);
823 isr_decl(tim6);
824 isr_decl(tim7);
825 isr_decl(dma2_channel1);
826 isr_decl(dma2_channel2);
827 isr_decl(dma2_channel3);
828 isr_decl(dma2_channel4_5);
829
830 #undef isr_decl
831
832 #define STM_ISR_WWDG_POS                0
833 #define STM_ISR_PVD_POS                 1
834 #define STM_ISR_TAMPER_STAMP_POS        2
835 #define STM_ISR_RTC_WKUP_POS            3
836 #define STM_ISR_FLASH_POS               4
837 #define STM_ISR_RCC_POS                 5
838 #define STM_ISR_EXTI0_POS               6
839 #define STM_ISR_EXTI1_POS               7
840 #define STM_ISR_EXTI2_POS               8
841 #define STM_ISR_EXTI3_POS               9
842 #define STM_ISR_EXTI4_POS               10
843 #define STM_ISR_DMA1_CHANNEL1_POS       11
844 #define STM_ISR_DMA1_CHANNEL2_POS       12
845 #define STM_ISR_DMA1_CHANNEL3_POS       13
846 #define STM_ISR_DMA1_CHANNEL4_POS       14
847 #define STM_ISR_DMA1_CHANNEL5_POS       15
848 #define STM_ISR_DMA1_CHANNEL6_POS       16
849 #define STM_ISR_DMA1_CHANNEL7_POS       17
850 #define STM_ISR_ADC1_2_POS              18
851 #define STM_ISR_USB_HP_POS              19
852 #define STM_ISR_USB_LP_POS              20
853 #define STM_ISR_CAN_RX1_POS             21
854 #define STM_ISR_CAN_SCE_POS             22
855 #define STM_ISR_EXTI9_5_POS             23
856 #define STM_ISR_TIM1_BRK_POS            24
857 #define STM_ISR_TIM1_UP_POS             25
858 #define STM_ISR_TIM1_TRG_COM_POS        26
859 #define STM_ISR_TIM1_CC_POS             27
860 #define STM_ISR_TIM2_POS                28
861 #define STM_ISR_TIM3_POS                29
862 #define STM_ISR_TIM4_POS                30
863 #define STM_ISR_I2C1_EV_POS             31
864 #define STM_ISR_I2C1_ER_POS             32
865 #define STM_ISR_I2C2_EV_POS             33
866 #define STM_ISR_I2C2_ER_POS             34
867 #define STM_ISR_SPI1_POS                35
868 #define STM_ISR_SPI2_POS                36
869 #define STM_ISR_USART1_POS              37
870 #define STM_ISR_USART2_POS              38
871 #define STM_ISR_USART3_POS              39
872 #define STM_ISR_EXTI15_10_POS           40
873 #define STM_ISR_RTC_ALARM_POS           41
874 #define STM_ISR_USB_WAKEUP_POS          42
875 #define STM_ISR_TIM8_BRK_POS            43
876 #define STM_ISR_TIM8_UP_POS             44
877 #define STM_ISR_TIM8_TRG_COM_POS        45
878 #define STM_ISR_TIM8_CC_POS             46
879 #define STM_ISR_ADC3_POS                47
880 #define STM_ISR_FSMC_POS                48
881 #define STM_ISR_SDIO_POS                49
882 #define STM_ISR_TIM5_POS                50
883 #define STM_ISR_SPI3_POS                51
884 #define STM_ISR_UART4_POS               52
885 #define STM_ISR_UART5_POS               53
886 #define STM_ISR_TIM6_POS                54
887 #define STM_ISR_TIM7_POS                55
888 #define STM_ISR_DMA2_CHANNEL1_POS       56
889 #define STM_ISR_DMA2_CHANNEL2_POS       57
890 #define STM_ISR_DMA2_CHANNEL3_POS       58
891 #define STM_ISR_DMA3_CHANNEL4_5_POS     59
892
893 #endif