Add support for multiple serial ports on STM32L
[fw/altos] / src / stm / stm32l.h
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 #ifndef _STM32L_H_
19 #define _STM32L_H_
20
21 #include <stdint.h>
22
23 typedef volatile uint32_t       vuint32_t;
24
25 struct stm_gpio {
26         vuint32_t       moder;
27         vuint32_t       otyper;
28         vuint32_t       ospeedr;
29         vuint32_t       pupdr;
30
31         vuint32_t       idr;
32         vuint32_t       odr;
33         vuint32_t       bsrr;
34         vuint32_t       lckr;
35
36         vuint32_t       afrl;
37         vuint32_t       afrh;
38 };
39
40 #define STM_MODER_SHIFT(pin)            ((pin) << 1)
41 #define STM_MODER_MASK                  3
42 #define STM_MODER_INPUT                 0
43 #define STM_MODER_OUTPUT                1
44 #define STM_MODER_ALTERNATE             2
45 #define STM_MODER_ANALOG                3
46
47 static inline void
48 stm_moder_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
49         gpio->moder = ((gpio->moder &
50                         ~(STM_MODER_MASK << STM_MODER_SHIFT(pin))) |
51                        value << STM_MODER_SHIFT(pin));
52 }
53         
54 static inline vuint32_t
55 stm_moder_get(struct stm_gpio *gpio, int pin) {
56         return (gpio->moder >> STM_MODER_SHIFT(pin)) & STM_MODER_MASK;
57 }
58
59 #define STM_OTYPER_SHIFT(pin)           (pin)
60 #define STM_OTYPER_MASK                 1
61 #define STM_OTYPER_PUSH_PULL            0
62 #define STM_OTYPER_OPEN_DRAIN           1
63
64 static inline void
65 stm_otyper_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
66         gpio->otyper = ((gpio->otyper &
67                          ~(STM_OTYPER_MASK << STM_OTYPER_SHIFT(pin))) |
68                         value << STM_OTYPER_SHIFT(pin));
69 }
70         
71 static inline vuint32_t
72 stm_otyper_get(struct stm_gpio *gpio, int pin) {
73         return (gpio->otyper >> STM_OTYPER_SHIFT(pin)) & STM_OTYPER_MASK;
74 }
75
76 #define STM_OSPEEDR_SHIFT(pin)          ((pin) << 1)
77 #define STM_OSPEEDR_MASK                3
78 #define STM_OSPEEDR_400kHz              0
79 #define STM_OSPEEDR_2MHz                1
80 #define STM_OSPEEDR_10MHz               2
81 #define STM_OSPEEDR_40MHz               3
82
83 static inline void
84 stm_ospeedr_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
85         gpio->ospeedr = ((gpio->ospeedr &
86                         ~(STM_OSPEEDR_MASK << STM_OSPEEDR_SHIFT(pin))) |
87                        value << STM_OSPEEDR_SHIFT(pin));
88 }
89         
90 static inline vuint32_t
91 stm_ospeedr_get(struct stm_gpio *gpio, int pin) {
92         return (gpio->ospeedr >> STM_OSPEEDR_SHIFT(pin)) & STM_OSPEEDR_MASK;
93 }
94
95 #define STM_PUPDR_SHIFT(pin)            ((pin) << 1)
96 #define STM_PUPDR_MASK                  3
97 #define STM_PUPDR_NONE                  0
98 #define STM_PUPDR_PULL_UP               1
99 #define STM_PUPDR_PULL_DOWN             2
100 #define STM_PUPDR_RESERVED              3
101
102 static inline void
103 stm_pupdr_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
104         gpio->pupdr = ((gpio->pupdr &
105                         ~(STM_PUPDR_MASK << STM_PUPDR_SHIFT(pin))) |
106                        value << STM_PUPDR_SHIFT(pin));
107 }
108         
109 static inline vuint32_t
110 stm_pupdr_get(struct stm_gpio *gpio, int pin) {
111         return (gpio->pupdr >> STM_PUPDR_SHIFT(pin)) & STM_PUPDR_MASK;
112 }
113
114 #define STM_AFR_SHIFT(pin)              ((pin) << 2)
115 #define STM_AFR_MASK                    0xf
116 #define STM_AFR_NONE                    0
117 #define STM_AFR_AF0                     0x0
118 #define STM_AFR_AF1                     0x1
119 #define STM_AFR_AF2                     0x2
120 #define STM_AFR_AF3                     0x3
121 #define STM_AFR_AF4                     0x4
122 #define STM_AFR_AF5                     0x5
123 #define STM_AFR_AF6                     0x6
124 #define STM_AFR_AF7                     0x7
125 #define STM_AFR_AF8                     0x8
126 #define STM_AFR_AF9                     0x9
127 #define STM_AFR_AF10                    0xa
128 #define STM_AFR_AF11                    0xb
129 #define STM_AFR_AF12                    0xc
130 #define STM_AFR_AF13                    0xd
131 #define STM_AFR_AF14                    0xe
132 #define STM_AFR_AF15                    0xf
133
134 static inline void
135 stm_afr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
136         if (pin < 8)
137                 gpio->afrl = ((gpio->afrl &
138                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
139                               value << STM_AFR_SHIFT(pin));
140         else {
141                 pin -= 8;
142                 gpio->afrh = ((gpio->afrh &
143                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
144                               value << STM_AFR_SHIFT(pin));
145         }
146 }
147         
148 static inline uint32_t
149 stm_afr_get(struct stm_gpio *gpio, int pin) {
150         if (pin < 8)
151                 return (gpio->afrl >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
152         else {
153                 pin -= 8;
154                 return (gpio->afrh >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
155         }
156 }
157
158 extern struct stm_gpio stm_gpioa;
159 extern struct stm_gpio stm_gpiob;
160 extern struct stm_gpio stm_gpioc;
161 extern struct stm_gpio stm_gpiod;
162 extern struct stm_gpio stm_gpioe;
163 extern struct stm_gpio stm_gpioh;
164
165 struct stm_usart {
166         vuint32_t       sr;     /* status register */
167         vuint32_t       dr;     /* data register */
168         vuint32_t       brr;    /* baud rate register */
169         vuint32_t       cr1;    /* control register 1 */
170
171         vuint32_t       cr2;    /* control register 2 */
172         vuint32_t       cr3;    /* control register 3 */
173         vuint32_t       gtpr;   /* guard time and prescaler */
174 };
175
176 extern struct stm_usart stm_usart1;
177 extern struct stm_usart stm_usart2;
178 extern struct stm_usart stm_usart3;
179
180 #define STM_USART_SR_CTS        (9)     /* CTS flag */
181 #define STM_USART_SR_LBD        (8)     /* LIN break detection flag */
182 #define STM_USART_SR_TXE        (7)     /* Transmit data register empty */
183 #define STM_USART_SR_TC         (6)     /* Transmission complete */
184 #define STM_USART_SR_RXNE       (5)     /* Read data register not empty */
185 #define STM_USART_SR_IDLE       (4)     /* IDLE line detected */
186 #define STM_USART_SR_ORE        (3)     /* Overrun error */
187 #define STM_USART_SR_NF         (2)     /* Noise detected flag */
188 #define STM_USART_SR_FE         (1)     /* Framing error */
189 #define STM_USART_SR_PE         (0)     /* Parity error */
190
191 #define STM_USART_CR1_OVER8     (15)    /* Oversampling mode */
192 #define STM_USART_CR1_UE        (13)    /* USART enable */
193 #define STM_USART_CR1_M         (12)    /* Word length */
194 #define STM_USART_CR1_WAKE      (11)    /* Wakeup method */
195 #define STM_USART_CR1_PCE       (10)    /* Parity control enable */
196 #define STM_USART_CR1_PS        (9)     /* Parity selection */
197 #define STM_USART_CR1_PEIE      (8)     /* PE interrupt enable */
198 #define STM_USART_CR1_TXEIE     (7)     /* TXE interrupt enable */
199 #define STM_USART_CR1_TCIE      (6)     /* Transmission complete interrupt enable */
200 #define STM_USART_CR1_RXNEIE    (5)     /* RXNE interrupt enable */
201 #define STM_USART_CR1_IDLEIE    (4)     /* IDLE interrupt enable */
202 #define STM_USART_CR1_TE        (3)     /* Transmitter enable */
203 #define STM_USART_CR1_RE        (2)     /* Receiver enable */
204 #define STM_USART_CR1_RWU       (1)     /* Receiver wakeup */
205 #define STM_USART_CR1_SBK       (0)     /* Send break */
206
207 #define STM_USART_CR2_LINEN     (14)    /* LIN mode enable */
208 #define STM_USART_CR2_STOP      (12)    /* STOP bits */
209 #define STM_USART_CR2_STOP_MASK 3
210 #define STM_USART_CR2_STOP_1    0
211 #define STM_USART_CR2_STOP_0_5  1
212 #define STM_USART_CR2_STOP_2    2
213 #define STM_USART_CR2_STOP_1_5  3
214
215 #define STM_USART_CR2_CLKEN     (11)    /* Clock enable */
216 #define STM_USART_CR2_CPOL      (10)    /* Clock polarity */
217 #define STM_USART_CR2_CPHA      (9)     /* Clock phase */
218 #define STM_USART_CR2_LBCL      (8)     /* Last bit clock pulse */
219 #define STM_USART_CR2_LBDIE     (6)     /* LIN break detection interrupt enable */
220 #define STM_USART_CR2_LBDL      (5)     /* lin break detection length */
221 #define STM_USART_CR2_ADD       (0)
222 #define STM_USART_CR2_ADD_MASK  0xf
223
224 #define STM_USART_CR3_ONEBITE   (11)    /* One sample bit method enable */
225 #define STM_USART_CR3_CTSIE     (10)    /* CTS interrupt enable */
226 #define STM_USART_CR3_CTSE      (9)     /* CTS enable */
227 #define STM_USART_CR3_RTSE      (8)     /* RTS enable */
228 #define STM_USART_CR3_DMAT      (7)     /* DMA enable transmitter */
229 #define STM_USART_CR3_DMAR      (6)     /* DMA enable receiver */
230 #define STM_USART_CR3_SCEN      (5)     /* Smartcard mode enable */
231 #define STM_USART_CR3_NACK      (4)     /* Smartcard NACK enable */
232 #define STM_USART_CR3_HDSEL     (3)     /* Half-duplex selection */
233 #define STM_USART_CR3_IRLP      (2)     /* IrDA low-power */
234 #define STM_USART_CR3_IREN      (1)     /* IrDA mode enable */
235 #define STM_USART_CR3_EIE       (0)     /* Error interrupt enable */
236
237 struct stm_spi {
238 };
239
240 extern struct stm_spi stm_spi1;
241
242 struct stm_tim {
243 };
244
245 extern struct stm_tim stm_tim9;
246 extern struct stm_tim stm_tim10;
247 extern struct stm_tim stm_tim11;
248
249 /* Flash interface */
250
251 struct stm_flash {
252         vuint32_t       acr;
253         vuint32_t       pecr;
254         vuint32_t       pdkeyr;
255         vuint32_t       pekeyr;
256
257         vuint32_t       prgkeyr;
258         vuint32_t       optkeyr;
259         vuint32_t       sr;
260         vuint32_t       obr;
261
262         vuint32_t       wrpr;
263 };
264
265 extern struct stm_flash stm_flash;
266
267 #define STM_FLASH_ACR_RUN_PD    (4)
268 #define STM_FLASH_ACR_SLEEP_PD  (3)
269 #define STM_FLASH_ACR_ACC64     (2)
270 #define STM_FLASH_ACR_PRFEN     (1)
271 #define STM_FLASH_ACR_LATENCY   (0)
272
273 struct stm_rcc {
274         vuint32_t       cr;
275         vuint32_t       icscr;
276         vuint32_t       cfgr;
277         vuint32_t       cir;
278
279         vuint32_t       ahbrstr;
280         vuint32_t       apb2rstr;
281         vuint32_t       abp1rstr;
282         vuint32_t       ahbenr;
283
284         vuint32_t       apb2enr;
285         vuint32_t       apb1enr;
286         vuint32_t       ahblenr;
287         vuint32_t       apb2lpenr;
288
289         vuint32_t       apb1lpenr;
290         vuint32_t       csr;
291 };
292
293 extern struct stm_rcc stm_rcc;
294
295 #define STM_RCC_CR_RTCPRE       (29)
296 #define  STM_RCC_CR_RTCPRE_HSE_DIV_2    0
297 #define  STM_RCC_CR_RTCPRE_HSE_DIV_4    1
298 #define  STM_RCC_CR_RTCPRE_HSE_DIV_8    2
299 #define  STM_RCC_CR_RTCPRE_HSE_DIV_16   3
300 #define  STM_RCC_CR_RTCPRE_HSE_MASK     3
301
302 #define STM_RCC_CR_CSSON        (28)
303 #define STM_RCC_CR_PLLRDY       (25)
304 #define STM_RCC_CR_PLLON        (24)
305 #define STM_RCC_CR_HSEBYP       (18)
306 #define STM_RCC_CR_HSERDY       (17)
307 #define STM_RCC_CR_HSEON        (16)
308 #define STM_RCC_CR_MSIRDY       (9)
309 #define STM_RCC_CR_MSION        (8)
310 #define STM_RCC_CR_HSIRDY       (1)
311 #define STM_RCC_CR_HSION        (0)
312
313 #define STM_RCC_CFGR_MCOPRE     (28)
314 #define  STM_RCC_CFGR_MCOPRE_DIV_1      0
315 #define  STM_RCC_CFGR_MCOPRE_DIV_2      1
316 #define  STM_RCC_CFGR_MCOPRE_DIV_4      2
317 #define  STM_RCC_CFGR_MCOPRE_DIV_8      3
318 #define  STM_RCC_CFGR_MCOPRE_DIV_16     4
319 #define  STM_RCC_CFGR_MCOPRE_DIV_MASK   7
320
321 #define STM_RCC_CFGR_MCOSEL     (24)
322 #define  STM_RCC_CFGR_MCOSEL_DISABLE    0
323 #define  STM_RCC_CFGR_MCOSEL_SYSCLK     1
324 #define  STM_RCC_CFGR_MCOSEL_HSI        2
325 #define  STM_RCC_CFGR_MCOSEL_MSI        3
326 #define  STM_RCC_CFGR_MCOSEL_HSE        4
327 #define  STM_RCC_CFGR_MCOSEL_PLL        5
328 #define  STM_RCC_CFGR_MCOSEL_LSI        6
329 #define  STM_RCC_CFGR_MCOSEL_LSE        7
330 #define  STM_RCC_CFGR_MCOSEL_MASK       7
331
332 #define STM_RCC_CFGR_PLLDIV     (22)
333 #define  STM_RCC_CFGR_PLLDIV_2          1
334 #define  STM_RCC_CFGR_PLLDIV_3          2
335 #define  STM_RCC_CFGR_PLLDIV_4          3
336 #define  STM_RCC_CFGR_PLLDIV_MASK       3
337
338 #define STM_RCC_CFGR_PLLMUL     (18)
339 #define  STM_RCC_CFGR_PLLMUL_3          0
340 #define  STM_RCC_CFGR_PLLMUL_4          1
341 #define  STM_RCC_CFGR_PLLMUL_6          2
342 #define  STM_RCC_CFGR_PLLMUL_8          3
343 #define  STM_RCC_CFGR_PLLMUL_12         4
344 #define  STM_RCC_CFGR_PLLMUL_16         5
345 #define  STM_RCC_CFGR_PLLMUL_24         6
346 #define  STM_RCC_CFGR_PLLMUL_32         7
347 #define  STM_RCC_CFGR_PLLMUL_48         8
348 #define  STM_RCC_CFGR_PLLMUL_MASK       0xf
349
350 #define STM_RCC_CFGR_PLLSRC     (16)
351
352 #define STM_RCC_CFGR_PPRE2      (11)
353 #define  STM_RCC_CFGR_PPRE2_DIV_1       0
354 #define  STM_RCC_CFGR_PPRE2_DIV_2       4
355 #define  STM_RCC_CFGR_PPRE2_DIV_4       5
356 #define  STM_RCC_CFGR_PPRE2_DIV_8       6
357 #define  STM_RCC_CFGR_PPRE2_DIV_16      7
358 #define  STM_RCC_CFGR_PPRE2_MASK        7
359
360 #define STM_RCC_CFGR_PPRE1      (8)
361 #define  STM_RCC_CFGR_PPRE1_DIV_1       0
362 #define  STM_RCC_CFGR_PPRE1_DIV_2       4
363 #define  STM_RCC_CFGR_PPRE1_DIV_4       5
364 #define  STM_RCC_CFGR_PPRE1_DIV_8       6
365 #define  STM_RCC_CFGR_PPRE1_DIV_16      7
366 #define  STM_RCC_CFGR_PPRE1_MASK        7
367
368 #define STM_RCC_CFGR_HPRE       (4)
369 #define  STM_RCC_CFGR_HPRE_DIV_1        0
370 #define  STM_RCC_CFGR_HPRE_DIV_2        8
371 #define  STM_RCC_CFGR_HPRE_DIV_4        9
372 #define  STM_RCC_CFGR_HPRE_DIV_8        0xa
373 #define  STM_RCC_CFGR_HPRE_DIV_16       0xb
374 #define  STM_RCC_CFGR_HPRE_DIV_64       0xc
375 #define  STM_RCC_CFGR_HPRE_DIV_128      0xd
376 #define  STM_RCC_CFGR_HPRE_DIV_256      0xe
377 #define  STM_RCC_CFGR_HPRE_DIV_512      0xf
378 #define  STM_RCC_CFGR_HPRE_MASK         0xf
379
380 #define STM_RCC_CFGR_SWS        (2)
381 #define  STM_RCC_CFGR_SWS_MSI           0
382 #define  STM_RCC_CFGR_SWS_HSI           1
383 #define  STM_RCC_CFGR_SWS_HSE           2
384 #define  STM_RCC_CFGR_SWS_PLL           3
385 #define  STM_RCC_CFGR_SWS_MASK          3
386
387 #define STM_RCC_CFGR_SW         (0)
388 #define  STM_RCC_CFGR_SW_MSI            0
389 #define  STM_RCC_CFGR_SW_HSI            1
390 #define  STM_RCC_CFGR_SW_HSE            2
391 #define  STM_RCC_CFGR_SW_PLL            3
392 #define  STM_RCC_CFGR_SW_MASK           3
393
394 #define STM_RCC_AHBENR_DMA1EN           (24)
395 #define STM_RCC_AHBENR_FLITFEN          (15)
396 #define STM_RCC_AHBENR_CRCEN            (12)
397 #define STM_RCC_AHBENR_GPIOHEN          (5)
398 #define STM_RCC_AHBENR_GPIOEEN          (4)
399 #define STM_RCC_AHBENR_GPIODEN          (3)
400 #define STM_RCC_AHBENR_GPIOCEN          (2)
401 #define STM_RCC_AHBENR_GPIOBEN          (1)
402 #define STM_RCC_AHBENR_GPIOAEN          (0)
403
404 #define STM_RCC_APB2ENR_USART1EN        (14)
405 #define STM_RCC_APB2ENR_SPI1EN          (12)
406 #define STM_RCC_APB2ENR_ADC1EN          (9)
407 #define STM_RCC_APB2ENR_TIM11EN         (4)
408 #define STM_RCC_APB2ENR_TIM10EN         (3)
409 #define STM_RCC_APB2ENR_TIM9EN          (2)
410 #define STM_RCC_APB2ENR_SYSCFGEN        (0)
411
412 #define STM_RCC_APB1ENR_COMPEN          (31)
413 #define STM_RCC_APB1ENR_DACEN           (29)
414 #define STM_RCC_APB1ENR_PWREN           (28)
415 #define STM_RCC_APB1ENR_USBEN           (23)
416 #define STM_RCC_APB1ENR_I2C2EN          (22)
417 #define STM_RCC_APB1ENR_I2C1EN          (21)
418 #define STM_RCC_APB1ENR_USART3EN        (18)
419 #define STM_RCC_APB1ENR_USART2EN        (17)
420 #define STM_RCC_APB1ENR_SPI2EN          (14)
421 #define STM_RCC_APB1ENR_WWDGEN          (11)
422 #define STM_RCC_APB1ENR_LCDEN           (9)
423 #define STM_RCC_APB1ENR_TIM7EN          (5)
424 #define STM_RCC_APB1ENR_TIM6EN          (4)
425 #define STM_RCC_APB1ENR_TIM4EN          (2)
426 #define STM_RCC_APB1ENR_TIM3EN          (1)
427 #define STM_RCC_APB1ENR_TIM2EN          (0)
428
429 struct stm_pwr {
430         vuint32_t       cr;
431         vuint32_t       csr;
432 };
433
434 extern struct stm_pwr stm_pwr;
435
436 #define STM_PWR_CR_LPRUN        (14)
437
438 #define STM_PWR_CR_VOS          (11)
439 #define  STM_PWR_CR_VOS_1_8             1
440 #define  STM_PWR_CR_VOS_1_5             2
441 #define  STM_PWR_CR_VOS_1_2             3
442 #define  STM_PWR_CR_VOS_MASK            3
443
444 #define STM_PWR_CR_FWU          (10)
445 #define STM_PWR_CR_ULP          (9)
446 #define STM_PWR_CR_DBP          (8)
447
448 #define STM_PWR_CR_PLS          (5)
449 #define  STM_PWR_CR_PLS_1_9     0
450 #define  STM_PWR_CR_PLS_2_1     1
451 #define  STM_PWR_CR_PLS_2_3     2
452 #define  STM_PWR_CR_PLS_2_5     3
453 #define  STM_PWR_CR_PLS_2_7     4
454 #define  STM_PWR_CR_PLS_2_9     5
455 #define  STM_PWR_CR_PLS_3_1     6
456 #define  STM_PWR_CR_PLS_EXT     7
457 #define  STM_PWR_CR_PLS_MASK    7
458
459 #define STM_PWR_CR_PVDE         (4)
460 #define STM_PWR_CR_CSBF         (3)
461 #define STM_PWR_CR_CWUF         (2)
462 #define STM_PWR_CR_PDDS         (1)
463 #define STM_PWR_CR_LPSDSR       (0)
464
465 #define STM_PWR_CSR_EWUP3       (10)
466 #define STM_PWR_CSR_EWUP2       (9)
467 #define STM_PWR_CSR_EWUP1       (8)
468 #define STM_PWR_CSR_REGLPF      (5)
469 #define STM_PWR_CSR_VOSF        (4)
470 #define STM_PWR_CSR_VREFINTRDYF (3)
471 #define STM_PWR_CSR_PVDO        (2)
472 #define STM_PWR_CSR_SBF         (1)
473 #define STM_PWR_CSR_WUF         (0)
474
475 struct stm_tim67 {
476         vuint32_t       cr1;
477         vuint32_t       cr2;
478         uint32_t        _unused_08;
479         vuint32_t       dier;
480
481         vuint32_t       sr;
482         vuint32_t       egr;
483         uint32_t        _unused_18;
484         uint32_t        _unused_1c;
485
486         uint32_t        _unused_20;
487         vuint32_t       cnt;
488         vuint32_t       psc;
489         vuint32_t       arr;
490 };
491
492 extern struct stm_tim67 stm_tim6;
493
494 #define STM_TIM67_CR1_ARPE      (7)
495 #define STM_TIM67_CR1_OPM       (3)
496 #define STM_TIM67_CR1_URS       (2)
497 #define STM_TIM67_CR1_UDIS      (1)
498 #define STM_TIM67_CR1_CEN       (0)
499
500 #define STM_TIM67_CR2_MMS       (4)
501 #define  STM_TIM67_CR2_MMS_RESET        0
502 #define  STM_TIM67_CR2_MMS_ENABLE       1
503 #define  STM_TIM67_CR2_MMS_UPDATE       2
504 #define  STM_TIM67_CR2_MMS_MASK         7
505
506 #define STM_TIM67_DIER_UDE      (8)
507 #define STM_TIM67_DIER_UIE      (0)
508
509 #define STM_TIM67_SR_UIF        (0)
510
511 #define STM_TIM67_EGR_UG        (0)
512
513 struct stm_nvic {
514         vuint32_t       iser[3];        /* 0x000 */
515
516         uint8_t         _unused00c[0x080 - 0x00c];
517
518         vuint32_t       icer[3];        /* 0x080 */
519
520         uint8_t         _unused08c[0x100 - 0x08c];
521
522         vuint32_t       ispr[3];        /* 0x100 */
523
524         uint8_t         _unused10c[0x180 - 0x10c];
525
526         vuint32_t       icpr[3];        /* 0x180 */
527
528         uint8_t         _unused18c[0x200 - 0x18c];
529
530         vuint32_t       iabr[3];        /* 0x200 */
531
532         uint8_t         _unused20c[0x300 - 0x20c];
533
534         vuint32_t       ipr[21];        /* 0x300 */
535
536         uint8_t         _unused324[0xe00 - 0x324];
537
538         vuint32_t       stir;           /* 0xe00 */
539 };
540
541 extern struct stm_nvic stm_nvic;
542
543 #define IRQ_REG(irq)    ((irq) >> 5)
544 #define IRQ_BIT(irq)    ((irq) & 0x1f)
545 #define IRQ_MASK(irq)   (1 << IRQ_BIT(irq))
546 #define IRQ_BOOL(v,irq) (((v) >> IRQ_BIT(irq)) & 1)
547
548 static inline void
549 stm_nvic_set_enable(int irq) {
550         stm_nvic.iser[IRQ_REG(irq)] = IRQ_MASK(irq);
551 }
552
553 static inline void
554 stm_nvic_clear_enable(int irq) {
555         stm_nvic.icer[IRQ_REG(irq)] = IRQ_MASK(irq);
556 }
557
558 static inline int
559 stm_nvic_enabled(int irq) {
560         return IRQ_BOOL(stm_nvic.iser[IRQ_REG(irq)], irq);
561 }
562         
563 static inline void
564 stm_nvic_set_pending(int irq) {
565         stm_nvic.ispr[IRQ_REG(irq)] = IRQ_MASK(irq);
566 }
567
568 static inline void
569 stm_nvic_clear_pending(int irq) {
570         stm_nvic.icpr[IRQ_REG(irq)] = IRQ_MASK(irq);
571 }
572
573 static inline int
574 stm_nvic_pending(int irq) {
575         return IRQ_BOOL(stm_nvic.ispr[IRQ_REG(irq)], irq);
576 }
577
578 static inline int
579 stm_nvic_active(int irq) {
580         return IRQ_BOOL(stm_nvic.iabr[IRQ_REG(irq)], irq);
581 }
582
583 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
584 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
585 #define IRQ_PRIO_MASK(irq)      (0xff << IRQ_PRIO_BIT(irq))
586
587 static inline void
588 stm_nvic_set_priority(int irq, uint8_t prio) {
589         int             n = IRQ_PRIO_REG(irq);
590         uint32_t        v;
591
592         v = stm_nvic.ipr[n];
593         v &= ~IRQ_PRIO_MASK(irq);
594         v |= (prio) << IRQ_PRIO_BIT(irq);
595         stm_nvic.ipr[n] = v;
596 }
597
598 static inline uint8_t
599 stm_nvic_get_priority(int irq) {
600         return (stm_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
601 }
602
603 #define isr(name) void stm_ ## name ## _isr(void);
604
605 isr(nmi)
606 isr(hardfault)
607 isr(memmanage)
608 isr(busfault)
609 isr(usagefault)
610 isr(svc)
611 isr(debugmon)
612 isr(pendsv)
613 isr(systick)
614 isr(wwdg)
615 isr(pvd)
616 isr(tamper_stamp)
617 isr(rtc_wkup)
618 isr(flash)
619 isr(rcc)
620 isr(exti0)
621 isr(exti1)
622 isr(exti2)
623 isr(exti3)
624 isr(exti4)
625 isr(dma1_channel1)
626 isr(dma1_channel2)
627 isr(dma1_channel3)
628 isr(dma1_channel4)
629 isr(dma1_channel5)
630 isr(dma1_channel6)
631 isr(dma1_channel7)
632 isr(adc1)
633 isr(usb_hp)
634 isr(usb_lp)
635 isr(dac)
636 isr(comp)
637 isr(exti9_5)
638 isr(lcd)
639 isr(tim9)
640 isr(tim10)
641 isr(tim11)
642 isr(tim2)
643 isr(tim3)
644 isr(tim4)
645 isr(i2c1_ev)
646 isr(i2c1_er)
647 isr(i2c2_ev)
648 isr(i2c2_er)
649 isr(spi1)
650 isr(spi2)
651 isr(usart1)
652 isr(usart2)
653 isr(usart3)
654 isr(exti15_10)
655 isr(rtc_alarm)
656 isr(usb_fs_wkup)
657 isr(tim6)
658 isr(tim7)
659
660 #undef isr
661
662 #define STM_ISR_WWDG_POS                0
663 #define STM_ISR_PVD_POS                 1
664 #define STM_ISR_TAMPER_STAMP_POS        2
665 #define STM_ISR_RTC_WKUP_POS            3
666 #define STM_ISR_FLASH_POS               4
667 #define STM_ISR_RCC_POS                 5
668 #define STM_ISR_EXTI0_POS               6
669 #define STM_ISR_EXTI1_POS               7
670 #define STM_ISR_EXTI2_POS               8
671 #define STM_ISR_EXTI3_POS               9
672 #define STM_ISR_EXTI4_POS               10
673 #define STM_ISR_DMA1_CHANNEL1_POS       11
674 #define STM_ISR_DMA2_CHANNEL1_POS       12
675 #define STM_ISR_DMA3_CHANNEL1_POS       13
676 #define STM_ISR_DMA4_CHANNEL1_POS       14
677 #define STM_ISR_DMA5_CHANNEL1_POS       15
678 #define STM_ISR_DMA6_CHANNEL1_POS       16
679 #define STM_ISR_DMA7_CHANNEL1_POS       17
680 #define STM_ISR_ADC1_POS                18
681 #define STM_ISR_USB_HP_POS              19
682 #define STM_ISR_USB_LP_POS              20
683 #define STM_ISR_DAC_POS                 21
684 #define STM_ISR_COMP_POS                22
685 #define STM_ISR_EXTI9_5_POS             23
686 #define STM_ISR_LCD_POS                 24
687 #define STM_ISR_TIM9_POS                25
688 #define STM_ISR_TIM10_POS               26
689 #define STM_ISR_TIM11_POS               27
690 #define STM_ISR_TIM2_POS                28
691 #define STM_ISR_TIM3_POS                29
692 #define STM_ISR_TIM4_POS                30
693 #define STM_ISR_I2C1_EV_POS             31
694 #define STM_ISR_I2C1_ER_POS             32
695 #define STM_ISR_I2C2_EV_POS             33
696 #define STM_ISR_I2C2_ER_POS             34
697 #define STM_ISR_SPI1_POS                35
698 #define STM_ISR_SPI2_POS                36
699 #define STM_ISR_USART1_POS              37
700 #define STM_ISR_USART2_POS              38
701 #define STM_ISR_USART3_POS              39
702 #define STM_ISR_EXTI15_10_POS           40
703 #define STM_ISR_RTC_ALARM_POS           41
704 #define STM_ISR_USB_FS_WKUP_POS         42
705 #define STM_ISR_TIM6_POS                43
706 #define STM_ISR_TIM7_POS                44
707
708 #endif /* _STM32L_H_ */