2 * Copyright © 2023 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.
24 typedef volatile uint32_t vuint32_t;
25 typedef volatile uint16_t vuint16_t;
26 typedef volatile void * vvoid_t;
45 extern struct stm_rcc stm_rcc;
47 //#define stm_rcc (*((struct stm_rcc *) 0x40021000))
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
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)
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
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
87 #define STM_RCC_CFGR_USBPRE (22)
88 #define STM_RCC_CFGR_USBPRE_1_5 0
89 #define STM_RCC_CFGR_USBPRE_1 1
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
109 #define STM_RCC_CFGR_PLLXTPRE (17)
110 #define STM_RCC_CFGR_PLLXTPRE_1 0
111 #define STM_RCC_CFGR_PLLXTPRE_2 1
113 #define STM_RCC_CFGR_PLLSRC (16)
114 #define STM_RCC_CFGR_PLLSRC_HSI_2 0
115 #define STM_RCC_CFGR_PLLSRC_HSE 1
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
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
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
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
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
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
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
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
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
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)
223 extern struct stm_systick stm_systick;
225 //#define stm_systick (*((struct stm_systick *) 0xe000e010))
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
234 /* The NVIC starts at 0xe000e100, so add that to the offsets to find the absolute address */
237 vuint32_t iser[3]; /* 0x000 0xe000e100 Set Enable Register */
239 uint8_t _unused00c[0x080 - 0x00c];
241 vuint32_t icer[3]; /* 0x080 0xe000e180 Clear Enable Register */
243 uint8_t _unused08c[0x100 - 0x08c];
245 vuint32_t ispr[3]; /* 0x100 0xe000e200 Set Pending Register */
247 uint8_t _unused10c[0x180 - 0x10c];
249 vuint32_t icpr[3]; /* 0x180 0xe000e280 Clear Pending Register */
251 uint8_t _unused18c[0x200 - 0x18c];
253 vuint32_t iabr[3]; /* 0x200 0xe000e300 Active Bit Register */
255 uint8_t _unused20c[0x300 - 0x20c];
257 vuint32_t ipr[31]; /* 0x300 0xe000e400 Priority Register */
259 uint8_t _unused37c[0xe00 - 0x37c]; /* covers SCB */
261 vuint32_t stir; /* 0xe00 0xe000ee00 Software Trigger Interrupt Register */
264 extern struct stm_nvic stm_nvic;
266 //#define stm_nvic (*((struct stm_nvic *) 0xe000e100))
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)
274 stm_nvic_set_enable(int irq) {
275 stm_nvic.iser[IRQ_REG(irq)] = IRQ_MASK(irq);
279 stm_nvic_clear_enable(int irq) {
280 stm_nvic.icer[IRQ_REG(irq)] = IRQ_MASK(irq);
284 stm_nvic_enabled(int irq) {
285 return IRQ_BOOL(stm_nvic.iser[IRQ_REG(irq)], irq);
289 stm_nvic_set_pending(int irq) {
290 stm_nvic.ispr[IRQ_REG(irq)] = IRQ_MASK(irq);
294 stm_nvic_clear_pending(int irq) {
295 stm_nvic.icpr[IRQ_REG(irq)] = IRQ_MASK(irq);
299 stm_nvic_pending(int irq) {
300 return IRQ_BOOL(stm_nvic.ispr[IRQ_REG(irq)], irq);
304 stm_nvic_active(int irq) {
305 return IRQ_BOOL(stm_nvic.iabr[IRQ_REG(irq)], irq);
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))
313 stm_nvic_set_priority(int irq, uint8_t prio) {
314 int n = IRQ_PRIO_REG(irq);
318 v &= (uint32_t) ~IRQ_PRIO_MASK(irq);
319 v |= (prio) << IRQ_PRIO_BIT(irq);
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);
349 extern struct stm_scb stm_scb;
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
362 extern struct stm_dbgmcu stm_dbgmcu;
364 static inline uint16_t
366 return stm_dbgmcu.idcode & 0xfff;
383 extern struct stm_flash stm_flash;
385 //#define stm_flash (*((struct stm_flash *) 0x40022000))
387 #define STM_FLASH_ACR_PRFTBS 5
388 #define STM_FLASH_ACR_PRFTBE 4
389 #define STM_FLASH_ACR_HLFCYA 3
390 #define STM_FLASH_ACR_LATENCY 0
391 #define STM_FLASH_ACR_LATENCY_0 0
392 #define STM_FLASH_ACR_LATENCY_1 1
393 #define STM_FLASH_ACR_LATENCY_2 2
395 #define STM_FLASH_SR_EOP 5
396 #define STM_FLASH_SR_WRPRTERR 4
397 #define STM_FLASH_SR_PGERR 2
398 #define STM_FLASH_SR_BSY 0
400 #define STM_FLASH_CR_EOPIE 12
401 #define STM_FLASH_CR_ERRIE 10
402 #define STM_FLASH_CR_OPTWRE 9
403 #define STM_FLASH_CR_LOCK 7
404 #define STM_FLASH_CR_STRT 6
405 #define STM_FLASH_CR_OPTER 5
406 #define STM_FLASH_CR_OPTPG 4
407 #define STM_FLASH_CR_MER 2
408 #define STM_FLASH_CR_PER 1
409 #define STM_FLASH_CR_PG 0
411 #define STM_FLASH_RDPRT_KEY 0x00A5
412 #define STM_FLASH_KEYR_KEY1 0x45670123
413 #define STM_FLASH_KEYR_KEY2 0xCDEF89AB
416 struct stm_flash_data {
420 vuint32_t device_id[3];
423 extern struct stm_flash_data stm_flash_data;
425 static inline uint32_t stm_flash_size(void) { return (uint32_t) stm_flash_data.f_size * 1024; }
427 //#define stm_flash_data (*((struct stm_flash_data *) 0x1ffff7e0))
439 #define STM_GPIO_CR(y) ((uint8_t) (y) >> 3)
440 #define STM_GPIO_CR_CNF(y) ((((uint8_t) (y) & 7) << 2) + 2)
441 #define STM_GPIO_CR_CNF_INPUT_ANALOG 0
442 #define STM_GPIO_CR_CNF_INPUT_FLOATING 1
443 #define STM_GPIO_CR_CNF_INPUT_PULL 2
444 #define STM_GPIO_CR_CNF_OUTPUT_PUSH_PULL 0
445 #define STM_GPIO_CR_CNF_OUTPUT_OPEN_DRAIN 1
446 #define STM_GPIO_CR_CNF_OUTPUT_AF_PUSH_PULL 2
447 #define STM_GPIO_CR_CNF_OUTPUT_AF_OPEN_DRAIN 3
448 #define STM_GPIO_CR_CNF_MASK 3U
449 #define STM_GPIO_CR_MODE(y) ((((y) & 7) << 2))
450 #define STM_GPIO_CR_MODE_INPUT 0
451 #define STM_GPIO_CR_MODE_OUTPUT_10MHZ 1
452 #define STM_GPIO_CR_MODE_OUTPUT_2MHZ 2
453 #define STM_GPIO_CR_MODE_OUTPUT_50MHZ 3
454 #define STM_GPIO_CR_MODE_MASK 3U
457 stm_gpio_conf(struct stm_gpio *gpio, int pin, uint8_t mode, uint8_t cnf)
459 uint8_t cr = STM_GPIO_CR(pin);
460 uint32_t v = gpio->cr[cr];
462 v &= ~((STM_GPIO_CR_CNF_MASK << STM_GPIO_CR_CNF(pin)) |
463 (STM_GPIO_CR_MODE_MASK << STM_GPIO_CR_MODE(pin)));
464 v |= (mode << STM_GPIO_CR_MODE(pin)) | (cnf << STM_GPIO_CR_CNF(pin));
469 stm_gpio_set(struct stm_gpio *gpio, int pin, uint8_t value) {
470 /* Use the bit set/reset register to do this atomically */
471 gpio->bsrr = ((uint32_t) (value ^ 1) << (pin + 16)) | ((uint32_t) value << pin);
475 stm_gpio_set_mask(struct stm_gpio *gpio, uint16_t bits, uint16_t mask) {
476 /* Use the bit set/reset register to do this atomically */
477 gpio->bsrr = ((uint32_t) (~bits & mask) << 16) | ((uint32_t) (bits & mask));
481 stm_gpio_set_bits(struct stm_gpio *gpio, uint16_t bits) {
486 stm_gpio_clr_bits(struct stm_gpio *gpio, uint16_t bits) {
487 gpio->bsrr = ((uint32_t) bits) << 16;
490 static inline uint8_t
491 stm_gpio_get(struct stm_gpio *gpio, int pin) {
492 return (gpio->idr >> pin) & 1;
495 static inline uint16_t
496 stm_gpio_get_all(struct stm_gpio *gpio) {
497 return (uint16_t) gpio->idr;
500 extern struct stm_gpio stm_gpioa;
501 extern struct stm_gpio stm_gpiob;
502 extern struct stm_gpio stm_gpioc;
503 extern struct stm_gpio stm_gpiod;
504 extern struct stm_gpio stm_gpioe;
506 //#define stm_gpioe (*((struct stm_gpio *) 0x40011800))
507 //#define stm_gpiod (*((struct stm_gpio *) 0x40011400))
508 //#define stm_gpioc (*((struct stm_gpio *) 0x40011000))
509 //#define stm_gpiob (*((struct stm_gpio *) 0x40010c00))
510 //#define stm_gpioa (*((struct stm_gpio *) 0x40010800))
519 extern struct stm_afio stm_afio;
521 //#define stm_afio (*((struct stm_afio *) 0x40010000))
523 #define STM_AFIO_MAPR_ADC2_ETRGREG_REMAP 20
524 #define STM_AFIO_MAPR_ADC2_ETRGINJ_REMAP 19
525 #define STM_AFIO_MAPR_ADC1_ETRGREG_REMAP 18
526 #define STM_AFIO_MAPR_ADC1_ETRGINJ_REMAP 17
527 #define STM_AFIO_MAPR_TIM5CH4_IREMAP 16
528 #define STM_AFIO_MAPR_PD01_REMAP 15
529 #define STM_AFIO_MAPR_CAN_REMAP 13
530 #define STM_AFIO_MAPR_CAN_REMAP_PA11_PA12 0
531 #define STM_AFIO_MAPR_CAN_REMAP_PB8_PB9 2
532 #define STM_AFIO_MAPR_CAN_REMAP_PD0_PD1 3
533 #define STM_AFIO_MAPR_CAN_REMAP_MASK 3
534 #define STM_AFIO_MAPR_TIM4_REMAP 12
535 #define STM_AFIO_MAPR_TIM3_REMAP 10
536 #define STM_AFIO_MAPR_TIM3_REMAP_PA6_PA7_PB0_PB1 0
537 #define STM_AFIO_MAPR_TIM3_REMAP_PB4_PB5_PB0_PB1 2
538 #define STM_AFIO_MAPR_TIM3_REMAP_PC6_PC7_PC8_PC9 3
539 #define STM_AFIO_MAPR_TIM3_REMAP_MASK 3
540 #define STM_AFIO_MAPR_TIM2_REMAP 8
541 #define STM_AFIO_MAPR_TIM2_REMAP_PA0_PA1_PA2_PA3 0
542 #define STM_AFIO_MAPR_TIM2_REMAP_PA15_PB3_PA2_PA3 1
543 #define STM_AFIO_MAPR_TIM2_REMAP_PA0_PA1_PB10_PB11 2
544 #define STM_AFIO_MAPR_TIM2_REMAP_PA15_PB3_PB10_PB11 3
545 #define STM_AFIO_MAPR_TIM2_REMAP_MASK 3
546 #define STM_AFIO_MAPR_TIM1_REMAP 6
547 #define STM_AFIO_MAPR_TIM1_REMAP_PA12_PA8_PA9_PA10_PA11_PB12_PB13_PB14_PB15 0
548 #define STM_AFIO_MAPR_TIM1_REMAP_PA12_PA8_PA9_PA10_PA11_PA6_PA7_PB0_PB1 1
549 #define STM_AFIO_MAPR_TIM1_REMAP_PE7_PE9_PE11_PE13_PE14_PE15_PE8_PE10_PE12 3
550 #define STM_AFIO_MAPR_TIM1_REMAP_MASK 3
551 #define STM_AFIO_MAPR_USART3_REMAP 4
552 #define STM_AFIO_MAPR_USART3_REMAP_PB10_PB11_PB12_PB13_PB14 0
553 #define STM_AFIO_MAPR_USART3_REMAP_PC10_PC11_PC12_PB13_PB14 1
554 #define STM_AFIO_MAPR_USART3_REMAP_PD8_PD9_PD10_PD11_PD12 3
555 #define STM_AFIO_MAPR_USART3_REMAP_MASK 3
556 #define STM_AFIO_MAPR_USART2_REMAP 3
557 #define STM_AFIO_MAPR_USART2_REMAP_PA0_PA1_PA2_PA3_PA4 0
558 #define STM_AFIO_MAPR_USART2_REMAP_PD3_PD4_PD5_PD6_PD7 1
559 #define STM_AFIO_MAPR_USART2_REMAP_MASK 1
560 #define STM_AFIO_MAPR_USART1_REMAP 2
561 #define STM_AFIO_MAPR_USART1_REMAP_PA9_PA10 0
562 #define STM_AFIO_MAPR_USART1_REMAP_PB6_PB7 1
563 #define STM_AFIO_MAPR_USART1_REMAP_MASK 1
564 #define STM_AFIO_MAPR_I2C1_REMAP 1
565 #define STM_AFIO_MAPR_I2C1_REMAP_PB6_PB7 0
566 #define STM_AFIO_MAPR_I2C1_REMAP_PB8_PB9 1
567 #define STM_AFIO_MAPR_I2C1_REMAP_MASK 1
568 #define STM_AFIO_MAPR_SPI1_REMAP 0
569 #define STM_AFIO_MAPR_SPI1_REMAP_PA4_PA5_PA6_PA7 0
570 #define STM_AFIO_MAPR_SPI1_REMAP_PA15_PB3_PB4_PB5 1
571 #define STM_AFIO_MAPR_SPI1_REMAP_MASK 1
574 stm_set_afio_mapr(uint8_t bit, uint32_t val, uint32_t mask) {
575 uint32_t mapr = stm_afio.mapr;
577 mapr &= ~(mask << bit);
578 mapr |= (val << bit);
579 stm_afio.mapr = mapr;
583 vuint32_t sr; /* status register */
584 vuint32_t dr; /* data register */
585 vuint32_t brr; /* baud rate register */
586 vuint32_t cr1; /* control register 1 */
588 vuint32_t cr2; /* control register 2 */
589 vuint32_t cr3; /* control register 3 */
590 vuint32_t gtpr; /* guard time and prescaler */
593 extern struct stm_usart stm_usart1;
594 extern struct stm_usart stm_usart2;
595 extern struct stm_usart stm_usart3;
597 //#define stm_usart1 (*((struct stm_usart *) 0x40013800))
598 //#define stm_usart2 (*((struct stm_usart *) 0x40004800))
599 //#define stm_usart3 (*((struct stm_usart *) 0x40004400))
601 #define STM_USART_SR_CTS (9) /* CTS flag */
602 #define STM_USART_SR_LBD (8) /* LIN break detection flag */
603 #define STM_USART_SR_TXE (7) /* Transmit data register empty */
604 #define STM_USART_SR_TC (6) /* Transmission complete */
605 #define STM_USART_SR_RXNE (5) /* Read data register not empty */
606 #define STM_USART_SR_IDLE (4) /* IDLE line detected */
607 #define STM_USART_SR_ORE (3) /* Overrun error */
608 #define STM_USART_SR_NE (2) /* Noise detected flag */
609 #define STM_USART_SR_FE (1) /* Framing error */
610 #define STM_USART_SR_PE (0) /* Parity error */
612 #define STM_USART_BRR_DIV_MANTISSA (4)
613 #define STM_USART_BRR_DIV_FRACTION (0)
615 #define STM_USART_CR1_UE (13) /* USART enable */
616 #define STM_USART_CR1_M (12) /* Word length */
617 #define STM_USART_CR1_WAKE (11) /* Wakeup method */
618 #define STM_USART_CR1_PCE (10) /* Parity control enable */
619 #define STM_USART_CR1_PS (9) /* Parity selection */
620 #define STM_USART_CR1_PEIE (8) /* PE interrupt enable */
621 #define STM_USART_CR1_TXEIE (7) /* TXE interrupt enable */
622 #define STM_USART_CR1_TCIE (6) /* Transmission complete interrupt enable */
623 #define STM_USART_CR1_RXNEIE (5) /* RXNE interrupt enable */
624 #define STM_USART_CR1_IDLEIE (4) /* IDLE interrupt enable */
625 #define STM_USART_CR1_TE (3) /* Transmitter enable */
626 #define STM_USART_CR1_RE (2) /* Receiver enable */
627 #define STM_USART_CR1_RWU (1) /* Receiver wakeup */
628 #define STM_USART_CR1_SBK (0) /* Send break */
630 #define STM_USART_CR2_LINEN (14) /* LIN mode enable */
631 #define STM_USART_CR2_STOP (12) /* STOP bits */
632 #define STM_USART_CR2_STOP_MASK 3UL
633 #define STM_USART_CR2_STOP_1 0
634 #define STM_USART_CR2_STOP_0_5 1
635 #define STM_USART_CR2_STOP_2 2
636 #define STM_USART_CR2_STOP_1_5 3
638 #define STM_USART_CR2_CLKEN (11) /* Clock enable */
639 #define STM_USART_CR2_CPOL (10) /* Clock polarity */
640 #define STM_USART_CR2_CPHA (9) /* Clock phase */
641 #define STM_USART_CR2_LBCL (8) /* Last bit clock pulse */
642 #define STM_USART_CR2_LBDIE (6) /* LIN break detection interrupt enable */
643 #define STM_USART_CR2_LBDL (5) /* lin break detection length */
644 #define STM_USART_CR2_ADD (0)
645 #define STM_USART_CR2_ADD_MASK 0xfUL
647 #define STM_USART_CR3_CTSIE (10) /* CTS interrupt enable */
648 #define STM_USART_CR3_CTSE (9) /* CTS enable */
649 #define STM_USART_CR3_RTSE (8) /* RTS enable */
650 #define STM_USART_CR3_DMAT (7) /* DMA enable transmitter */
651 #define STM_USART_CR3_DMAR (6) /* DMA enable receiver */
652 #define STM_USART_CR3_SCEN (5) /* Smartcard mode enable */
653 #define STM_USART_CR3_NACK (4) /* Smartcard NACK enable */
654 #define STM_USART_CR3_HDSEL (3) /* Half-duplex selection */
655 #define STM_USART_CR3_IRLP (2) /* IrDA low-power */
656 #define STM_USART_CR3_IREN (1) /* IrDA mode enable */
657 #define STM_USART_CR3_EIE (0) /* Error interrupt enable */
661 uint8_t reserved_20[0x40 - 0x20];
673 * Need a pull-up on a separate GPIO
675 #define STM_USB_EPR_CTR_RX 15
676 #define STM_USB_EPR_CTR_RX_WRITE_INVARIANT 1
677 #define STM_USB_EPR_DTOG_RX 14
678 #define STM_USB_EPR_DTOG_RX_WRITE_INVARIANT 0
679 #define STM_USB_EPR_STAT_RX 12
680 #define STM_USB_EPR_STAT_RX_DISABLED 0
681 #define STM_USB_EPR_STAT_RX_STALL 1
682 #define STM_USB_EPR_STAT_RX_NAK 2
683 #define STM_USB_EPR_STAT_RX_VALID 3
684 #define STM_USB_EPR_STAT_RX_MASK 3UL
685 #define STM_USB_EPR_STAT_RX_WRITE_INVARIANT 0
686 #define STM_USB_EPR_SETUP 11
687 #define STM_USB_EPR_EP_TYPE 9
688 #define STM_USB_EPR_EP_TYPE_BULK 0
689 #define STM_USB_EPR_EP_TYPE_CONTROL 1
690 #define STM_USB_EPR_EP_TYPE_ISO 2
691 #define STM_USB_EPR_EP_TYPE_INTERRUPT 3
692 #define STM_USB_EPR_EP_TYPE_MASK 3UL
693 #define STM_USB_EPR_EP_KIND 8
694 #define STM_USB_EPR_EP_KIND_DBL_BUF 1 /* Bulk */
695 #define STM_USB_EPR_EP_KIND_STATUS_OUT 1 /* Control */
696 #define STM_USB_EPR_CTR_TX 7
697 #define STM_USB_CTR_TX_WRITE_INVARIANT 1
698 #define STM_USB_EPR_DTOG_TX 6
699 #define STM_USB_EPR_DTOG_TX_WRITE_INVARIANT 0
700 #define STM_USB_EPR_STAT_TX 4
701 #define STM_USB_EPR_STAT_TX_DISABLED 0
702 #define STM_USB_EPR_STAT_TX_STALL 1
703 #define STM_USB_EPR_STAT_TX_NAK 2
704 #define STM_USB_EPR_STAT_TX_VALID 3
705 #define STM_USB_EPR_STAT_TX_WRITE_INVARIANT 0
706 #define STM_USB_EPR_STAT_TX_MASK 3UL
707 #define STM_USB_EPR_EA 0
708 #define STM_USB_EPR_EA_MASK 0xfUL
710 #define STM_USB_CNTR_CTRM 15
711 #define STM_USB_CNTR_PMAOVRM 14
712 #define STM_USB_CNTR_ERRM 13
713 #define STM_USB_CNTR_WKUPM 12
714 #define STM_USB_CNTR_SUSPM 11
715 #define STM_USB_CNTR_RESETM 10
716 #define STM_USB_CNTR_SOFM 9
717 #define STM_USB_CNTR_ESOFM 8
718 #define STM_USB_CNTR_RESUME 4
719 #define STM_USB_CNTR_FSUSP 3
720 #define STM_USB_CNTR_LP_MODE 2
721 #define STM_USB_CNTR_PDWN 1
722 #define STM_USB_CNTR_FRES 0
724 #define STM_USB_ISTR_CTR 15
725 #define STM_USB_ISTR_PMAOVR 14
726 #define STM_USB_ISTR_ERR 13
727 #define STM_USB_ISTR_WKUP 12
728 #define STM_USB_ISTR_SUSP 11
729 #define STM_USB_ISTR_RESET 10
730 #define STM_USB_ISTR_SOF 9
731 #define STM_USB_ISTR_ESOF 8
732 #define STM_USB_ISTR_DIR 4
733 #define STM_USB_ISTR_EP_ID 0
734 #define STM_USB_ISTR_EP_ID_MASK 0xfUL
736 #define STM_USB_FNR_RXDP 15
737 #define STM_USB_FNR_RXDM 14
738 #define STM_USB_FNR_LCK 13
739 #define STM_USB_FNR_LSOF 11
740 #define STM_USB_FNR_LSOF_MASK 0x3UL
741 #define STM_USB_FNR_FN 0
742 #define STM_USB_FNR_FN_MASK 0x7ffUL
744 #define STM_USB_DADDR_EF 7
745 #define STM_USB_DADDR_ADD 0
746 #define STM_USB_DADDR_ADD_MASK 0x7fUL
748 extern struct stm_usb stm_usb;
751 //#define stm_usb (*((struct stm_usb *) 0x40005c00))
770 #define STM_USB_BDT_COUNT_RX_BL_SIZE 15
771 #define STM_USB_BDT_COUNT_RX_NUM_BLOCK 10
772 #define STM_USB_BDT_COUNT_RX_NUM_BLOCK_MASK 0x1fUL
773 #define STM_USB_BDT_COUNT_RX_COUNT_RX 0
774 #define STM_USB_BDT_COUNT_RX_COUNT_RX_MASK 0x3ffUL
776 #define STM_USB_BDT_SIZE 8
778 extern uint8_t stm_usb_sram[] __attribute__ ((aligned(4)));
780 //#define stm_usb_sram ((uint8_t *)0x40006000)
782 struct stm_dma_channel {
790 #define STM_NUM_DMA 7
795 struct stm_dma_channel channel[STM_NUM_DMA];
798 extern struct stm_dma stm_dma;
800 #define stm_dma (*((struct stm_dma *) 0x40020000))
802 /* DMA channels go from 1 to 7, instead of 0 to 6 (sigh)
805 #define STM_DMA_INDEX(channel) ((channel) - 1)
807 #define STM_DMA_ISR(index) ((index) << 2)
808 #define STM_DMA_ISR_MASK 0xfUL
809 #define STM_DMA_ISR_TEIF 3
810 #define STM_DMA_ISR_HTIF 2
811 #define STM_DMA_ISR_TCIF 1
812 #define STM_DMA_ISR_GIF 0
814 #define STM_DMA_IFCR(index) ((index) << 2)
815 #define STM_DMA_IFCR_MASK 0xfUL
816 #define STM_DMA_IFCR_CTEIF 3
817 #define STM_DMA_IFCR_CHTIF 2
818 #define STM_DMA_IFCR_CTCIF 1
819 #define STM_DMA_IFCR_CGIF 0
821 #define STM_DMA_CCR_MEM2MEM (14)
823 #define STM_DMA_CCR_PL (12)
824 #define STM_DMA_CCR_PL_LOW (0)
825 #define STM_DMA_CCR_PL_MEDIUM (1)
826 #define STM_DMA_CCR_PL_HIGH (2)
827 #define STM_DMA_CCR_PL_VERY_HIGH (3)
828 #define STM_DMA_CCR_PL_MASK (3)
830 #define STM_DMA_CCR_MSIZE (10)
831 #define STM_DMA_CCR_MSIZE_8 (0)
832 #define STM_DMA_CCR_MSIZE_16 (1)
833 #define STM_DMA_CCR_MSIZE_32 (2)
834 #define STM_DMA_CCR_MSIZE_MASK (3)
836 #define STM_DMA_CCR_PSIZE (8)
837 #define STM_DMA_CCR_PSIZE_8 (0)
838 #define STM_DMA_CCR_PSIZE_16 (1)
839 #define STM_DMA_CCR_PSIZE_32 (2)
840 #define STM_DMA_CCR_PSIZE_MASK (3)
842 #define STM_DMA_CCR_MINC (7)
843 #define STM_DMA_CCR_PINC (6)
844 #define STM_DMA_CCR_CIRC (5)
845 #define STM_DMA_CCR_DIR (4)
846 #define STM_DMA_CCR_DIR_PER_TO_MEM 0
847 #define STM_DMA_CCR_DIR_MEM_TO_PER 1
848 #define STM_DMA_CCR_TEIE (3)
849 #define STM_DMA_CCR_HTIE (2)
850 #define STM_DMA_CCR_TCIE (1)
851 #define STM_DMA_CCR_EN (0)
853 #define STM_DMA_CHANNEL_ADC1 1
854 #define STM_DMA_CHANNEL_SPI1_RX 2
855 #define STM_DMA_CHANNEL_SPI1_TX 3
856 #define STM_DMA_CHANNEL_SPI2_RX 4
857 #define STM_DMA_CHANNEL_SPI2_TX 5
858 #define STM_DMA_CHANNEL_USART3_TX 2
859 #define STM_DMA_CHANNEL_USART3_RX 3
860 #define STM_DMA_CHANNEL_USART1_TX 4
861 #define STM_DMA_CHANNEL_USART1_RX 5
862 #define STM_DMA_CHANNEL_USART2_RX 6
863 #define STM_DMA_CHANNEL_USART2_TX 7
864 #define STM_DMA_CHANNEL_I2C2_TX 4
865 #define STM_DMA_CHANNEL_I2C2_RX 5
866 #define STM_DMA_CHANNEL_I2C1_TX 6
867 #define STM_DMA_CHANNEL_I2C1_RX 7
868 #define STM_DMA_CHANNEL_TIM1_CH1 2
869 #define STM_DMA_CHANNEL_TIM1_CH4 4
870 #define STM_DMA_CHANNEL_TIM1_TRIG 4
871 #define STM_DMA_CHANNEL_TIM1_COM 4
872 #define STM_DMA_CHANNEL_TIM1_UP 5
873 #define STM_DMA_CHANNEL_TIM1_CH3 6
874 #define STM_DMA_CHANNEL_TIM2_CH3 1
875 #define STM_DMA_CHANNEL_TIM2_UP 2
876 #define STM_DMA_CHANNEL_TIM2_CH1 5
877 #define STM_DMA_CHANNEL_TIM2_CH2 7
878 #define STM_DMA_CHANNEL_TIM2_CH4 7
879 #define STM_DMA_CHANNEL_TIM3_CH3 2
880 #define STM_DMA_CHANNEL_TIM3_CH4 3
881 #define STM_DMA_CHANNEL_TIM3_UP 3
882 #define STM_DMA_CHANNEL_TIM3_CH1 6
883 #define STM_DMA_CHANNEL_TIM3_TRIG 6
884 #define STM_DMA_CHANNEL_TIM4_CH1 1
885 #define STM_DMA_CHANNEL_TIM4_CH2 4
886 #define STM_DMA_CHANNEL_TIM4_CH3 5
887 #define STM_DMA_CHANNEL_TIM4_UP 7
889 /* high density, xl-density and connectivity devices also have dma2 */
891 #define STM_DMA2_CHANNEL_ADC3 5
892 #define STM_DMA2_CHANNEL_SPI3_RX 1
893 #define STM_DMA2_CHANNEL_SPI3_TX 2
894 #define STM_DMA2_CHANNEL_UART4_RX 3
895 #define STM_DMA2_CHANNEL_UART4_TX 5
896 #define STM_DMA2_CHANNEL_TIM5_CH4 1
897 #define STM_DMA2_CHANNEL_TIM5_TRIG 1
898 #define STM_DMA2_CHANNEL_TIM5_CH3 2
899 #define STM_DMA2_CHANNEL_TIM5_UP 2
900 #define STM_DMA2_CHANNEL_TIM5_CH2 4
901 #define STM_DMA2_CHANNEL_TIM5_CH1 5
902 #define STM_DMA2_CHANNEL_TIM6_UP 3
903 #define STM_DMA2_CHANNEL_DAC_CHANNEL1 3
904 #define STM_DMA2_CHANNEL_TIM7_UP 4
905 #define STM_DMA2_CHANNEL_DAC_CHANNEL2 4
906 #define STM_DMA2_CHANNEL_TIM8_CH3 1
907 #define STM_DMA2_CHANNEL_TIM8_UP 1
908 #define STM_DMA2_CHANNEL_TIM8_CH4 2
909 #define STM_DMA2_CHANNEL_TIM8_TRIG 2
910 #define STM_DMA2_CHANNEL_TIM8_COM 2
911 #define STM_DMA2_CHANNEL_TIM8_CH1 3
912 #define STM_DMA2_CHANNEL_TIM8_CH2 5
928 extern struct stm_spi stm_spi1, stm_spi2;
930 //#define stm_spi1 (*((struct stm_spi *) 0x40013000))
931 //#define stm_spi2 (*((struct stm_spi *) 0x40003800))
933 /* SPI channels go from 1 to 2, instead of 0 to 1 (sigh)
936 #define STM_NUM_SPI 2
938 #define STM_SPI_INDEX(channel) ((channel) - 1)
940 #define STM_SPI_CR1_BIDIMODE 15
941 #define STM_SPI_CR1_BIDIOE 14
942 #define STM_SPI_CR1_CRCEN 13
943 #define STM_SPI_CR1_CRCNEXT 12
944 #define STM_SPI_CR1_DFF 11
945 #define STM_SPI_CR1_RXONLY 10
946 #define STM_SPI_CR1_SSM 9
947 #define STM_SPI_CR1_SSI 8
948 #define STM_SPI_CR1_LSBFIRST 7
949 #define STM_SPI_CR1_SPE 6
950 #define STM_SPI_CR1_BR 3
951 #define STM_SPI_CR1_BR_PCLK_2 0
952 #define STM_SPI_CR1_BR_PCLK_4 1
953 #define STM_SPI_CR1_BR_PCLK_8 2
954 #define STM_SPI_CR1_BR_PCLK_16 3
955 #define STM_SPI_CR1_BR_PCLK_32 4
956 #define STM_SPI_CR1_BR_PCLK_64 5
957 #define STM_SPI_CR1_BR_PCLK_128 6
958 #define STM_SPI_CR1_BR_PCLK_256 7
959 #define STM_SPI_CR1_BR_MASK 7UL
961 #define STM_SPI_CR1_MSTR 2
962 #define STM_SPI_CR1_CPOL 1
963 #define STM_SPI_CR1_CPHA 0
965 #define STM_SPI_CR2_TXEIE 7
966 #define STM_SPI_CR2_RXNEIE 6
967 #define STM_SPI_CR2_ERRIE 5
968 #define STM_SPI_CR2_SSOE 2
969 #define STM_SPI_CR2_TXDMAEN 1
970 #define STM_SPI_CR2_RXDMAEN 0
972 #define STM_SPI_SR_FRE 8
973 #define STM_SPI_SR_BSY 7
974 #define STM_SPI_SR_OVR 6
975 #define STM_SPI_SR_MODF 5
976 #define STM_SPI_SR_CRCERR 4
977 #define STM_SPI_SR_UDR 3
978 #define STM_SPI_SR_CHSIDE 2
979 #define STM_SPI_SR_TXE 1
980 #define STM_SPI_SR_RXNE 0
984 #define isr_decl(name) void stm_ ## name ## _isr(void)
993 isr_decl(usagefault);
1000 isr_decl(tamper_stamp);
1009 isr_decl(dma1_channel1);
1010 isr_decl(dma1_channel2);
1011 isr_decl(dma1_channel3);
1012 isr_decl(dma1_channel4);
1013 isr_decl(dma1_channel5);
1014 isr_decl(dma1_channel6);
1015 isr_decl(dma1_channel7);
1024 isr_decl(tim1_trg_com);
1038 isr_decl(exti15_10);
1039 isr_decl(rtc_alarm);
1040 isr_decl(usb_wakeup);
1043 isr_decl(tim8_trg_com);
1054 isr_decl(dma2_channel1);
1055 isr_decl(dma2_channel2);
1056 isr_decl(dma2_channel3);
1057 isr_decl(dma2_channel4_5);
1061 #define STM_ISR_WWDG_POS 0
1062 #define STM_ISR_PVD_POS 1
1063 #define STM_ISR_TAMPER_STAMP_POS 2
1064 #define STM_ISR_RTC_WKUP_POS 3
1065 #define STM_ISR_FLASH_POS 4
1066 #define STM_ISR_RCC_POS 5
1067 #define STM_ISR_EXTI0_POS 6
1068 #define STM_ISR_EXTI1_POS 7
1069 #define STM_ISR_EXTI2_POS 8
1070 #define STM_ISR_EXTI3_POS 9
1071 #define STM_ISR_EXTI4_POS 10
1072 #define STM_ISR_DMA1_CHANNEL1_POS 11
1073 #define STM_ISR_DMA1_CHANNEL2_POS 12
1074 #define STM_ISR_DMA1_CHANNEL3_POS 13
1075 #define STM_ISR_DMA1_CHANNEL4_POS 14
1076 #define STM_ISR_DMA1_CHANNEL5_POS 15
1077 #define STM_ISR_DMA1_CHANNEL6_POS 16
1078 #define STM_ISR_DMA1_CHANNEL7_POS 17
1079 #define STM_ISR_ADC1_2_POS 18
1080 #define STM_ISR_USB_HP_POS 19
1081 #define STM_ISR_USB_LP_POS 20
1082 #define STM_ISR_CAN_RX1_POS 21
1083 #define STM_ISR_CAN_SCE_POS 22
1084 #define STM_ISR_EXTI9_5_POS 23
1085 #define STM_ISR_TIM1_BRK_POS 24
1086 #define STM_ISR_TIM1_UP_POS 25
1087 #define STM_ISR_TIM1_TRG_COM_POS 26
1088 #define STM_ISR_TIM1_CC_POS 27
1089 #define STM_ISR_TIM2_POS 28
1090 #define STM_ISR_TIM3_POS 29
1091 #define STM_ISR_TIM4_POS 30
1092 #define STM_ISR_I2C1_EV_POS 31
1093 #define STM_ISR_I2C1_ER_POS 32
1094 #define STM_ISR_I2C2_EV_POS 33
1095 #define STM_ISR_I2C2_ER_POS 34
1096 #define STM_ISR_SPI1_POS 35
1097 #define STM_ISR_SPI2_POS 36
1098 #define STM_ISR_USART1_POS 37
1099 #define STM_ISR_USART2_POS 38
1100 #define STM_ISR_USART3_POS 39
1101 #define STM_ISR_EXTI15_10_POS 40
1102 #define STM_ISR_RTC_ALARM_POS 41
1103 #define STM_ISR_USB_WAKEUP_POS 42
1104 #define STM_ISR_TIM8_BRK_POS 43
1105 #define STM_ISR_TIM8_UP_POS 44
1106 #define STM_ISR_TIM8_TRG_COM_POS 45
1107 #define STM_ISR_TIM8_CC_POS 46
1108 #define STM_ISR_ADC3_POS 47
1109 #define STM_ISR_FSMC_POS 48
1110 #define STM_ISR_SDIO_POS 49
1111 #define STM_ISR_TIM5_POS 50
1112 #define STM_ISR_SPI3_POS 51
1113 #define STM_ISR_UART4_POS 52
1114 #define STM_ISR_UART5_POS 53
1115 #define STM_ISR_TIM6_POS 54
1116 #define STM_ISR_TIM7_POS 55
1117 #define STM_ISR_DMA2_CHANNEL1_POS 56
1118 #define STM_ISR_DMA2_CHANNEL2_POS 57
1119 #define STM_ISR_DMA2_CHANNEL3_POS 58
1120 #define STM_ISR_DMA3_CHANNEL4_5_POS 59