90f070ce4b68532b23f4b0077626964e07c7d9fe
[fw/altos] / src / stm32l0 / stm32l0.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; 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 _STM32L0_H_
20 #define _STM32L0_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_gpio {
29         vuint32_t       moder;
30         vuint32_t       otyper;
31         vuint32_t       ospeedr;
32         vuint32_t       pupdr;
33
34         vuint32_t       idr;
35         vuint32_t       odr;
36         vuint32_t       bsrr;
37         vuint32_t       lckr;
38
39         vuint32_t       afrl;
40         vuint32_t       afrh;
41         vuint32_t       brr;
42 };
43
44 #define STM_MODER_SHIFT(pin)            ((pin) << 1)
45 #define STM_MODER_MASK                  3
46 #define STM_MODER_INPUT                 0
47 #define STM_MODER_OUTPUT                1
48 #define STM_MODER_ALTERNATE             2
49 #define STM_MODER_ANALOG                3
50
51 static inline void
52 stm_moder_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
53         gpio->moder = ((gpio->moder &
54                         ~(STM_MODER_MASK << STM_MODER_SHIFT(pin))) |
55                        value << STM_MODER_SHIFT(pin));
56 }
57
58 static inline uint32_t
59 stm_spread_mask(uint16_t mask) {
60         uint32_t m = mask;
61
62         /* 0000000000000000mmmmmmmmmmmmmmmm */
63         m = (m & 0xff) | ((m & 0xff00) << 8);
64         /* 00000000mmmmmmmm00000000mmmmmmmm */
65         m = (m & 0x000f000f) | ((m & 0x00f000f0) << 4);
66         /* 0000mmmm0000mmmm0000mmmm0000mmmm */
67         m = (m & 0x03030303) | ((m & 0x0c0c0c0c) << 2);
68         /* 00mm00mm00mm00mm00mm00mm00mm00mm */
69         m = (m & 0x11111111) | ((m & 0x22222222) << 2);
70         /* 0m0m0m0m0m0m0m0m0m0m0m0m0m0m0m0m */
71         return m;
72 }
73
74 static inline void
75 stm_moder_set_mask(struct stm_gpio *gpio, uint16_t mask, uint32_t value) {
76         uint32_t        bits32 = stm_spread_mask(mask);
77         uint32_t        mask32 = 3 * bits32;
78         uint32_t        value32 = (value & 3) * bits32;
79
80         gpio->moder = ((gpio->moder & ~mask32) | value32);
81 }
82
83 static inline uint32_t
84 stm_moder_get(struct stm_gpio *gpio, int pin) {
85         return (gpio->moder >> STM_MODER_SHIFT(pin)) & STM_MODER_MASK;
86 }
87
88 #define STM_OTYPER_SHIFT(pin)           (pin)
89 #define STM_OTYPER_MASK                 1
90 #define STM_OTYPER_PUSH_PULL            0
91 #define STM_OTYPER_OPEN_DRAIN           1
92
93 static inline void
94 stm_otyper_set(struct stm_gpio *gpio, int pin, vuint32_t value) {
95         gpio->otyper = ((gpio->otyper &
96                          ~(STM_OTYPER_MASK << STM_OTYPER_SHIFT(pin))) |
97                         value << STM_OTYPER_SHIFT(pin));
98 }
99
100 static inline uint32_t
101 stm_otyper_get(struct stm_gpio *gpio, int pin) {
102         return (gpio->otyper >> STM_OTYPER_SHIFT(pin)) & STM_OTYPER_MASK;
103 }
104
105 #define STM_OSPEEDR_SHIFT(pin)          ((pin) << 1)
106 #define STM_OSPEEDR_MASK                3
107 #define STM_OSPEEDR_LOW                 0
108 #define STM_OSPEEDR_MEDIUM              1
109 #define STM_OSPEEDR_HIGH                2
110 #define STM_OSPEEDR_VERY_HIGH           3
111
112 static inline void
113 stm_ospeedr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
114         gpio->ospeedr = ((gpio->ospeedr &
115                         ~(STM_OSPEEDR_MASK << STM_OSPEEDR_SHIFT(pin))) |
116                        value << STM_OSPEEDR_SHIFT(pin));
117 }
118
119 static inline void
120 stm_ospeedr_set_mask(struct stm_gpio *gpio, uint16_t mask, uint32_t value) {
121         uint32_t        bits32 = stm_spread_mask(mask);
122         uint32_t        mask32 = 3 * bits32;
123         uint32_t        value32 = (value & 3) * bits32;
124
125         gpio->ospeedr = ((gpio->ospeedr & ~mask32) | value32);
126 }
127
128 static inline uint32_t
129 stm_ospeedr_get(struct stm_gpio *gpio, int pin) {
130         return (gpio->ospeedr >> STM_OSPEEDR_SHIFT(pin)) & STM_OSPEEDR_MASK;
131 }
132
133 #define STM_PUPDR_SHIFT(pin)            ((pin) << 1)
134 #define STM_PUPDR_MASK                  3
135 #define STM_PUPDR_NONE                  0
136 #define STM_PUPDR_PULL_UP               1
137 #define STM_PUPDR_PULL_DOWN             2
138 #define STM_PUPDR_RESERVED              3
139
140 static inline void
141 stm_pupdr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
142         gpio->pupdr = ((gpio->pupdr &
143                         ~(STM_PUPDR_MASK << STM_PUPDR_SHIFT(pin))) |
144                        value << STM_PUPDR_SHIFT(pin));
145 }
146
147 static inline void
148 stm_pupdr_set_mask(struct stm_gpio *gpio, uint16_t mask, uint32_t value) {
149         uint32_t        bits32 = stm_spread_mask(mask);
150         uint32_t        mask32 = 3 * bits32;
151         uint32_t        value32 = (value & 3) * bits32;
152
153         gpio->pupdr = (gpio->pupdr & ~mask32) | value32;
154 }
155
156 static inline uint32_t
157 stm_pupdr_get(struct stm_gpio *gpio, int pin) {
158         return (gpio->pupdr >> STM_PUPDR_SHIFT(pin)) & STM_PUPDR_MASK;
159 }
160
161 #define STM_AFR_SHIFT(pin)              ((pin) << 2)
162 #define STM_AFR_MASK                    0xf
163 #define STM_AFR_AF0                     0x0
164 #define STM_AFR_AF1                     0x1
165 #define STM_AFR_AF2                     0x2
166 #define STM_AFR_AF3                     0x3
167 #define STM_AFR_AF4                     0x4
168 #define STM_AFR_AF5                     0x5
169 #define STM_AFR_AF6                     0x6
170 #define STM_AFR_AF7                     0x7
171 #define STM_AFR_AF8                     0x8
172 #define STM_AFR_AF9                     0x9
173 #define STM_AFR_AF10                    0xa
174 #define STM_AFR_AF11                    0xb
175 #define STM_AFR_AF12                    0xc
176 #define STM_AFR_AF13                    0xd
177 #define STM_AFR_AF14                    0xe
178 #define STM_AFR_AF15                    0xf
179
180 static inline void
181 stm_afr_set(struct stm_gpio *gpio, int pin, uint32_t value) {
182         /*
183          * Set alternate pin mode too
184          */
185         stm_moder_set(gpio, pin, STM_MODER_ALTERNATE);
186         if (pin < 8)
187                 gpio->afrl = ((gpio->afrl &
188                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
189                               value << STM_AFR_SHIFT(pin));
190         else {
191                 pin -= 8;
192                 gpio->afrh = ((gpio->afrh &
193                                ~(STM_AFR_MASK << STM_AFR_SHIFT(pin))) |
194                               value << STM_AFR_SHIFT(pin));
195         }
196 }
197         
198 static inline uint32_t
199 stm_afr_get(struct stm_gpio *gpio, int pin) {
200         if (pin < 8)
201                 return (gpio->afrl >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
202         else {
203                 pin -= 8;
204                 return (gpio->afrh >> STM_AFR_SHIFT(pin)) & STM_AFR_MASK;
205         }
206 }
207
208 static inline void
209 stm_gpio_set(struct stm_gpio *gpio, int pin, uint8_t value) {
210         /* Use the bit set/reset register to do this atomically */
211         gpio->bsrr = value ? (1 << pin) : (1 << (pin + 16));
212 }
213
214 static inline void
215 stm_gpio_set_mask(struct stm_gpio *gpio, uint16_t bits, uint16_t mask) {
216         /* Use the bit set/reset register to do this atomically */
217         gpio->bsrr = ((uint32_t) (~bits & mask) << 16) | ((uint32_t) (bits & mask));
218 }
219
220 static inline void
221 stm_gpio_set_bits(struct stm_gpio *gpio, uint16_t bits) {
222         gpio->bsrr = bits;
223 }
224
225 static inline void
226 stm_gpio_clr_bits(struct stm_gpio *gpio, uint16_t bits) {
227         gpio->bsrr = ((uint32_t) bits) << 16;
228 }
229
230 static inline uint8_t
231 stm_gpio_get(struct stm_gpio *gpio, int pin) {
232         return (gpio->idr >> pin) & 1;
233 }
234
235 static inline uint16_t
236 stm_gpio_get_all(struct stm_gpio *gpio) {
237         return gpio->idr;
238 }
239
240 /*
241  * We can't define these in registers.ld or our fancy
242  * ao_enable_gpio macro will expand into a huge pile of code
243  * as the compiler won't do correct constant folding and
244  * dead-code elimination
245  */
246
247 extern struct stm_gpio stm_gpioa;
248 extern struct stm_gpio stm_gpiob;
249 extern struct stm_gpio stm_gpioc;
250 extern struct stm_gpio stm_gpiod;
251 extern struct stm_gpio stm_gpioe;
252 extern struct stm_gpio stm_gpioh;
253
254 #define stm_gpiob  (*((struct stm_gpio *) 0x50000400))
255 #define stm_gpioa  (*((struct stm_gpio *) 0x50000000))
256
257 struct stm_usart {
258         vuint32_t       cr1;    /* control register 1 */
259         vuint32_t       cr2;    /* control register 2 */
260         vuint32_t       cr3;    /* control register 3 */
261         vuint32_t       brr;    /* baud rate register */
262
263         vuint32_t       gtpr;   /* guard time and prescaler */
264         vuint32_t       rtor;   /* receiver timeout register */
265         vuint32_t       rqr;    /* request register */
266         vuint32_t       isr;    /* interrupt and status register */
267
268         vuint32_t       icr;    /* interrupt flag clear register */
269         vuint32_t       rdr;    /* receive data register */
270         vuint32_t       tdr;    /* transmit data register */
271 };
272
273 #define STM_USART_CR1_M1        28
274 #define STM_USART_CR1_EOBIE     27
275 #define STM_USART_CR1_RTOIE     26
276 #define STM_USART_CR1_DEAT      21
277 #define STM_USART_CR1_DEDT      16
278 #define STM_USART_CR1_OVER8     15
279 #define STM_USART_CR1_CMIE      14
280 #define STM_USART_CR1_MME       13
281 #define STM_USART_CR1_M0        12
282 #define STM_USART_CR1_WAKE      11
283 #define STM_USART_CR1_PCE       10
284 #define STM_USART_CR1_PS        9
285 #define STM_USART_CR1_PEIE      8
286 #define STM_USART_CR1_TXEIE     7
287 #define STM_USART_CR1_TCIE      6
288 #define STM_USART_CR1_RXNEIE    5
289 #define STM_USART_CR1_IDLEIE    4
290 #define STM_USART_CR1_TE        3
291 #define STM_USART_CR1_RE        2
292 #define STM_USART_CR1_UESM      1
293 #define STM_USART_CR1_UE        0
294
295 #define STM_USART_CR2_ADD       24
296 #define STM_USART_CR2_RTOEN     23
297 #define STM_USART_CR2_ABRMOD    21
298 #define STM_USART_CR2_ABREN     20
299 #define STM_USART_CR2_MSBFIRST  19
300 #define STM_USART_CR2_DATAINV   18
301 #define STM_USART_CR2_TXINV     17
302 #define STM_USART_CR2_RXINV     16
303 #define STM_USART_CR2_SWAP      15
304 #define STM_USART_CR2_LINEN     14
305 #define STM_USART_CR2_STOP      12
306 #define STM_USART_CR2_CLKEN     11
307 #define STM_USART_CR2_CPOL      10
308 #define STM_USART_CR2_CHPA      9
309 #define STM_USART_CR2_LBCL      8
310 #define STM_USART_CR2_LBDIE     6
311 #define STM_USART_CR2_LBDL      5
312 #define STM_USART_CR2_ADDM7     4
313
314 #define STM_USART_CR3_WUFIE     22
315 #define STM_USART_CR3_WUS       20
316 #define STM_USART_CR3_SCARCNT   17
317 #define STM_USART_CR3_DEP       15
318 #define STM_USART_CR3_DEM       14
319 #define STM_USART_CR3_DDRE      13
320 #define STM_USART_CR3_OVRDIS    12
321 #define STM_USART_CR3_ONEBIT    11
322 #define STM_USART_CR3_CTIIE     10
323 #define STM_USART_CR3_CTSE      9
324 #define STM_USART_CR3_RTSE      8
325 #define STM_USART_CR3_DMAT      7
326 #define STM_USART_CR3_DMAR      6
327 #define STM_USART_CR3_SCEN      5
328 #define STM_USART_CR3_NACK      4
329 #define STM_USART_CR3_HDSEL     3
330 #define STM_USART_CR3_IRLP      2
331 #define STM_USART_CR3_IREN      1
332 #define STM_USART_CR3_EIE       0
333
334 #define STM_USART_GTPR_GT       8
335 #define STM_USART_GTPR_PSC      0
336
337 #define STM_USART_RQR_TXFRQ     4
338 #define STM_USART_RQR_RXFRQ     3
339 #define STM_USART_RQR_MMRQ      2
340 #define STM_USART_RQR_SBKRQ     1
341 #define STM_USART_RQR_ABRRQ     0
342
343 #define STM_USART_ISR_REACK     22
344 #define STM_USART_ISR_TEACK     21
345 #define STM_USART_ISR_WUF       20
346 #define STM_USART_ISR_RWU       19
347 #define STM_USART_ISR_SBKF      18
348 #define STM_USART_ISR_CMF       17
349 #define STM_USART_ISR_BUSY      16
350 #define STM_USART_ISR_ABRF      15
351 #define STM_USART_ISR_ABRE      14
352 #define STM_USART_ISR_EOBF      12
353 #define STM_USART_ISR_RTOF      11
354 #define STM_USART_ISR_CTS       10
355 #define STM_USART_ISR_CTSIF     9
356 #define STM_USART_ISR_LBDF      8
357 #define STM_USART_ISR_TXE       7
358 #define STM_USART_ISR_TC        6
359 #define STM_USART_ISR_RXNE      5
360 #define STM_USART_ISR_IDLE      4
361 #define STM_USART_ISR_ORE       3
362 #define STM_USART_ISR_NF        2
363 #define STM_USART_ISR_FE        1
364 #define STM_USART_ISR_PE        0
365
366 #define STM_USART_ICR_WUCF      20
367 #define STM_USART_ICR_CMCF      17
368 #define STM_USART_ICR_EOBCF     12
369 #define STM_USART_ICR_RTOCF     11
370 #define STM_USART_ICR_CTSCF     9
371 #define STM_USART_ICR_LBDCF     8
372 #define STM_USART_ICR_TCCF      6
373 #define STM_USART_ICR_IDLECF    4
374 #define STM_USART_ICR_ORECF     3
375 #define STM_USART_ICR_NCF       2
376 #define STM_USART_ICR_FECF      1
377 #define STM_USART_ICR_PECF      0
378
379 extern struct stm_usart stm_usart1;
380 extern struct stm_usart stm_usart2;
381 #define stm_usart1 (*((struct stm_usart *) 0x40013800))
382 #define stm_usart2 (*((struct stm_usart *) 0x40004400))
383
384 struct stm_lpuart {
385         vuint32_t       cr1;
386         vuint32_t       cr2;
387         vuint32_t       cr3;
388         vuint32_t       brr;
389
390         uint32_t        unused_10;
391         uint32_t        unused_14;
392         vuint32_t       rqr;
393         vuint32_t       isr;
394
395         vuint32_t       icr;
396         vuint32_t       rdr;
397         vuint32_t       tdr;
398 };
399 extern struct stm_lpuart stm_lpuart1;
400
401 #define stm_lpuart1 (*((struct stm_lpuart *) 0x40004800))
402
403 #define STM_LPUART_CR1_M1       28
404 #define STM_LPUART_CR1_DEAT     21
405 #define STM_LPUART_CR1_DEDT     16
406 #define STM_LPUART_CR1_CMIE     14
407 #define STM_LPUART_CR1_MME      13
408 #define STM_LPUART_CR1_M0       12
409 #define STM_LPUART_CR1_WAKE     11
410 #define STM_LPUART_CR1_PCE      10
411 #define STM_LPUART_CR1_PS       9
412 #define STM_LPUART_CR1_PEIE     8
413 #define STM_LPUART_CR1_TXEIE    7
414 #define STM_LPUART_CR1_TCIE     6
415 #define STM_LPUART_CR1_RXNEIE   5
416 #define STM_LPUART_CR1_IDLEIE   4
417 #define STM_LPUART_CR1_TE       3
418 #define STM_LPUART_CR1_RE       2
419 #define STM_LPUART_CR1_UESM     1
420 #define STM_LPUART_CR1_UE       0
421
422 #define STM_LPUART_CR2_ADD      24
423 #define STM_LPUART_CR2_MSBFIRST 19
424 #define STM_LPUART_CR2_DATAINV  18
425 #define STM_LPUART_CR2_TXINV    17
426 #define STM_LPUART_CR2_RXINV    16
427 #define STM_LPUART_CR2_SWAP     15
428 #define STM_LPUART_CR2_STOP     12
429 #define STM_LPUART_CR2_ADDM7    4
430
431 #define STM_LPUART_CR3_UCESM    23
432 #define STM_LPUART_CR3_WUFIE    22
433 #define STM_LPUART_CR3_WUS      20
434 #define STM_LPUART_CR3_DEP      15
435 #define STM_LPUART_CR3_DEM      14
436 #define STM_LPUART_CR3_DDRE     13
437 #define STM_LPUART_CR3_OVRDIS   12
438 #define STM_LPUART_CR3_CTSIE    10
439 #define STM_LPUART_CR3_CTSE     9
440 #define STM_LPUART_CR3_RTSE     8
441 #define STM_LPUART_CR3_DMAT     7
442 #define STM_LPUART_CR3_DMAR     6
443 #define STM_LPUART_CR3_HDSEL    3
444 #define STM_LPUART_CR3_EIE      0
445
446 #define STM_LPUART_RQR_RXFRQ    3
447 #define STM_LPUART_RQR_MMRQ     2
448 #define STM_LPUART_RQR_SBKRQ    1
449
450 #define STM_LPUART_ISR_REACK    22
451 #define STM_LPUART_ISR_TEACK    21
452 #define STM_LPUART_ISR_WUF      20
453 #define STM_LPUART_ISR_RWU      19
454 #define STM_LPUART_ISR_SBKF     18
455 #define STM_LPUART_ISR_CMF      17
456 #define STM_LPUART_ISR_BUSY     16
457 #define STM_LPUART_ISR_CTS      10
458 #define STM_LPUART_ISR_CTSIF    9
459 #define STM_LPUART_ISR_TXE      7
460 #define STM_LPUART_ISR_TC       6
461 #define STM_LPUART_ISR_RXNE     5
462 #define STM_LPUART_ISR_IDLE     4
463 #define STM_LPUART_ISR_ORE      3
464 #define STM_LPUART_ISR_NF       2
465 #define STM_LPUART_ISR_FE       1
466 #define STM_LPUART_ISR_PE       1
467
468 #define STM_LPUART_ICR_WUCF     20
469 #define STM_LPUART_ICR_CMCF     17
470 #define STM_LPUART_ICR_CTSCF    9
471 #define STM_LPUART_ICR_TCCF     6
472 #define STM_LPUART_ICR_IDLECF   4
473 #define STM_LPUART_ICR_ORECF    3
474 #define STM_LPUART_ICR_NCF      2
475 #define STM_LPUART_ICR_FECF     1
476 #define STM_LPUART_ICR_PECF     0
477
478 struct stm_tim {
479 };
480
481 extern struct stm_tim stm_tim9;
482
483 struct stm_tim1011 {
484         vuint32_t       cr1;
485         uint32_t        unused_4;
486         vuint32_t       smcr;
487         vuint32_t       dier;
488         vuint32_t       sr;
489         vuint32_t       egr;
490         vuint32_t       ccmr1;
491         uint32_t        unused_1c;
492         vuint32_t       ccer;
493         vuint32_t       cnt;
494         vuint32_t       psc;
495         vuint32_t       arr;
496         uint32_t        unused_30;
497         vuint32_t       ccr1;
498         uint32_t        unused_38;
499         uint32_t        unused_3c;
500         uint32_t        unused_40;
501         uint32_t        unused_44;
502         uint32_t        unused_48;
503         uint32_t        unused_4c;
504         vuint32_t       or;
505 };
506
507 extern struct stm_tim1011 stm_tim10;
508 extern struct stm_tim1011 stm_tim11;
509
510 #define STM_TIM1011_CR1_CKD     8
511 #define  STM_TIM1011_CR1_CKD_1          0
512 #define  STM_TIM1011_CR1_CKD_2          1
513 #define  STM_TIM1011_CR1_CKD_4          2
514 #define  STM_TIM1011_CR1_CKD_MASK       3
515 #define STM_TIM1011_CR1_ARPE    7
516 #define STM_TIM1011_CR1_URS     2
517 #define STM_TIM1011_CR1_UDIS    1
518 #define STM_TIM1011_CR1_CEN     0
519
520 #define STM_TIM1011_SMCR_ETP    15
521 #define STM_TIM1011_SMCR_ECE    14
522 #define STM_TIM1011_SMCR_ETPS   12
523 #define  STM_TIM1011_SMCR_ETPS_OFF      0
524 #define  STM_TIM1011_SMCR_ETPS_2        1
525 #define  STM_TIM1011_SMCR_ETPS_4        2
526 #define  STM_TIM1011_SMCR_ETPS_8        3
527 #define  STM_TIM1011_SMCR_ETPS_MASK     3
528 #define STM_TIM1011_SMCR_ETF    8
529 #define  STM_TIM1011_SMCR_ETF_NONE              0
530 #define  STM_TIM1011_SMCR_ETF_CK_INT_2          1
531 #define  STM_TIM1011_SMCR_ETF_CK_INT_4          2
532 #define  STM_TIM1011_SMCR_ETF_CK_INT_8          3
533 #define  STM_TIM1011_SMCR_ETF_DTS_2_6           4
534 #define  STM_TIM1011_SMCR_ETF_DTS_2_8           5
535 #define  STM_TIM1011_SMCR_ETF_DTS_4_6           6
536 #define  STM_TIM1011_SMCR_ETF_DTS_4_8           7
537 #define  STM_TIM1011_SMCR_ETF_DTS_8_6           8
538 #define  STM_TIM1011_SMCR_ETF_DTS_8_8           9
539 #define  STM_TIM1011_SMCR_ETF_DTS_16_5          10
540 #define  STM_TIM1011_SMCR_ETF_DTS_16_6          11
541 #define  STM_TIM1011_SMCR_ETF_DTS_16_8          12
542 #define  STM_TIM1011_SMCR_ETF_DTS_32_5          13
543 #define  STM_TIM1011_SMCR_ETF_DTS_32_6          14
544 #define  STM_TIM1011_SMCR_ETF_DTS_32_8          15
545 #define  STM_TIM1011_SMCR_ETF_MASK              15
546
547 #define STM_TIM1011_DIER_CC1E   1
548 #define STM_TIM1011_DIER_UIE    0
549
550 #define STM_TIM1011_SR_CC1OF    9
551 #define STM_TIM1011_SR_CC1IF    1
552 #define STM_TIM1011_SR_UIF      0
553
554 #define STM_TIM1011_EGR_CC1G    1
555 #define STM_TIM1011_EGR_UG      0
556
557 #define STM_TIM1011_CCMR1_OC1CE 7
558 #define STM_TIM1011_CCMR1_OC1M  4
559 #define  STM_TIM1011_CCMR1_OC1M_FROZEN                  0
560 #define  STM_TIM1011_CCMR1_OC1M_SET_1_ACTIVE_ON_MATCH   1
561 #define  STM_TIM1011_CCMR1_OC1M_SET_1_INACTIVE_ON_MATCH 2
562 #define  STM_TIM1011_CCMR1_OC1M_TOGGLE                  3
563 #define  STM_TIM1011_CCMR1_OC1M_FORCE_INACTIVE          4
564 #define  STM_TIM1011_CCMR1_OC1M_FORCE_ACTIVE            5
565 #define  STM_TIM1011_CCMR1_OC1M_PWM_MODE_1              6
566 #define  STM_TIM1011_CCMR1_OC1M_PWM_MODE_2              7
567 #define  STM_TIM1011_CCMR1_OC1M_MASK                    7
568 #define STM_TIM1011_CCMR1_OC1PE 3
569 #define STM_TIM1011_CCMR1_OC1FE 2
570 #define STM_TIM1011_CCMR1_CC1S  0
571 #define  STM_TIM1011_CCMR1_CC1S_OUTPUT                  0
572 #define  STM_TIM1011_CCMR1_CC1S_INPUT_TI1               1
573 #define  STM_TIM1011_CCMR1_CC1S_INPUT_TI2               2
574 #define  STM_TIM1011_CCMR1_CC1S_INPUT_TRC               3
575 #define  STM_TIM1011_CCMR1_CC1S_MASK                    3
576
577 #define  STM_TIM1011_CCMR1_IC1F_NONE            0
578 #define  STM_TIM1011_CCMR1_IC1F_CK_INT_2        1
579 #define  STM_TIM1011_CCMR1_IC1F_CK_INT_4        2
580 #define  STM_TIM1011_CCMR1_IC1F_CK_INT_8        3
581 #define  STM_TIM1011_CCMR1_IC1F_DTS_2_6         4
582 #define  STM_TIM1011_CCMR1_IC1F_DTS_2_8         5
583 #define  STM_TIM1011_CCMR1_IC1F_DTS_4_6         6
584 #define  STM_TIM1011_CCMR1_IC1F_DTS_4_8         7
585 #define  STM_TIM1011_CCMR1_IC1F_DTS_8_6         8
586 #define  STM_TIM1011_CCMR1_IC1F_DTS_8_8         9
587 #define  STM_TIM1011_CCMR1_IC1F_DTS_16_5        10
588 #define  STM_TIM1011_CCMR1_IC1F_DTS_16_6        11
589 #define  STM_TIM1011_CCMR1_IC1F_DTS_16_8        12
590 #define  STM_TIM1011_CCMR1_IC1F_DTS_32_5        13
591 #define  STM_TIM1011_CCMR1_IC1F_DTS_32_6        14
592 #define  STM_TIM1011_CCMR1_IC1F_DTS_32_8        15
593 #define  STM_TIM1011_CCMR1_IC1F_MASK            15
594 #define STM_TIM1011_CCMR1_IC1PSC        2
595 #define  STM_TIM1011_CCMR1_IC1PSC_1             0
596 #define  STM_TIM1011_CCMR1_IC1PSC_2             1
597 #define  STM_TIM1011_CCMR1_IC1PSC_4             2
598 #define  STM_TIM1011_CCMR1_IC1PSC_8             3
599 #define  STM_TIM1011_CCMR1_IC1PSC_MASK          3
600 #define STM_TIM1011_CCMR1_CC1S          0
601
602 #define STM_TIM1011_CCER_CC1NP          3
603 #define STM_TIM1011_CCER_CC1P           1
604 #define STM_TIM1011_CCER_CC1E           0
605
606 #define STM_TIM1011_OR_TI1_RMP_RI       3
607 #define STM_TIM1011_ETR_RMP             2
608 #define STM_TIM1011_TI1_RMP             0
609 #define  STM_TIM1011_TI1_RMP_GPIO               0
610 #define  STM_TIM1011_TI1_RMP_LSI                1
611 #define  STM_TIM1011_TI1_RMP_LSE                2
612 #define  STM_TIM1011_TI1_RMP_RTC                3
613 #define  STM_TIM1011_TI1_RMP_MASK               3
614
615 struct stm_rcc {
616         vuint32_t       cr;
617         vuint32_t       icscr;
618         uint32_t        unused08;
619         vuint32_t       cfgr;
620
621         vuint32_t       cier;
622         vuint32_t       cifr;
623         vuint32_t       cicr;
624         vuint32_t       iopstr;
625
626         vuint32_t       ahbrstr;
627         vuint32_t       apb2rstr;
628         vuint32_t       apb1rstr;
629         vuint32_t       iopenr;
630
631         vuint32_t       ahbenr;
632         vuint32_t       apb2enr;
633         vuint32_t       apb1enr;
634         vuint32_t       iopsmen;
635
636         vuint32_t       ahbsmenr;
637         vuint32_t       apb2smenr;
638         vuint32_t       apb1smenr;
639         vuint32_t       ccipr;
640
641         vuint32_t       csr;
642 };
643
644 extern struct stm_rcc stm_rcc;
645
646 /* Nominal high speed internal oscillator frequency is 16MHz */
647 #define STM_HSI_FREQ            16000000
648 #define STM_MSI_FREQ_65536      65536
649 #define STM_MSI_FREQ_131072     131072
650 #define STM_MSI_FREQ_262144     262144
651 #define STM_MSI_FREQ_524288     524288
652 #define STM_MSI_FREQ_1048576    1048576
653 #define STM_MSI_FREQ_2097152    2097152
654 #define STM_MSI_FREQ_4194304    4194304
655
656 #define STM_RCC_CR_RTCPRE       (29)
657 #define  STM_RCC_CR_RTCPRE_HSE_DIV_2    0
658 #define  STM_RCC_CR_RTCPRE_HSE_DIV_4    1
659 #define  STM_RCC_CR_RTCPRE_HSE_DIV_8    2
660 #define  STM_RCC_CR_RTCPRE_HSE_DIV_16   3
661 #define  STM_RCC_CR_RTCPRE_HSE_MASK     3
662
663 #define STM_RCC_CR_CSSON        (28)
664 #define STM_RCC_CR_PLLRDY       (25)
665 #define STM_RCC_CR_PLLON        (24)
666 #define STM_RCC_CR_HSEBYP       (18)
667 #define STM_RCC_CR_HSERDY       (17)
668 #define STM_RCC_CR_HSEON        (16)
669 #define STM_RCC_CR_MSIRDY       (9)
670 #define STM_RCC_CR_MSION        (8)
671 #define STM_RCC_CR_HSIRDY       (1)
672 #define STM_RCC_CR_HSION        (0)
673
674 #define STM_RCC_ICSCR_HSI16CAL  0
675 #define STM_RCC_ICSCR_HSI16TRIM 8
676 #define STM_RCC_ICSCR_MSIRANGE  13
677 #define  STM_RCC_ICSCR_MSIRANGE_65536   0
678 #define  STM_RCC_ICSCR_MSIRANGE_131072
679 #define  STM_RCC_ICSCR_MSIRANGE_262144  2
680 #define  STM_RCC_ICSCR_MSIRANGE_524288  3
681 #define  STM_RCC_ICSCR_MSIRANGE_1048576 4
682 #define  STM_RCC_ICSCR_MSIRANGE_2097152 5
683 #define  STM_RCC_ICSCR_MSIRANGE_4194304 6
684 #define  STM_RCC_ICSCR_MSIRANGE_MASK    0x7
685 #define STM_RCC_ICSCR_MSICAL    16
686 #define STM_RCC_ICSCR_MSITRIM   24
687
688 #define STM_RCC_CFGR_MCOPRE     (28)
689 #define  STM_RCC_CFGR_MCOPRE_DIV_1      0
690 #define  STM_RCC_CFGR_MCOPRE_DIV_2      1
691 #define  STM_RCC_CFGR_MCOPRE_DIV_4      2
692 #define  STM_RCC_CFGR_MCOPRE_DIV_8      3
693 #define  STM_RCC_CFGR_MCOPRE_DIV_16     4
694 #define  STM_RCC_CFGR_MCOPRE_MASK       7
695
696 #define STM_RCC_CFGR_MCOSEL     (24)
697 #define  STM_RCC_CFGR_MCOSEL_DISABLE    0
698 #define  STM_RCC_CFGR_MCOSEL_SYSCLK     1
699 #define  STM_RCC_CFGR_MCOSEL_HSI        2
700 #define  STM_RCC_CFGR_MCOSEL_MSI        3
701 #define  STM_RCC_CFGR_MCOSEL_HSE        4
702 #define  STM_RCC_CFGR_MCOSEL_PLL        5
703 #define  STM_RCC_CFGR_MCOSEL_LSI        6
704 #define  STM_RCC_CFGR_MCOSEL_LSE        7
705 #define  STM_RCC_CFGR_MCOSEL_MASK       7
706
707 #define STM_RCC_CFGR_PLLDIV     (22)
708 #define  STM_RCC_CFGR_PLLDIV_2          1
709 #define  STM_RCC_CFGR_PLLDIV_3          2
710 #define  STM_RCC_CFGR_PLLDIV_4          3
711 #define  STM_RCC_CFGR_PLLDIV_MASK       3
712
713 #define STM_RCC_CFGR_PLLMUL     (18)
714 #define  STM_RCC_CFGR_PLLMUL_3          0
715 #define  STM_RCC_CFGR_PLLMUL_4          1
716 #define  STM_RCC_CFGR_PLLMUL_6          2
717 #define  STM_RCC_CFGR_PLLMUL_8          3
718 #define  STM_RCC_CFGR_PLLMUL_12         4
719 #define  STM_RCC_CFGR_PLLMUL_16         5
720 #define  STM_RCC_CFGR_PLLMUL_24         6
721 #define  STM_RCC_CFGR_PLLMUL_32         7
722 #define  STM_RCC_CFGR_PLLMUL_48         8
723 #define  STM_RCC_CFGR_PLLMUL_MASK       0xf
724
725 #define STM_RCC_CFGR_PLLSRC     (16)
726
727 #define STM_RCC_CFGR_PPRE2      (11)
728 #define  STM_RCC_CFGR_PPRE2_DIV_1       0
729 #define  STM_RCC_CFGR_PPRE2_DIV_2       4
730 #define  STM_RCC_CFGR_PPRE2_DIV_4       5
731 #define  STM_RCC_CFGR_PPRE2_DIV_8       6
732 #define  STM_RCC_CFGR_PPRE2_DIV_16      7
733 #define  STM_RCC_CFGR_PPRE2_MASK        7
734
735 #define STM_RCC_CFGR_PPRE1      (8)
736 #define  STM_RCC_CFGR_PPRE1_DIV_1       0
737 #define  STM_RCC_CFGR_PPRE1_DIV_2       4
738 #define  STM_RCC_CFGR_PPRE1_DIV_4       5
739 #define  STM_RCC_CFGR_PPRE1_DIV_8       6
740 #define  STM_RCC_CFGR_PPRE1_DIV_16      7
741 #define  STM_RCC_CFGR_PPRE1_MASK        7
742
743 #define STM_RCC_CFGR_HPRE       (4)
744 #define  STM_RCC_CFGR_HPRE_DIV_1        0
745 #define  STM_RCC_CFGR_HPRE_DIV_2        8
746 #define  STM_RCC_CFGR_HPRE_DIV_4        9
747 #define  STM_RCC_CFGR_HPRE_DIV_8        0xa
748 #define  STM_RCC_CFGR_HPRE_DIV_16       0xb
749 #define  STM_RCC_CFGR_HPRE_DIV_64       0xc
750 #define  STM_RCC_CFGR_HPRE_DIV_128      0xd
751 #define  STM_RCC_CFGR_HPRE_DIV_256      0xe
752 #define  STM_RCC_CFGR_HPRE_DIV_512      0xf
753 #define  STM_RCC_CFGR_HPRE_MASK         0xf
754
755 #define STM_RCC_CFGR_SWS        (2)
756 #define  STM_RCC_CFGR_SWS_MSI           0
757 #define  STM_RCC_CFGR_SWS_HSI           1
758 #define  STM_RCC_CFGR_SWS_HSE           2
759 #define  STM_RCC_CFGR_SWS_PLL           3
760 #define  STM_RCC_CFGR_SWS_MASK          3
761
762 #define STM_RCC_CFGR_SW         (0)
763 #define  STM_RCC_CFGR_SW_MSI            0
764 #define  STM_RCC_CFGR_SW_HSI            1
765 #define  STM_RCC_CFGR_SW_HSE            2
766 #define  STM_RCC_CFGR_SW_PLL            3
767 #define  STM_RCC_CFGR_SW_MASK           3
768
769 #define STM_RCC_IOPENR_IOPAEN           0
770 #define STM_RCC_IOPENR_IOPBEN           1
771 #define STM_RCC_IOPENR_IOPCEN           2
772 #define STM_RCC_IOPENR_IOPDEN           3
773 #define STM_RCC_IOPENR_IOPEEN           4
774 #define STM_RCC_IOPENR_IOPHEN           7
775
776 #define STM_RCC_AHBENR_DMA1EN           0
777 #define STM_RCC_AHBENR_MIFEN            8
778 #define STM_RCC_AHBENR_CRCEN            12
779 #define STM_RCC_AHBENR_CRYPEN           24
780
781 #define STM_RCC_APB2ENR_DBGEN           (22)
782 #define STM_RCC_APB2ENR_USART1EN        (14)
783 #define STM_RCC_APB2ENR_SPI1EN          (12)
784 #define STM_RCC_APB2ENR_ADCEN           (9)
785 #define STM_RCC_APB2ENR_FWEN            (7)
786 #define STM_RCC_APB2ENR_TIM22EN         (5)
787 #define STM_RCC_APB2ENR_TIM21EN         (2)
788 #define STM_RCC_APB2ENR_SYSCFGEN        (0)
789
790 #define STM_RCC_APB1ENR_LPTIM1EN        31
791 #define STM_RCC_APB1ENR_I2C3EN          30
792 #define STM_RCC_APB1ENR_PWREN           28
793 #define STM_RCC_APB1ENR_I2C2EN          22
794 #define STM_RCC_APB1ENR_I2C1EN          21
795 #define STM_RCC_APB1ENR_USART5EN        20
796 #define STM_RCC_APB1ENR_USART4EN        19
797 #define STM_RCC_APB1ENR_LPUART1EN       18
798 #define STM_RCC_APB1ENR_USART2EN        17
799 #define STM_RCC_APB1ENR_SPI2EN          14
800 #define STM_RCC_APB1ENR_WWDGEN          11
801 #define STM_RCC_APB1ENR_TIM7EN          5
802 #define STM_RCC_APB1ENR_TIM6EN          4
803 #define STM_RCC_APB1ENR_TIM3EN          1
804 #define STM_RCC_APB1ENR_TIM2EN          0
805
806 #define STM_RCC_CCIPR_LPTIM1SEL         18
807 #define STM_RCC_CCIPR_I2C3SEL           16
808 #define STM_RCC_CCIPR_I2C1SEL           12
809 #define STM_RCC_CCIPR_LPUART1SEL        10
810 #define  STM_RCC_CCIPR_LPUART1SEL_APB           0
811 #define  STM_RCC_CCIPR_LPUART1SEL_SYSTEM        1
812 #define  STM_RCC_CCIPR_LPUART1SEL_HSI16         2
813 #define  STM_RCC_CCIPR_LPUART1SEL_LSE           3
814 #define STM_RCC_CCIPR_USART2SEL         2
815 #define STM_RCC_CCIPR_USART1SEL         0
816
817 #define STM_RCC_CSR_LPWRRSTF            (31)
818 #define STM_RCC_CSR_WWDGRSTF            (30)
819 #define STM_RCC_CSR_IWDGRSTF            (29)
820 #define STM_RCC_CSR_SFTRSTF             (28)
821 #define STM_RCC_CSR_PORRSTF             (27)
822 #define STM_RCC_CSR_PINRSTF             (26)
823 #define STM_RCC_CSR_OBLRSTF             (25)
824 #define STM_RCC_CSR_RMVF                (24)
825 #define STM_RCC_CSR_RTFRST              (23)
826 #define STM_RCC_CSR_RTCEN               (22)
827 #define STM_RCC_CSR_RTCSEL              (16)
828
829 #define  STM_RCC_CSR_RTCSEL_NONE                0
830 #define  STM_RCC_CSR_RTCSEL_LSE                 1
831 #define  STM_RCC_CSR_RTCSEL_LSI                 2
832 #define  STM_RCC_CSR_RTCSEL_HSE                 3
833 #define  STM_RCC_CSR_RTCSEL_MASK                3
834
835 #define STM_RCC_CSR_LSEBYP              (10)
836 #define STM_RCC_CSR_LSERDY              (9)
837 #define STM_RCC_CSR_LSEON               (8)
838 #define STM_RCC_CSR_LSIRDY              (1)
839 #define STM_RCC_CSR_LSION               (0)
840
841 struct stm_pwr {
842         vuint32_t       cr;
843         vuint32_t       csr;
844 };
845
846 extern struct stm_pwr stm_pwr;
847
848
849 #define STM_PWR_CR_LPDS         16
850 #define STM_PWR_CR_LPRUN        14
851 #define STM_PWR_CR_DS_EE_KOFF   13
852 #define STM_PWR_CR_VOS          11
853 #define  STM_PWR_CR_VOS_1_8             1
854 #define  STM_PWR_CR_VOS_1_5             2
855 #define  STM_PWR_CR_VOS_1_2             3
856 #define  STM_PWR_CR_VOS_MASK            3
857 #define STM_PWR_CR_FWU          10
858 #define STM_PWR_CR_ULP          9
859 #define STM_PWR_CR_DBP          8
860 #define STM_PWR_CR_PLS          5
861 #define  STM_PWR_CR_PLS_1_9     0
862 #define  STM_PWR_CR_PLS_2_1     1
863 #define  STM_PWR_CR_PLS_2_3     2
864 #define  STM_PWR_CR_PLS_2_5     3
865 #define  STM_PWR_CR_PLS_2_7     4
866 #define  STM_PWR_CR_PLS_2_9     5
867 #define  STM_PWR_CR_PLS_3_1     6
868 #define  STM_PWR_CR_PLS_EXT     7
869 #define  STM_PWR_CR_PLS_MASK    7
870 #define STM_PWR_CR_PVDE         4
871 #define STM_PWR_CR_CSBF         3
872 #define STM_PWR_CR_CWUF         2
873 #define STM_PWR_CR_PDDS         1
874 #define STM_PWR_CR_LPSDSR       0
875
876 #define STM_PWR_CSR_EWUP3       (10)
877 #define STM_PWR_CSR_EWUP2       (9)
878 #define STM_PWR_CSR_EWUP1       (8)
879 #define STM_PWR_CSR_REGLPF      (5)
880 #define STM_PWR_CSR_VOSF        (4)
881 #define STM_PWR_CSR_VREFINTRDYF (3)
882 #define STM_PWR_CSR_PVDO        (2)
883 #define STM_PWR_CSR_SBF         (1)
884 #define STM_PWR_CSR_WUF         (0)
885
886 struct stm_tim67 {
887         vuint32_t       cr1;
888         vuint32_t       cr2;
889         uint32_t        _unused_08;
890         vuint32_t       dier;
891
892         vuint32_t       sr;
893         vuint32_t       egr;
894         uint32_t        _unused_18;
895         uint32_t        _unused_1c;
896
897         uint32_t        _unused_20;
898         vuint32_t       cnt;
899         vuint32_t       psc;
900         vuint32_t       arr;
901 };
902
903 extern struct stm_tim67 stm_tim6;
904
905 #define STM_TIM67_CR1_ARPE      (7)
906 #define STM_TIM67_CR1_OPM       (3)
907 #define STM_TIM67_CR1_URS       (2)
908 #define STM_TIM67_CR1_UDIS      (1)
909 #define STM_TIM67_CR1_CEN       (0)
910
911 #define STM_TIM67_CR2_MMS       (4)
912 #define  STM_TIM67_CR2_MMS_RESET        0
913 #define  STM_TIM67_CR2_MMS_ENABLE       1
914 #define  STM_TIM67_CR2_MMS_UPDATE       2
915 #define  STM_TIM67_CR2_MMS_MASK         7
916
917 #define STM_TIM67_DIER_UDE      (8)
918 #define STM_TIM67_DIER_UIE      (0)
919
920 #define STM_TIM67_SR_UIF        (0)
921
922 #define STM_TIM67_EGR_UG        (0)
923
924 struct stm_lcd {
925         vuint32_t       cr;
926         vuint32_t       fcr;
927         vuint32_t       sr;
928         vuint32_t       clr;
929         uint32_t        unused_0x10;
930         vuint32_t       ram[8*2];
931 };
932
933 extern struct stm_lcd stm_lcd;
934
935 #define STM_LCD_CR_MUX_SEG              (7)
936
937 #define STM_LCD_CR_BIAS                 (5)
938 #define  STM_LCD_CR_BIAS_1_4            0
939 #define  STM_LCD_CR_BIAS_1_2            1
940 #define  STM_LCD_CR_BIAS_1_3            2
941 #define  STM_LCD_CR_BIAS_MASK           3
942
943 #define STM_LCD_CR_DUTY                 (2)
944 #define  STM_LCD_CR_DUTY_STATIC         0
945 #define  STM_LCD_CR_DUTY_1_2            1
946 #define  STM_LCD_CR_DUTY_1_3            2
947 #define  STM_LCD_CR_DUTY_1_4            3
948 #define  STM_LCD_CR_DUTY_1_8            4
949 #define  STM_LCD_CR_DUTY_MASK           7
950
951 #define STM_LCD_CR_VSEL                 (1)
952 #define STM_LCD_CR_LCDEN                (0)
953
954 #define STM_LCD_FCR_PS                  (22)
955 #define  STM_LCD_FCR_PS_1               0x0
956 #define  STM_LCD_FCR_PS_2               0x1
957 #define  STM_LCD_FCR_PS_4               0x2
958 #define  STM_LCD_FCR_PS_8               0x3
959 #define  STM_LCD_FCR_PS_16              0x4
960 #define  STM_LCD_FCR_PS_32              0x5
961 #define  STM_LCD_FCR_PS_64              0x6
962 #define  STM_LCD_FCR_PS_128             0x7
963 #define  STM_LCD_FCR_PS_256             0x8
964 #define  STM_LCD_FCR_PS_512             0x9
965 #define  STM_LCD_FCR_PS_1024            0xa
966 #define  STM_LCD_FCR_PS_2048            0xb
967 #define  STM_LCD_FCR_PS_4096            0xc
968 #define  STM_LCD_FCR_PS_8192            0xd
969 #define  STM_LCD_FCR_PS_16384           0xe
970 #define  STM_LCD_FCR_PS_32768           0xf
971 #define  STM_LCD_FCR_PS_MASK            0xf
972
973 #define STM_LCD_FCR_DIV                 (18)
974 #define STM_LCD_FCR_DIV_16              0x0
975 #define STM_LCD_FCR_DIV_17              0x1
976 #define STM_LCD_FCR_DIV_18              0x2
977 #define STM_LCD_FCR_DIV_19              0x3
978 #define STM_LCD_FCR_DIV_20              0x4
979 #define STM_LCD_FCR_DIV_21              0x5
980 #define STM_LCD_FCR_DIV_22              0x6
981 #define STM_LCD_FCR_DIV_23              0x7
982 #define STM_LCD_FCR_DIV_24              0x8
983 #define STM_LCD_FCR_DIV_25              0x9
984 #define STM_LCD_FCR_DIV_26              0xa
985 #define STM_LCD_FCR_DIV_27              0xb
986 #define STM_LCD_FCR_DIV_28              0xc
987 #define STM_LCD_FCR_DIV_29              0xd
988 #define STM_LCD_FCR_DIV_30              0xe
989 #define STM_LCD_FCR_DIV_31              0xf
990 #define STM_LCD_FCR_DIV_MASK            0xf
991
992 #define STM_LCD_FCR_BLINK               (16)
993 #define  STM_LCD_FCR_BLINK_DISABLE              0
994 #define  STM_LCD_FCR_BLINK_SEG0_COM0            1
995 #define  STM_LCD_FCR_BLINK_SEG0_COMALL          2
996 #define  STM_LCD_FCR_BLINK_SEGALL_COMALL        3
997 #define  STM_LCD_FCR_BLINK_MASK                 3
998
999 #define STM_LCD_FCR_BLINKF              (13)
1000 #define  STM_LCD_FCR_BLINKF_8                   0
1001 #define  STM_LCD_FCR_BLINKF_16                  1
1002 #define  STM_LCD_FCR_BLINKF_32                  2
1003 #define  STM_LCD_FCR_BLINKF_64                  3
1004 #define  STM_LCD_FCR_BLINKF_128                 4
1005 #define  STM_LCD_FCR_BLINKF_256                 5
1006 #define  STM_LCD_FCR_BLINKF_512                 6
1007 #define  STM_LCD_FCR_BLINKF_1024                7
1008 #define  STM_LCD_FCR_BLINKF_MASK                7
1009
1010 #define STM_LCD_FCR_CC                  (10)
1011 #define  STM_LCD_FCR_CC_MASK                    7
1012
1013 #define STM_LCD_FCR_DEAD                (7)
1014 #define  STM_LCD_FCR_DEAD_MASK                  7
1015
1016 #define STM_LCD_FCR_PON                 (4)
1017 #define  STM_LCD_FCR_PON_MASK                   7
1018
1019 #define STM_LCD_FCR_UDDIE               (3)
1020 #define STM_LCD_FCR_SOFIE               (1)
1021 #define STM_LCD_FCR_HD                  (0)
1022
1023 #define STM_LCD_SR_FCRSF                (5)
1024 #define STM_LCD_SR_RDY                  (4)
1025 #define STM_LCD_SR_UDD                  (3)
1026 #define STM_LCD_SR_UDR                  (2)
1027 #define STM_LCD_SR_SOF                  (1)
1028 #define STM_LCD_SR_ENS                  (0)
1029
1030 #define STM_LCD_CLR_UDDC                (3)
1031 #define STM_LCD_CLR_SOFC                (1)
1032
1033 /* The SYSTICK starts at 0xe000e010 */
1034
1035 struct stm_systick {
1036         vuint32_t       csr;
1037         vuint32_t       rvr;
1038         vuint32_t       cvr;
1039         vuint32_t       calib;
1040 };
1041
1042 extern struct stm_systick stm_systick;
1043
1044 #define STM_SYSTICK_CSR_ENABLE          0
1045 #define STM_SYSTICK_CSR_TICKINT         1
1046 #define STM_SYSTICK_CSR_CLKSOURCE       2
1047 #define  STM_SYSTICK_CSR_CLKSOURCE_HCLK_8               0
1048 #define  STM_SYSTICK_CSR_CLKSOURCE_HCLK                 1
1049 #define STM_SYSTICK_CSR_COUNTFLAG       16
1050
1051 /* The NVIC starts at 0xe000e100, so add that to the offsets to find the absolute address */
1052
1053 struct stm_nvic {
1054         vuint32_t       iser;           /* 0x000 0xe000e100 Set Enable Register */
1055
1056         uint8_t         _unused020[0x080 - 0x004];
1057
1058         vuint32_t       icer;           /* 0x080 0xe000e180 Clear Enable Register */
1059
1060         uint8_t         _unused0a0[0x100 - 0x084];
1061
1062         vuint32_t       ispr;           /* 0x100 0xe000e200 Set Pending Register */
1063
1064         uint8_t         _unused120[0x180 - 0x104];
1065
1066         vuint32_t       icpr;           /* 0x180 0xe000e280 Clear Pending Register */
1067
1068         uint8_t         _unused1a0[0x300 - 0x184];
1069
1070         vuint32_t       ipr[8];         /* 0x300 0xe000e400 Priority Register */
1071 };
1072
1073 extern struct stm_nvic stm_nvic;
1074
1075 #define IRQ_MASK(irq)   (1 << (irq))
1076 #define IRQ_BOOL(v,irq) (((v) >> (irq)) & 1)
1077
1078 static inline void
1079 stm_nvic_set_enable(int irq) {
1080         stm_nvic.iser = IRQ_MASK(irq);
1081 }
1082
1083 static inline void
1084 stm_nvic_clear_enable(int irq) {
1085         stm_nvic.icer = IRQ_MASK(irq);
1086 }
1087
1088 static inline int
1089 stm_nvic_enabled(int irq) {
1090         return IRQ_BOOL(stm_nvic.iser, irq);
1091 }
1092
1093 static inline void
1094 stm_nvic_set_pending(int irq) {
1095         stm_nvic.ispr = IRQ_MASK(irq);
1096 }
1097
1098 static inline void
1099 stm_nvic_clear_pending(int irq) {
1100         stm_nvic.icpr = IRQ_MASK(irq);
1101 }
1102
1103 static inline int
1104 stm_nvic_pending(int irq) {
1105         return IRQ_BOOL(stm_nvic.ispr, irq);
1106 }
1107
1108 #define IRQ_PRIO_REG(irq)       ((irq) >> 2)
1109 #define IRQ_PRIO_BIT(irq)       (((irq) & 3) << 3)
1110 #define IRQ_PRIO_MASK(irq)      (0xff << IRQ_PRIO_BIT(irq))
1111
1112 static inline void
1113 stm_nvic_set_priority(int irq, uint8_t prio) {
1114         int             n = IRQ_PRIO_REG(irq);
1115         uint32_t        v;
1116
1117         v = stm_nvic.ipr[n];
1118         v &= ~IRQ_PRIO_MASK(irq);
1119         v |= (prio) << IRQ_PRIO_BIT(irq);
1120         stm_nvic.ipr[n] = v;
1121 }
1122
1123 static inline uint8_t
1124 stm_nvic_get_priority(int irq) {
1125         return (stm_nvic.ipr[IRQ_PRIO_REG(irq)] >> IRQ_PRIO_BIT(irq)) & IRQ_PRIO_MASK(0);
1126 }
1127
1128 struct stm_scb {
1129         vuint32_t       cpuid;
1130         vuint32_t       icsr;
1131         vuint32_t       vtor;
1132         vuint32_t       aircr;
1133
1134         vuint32_t       scr;
1135         vuint32_t       ccr;
1136         vuint32_t       shpr1;
1137         vuint32_t       shpr2;
1138
1139         vuint32_t       shpr3;
1140         vuint32_t       shcrs;
1141         vuint32_t       cfsr;
1142         vuint32_t       hfsr;
1143
1144         uint32_t        unused_30;
1145         vuint32_t       mmfar;
1146         vuint32_t       bfar;
1147 };
1148
1149 extern struct stm_scb stm_scb;
1150
1151 #define STM_SCB_AIRCR_VECTKEY           16
1152 #define  STM_SCB_AIRCR_VECTKEY_KEY              0x05fa
1153 #define STM_SCB_AIRCR_PRIGROUP          8
1154 #define STM_SCB_AIRCR_SYSRESETREQ       2
1155 #define STM_SCB_AIRCR_VECTCLRACTIVE     1
1156 #define STM_SCB_AIRCR_VECTRESET         0
1157
1158 #define STM_SCB_SCR_SVONPEND            4
1159 #define STM_SCB_SCR_SLEEPDEEP           2
1160 #define STM_SCB_SCR_SLEEPONEXIT         1
1161
1162 struct stm_mpu {
1163         vuint32_t       typer;
1164         vuint32_t       cr;
1165         vuint32_t       rnr;
1166         vuint32_t       rbar;
1167
1168         vuint32_t       rasr;
1169         vuint32_t       rbar_a1;
1170         vuint32_t       rasr_a1;
1171         vuint32_t       rbar_a2;
1172         vuint32_t       rasr_a2;
1173         vuint32_t       rbar_a3;
1174         vuint32_t       rasr_a3;
1175 };
1176
1177 extern struct stm_mpu stm_mpu;
1178
1179 #define STM_MPU_TYPER_IREGION   16
1180 #define  STM_MPU_TYPER_IREGION_MASK     0xff
1181 #define STM_MPU_TYPER_DREGION   8
1182 #define  STM_MPU_TYPER_DREGION_MASK     0xff
1183 #define STM_MPU_TYPER_SEPARATE  0
1184
1185 #define STM_MPU_CR_PRIVDEFENA   2
1186 #define STM_MPU_CR_HFNMIENA     1
1187 #define STM_MPU_CR_ENABLE       0
1188
1189 #define STM_MPU_RNR_REGION      0
1190 #define STM_MPU_RNR_REGION_MASK         0xff
1191
1192 #define STM_MPU_RBAR_ADDR       5
1193 #define STM_MPU_RBAR_ADDR_MASK          0x7ffffff
1194
1195 #define STM_MPU_RBAR_VALID      4
1196 #define STM_MPU_RBAR_REGION     0
1197 #define STM_MPU_RBAR_REGION_MASK        0xf
1198
1199 #define STM_MPU_RASR_XN         28
1200 #define STM_MPU_RASR_AP         24
1201 #define  STM_MPU_RASR_AP_NONE_NONE      0
1202 #define  STM_MPU_RASR_AP_RW_NONE        1
1203 #define  STM_MPU_RASR_AP_RW_RO          2
1204 #define  STM_MPU_RASR_AP_RW_RW          3
1205 #define  STM_MPU_RASR_AP_RO_NONE        5
1206 #define  STM_MPU_RASR_AP_RO_RO          6
1207 #define  STM_MPU_RASR_AP_MASK           7
1208 #define STM_MPU_RASR_TEX        19
1209 #define  STM_MPU_RASR_TEX_MASK          7
1210 #define STM_MPU_RASR_S          18
1211 #define STM_MPU_RASR_C          17
1212 #define STM_MPU_RASR_B          16
1213 #define STM_MPU_RASR_SRD        8
1214 #define  STM_MPU_RASR_SRD_MASK          0xff
1215 #define STM_MPU_RASR_SIZE       1
1216 #define  STM_MPU_RASR_SIZE_MASK         0x1f
1217 #define STM_MPU_RASR_ENABLE     0
1218
1219 #define isr_decl(name) void stm_ ## name ## _isr(void)
1220
1221 isr_decl(halt);
1222 isr_decl(ignore);
1223
1224 isr_decl(nmi);
1225 isr_decl(hardfault);
1226 isr_decl(usagefault);
1227 isr_decl(svc);
1228 isr_decl(debugmon);
1229 isr_decl(pendsv);
1230 isr_decl(systick);
1231 isr_decl(wwdg);
1232 isr_decl(pvd);
1233 isr_decl(rtc);
1234 isr_decl(flash);
1235 isr_decl(rcc_crs);
1236 isr_decl(exti1_0);
1237 isr_decl(exti3_2);
1238 isr_decl(exti15_4);
1239 isr_decl(dma1_channel1);
1240 isr_decl(dma1_channel3_2);
1241 isr_decl(dma1_channel7_4);
1242 isr_decl(adc_comp);
1243 isr_decl(lptim1);
1244 isr_decl(usart4_usart5);
1245 isr_decl(tim2);
1246 isr_decl(tim3);
1247 isr_decl(tim4);
1248 isr_decl(tim6);
1249 isr_decl(tim7);
1250 isr_decl(tim21);
1251 isr_decl(i2c3);
1252 isr_decl(tim22);
1253 isr_decl(i2c1);
1254 isr_decl(i2c2);
1255 isr_decl(spi1);
1256 isr_decl(spi2);
1257 isr_decl(usart1);
1258 isr_decl(usart2);
1259 isr_decl(usart3);
1260 isr_decl(lpuart1_aes);
1261
1262 #undef isr_decl
1263
1264 #define STM_ISR_WWDG_POS                0
1265 #define STM_ISR_PVD_POS                 1
1266 #define STM_ISR_RTC_POS                 2
1267 #define STM_ISR_FLASH_POS               3
1268 #define STM_ISR_RCC_CRS_POS             4
1269 #define STM_ISR_EXTI1_0_POS             5
1270 #define STM_ISR_EXTI3_2_POS             6
1271 #define STM_ISR_EXTI15_4_POS            7
1272 #define STM_ISR_DMA1_CHANNEL1_POS       9
1273 #define STM_ISR_DMA1_CHANNEL3_2_POS     10
1274 #define STM_ISR_DMA1_CHANNEL7_4_POS     11
1275 #define STM_ISR_ADC_COMP_POS            12
1276 #define STM_ISR_LPTIM1_POS              13
1277 #define STM_ISR_USART4_USART5_POS       14
1278 #define STM_ISR_TIM2_POS                15
1279 #define STM_ISR_TIM3_POS                16
1280 #define STM_ISR_TIM6_POS                17
1281 #define STM_ISR_TIM7_POS                18
1282 #define STM_ISR_TIM21_POS               20
1283 #define STM_ISR_I2C3_POS                21
1284 #define STM_ISR_TIM22_POS               22
1285 #define STM_ISR_I2C1_POS                23
1286 #define STM_ISR_I2C2_POS                24
1287 #define STM_ISR_SPI1_POS                25
1288 #define STM_ISR_SPI2_POS                26
1289 #define STM_ISR_USART1_POS              27
1290 #define STM_ISR_USART2_POS              28
1291 #define STM_ISR_LPUART1_AES_POS         29
1292
1293 struct stm_syscfg {
1294         vuint32_t       memrmp;
1295         vuint32_t       pmc;
1296         vuint32_t       exticr[4];
1297 };
1298
1299 extern struct stm_syscfg stm_syscfg;
1300
1301 #define STM_SYSCFG_MEMRMP_MEM_MODE      0
1302 #define  STM_SYSCFG_MEMRMP_MEM_MODE_MAIN_FLASH          0
1303 #define  STM_SYSCFG_MEMRMP_MEM_MODE_SYSTEM_FLASH        1
1304 #define  STM_SYSCFG_MEMRMP_MEM_MODE_SRAM                3
1305 #define  STM_SYSCFG_MEMRMP_MEM_MODE_MASK                3
1306
1307 #define STM_SYSCFG_PMC_USB_PU           0
1308
1309 #define STM_SYSCFG_EXTICR_PA            0
1310 #define STM_SYSCFG_EXTICR_PB            1
1311 #define STM_SYSCFG_EXTICR_PC            2
1312 #define STM_SYSCFG_EXTICR_PD            3
1313 #define STM_SYSCFG_EXTICR_PE            4
1314 #define STM_SYSCFG_EXTICR_PH            5
1315
1316 static inline void
1317 stm_exticr_set(struct stm_gpio *gpio, int pin) {
1318         uint8_t reg = pin >> 2;
1319         uint8_t shift = (pin & 3) << 2;
1320         uint8_t val = 0;
1321
1322         /* Enable SYSCFG */
1323         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
1324
1325         if (gpio == &stm_gpioa)
1326                 val = STM_SYSCFG_EXTICR_PA;
1327         else if (gpio == &stm_gpiob)
1328                 val = STM_SYSCFG_EXTICR_PB;
1329         else if (gpio == &stm_gpioc)
1330                 val = STM_SYSCFG_EXTICR_PC;
1331         else if (gpio == &stm_gpiod)
1332                 val = STM_SYSCFG_EXTICR_PD;
1333         else if (gpio == &stm_gpioe)
1334                 val = STM_SYSCFG_EXTICR_PE;
1335
1336         stm_syscfg.exticr[reg] = (stm_syscfg.exticr[reg] & ~(0xf << shift)) | val << shift;
1337 }
1338
1339 struct stm_dma_channel {
1340         vuint32_t       ccr;
1341         vuint32_t       cndtr;
1342         vvoid_t         cpar;
1343         vvoid_t         cmar;
1344         vuint32_t       reserved;
1345 };
1346
1347 #define STM_NUM_DMA     7
1348
1349 struct stm_dma {
1350         vuint32_t               isr;
1351         vuint32_t               ifcr;
1352         struct stm_dma_channel  channel[STM_NUM_DMA];
1353         uint8_t                 unused94[0xa8 - 0x94];
1354         vuint32_t               cselr;
1355 };
1356
1357 extern struct stm_dma stm_dma1;
1358 #define stm_dma1 (*(struct stm_dma *) 0x40020000)
1359
1360 /* DMA channels go from 1 to 7, instead of 0 to 6 (sigh)
1361  */
1362
1363 #define STM_DMA_INDEX(channel)          ((channel) - 1)
1364
1365 #define STM_DMA_ISR(index)              ((index) << 2)
1366 #define STM_DMA_ISR_MASK                        0xf
1367 #define STM_DMA_ISR_TEIF                        3
1368 #define STM_DMA_ISR_HTIF                        2
1369 #define STM_DMA_ISR_TCIF                        1
1370 #define STM_DMA_ISR_GIF                         0
1371
1372 #define STM_DMA_IFCR(index)             ((index) << 2)
1373 #define STM_DMA_IFCR_MASK                       0xf
1374 #define STM_DMA_IFCR_CTEIF                      3
1375 #define STM_DMA_IFCR_CHTIF                      2
1376 #define STM_DMA_IFCR_CTCIF                      1
1377 #define STM_DMA_IFCR_CGIF                       0
1378
1379 #define STM_DMA_CCR_MEM2MEM             (14)
1380
1381 #define STM_DMA_CCR_PL                  (12)
1382 #define  STM_DMA_CCR_PL_LOW                     (0)
1383 #define  STM_DMA_CCR_PL_MEDIUM                  (1)
1384 #define  STM_DMA_CCR_PL_HIGH                    (2)
1385 #define  STM_DMA_CCR_PL_VERY_HIGH               (3)
1386 #define  STM_DMA_CCR_PL_MASK                    (3)
1387
1388 #define STM_DMA_CCR_MSIZE               (10)
1389 #define  STM_DMA_CCR_MSIZE_8                    (0)
1390 #define  STM_DMA_CCR_MSIZE_16                   (1)
1391 #define  STM_DMA_CCR_MSIZE_32                   (2)
1392 #define  STM_DMA_CCR_MSIZE_MASK                 (3)
1393
1394 #define STM_DMA_CCR_PSIZE               (8)
1395 #define  STM_DMA_CCR_PSIZE_8                    (0)
1396 #define  STM_DMA_CCR_PSIZE_16                   (1)
1397 #define  STM_DMA_CCR_PSIZE_32                   (2)
1398 #define  STM_DMA_CCR_PSIZE_MASK                 (3)
1399
1400 #define STM_DMA_CCR_MINC                (7)
1401 #define STM_DMA_CCR_PINC                (6)
1402 #define STM_DMA_CCR_CIRC                (5)
1403 #define STM_DMA_CCR_DIR                 (4)
1404 #define  STM_DMA_CCR_DIR_PER_TO_MEM             0
1405 #define  STM_DMA_CCR_DIR_MEM_TO_PER             1
1406 #define STM_DMA_CCR_TEIE                (3)
1407 #define STM_DMA_CCR_HTIE                (2)
1408 #define STM_DMA_CCR_TCIE                (1)
1409 #define STM_DMA_CCR_EN                  (0)
1410
1411 #define STM_DMA_CSELR_C7S_SPI2_TX               0x2
1412 #define STM_DMA_CSELR_C7S_USART2_TX             0x4
1413 #define STM_DMA_CSELR_C7S_LPUART1_TX            0x5
1414 #define STM_DMA_CSELR_C7S_I2C1_RX               0x6
1415 #define STM_DMA_CSELR_C7S_TIM2_CH2_TIM2_CH4     0x8
1416 #define STM_DMA_CSELR_C7S_USART4_TX             0xc
1417 #define STM_DMA_CSELR_C7S_USART5_TX             0xd
1418
1419 #define STM_DMA_CSELR_C6S_SPI2_RX               0x2
1420 #define STM_DMA_CSELR_C6S_USART2_RX             0x4
1421 #define STM_DMA_CSELR_C6S_LPUART1_RX            0x5
1422 #define STM_DMA_CSELR_C6S_I2C1_TX               0x6
1423 #define STM_DMA_CSELR_C6S_TIM3_TRIG             0xa
1424 #define STM_DMA_CSELR_C6S_USART4_RX             0xc
1425 #define STM_DMA_CSELR_C6S_USART5_RX             0xd
1426
1427 #define STM_DMA_CSELR_C5S_SPI2_TX               0x2
1428 #define STM_DMA_CSELR_C5S_USART1_RX             0x3
1429 #define STM_DMA_CSELR_C5S_USART2_RX             0x4
1430 #define STM_DMA_CSELR_C5S_I2C2_RX               0x7
1431 #define STM_DMA_CSELR_C5S_TIM2_CH1              0x8
1432 #define STM_DMA_CSELR_C5S_TIM3_CH1              0xa
1433 #define STM_DMA_CSELR_C5S_AES_IN                0xb
1434 #define STM_DMA_CSELR_C5S_I2C3_RX               0xe
1435
1436 #define STM_DMA_CSELR_C4S_SPI2_RX               0x2
1437 #define STM_DMA_CSELR_C4S_USART1_TX             0x3
1438 #define STM_DMA_CSELR_C4S_USART2_TX             0x4
1439 #define STM_DMA_CSELR_C4S_I2C2_TX               0x7
1440 #define STM_DMA_CSELR_C4S_TIM2_CH4              0x8
1441 #define STM_DMA_CSELR_C4S_I2C3_TX               0xe
1442 #define STM_DMA_CSELR_C4S_TIM7_UP               0xf
1443
1444 #define STM_DMA_CSELR_C3S_SPI1_TX               0x1
1445 #define STM_DMA_CSELR_C3S_USART1_RX             0x3
1446 #define STM_DMA_CSELR_C3S_LPUART1_RX            0x5
1447 #define STM_DMA_CSELR_C3S_I2C1_RX               0x6
1448 #define STM_DMA_CSELR_C3S_TIM2_CH2              0x8
1449 #define STM_DMA_CSELR_C3S_TIM4_CH4_TIM4_UP      0xa
1450 #define STM_DMA_CSELR_C3S_AES_OUT               0xb
1451 #define STM_DMA_CSELR_C3S_USART4_TX             0xc
1452 #define STM_DMA_CSELR_C3S_USART5_TX             0xd
1453 #define STM_DMA_CSELR_C3S_I2C3_RX               0xe
1454
1455 #define STM_DMA_CSELR_C2S_ADC                   0x0
1456 #define STM_DMA_CSELR_C2S_SPI1_RX               0x1
1457 #define STM_DMA_CSELR_C2S_USART1_TX             0x3
1458 #define STM_DMA_CSELR_C2S_LPUART1_TX            0x5
1459 #define STM_DMA_CSELR_C2S_I2C1_TX               0x6
1460 #define STM_DMA_CSELR_C2S_TIM2_UP               0x8
1461 #define STM_DMA_CSELR_C2S_TIM6_UP               0x9
1462 #define STM_DMA_CSELR_C2S_TIM3_CH3              0xa
1463 #define STM_DMA_CSELR_C2S_AES_OUT               0xb
1464 #define STM_DMA_CSELR_C2S_USART4_RX             0xc
1465 #define STM_DMA_CSELR_C2S_USART5_RX             0xd
1466 #define STM_DMA_CSELR_C2S_I2C3_TX               0xe
1467
1468 #define STM_DMA_CSELR_C1S_ADC                   0x0
1469 #define STM_DMA_CSELR_C1S_TIM2_CH3              0x8
1470 #define STM_DMA_CSELR_C1S_AES_IN                0xb
1471
1472 #define STM_NUM_SPI     1
1473
1474 struct stm_spi {
1475         vuint32_t       cr1;
1476         vuint32_t       cr2;
1477         vuint32_t       sr;
1478         vuint32_t       dr;
1479         vuint32_t       crcpr;
1480         vuint32_t       rxcrcr;
1481         vuint32_t       txcrcr;
1482 };
1483
1484 extern struct stm_spi stm_spi1;
1485 #define stm_spi1 (*(struct stm_spi *) 0x40013000)
1486
1487 /* SPI channels go from 1 to 3, instead of 0 to 2 (sigh)
1488  */
1489
1490 #define STM_SPI_INDEX(channel)          ((channel) - 1)
1491
1492 #define STM_SPI_CR1_BIDIMODE            15
1493 #define STM_SPI_CR1_BIDIOE              14
1494 #define STM_SPI_CR1_CRCEN               13
1495 #define STM_SPI_CR1_CRCNEXT             12
1496 #define STM_SPI_CR1_DFF                 11
1497 #define STM_SPI_CR1_RXONLY              10
1498 #define STM_SPI_CR1_SSM                 9
1499 #define STM_SPI_CR1_SSI                 8
1500 #define STM_SPI_CR1_LSBFIRST            7
1501 #define STM_SPI_CR1_SPE                 6
1502 #define STM_SPI_CR1_BR                  3
1503 #define  STM_SPI_CR1_BR_PCLK_2                  0
1504 #define  STM_SPI_CR1_BR_PCLK_4                  1
1505 #define  STM_SPI_CR1_BR_PCLK_8                  2
1506 #define  STM_SPI_CR1_BR_PCLK_16                 3
1507 #define  STM_SPI_CR1_BR_PCLK_32                 4
1508 #define  STM_SPI_CR1_BR_PCLK_64                 5
1509 #define  STM_SPI_CR1_BR_PCLK_128                6
1510 #define  STM_SPI_CR1_BR_PCLK_256                7
1511 #define  STM_SPI_CR1_BR_MASK                    7
1512
1513 #define STM_SPI_CR1_MSTR                2
1514 #define STM_SPI_CR1_CPOL                1
1515 #define STM_SPI_CR1_CPHA                0
1516
1517 #define STM_SPI_CR2_TXEIE       7
1518 #define STM_SPI_CR2_RXNEIE      6
1519 #define STM_SPI_CR2_ERRIE       5
1520 #define STM_SPI_CR2_SSOE        2
1521 #define STM_SPI_CR2_TXDMAEN     1
1522 #define STM_SPI_CR2_RXDMAEN     0
1523
1524 #define STM_SPI_SR_FRE          8
1525 #define STM_SPI_SR_BSY          7
1526 #define STM_SPI_SR_OVR          6
1527 #define STM_SPI_SR_MODF         5
1528 #define STM_SPI_SR_CRCERR       4
1529 #define STM_SPI_SR_UDR          3
1530 #define STM_SPI_SR_CHSIDE       2
1531 #define STM_SPI_SR_TXE          1
1532 #define STM_SPI_SR_RXNE         0
1533
1534 struct stm_adc {
1535         vuint32_t       isr;
1536         vuint32_t       ier;
1537         vuint32_t       cr;
1538         vuint32_t       cfgr1;
1539
1540         vuint32_t       cfgr2;
1541         vuint32_t       smpr;
1542         vuint32_t       r_18;
1543         vuint32_t       r_1c;
1544
1545         vuint32_t       tr;
1546         vuint32_t       r_24;
1547         vuint32_t       chselr;
1548         vuint32_t       r_2c;
1549
1550         vuint32_t       r_30[4];
1551
1552         vuint32_t       dr;
1553
1554         uint8_t         r_44[0xb4 - 0x44];
1555
1556         vuint32_t       calfact;
1557
1558         uint8_t         r_b8[0x308 - 0xb8];
1559         vuint32_t       ccr;
1560 };
1561
1562 extern struct stm_adc stm_adc;
1563 #define stm_adc (*(struct stm_adc *) 0x40012400)
1564
1565 #define STM_ADC_ISR_AWD         7
1566 #define STM_ADC_ISR_OVR         4
1567 #define STM_ADC_ISR_EOSEQ       3
1568 #define STM_ADC_ISR_EOC         2
1569 #define STM_ADC_ISR_EOSMP       1
1570 #define STM_ADC_ISR_ADRDY       0
1571
1572 #define STM_ADC_IER_AWDIE       7
1573 #define STM_ADC_IER_OVRIE       4
1574 #define STM_ADC_IER_EOSEQIE     3
1575 #define STM_ADC_IER_EOCIE       2
1576 #define STM_ADC_IER_EOSMPIE     1
1577 #define STM_ADC_IER_ADRDYIE     0
1578
1579 #define STM_ADC_CR_ADCAL        31
1580 #define STM_ADC_CR_ADVREGEN     28
1581 #define STM_ADC_CR_ADSTP        4
1582 #define STM_ADC_CR_ADSTART      2
1583 #define STM_ADC_CR_ADDIS        1
1584 #define STM_ADC_CR_ADEN         0
1585
1586 #define STM_ADC_CFGR1_AWDCH     26
1587 #define STM_ADC_CFGR1_AWDEN     23
1588 #define STM_ADC_CFGR1_AWDSGL    22
1589 #define STM_ADC_CFGR1_DISCEN    16
1590 #define STM_ADC_CFGR1_AUTOOFF   15
1591 #define STM_ADC_CFGR1_WAIT      14
1592 #define STM_ADC_CFGR1_CONT      13
1593 #define STM_ADC_CFGR1_OVRMOD    12
1594 #define STM_ADC_CFGR1_EXTEN     10
1595 #define  STM_ADC_CFGR1_EXTEN_DISABLE    0
1596 #define  STM_ADC_CFGR1_EXTEN_RISING     1
1597 #define  STM_ADC_CFGR1_EXTEN_FALLING    2
1598 #define  STM_ADC_CFGR1_EXTEN_BOTH       3
1599 #define  STM_ADC_CFGR1_EXTEN_MASK       3
1600
1601 #define STM_ADC_CFGR1_EXTSEL    6
1602 #define STM_ADC_CFGR1_ALIGN     5
1603 #define STM_ADC_CFGR1_RES       3
1604 #define  STM_ADC_CFGR1_RES_12           0
1605 #define  STM_ADC_CFGR1_RES_10           1
1606 #define  STM_ADC_CFGR1_RES_8            2
1607 #define  STM_ADC_CFGR1_RES_6            3
1608 #define  STM_ADC_CFGR1_RES_MASK         3
1609 #define STM_ADC_CFGR1_SCANDIR   2
1610 #define  STM_ADC_CFGR1_SCANDIR_UP       0
1611 #define  STM_ADC_CFGR1_SCANDIR_DOWN     1
1612 #define STM_ADC_CFGR1_DMACFG    1
1613 #define  STM_ADC_CFGR1_DMACFG_ONESHOT   0
1614 #define  STM_ADC_CFGR1_DMACFG_CIRCULAR  1
1615 #define STM_ADC_CFGR1_DMAEN     0
1616
1617 #define STM_ADC_CFGR2_CKMODE    30
1618 #define  STM_ADC_CFGR2_CKMODE_ADCCLK    0
1619 #define  STM_ADC_CFGR2_CKMODE_PCLK_2    1
1620 #define  STM_ADC_CFGR2_CKMODE_PCLK_4    2
1621 #define  STM_ADC_CFGR2_CKMODE_PCLK      3
1622
1623 #define STM_ADC_SMPR_SMP        0
1624 #define  STM_ADC_SMPR_SMP_1_5           0
1625 #define  STM_ADC_SMPR_SMP_7_5           1
1626 #define  STM_ADC_SMPR_SMP_13_5          2
1627 #define  STM_ADC_SMPR_SMP_28_5          3
1628 #define  STM_ADC_SMPR_SMP_41_5          4
1629 #define  STM_ADC_SMPR_SMP_55_5          5
1630 #define  STM_ADC_SMPR_SMP_71_5          6
1631 #define  STM_ADC_SMPR_SMP_239_5         7
1632
1633 #define STM_ADC_TR_HT           16
1634 #define STM_ADC_TR_LT           0
1635
1636 #define STM_ADC_CCR_LFMEN       25
1637 #define STM_ADC_CCR_VLCDEN      24
1638 #define STM_ADC_CCR_TSEN        23
1639 #define STM_ADC_CCR_VREFEN      22
1640 #define STM_ADC_CCR_PRESC       18
1641
1642 #define STM_ADC_CHSEL_TEMP      18
1643 #define STM_ADC_CHSEL_VREF      17
1644 #define STM_ADC_CHSEL_VLCD      16
1645
1646 struct stm_cal {
1647         uint16_t        ts_cal_cold;    /* 30°C */
1648         uint16_t        vrefint_cal;
1649         uint16_t        unused_c0;
1650         uint16_t        ts_cal_hot;     /* 110°C */
1651 };
1652
1653 extern struct stm_cal   stm_cal;
1654
1655 #define stm_temp_cal_cold       30
1656 #define stm_temp_cal_hot        110
1657
1658 struct stm_dbg_mcu {
1659         uint32_t        idcode;
1660 };
1661
1662 extern struct stm_dbg_mcu       stm_dbg_mcu;
1663
1664 static inline uint16_t
1665 stm_dev_id(void) {
1666         return stm_dbg_mcu.idcode & 0xfff;
1667 }
1668
1669 struct stm_flash_size {
1670         uint16_t        f_size;
1671 };
1672
1673 extern struct stm_flash_size    stm_flash_size_reg;
1674 #define stm_flash_size_reg      (*((struct stm_flash_size *) 0x1ff8007c))
1675
1676 /* Returns flash size in bytes */
1677 extern uint32_t
1678 stm_flash_size(void);
1679
1680 struct stm_device_id {
1681         char            lot_num_4_6[3];
1682         uint8_t         waf_num;
1683         char            lot_num_0_3[4];
1684         uint8_t         unique_id[4];
1685 };
1686
1687 extern struct stm_device_id     stm_device_id;
1688 #define stm_device_id   (*((struct stm_device_id *) 0x1ff80050))
1689
1690 #define STM_NUM_I2C     2
1691
1692 #define STM_I2C_INDEX(channel)  ((channel) - 1)
1693
1694 struct stm_i2c {
1695         vuint32_t       cr1;
1696         vuint32_t       cr2;
1697         vuint32_t       oar1;
1698         vuint32_t       oar2;
1699         vuint32_t       dr;
1700         vuint32_t       sr1;
1701         vuint32_t       sr2;
1702         vuint32_t       ccr;
1703         vuint32_t       trise;
1704 };
1705
1706 extern struct stm_i2c stm_i2c1, stm_i2c2;
1707
1708 #define STM_I2C_CR1_SWRST       15
1709 #define STM_I2C_CR1_ALERT       13
1710 #define STM_I2C_CR1_PEC         12
1711 #define STM_I2C_CR1_POS         11
1712 #define STM_I2C_CR1_ACK         10
1713 #define STM_I2C_CR1_STOP        9
1714 #define STM_I2C_CR1_START       8
1715 #define STM_I2C_CR1_NOSTRETCH   7
1716 #define STM_I2C_CR1_ENGC        6
1717 #define STM_I2C_CR1_ENPEC       5
1718 #define STM_I2C_CR1_ENARP       4
1719 #define STM_I2C_CR1_SMBTYPE     3
1720 #define STM_I2C_CR1_SMBUS       1
1721 #define STM_I2C_CR1_PE          0
1722
1723 #define STM_I2C_CR2_LAST        12
1724 #define STM_I2C_CR2_DMAEN       11
1725 #define STM_I2C_CR2_ITBUFEN     10
1726 #define STM_I2C_CR2_ITEVTEN     9
1727 #define STM_I2C_CR2_ITERREN     8
1728 #define STM_I2C_CR2_FREQ        0
1729 #define  STM_I2C_CR2_FREQ_2_MHZ         2
1730 #define  STM_I2C_CR2_FREQ_4_MHZ         4
1731 #define  STM_I2C_CR2_FREQ_8_MHZ         8
1732 #define  STM_I2C_CR2_FREQ_16_MHZ        16
1733 #define  STM_I2C_CR2_FREQ_24_MHZ        24
1734 #define  STM_I2C_CR2_FREQ_32_MHZ        32
1735 #define  STM_I2C_CR2_FREQ_MASK          0x3f
1736
1737 #define STM_I2C_SR1_SMBALERT    15
1738 #define STM_I2C_SR1_TIMEOUT     14
1739 #define STM_I2C_SR1_PECERR      12
1740 #define STM_I2C_SR1_OVR         11
1741 #define STM_I2C_SR1_AF          10
1742 #define STM_I2C_SR1_ARLO        9
1743 #define STM_I2C_SR1_BERR        8
1744 #define STM_I2C_SR1_TXE         7
1745 #define STM_I2C_SR1_RXNE        6
1746 #define STM_I2C_SR1_STOPF       4
1747 #define STM_I2C_SR1_ADD10       3
1748 #define STM_I2C_SR1_BTF         2
1749 #define STM_I2C_SR1_ADDR        1
1750 #define STM_I2C_SR1_SB          0
1751
1752 #define STM_I2C_SR2_PEC         8
1753 #define  STM_I2C_SR2_PEC_MASK   0xff00
1754 #define STM_I2C_SR2_DUALF       7
1755 #define STM_I2C_SR2_SMBHOST     6
1756 #define STM_I2C_SR2_SMBDEFAULT  5
1757 #define STM_I2C_SR2_GENCALL     4
1758 #define STM_I2C_SR2_TRA         2
1759 #define STM_I2C_SR2_BUSY        1
1760 #define STM_I2C_SR2_MSL         0
1761
1762 #define STM_I2C_CCR_FS          15
1763 #define STM_I2C_CCR_DUTY        14
1764 #define STM_I2C_CCR_CCR         0
1765 #define  STM_I2C_CCR_MASK       0x7ff
1766
1767 struct stm_tim234 {
1768         vuint32_t       cr1;
1769         vuint32_t       cr2;
1770         vuint32_t       smcr;
1771         vuint32_t       dier;
1772
1773         vuint32_t       sr;
1774         vuint32_t       egr;
1775         vuint32_t       ccmr1;
1776         vuint32_t       ccmr2;
1777
1778         vuint32_t       ccer;
1779         vuint32_t       cnt;
1780         vuint32_t       psc;
1781         vuint32_t       arr;
1782
1783         uint32_t        reserved_30;
1784         vuint32_t       ccr1;
1785         vuint32_t       ccr2;
1786         vuint32_t       ccr3;
1787
1788         vuint32_t       ccr4;
1789         uint32_t        reserved_44;
1790         vuint32_t       dcr;
1791         vuint32_t       dmar;
1792
1793         uint32_t        reserved_50;
1794 };
1795
1796 extern struct stm_tim234 stm_tim2, stm_tim3, stm_tim4;
1797
1798 #define STM_TIM234_CR1_CKD      8
1799 #define  STM_TIM234_CR1_CKD_1           0
1800 #define  STM_TIM234_CR1_CKD_2           1
1801 #define  STM_TIM234_CR1_CKD_4           2
1802 #define  STM_TIM234_CR1_CKD_MASK        3
1803 #define STM_TIM234_CR1_ARPE     7
1804 #define STM_TIM234_CR1_CMS      5
1805 #define  STM_TIM234_CR1_CMS_EDGE        0
1806 #define  STM_TIM234_CR1_CMS_CENTER_1    1
1807 #define  STM_TIM234_CR1_CMS_CENTER_2    2
1808 #define  STM_TIM234_CR1_CMS_CENTER_3    3
1809 #define  STM_TIM234_CR1_CMS_MASK        3
1810 #define STM_TIM234_CR1_DIR      4
1811 #define  STM_TIM234_CR1_DIR_UP          0
1812 #define  STM_TIM234_CR1_DIR_DOWN        1
1813 #define STM_TIM234_CR1_OPM      3
1814 #define STM_TIM234_CR1_URS      2
1815 #define STM_TIM234_CR1_UDIS     1
1816 #define STM_TIM234_CR1_CEN      0
1817
1818 #define STM_TIM234_CR2_TI1S     7
1819 #define STM_TIM234_CR2_MMS      4
1820 #define  STM_TIM234_CR2_MMS_RESET               0
1821 #define  STM_TIM234_CR2_MMS_ENABLE              1
1822 #define  STM_TIM234_CR2_MMS_UPDATE              2
1823 #define  STM_TIM234_CR2_MMS_COMPARE_PULSE       3
1824 #define  STM_TIM234_CR2_MMS_COMPARE_OC1REF      4
1825 #define  STM_TIM234_CR2_MMS_COMPARE_OC2REF      5
1826 #define  STM_TIM234_CR2_MMS_COMPARE_OC3REF      6
1827 #define  STM_TIM234_CR2_MMS_COMPARE_OC4REF      7
1828 #define  STM_TIM234_CR2_MMS_MASK                7
1829 #define STM_TIM234_CR2_CCDS     3
1830
1831 #define STM_TIM234_SMCR_ETP     15
1832 #define STM_TIM234_SMCR_ECE     14
1833 #define STM_TIM234_SMCR_ETPS    12
1834 #define  STM_TIM234_SMCR_ETPS_OFF               0
1835 #define  STM_TIM234_SMCR_ETPS_DIV_2             1
1836 #define  STM_TIM234_SMCR_ETPS_DIV_4             2
1837 #define  STM_TIM234_SMCR_ETPS_DIV_8             3
1838 #define  STM_TIM234_SMCR_ETPS_MASK              3
1839 #define STM_TIM234_SMCR_ETF     8
1840 #define  STM_TIM234_SMCR_ETF_NONE               0
1841 #define  STM_TIM234_SMCR_ETF_INT_N_2            1
1842 #define  STM_TIM234_SMCR_ETF_INT_N_4            2
1843 #define  STM_TIM234_SMCR_ETF_INT_N_8            3
1844 #define  STM_TIM234_SMCR_ETF_DTS_2_N_6          4
1845 #define  STM_TIM234_SMCR_ETF_DTS_2_N_8          5
1846 #define  STM_TIM234_SMCR_ETF_DTS_4_N_6          6
1847 #define  STM_TIM234_SMCR_ETF_DTS_4_N_8          7
1848 #define  STM_TIM234_SMCR_ETF_DTS_8_N_6          8
1849 #define  STM_TIM234_SMCR_ETF_DTS_8_N_8          9
1850 #define  STM_TIM234_SMCR_ETF_DTS_16_N_5         10
1851 #define  STM_TIM234_SMCR_ETF_DTS_16_N_6         11
1852 #define  STM_TIM234_SMCR_ETF_DTS_16_N_8         12
1853 #define  STM_TIM234_SMCR_ETF_DTS_32_N_5         13
1854 #define  STM_TIM234_SMCR_ETF_DTS_32_N_6         14
1855 #define  STM_TIM234_SMCR_ETF_DTS_32_N_8         15
1856 #define  STM_TIM234_SMCR_ETF_MASK               15
1857 #define STM_TIM234_SMCR_MSM     7
1858 #define STM_TIM234_SMCR_TS      4
1859 #define  STM_TIM234_SMCR_TS_ITR0                0
1860 #define  STM_TIM234_SMCR_TS_ITR1                1
1861 #define  STM_TIM234_SMCR_TS_ITR2                2
1862 #define  STM_TIM234_SMCR_TS_ITR3                3
1863 #define  STM_TIM234_SMCR_TS_TI1F_ED             4
1864 #define  STM_TIM234_SMCR_TS_TI1FP1              5
1865 #define  STM_TIM234_SMCR_TS_TI2FP2              6
1866 #define  STM_TIM234_SMCR_TS_ETRF                7
1867 #define  STM_TIM234_SMCR_TS_MASK                7
1868 #define STM_TIM234_SMCR_OCCS    3
1869 #define STM_TIM234_SMCR_SMS     0
1870 #define  STM_TIM234_SMCR_SMS_DISABLE            0
1871 #define  STM_TIM234_SMCR_SMS_ENCODER_MODE_1     1
1872 #define  STM_TIM234_SMCR_SMS_ENCODER_MODE_2     2
1873 #define  STM_TIM234_SMCR_SMS_ENCODER_MODE_3     3
1874 #define  STM_TIM234_SMCR_SMS_RESET_MODE         4
1875 #define  STM_TIM234_SMCR_SMS_GATED_MODE         5
1876 #define  STM_TIM234_SMCR_SMS_TRIGGER_MODE       6
1877 #define  STM_TIM234_SMCR_SMS_EXTERNAL_CLOCK     7
1878 #define  STM_TIM234_SMCR_SMS_MASK               7
1879
1880 #define STM_TIM234_DIER_TDE             14
1881 #define STM_TIM234_DIER_CC4DE           12
1882 #define STM_TIM234_DIER_CC3DE           11
1883 #define STM_TIM234_DIER_CC2DE           10
1884 #define STM_TIM234_DIER_CC1DE           9
1885 #define STM_TIM234_DIER_UDE             8
1886
1887 #define STM_TIM234_DIER_TIE             6
1888 #define STM_TIM234_DIER_CC4IE           4
1889 #define STM_TIM234_DIER_CC3IE           3
1890 #define STM_TIM234_DIER_CC2IE           2
1891 #define STM_TIM234_DIER_CC1IE           1
1892 #define STM_TIM234_DIER_UIE             0
1893
1894 #define STM_TIM234_SR_CC4OF     12
1895 #define STM_TIM234_SR_CC3OF     11
1896 #define STM_TIM234_SR_CC2OF     10
1897 #define STM_TIM234_SR_CC1OF     9
1898 #define STM_TIM234_SR_TIF       6
1899 #define STM_TIM234_SR_CC4IF     4
1900 #define STM_TIM234_SR_CC3IF     3
1901 #define STM_TIM234_SR_CC2IF     2
1902 #define STM_TIM234_SR_CC1IF     1
1903 #define STM_TIM234_SR_UIF       0
1904
1905 #define STM_TIM234_EGR_TG       6
1906 #define STM_TIM234_EGR_CC4G     4
1907 #define STM_TIM234_EGR_CC3G     3
1908 #define STM_TIM234_EGR_CC2G     2
1909 #define STM_TIM234_EGR_CC1G     1
1910 #define STM_TIM234_EGR_UG       0
1911
1912 #define STM_TIM234_CCMR1_OC2CE  15
1913 #define STM_TIM234_CCMR1_OC2M   12
1914 #define  STM_TIM234_CCMR1_OC2M_FROZEN                   0
1915 #define  STM_TIM234_CCMR1_OC2M_SET_HIGH_ON_MATCH        1
1916 #define  STM_TIM234_CCMR1_OC2M_SET_LOW_ON_MATCH         2
1917 #define  STM_TIM234_CCMR1_OC2M_TOGGLE                   3
1918 #define  STM_TIM234_CCMR1_OC2M_FORCE_LOW                4
1919 #define  STM_TIM234_CCMR1_OC2M_FORCE_HIGH               5
1920 #define  STM_TIM234_CCMR1_OC2M_PWM_MODE_1               6
1921 #define  STM_TIM234_CCMR1_OC2M_PWM_MODE_2               7
1922 #define  STM_TIM234_CCMR1_OC2M_MASK                     7
1923 #define STM_TIM234_CCMR1_OC2PE  11
1924 #define STM_TIM234_CCMR1_OC2FE  10
1925 #define STM_TIM234_CCMR1_CC2S   8
1926 #define  STM_TIM234_CCMR1_CC2S_OUTPUT                   0
1927 #define  STM_TIM234_CCMR1_CC2S_INPUT_TI2                1
1928 #define  STM_TIM234_CCMR1_CC2S_INPUT_TI1                2
1929 #define  STM_TIM234_CCMR1_CC2S_INPUT_TRC                3
1930 #define  STM_TIM234_CCMR1_CC2S_MASK                     3
1931
1932 #define STM_TIM234_CCMR1_OC1CE  7
1933 #define STM_TIM234_CCMR1_OC1M   4
1934 #define  STM_TIM234_CCMR1_OC1M_FROZEN                   0
1935 #define  STM_TIM234_CCMR1_OC1M_SET_HIGH_ON_MATCH        1
1936 #define  STM_TIM234_CCMR1_OC1M_SET_LOW_ON_MATCH         2
1937 #define  STM_TIM234_CCMR1_OC1M_TOGGLE                   3
1938 #define  STM_TIM234_CCMR1_OC1M_FORCE_LOW                4
1939 #define  STM_TIM234_CCMR1_OC1M_FORCE_HIGH               5
1940 #define  STM_TIM234_CCMR1_OC1M_PWM_MODE_1               6
1941 #define  STM_TIM234_CCMR1_OC1M_PWM_MODE_2               7
1942 #define  STM_TIM234_CCMR1_OC1M_MASK                     7
1943 #define STM_TIM234_CCMR1_OC1PE  3
1944 #define STM_TIM234_CCMR1_OC1FE  2
1945 #define STM_TIM234_CCMR1_CC1S   0
1946 #define  STM_TIM234_CCMR1_CC1S_OUTPUT                   0
1947 #define  STM_TIM234_CCMR1_CC1S_INPUT_TI1                1
1948 #define  STM_TIM234_CCMR1_CC1S_INPUT_TI2                2
1949 #define  STM_TIM234_CCMR1_CC1S_INPUT_TRC                3
1950 #define  STM_TIM234_CCMR1_CC1S_MASK                     3
1951
1952 #define STM_TIM234_CCMR1_IC2F   12
1953 #define  STM_TIM234_CCMR1_IC2F_NONE                     0
1954 #define  STM_TIM234_CCMR1_IC2F_CK_INT_N_2               1
1955 #define  STM_TIM234_CCMR1_IC2F_CK_INT_N_4               2
1956 #define  STM_TIM234_CCMR1_IC2F_CK_INT_N_8               3
1957 #define  STM_TIM234_CCMR1_IC2F_DTS_2_N_6                4
1958 #define  STM_TIM234_CCMR1_IC2F_DTS_2_N_8                5
1959 #define  STM_TIM234_CCMR1_IC2F_DTS_4_N_6                6
1960 #define  STM_TIM234_CCMR1_IC2F_DTS_4_N_8                7
1961 #define  STM_TIM234_CCMR1_IC2F_DTS_8_N_6                8
1962 #define  STM_TIM234_CCMR1_IC2F_DTS_8_N_8                9
1963 #define  STM_TIM234_CCMR1_IC2F_DTS_16_N_5               10
1964 #define  STM_TIM234_CCMR1_IC2F_DTS_16_N_6               11
1965 #define  STM_TIM234_CCMR1_IC2F_DTS_16_N_8               12
1966 #define  STM_TIM234_CCMR1_IC2F_DTS_32_N_5               13
1967 #define  STM_TIM234_CCMR1_IC2F_DTS_32_N_6               14
1968 #define  STM_TIM234_CCMR1_IC2F_DTS_32_N_8               15
1969 #define STM_TIM234_CCMR1_IC2PSC 10
1970 #define  STM_TIM234_CCMR1_IC2PSC_NONE                   0
1971 #define  STM_TIM234_CCMR1_IC2PSC_2                      1
1972 #define  STM_TIM234_CCMR1_IC2PSC_4                      2
1973 #define  STM_TIM234_CCMR1_IC2PSC_8                      3
1974 #define STM_TIM234_CCMR1_IC1F   4
1975 #define  STM_TIM234_CCMR1_IC1F_NONE                     0
1976 #define  STM_TIM234_CCMR1_IC1F_CK_INT_N_2               1
1977 #define  STM_TIM234_CCMR1_IC1F_CK_INT_N_4               2
1978 #define  STM_TIM234_CCMR1_IC1F_CK_INT_N_8               3
1979 #define  STM_TIM234_CCMR1_IC1F_DTS_2_N_6                4
1980 #define  STM_TIM234_CCMR1_IC1F_DTS_2_N_8                5
1981 #define  STM_TIM234_CCMR1_IC1F_DTS_4_N_6                6
1982 #define  STM_TIM234_CCMR1_IC1F_DTS_4_N_8                7
1983 #define  STM_TIM234_CCMR1_IC1F_DTS_8_N_6                8
1984 #define  STM_TIM234_CCMR1_IC1F_DTS_8_N_8                9
1985 #define  STM_TIM234_CCMR1_IC1F_DTS_16_N_5               10
1986 #define  STM_TIM234_CCMR1_IC1F_DTS_16_N_6               11
1987 #define  STM_TIM234_CCMR1_IC1F_DTS_16_N_8               12
1988 #define  STM_TIM234_CCMR1_IC1F_DTS_32_N_5               13
1989 #define  STM_TIM234_CCMR1_IC1F_DTS_32_N_6               14
1990 #define  STM_TIM234_CCMR1_IC1F_DTS_32_N_8               15
1991 #define STM_TIM234_CCMR1_IC1PSC 2
1992 #define  STM_TIM234_CCMR1_IC1PSC_NONE                   0
1993 #define  STM_TIM234_CCMR1_IC1PSC_2                      1
1994 #define  STM_TIM234_CCMR1_IC1PSC_4                      2
1995 #define  STM_TIM234_CCMR1_IC1PSC_8                      3
1996
1997 #define STM_TIM234_CCMR2_OC4CE  15
1998 #define STM_TIM234_CCMR2_OC4M   12
1999 #define  STM_TIM234_CCMR2_OC4M_FROZEN                   0
2000 #define  STM_TIM234_CCMR2_OC4M_SET_HIGH_ON_MATCH        1
2001 #define  STM_TIM234_CCMR2_OC4M_SET_LOW_ON_MATCH         2
2002 #define  STM_TIM234_CCMR2_OC4M_TOGGLE                   3
2003 #define  STM_TIM234_CCMR2_OC4M_FORCE_LOW                4
2004 #define  STM_TIM234_CCMR2_OC4M_FORCE_HIGH               5
2005 #define  STM_TIM234_CCMR2_OC4M_PWM_MODE_1               6
2006 #define  STM_TIM234_CCMR2_OC4M_PWM_MODE_2               7
2007 #define  STM_TIM234_CCMR2_OC4M_MASK                     7
2008 #define STM_TIM234_CCMR2_OC4PE  11
2009 #define STM_TIM234_CCMR2_OC4FE  10
2010 #define STM_TIM234_CCMR2_CC4S   8
2011 #define  STM_TIM234_CCMR2_CC4S_OUTPUT                   0
2012 #define  STM_TIM234_CCMR2_CC4S_INPUT_TI4                1
2013 #define  STM_TIM234_CCMR2_CC4S_INPUT_TI3                2
2014 #define  STM_TIM234_CCMR2_CC4S_INPUT_TRC                3
2015 #define  STM_TIM234_CCMR2_CC4S_MASK                     3
2016
2017 #define STM_TIM234_CCMR2_OC3CE  7
2018 #define STM_TIM234_CCMR2_OC3M   4
2019 #define  STM_TIM234_CCMR2_OC3M_FROZEN                   0
2020 #define  STM_TIM234_CCMR2_OC3M_SET_HIGH_ON_MATCH        1
2021 #define  STM_TIM234_CCMR2_OC3M_SET_LOW_ON_MATCH         2
2022 #define  STM_TIM234_CCMR2_OC3M_TOGGLE                   3
2023 #define  STM_TIM234_CCMR2_OC3M_FORCE_LOW                4
2024 #define  STM_TIM234_CCMR2_OC3M_FORCE_HIGH               5
2025 #define  STM_TIM234_CCMR2_OC3M_PWM_MODE_1               6
2026 #define  STM_TIM234_CCMR2_OC3M_PWM_MODE_2               7
2027 #define  STM_TIM234_CCMR2_OC3M_MASK                     7
2028 #define STM_TIM234_CCMR2_OC3PE  3
2029 #define STM_TIM234_CCMR2_OC3FE  2
2030 #define STM_TIM234_CCMR2_CC3S   0
2031 #define  STM_TIM234_CCMR2_CC3S_OUTPUT                   0
2032 #define  STM_TIM234_CCMR2_CC3S_INPUT_TI3                1
2033 #define  STM_TIM234_CCMR2_CC3S_INPUT_TI4                2
2034 #define  STM_TIM234_CCMR2_CC3S_INPUT_TRC                3
2035 #define  STM_TIM234_CCMR2_CC3S_MASK                     3
2036
2037 #define STM_TIM234_CCER_CC4NP   15
2038 #define STM_TIM234_CCER_CC4P    13
2039 #define  STM_TIM234_CCER_CC4P_ACTIVE_HIGH       0
2040 #define  STM_TIM234_CCER_CC4P_ACTIVE_LOW        1
2041 #define STM_TIM234_CCER_CC4E    12
2042 #define STM_TIM234_CCER_CC3NP   11
2043 #define STM_TIM234_CCER_CC3P    9
2044 #define  STM_TIM234_CCER_CC3P_ACTIVE_HIGH       0
2045 #define  STM_TIM234_CCER_CC3P_ACTIVE_LOW        1
2046 #define STM_TIM234_CCER_CC3E    8
2047 #define STM_TIM234_CCER_CC2NP   7
2048 #define STM_TIM234_CCER_CC2P    5
2049 #define  STM_TIM234_CCER_CC2P_ACTIVE_HIGH       0
2050 #define  STM_TIM234_CCER_CC2P_ACTIVE_LOW        1
2051 #define STM_TIM234_CCER_CC2E    4
2052 #define STM_TIM234_CCER_CC1NP   3
2053 #define STM_TIM234_CCER_CC1P    1
2054 #define  STM_TIM234_CCER_CC1P_ACTIVE_HIGH       0
2055 #define  STM_TIM234_CCER_CC1P_ACTIVE_LOW        1
2056 #define STM_TIM234_CCER_CC1E    0
2057
2058 struct stm_exti {
2059         vuint32_t       imr;
2060         vuint32_t       emr;
2061         vuint32_t       rtsr;
2062         vuint32_t       ftsr;
2063
2064         vuint32_t       swier;
2065         vuint32_t       pr;
2066 };
2067
2068 extern struct stm_exti stm_exti;
2069 #define stm_exti (*(struct stm_exti *) 0x40010400)
2070
2071 struct stm_vrefint_cal {
2072         vuint16_t       vrefint_cal;
2073 };
2074
2075 extern struct stm_vrefint_cal stm_vrefint_cal;
2076 #define stm_vrefint_cal (*(struct stm_vrefint_cal *) 0x1ff80078)
2077
2078 /* Flash interface */
2079
2080 struct stm_flash {
2081         vuint32_t       acr;
2082         vuint32_t       pecr;
2083         vuint32_t       pdkeyr;
2084         vuint32_t       pekeyr;
2085
2086         vuint32_t       prgkeyr;
2087         vuint32_t       optkeyr;
2088         vuint32_t       sr;
2089         vuint32_t       obr;
2090
2091         vuint32_t       wrpr;
2092 };
2093
2094
2095 extern uint32_t __storage[], __storage_size[];
2096
2097 #define STM_FLASH_PAGE_SIZE     128
2098
2099 #define ao_storage_unit         128
2100 #define ao_storage_total        ((uintptr_t) __storage_size)
2101 #define ao_storage_block        STM_FLASH_PAGE_SIZE
2102 #define AO_STORAGE_ERASED_BYTE  0x00
2103
2104 extern struct stm_flash stm_flash;
2105
2106 #define STM_FLASH_ACR_PRE_READ  (6)
2107 #define STM_FLASH_ACR_DISAB_BUF (5)
2108 #define STM_FLASH_ACR_RUN_PD    (4)
2109 #define STM_FLASH_ACR_SLEEP_PD  (3)
2110 #define STM_FLASH_ACR_PRFEN     (1)
2111 #define STM_FLASH_ACR_LATENCY   (0)
2112
2113 #define STM_FLASH_PECR_NZDISABLE        23
2114 #define STM_FLASH_PECR_OBL_LAUNCH       18
2115 #define STM_FLASH_PECR_ERRIE            17
2116 #define STM_FLASH_PECR_EOPIE            16
2117 #define STM_FLASH_PECR_PARRALELLBANK    15
2118 #define STM_FLASH_PECR_FPRG             10
2119 #define STM_FLASH_PECR_ERASE            9
2120 #define STM_FLASH_PECR_FIX              8
2121 #define STM_FLASH_PECR_DATA             4
2122 #define STM_FLASH_PECR_PROG             3
2123 #define STM_FLASH_PECR_OPT_LOCK         2
2124 #define STM_FLASH_PECR_PRG_LOCK         1
2125 #define STM_FLASH_PECR_PE_LOCK          0
2126
2127 #define STM_FLASH_SR_OPTVERR            11
2128 #define STM_FLASH_SR_SIZERR             10
2129 #define STM_FLASH_SR_PGAERR             9
2130 #define STM_FLASH_SR_WRPERR             8
2131 #define STM_FLASH_SR_READY              3
2132 #define STM_FLASH_SR_ENDHV              2
2133 #define STM_FLASH_SR_EOP                1
2134 #define STM_FLASH_SR_BSY                0
2135
2136 #define STM_FLASH_OPTKEYR_OPTKEY1 0xFBEAD9C8
2137 #define STM_FLASH_OPTKEYR_OPTKEY2 0x24252627
2138
2139 #define STM_FLASH_PEKEYR_PEKEY1 0x89ABCDEF
2140 #define STM_FLASH_PEKEYR_PEKEY2 0x02030405
2141
2142 #define STM_FLASH_PRGKEYR_PRGKEY1 0x8C9DAEBF
2143 #define STM_FLASH_PRGKEYR_PRGKEY2 0x13141516
2144
2145 #endif /* _STM32L0_H_ */