distortos  v0.5.0
object-oriented C++ RTOS for microcontrollers
STM32-GPIOv2.hpp
Go to the documentation of this file.
1 
12 #ifndef SOURCE_CHIP_STM32_PERIPHERALS_GPIOV2_INCLUDE_DISTORTOS_CHIP_STM32_GPIOV2_HPP_
13 #define SOURCE_CHIP_STM32_PERIPHERALS_GPIOV2_INCLUDE_DISTORTOS_CHIP_STM32_GPIOV2_HPP_
14 
16 
17 #include <utility>
18 
19 namespace distortos
20 {
21 
22 namespace chip
23 {
24 
25 /*---------------------------------------------------------------------------------------------------------------------+
26 | global types
27 +---------------------------------------------------------------------------------------------------------------------*/
28 
29 #ifdef GPIOA
30 static_assert((GPIOA_BASE & 15) == 0, "Cannot combine pin number with GPIOA address!");
31 #endif // def GPIOA
32 
33 #ifdef GPIOB
34 static_assert((GPIOB_BASE & 15) == 0, "Cannot combine pin number with GPIOB address!");
35 #endif // def GPIOB
36 
37 #ifdef GPIOC
38 static_assert((GPIOC_BASE & 15) == 0, "Cannot combine pin number with GPIOC address!");
39 #endif // def GPIOC
40 
41 #ifdef GPIOD
42 static_assert((GPIOD_BASE & 15) == 0, "Cannot combine pin number with GPIOD address!");
43 #endif // def GPIOD
44 
45 #ifdef GPIOE
46 static_assert((GPIOE_BASE & 15) == 0, "Cannot combine pin number with GPIOE address!");
47 #endif // def GPIOE
48 
49 #ifdef GPIOF
50 static_assert((GPIOF_BASE & 15) == 0, "Cannot combine pin number with GPIOF address!");
51 #endif // def GPIOF
52 
53 #ifdef GPIOG
54 static_assert((GPIOG_BASE & 15) == 0, "Cannot combine pin number with GPIOG address!");
55 #endif // def GPIOG
56 
57 #ifdef GPIOH
58 static_assert((GPIOH_BASE & 15) == 0, "Cannot combine pin number with GPIOH address!");
59 #endif // def GPIOH
60 
61 #ifdef GPIOI
62 static_assert((GPIOI_BASE & 15) == 0, "Cannot combine pin number with GPIOI address!");
63 #endif // def GPIOI
64 
65 #ifdef GPIOJ
66 static_assert((GPIOJ_BASE & 15) == 0, "Cannot combine pin number with GPIOJ address!");
67 #endif // def GPIOJ
68 
69 #ifdef GPIOK
70 static_assert((GPIOK_BASE & 15) == 0, "Cannot combine pin number with GPIOK address!");
71 #endif // def GPIOK
72 
74 enum class Pin : uint32_t
75 {
76 #ifdef GPIOA
77 
79  pa0 = GPIOA_BASE | 0,
81  pa1 = GPIOA_BASE | 1,
83  pa2 = GPIOA_BASE | 2,
85  pa3 = GPIOA_BASE | 3,
87  pa4 = GPIOA_BASE | 4,
89  pa5 = GPIOA_BASE | 5,
91  pa6 = GPIOA_BASE | 6,
93  pa7 = GPIOA_BASE | 7,
95  pa8 = GPIOA_BASE | 8,
97  pa9 = GPIOA_BASE | 9,
99  pa10 = GPIOA_BASE | 10,
101  pa11 = GPIOA_BASE | 11,
103  pa12 = GPIOA_BASE | 12,
105  pa13 = GPIOA_BASE | 13,
107  pa14 = GPIOA_BASE | 14,
109  pa15 = GPIOA_BASE | 15,
110 
111 #endif // def GPIOA
112 
113 #ifdef GPIOB
114 
116  pb0 = GPIOB_BASE | 0,
118  pb1 = GPIOB_BASE | 1,
120  pb2 = GPIOB_BASE | 2,
122  pb3 = GPIOB_BASE | 3,
124  pb4 = GPIOB_BASE | 4,
126  pb5 = GPIOB_BASE | 5,
128  pb6 = GPIOB_BASE | 6,
130  pb7 = GPIOB_BASE | 7,
132  pb8 = GPIOB_BASE | 8,
134  pb9 = GPIOB_BASE | 9,
136  pb10 = GPIOB_BASE | 10,
138  pb11 = GPIOB_BASE | 11,
140  pb12 = GPIOB_BASE | 12,
142  pb13 = GPIOB_BASE | 13,
144  pb14 = GPIOB_BASE | 14,
146  pb15 = GPIOB_BASE | 15,
147 
148 #endif // def GPIOB
149 
150 #ifdef GPIOC
151 
153  pc0 = GPIOC_BASE | 0,
155  pc1 = GPIOC_BASE | 1,
157  pc2 = GPIOC_BASE | 2,
159  pc3 = GPIOC_BASE | 3,
161  pc4 = GPIOC_BASE | 4,
163  pc5 = GPIOC_BASE | 5,
165  pc6 = GPIOC_BASE | 6,
167  pc7 = GPIOC_BASE | 7,
169  pc8 = GPIOC_BASE | 8,
171  pc9 = GPIOC_BASE | 9,
173  pc10 = GPIOC_BASE | 10,
175  pc11 = GPIOC_BASE | 11,
177  pc12 = GPIOC_BASE | 12,
179  pc13 = GPIOC_BASE | 13,
181  pc14 = GPIOC_BASE | 14,
183  pc15 = GPIOC_BASE | 15,
184 
185 #endif // def GPIOC
186 
187 #ifdef GPIOD
188 
190  pd0 = GPIOD_BASE | 0,
192  pd1 = GPIOD_BASE | 1,
194  pd2 = GPIOD_BASE | 2,
196  pd3 = GPIOD_BASE | 3,
198  pd4 = GPIOD_BASE | 4,
200  pd5 = GPIOD_BASE | 5,
202  pd6 = GPIOD_BASE | 6,
204  pd7 = GPIOD_BASE | 7,
206  pd8 = GPIOD_BASE | 8,
208  pd9 = GPIOD_BASE | 9,
210  pd10 = GPIOD_BASE | 10,
212  pd11 = GPIOD_BASE | 11,
214  pd12 = GPIOD_BASE | 12,
216  pd13 = GPIOD_BASE | 13,
218  pd14 = GPIOD_BASE | 14,
220  pd15 = GPIOD_BASE | 15,
221 
222 #endif // def GPIOD
223 
224 #ifdef GPIOE
225 
227  pe0 = GPIOE_BASE | 0,
229  pe1 = GPIOE_BASE | 1,
231  pe2 = GPIOE_BASE | 2,
233  pe3 = GPIOE_BASE | 3,
235  pe4 = GPIOE_BASE | 4,
237  pe5 = GPIOE_BASE | 5,
239  pe6 = GPIOE_BASE | 6,
241  pe7 = GPIOE_BASE | 7,
243  pe8 = GPIOE_BASE | 8,
245  pe9 = GPIOE_BASE | 9,
247  pe10 = GPIOE_BASE | 10,
249  pe11 = GPIOE_BASE | 11,
251  pe12 = GPIOE_BASE | 12,
253  pe13 = GPIOE_BASE | 13,
255  pe14 = GPIOE_BASE | 14,
257  pe15 = GPIOE_BASE | 15,
258 
259 #endif // def GPIOE
260 
261 #ifdef GPIOF
262 
264  pf0 = GPIOF_BASE | 0,
266  pf1 = GPIOF_BASE | 1,
268  pf2 = GPIOF_BASE | 2,
270  pf3 = GPIOF_BASE | 3,
272  pf4 = GPIOF_BASE | 4,
274  pf5 = GPIOF_BASE | 5,
276  pf6 = GPIOF_BASE | 6,
278  pf7 = GPIOF_BASE | 7,
280  pf8 = GPIOF_BASE | 8,
282  pf9 = GPIOF_BASE | 9,
284  pf10 = GPIOF_BASE | 10,
286  pf11 = GPIOF_BASE | 11,
288  pf12 = GPIOF_BASE | 12,
290  pf13 = GPIOF_BASE | 13,
292  pf14 = GPIOF_BASE | 14,
294  pf15 = GPIOF_BASE | 15,
295 
296 #endif // def GPIOF
297 
298 #ifdef GPIOG
299 
301  pg0 = GPIOG_BASE | 0,
303  pg1 = GPIOG_BASE | 1,
305  pg2 = GPIOG_BASE | 2,
307  pg3 = GPIOG_BASE | 3,
309  pg4 = GPIOG_BASE | 4,
311  pg5 = GPIOG_BASE | 5,
313  pg6 = GPIOG_BASE | 6,
315  pg7 = GPIOG_BASE | 7,
317  pg8 = GPIOG_BASE | 8,
319  pg9 = GPIOG_BASE | 9,
321  pg10 = GPIOG_BASE | 10,
323  pg11 = GPIOG_BASE | 11,
325  pg12 = GPIOG_BASE | 12,
327  pg13 = GPIOG_BASE | 13,
329  pg14 = GPIOG_BASE | 14,
331  pg15 = GPIOG_BASE | 15,
332 
333 #endif // def GPIOG
334 
335 #ifdef GPIOH
336 
338  ph0 = GPIOH_BASE | 0,
340  ph1 = GPIOH_BASE | 1,
342  ph2 = GPIOH_BASE | 2,
344  ph3 = GPIOH_BASE | 3,
346  ph4 = GPIOH_BASE | 4,
348  ph5 = GPIOH_BASE | 5,
350  ph6 = GPIOH_BASE | 6,
352  ph7 = GPIOH_BASE | 7,
354  ph8 = GPIOH_BASE | 8,
356  ph9 = GPIOH_BASE | 9,
358  ph10 = GPIOH_BASE | 10,
360  ph11 = GPIOH_BASE | 11,
362  ph12 = GPIOH_BASE | 12,
364  ph13 = GPIOH_BASE | 13,
366  ph14 = GPIOH_BASE | 14,
368  ph15 = GPIOH_BASE | 15,
369 
370 #endif // def GPIOH
371 
372 #ifdef GPIOI
373 
375  pi0 = GPIOI_BASE | 0,
377  pi1 = GPIOI_BASE | 1,
379  pi2 = GPIOI_BASE | 2,
381  pi3 = GPIOI_BASE | 3,
383  pi4 = GPIOI_BASE | 4,
385  pi5 = GPIOI_BASE | 5,
387  pi6 = GPIOI_BASE | 6,
389  pi7 = GPIOI_BASE | 7,
391  pi8 = GPIOI_BASE | 8,
393  pi9 = GPIOI_BASE | 9,
395  pi10 = GPIOI_BASE | 10,
397  pi11 = GPIOI_BASE | 11,
399  pi12 = GPIOI_BASE | 12,
401  pi13 = GPIOI_BASE | 13,
403  pi14 = GPIOI_BASE | 14,
405  pi15 = GPIOI_BASE | 15,
406 
407 #endif // def GPIOI
408 
409 #ifdef GPIOJ
410 
412  pj0 = GPIOJ_BASE | 0,
414  pj1 = GPIOJ_BASE | 1,
416  pj2 = GPIOJ_BASE | 2,
418  pj3 = GPIOJ_BASE | 3,
420  pj4 = GPIOJ_BASE | 4,
422  pj5 = GPIOJ_BASE | 5,
424  pj6 = GPIOJ_BASE | 6,
426  pj7 = GPIOJ_BASE | 7,
428  pj8 = GPIOJ_BASE | 8,
430  pj9 = GPIOJ_BASE | 9,
432  pj10 = GPIOJ_BASE | 10,
434  pj11 = GPIOJ_BASE | 11,
436  pj12 = GPIOJ_BASE | 12,
438  pj13 = GPIOJ_BASE | 13,
440  pj14 = GPIOJ_BASE | 14,
442  pj15 = GPIOJ_BASE | 15,
443 
444 #endif // def GPIOJ
445 
446 #ifdef GPIOK
447 
449  pk0 = GPIOK_BASE | 0,
451  pk1 = GPIOK_BASE | 1,
453  pk2 = GPIOK_BASE | 2,
455  pk3 = GPIOK_BASE | 3,
457  pk4 = GPIOK_BASE | 4,
459  pk5 = GPIOK_BASE | 5,
461  pk6 = GPIOK_BASE | 6,
463  pk7 = GPIOK_BASE | 7,
465  pk8 = GPIOK_BASE | 8,
467  pk9 = GPIOK_BASE | 9,
469  pk10 = GPIOK_BASE | 10,
471  pk11 = GPIOK_BASE | 11,
473  pk12 = GPIOK_BASE | 12,
475  pk13 = GPIOK_BASE | 13,
477  pk14 = GPIOK_BASE | 14,
479  pk15 = GPIOK_BASE | 15,
480 
481 #endif // def GPIOK
482 };
483 
485 enum class PinAlternateFunction : uint8_t
486 {
488  af0,
490  af1,
492  af2,
494  af3,
496  af4,
498  af5,
500  af6,
502  af7,
503 #ifdef CONFIG_CHIP_STM32_GPIOV2_HAS_4_AF_BITS
504  af8,
507  af9,
509  af10,
511  af11,
513  af12,
515  af13,
517  af14,
519  af15,
520 #endif // def CONFIG_CHIP_STM32_GPIOV2_HAS_4_AF_BITS
521 };
522 
524 enum class PinMode : uint8_t
525 {
527  input,
529  output,
533  analog,
534 };
535 
537 enum class PinOutputSpeed : uint8_t
538 {
540  low,
542  medium,
543 #ifdef CONFIG_CHIP_STM32_GPIOV2_HAS_HIGH_SPEED
544  high,
546 #endif // def CONFIG_CHIP_STM32_GPIOV2_HAS_HIGH_SPEED
547  veryHigh = 3,
549 };
550 
552 enum class PinPull : uint8_t
553 {
555  none,
557  up,
559  down,
560 };
561 
562 /*---------------------------------------------------------------------------------------------------------------------+
563 | global functions
564 +---------------------------------------------------------------------------------------------------------------------*/
565 
578 void configurePin(Pin pin, PinMode mode, bool openDrain, PinOutputSpeed outputSpeed, PinPull pull,
579  PinAlternateFunction alternateFunction, bool initialState = {});
580 
591 inline void configureAlternateFunctionPin(const Pin pin, const PinAlternateFunction alternateFunction,
592  const bool openDrain = {}, const PinOutputSpeed outputSpeed = {}, const PinPull pull = {})
593 {
594  configurePin(pin, PinMode::alternateFunction, openDrain, outputSpeed, pull, alternateFunction, {});
595 }
596 
610 __attribute__ ((deprecated("Use configureAlternateFunctionPin(Pin, PinAlternateFunction, bool, PinOutputSpeed, "
611  "PinPull)")))
612 inline void configureAlternateFunctionPin(const Pin pin, const bool openDrain, const PinOutputSpeed outputSpeed,
613  const PinPull pull, const PinAlternateFunction alternateFunction)
614 {
615  configurePin(pin, PinMode::alternateFunction, openDrain, outputSpeed, pull, alternateFunction, {});
616 }
617 
624 inline void configureAnalogPin(const Pin pin)
625 {
626  configurePin(pin, PinMode::analog, {}, {}, {}, {}, {});
627 }
628 
636 inline void configureInputPin(const Pin pin, const PinPull pull = {})
637 {
638  configurePin(pin, PinMode::input, {}, {}, pull, {}, {});
639 }
640 
651 inline void configureOutputPin(const Pin pin, const bool openDrain = {}, const PinOutputSpeed outputSpeed = {},
652  const PinPull pull = {}, const bool initialState = {})
653 {
654  configurePin(pin, PinMode::output, openDrain, outputSpeed, pull, {}, initialState);
655 }
656 
665 constexpr std::pair<GPIO_TypeDef*, uint8_t> decodePin(const Pin pin)
666 {
667  return std::make_pair(reinterpret_cast<GPIO_TypeDef*>(static_cast<uint32_t>(pin) & ~15),
668  static_cast<uint32_t>(pin) & 15);
669 }
670 
671 } // namespace chip
672 
673 } // namespace distortos
674 
675 #endif // SOURCE_CHIP_STM32_PERIPHERALS_GPIOV2_INCLUDE_DISTORTOS_CHIP_STM32_GPIOV2_HPP_
PinAlternateFunction
all possible alternate functions of pin
Definition: STM32-GPIOv2.hpp:485
constexpr std::pair< GPIO_TypeDef *, uint8_t > decodePin(const Pin pin)
Decodes pin identifier into address of associated GPIO port and its pin number.
Definition: STM32-GPIOv2.hpp:665
CMSIS proxy header for STM32F4.
void configureAnalogPin(const Pin pin)
Configures pin for "analog" mode.
Definition: STM32-GPIOv2.hpp:624
no pull-up/pull-down
PinPull
all possible pull-up/pull-down configurations of pin
Definition: STM32-GPIOv2.hpp:552
Top-level namespace of distortos project.
void configureOutputPin(const Pin pin, const bool openDrain={}, const PinOutputSpeed outputSpeed={}, const PinPull pull={}, const bool initialState={})
Configures pin for "output" mode.
Definition: STM32-GPIOv2.hpp:651
PinMode
all possible modes of pin
Definition: STM32-GPIOv2.hpp:524
PinOutputSpeed
all possible output speeds of pin
Definition: STM32-GPIOv2.hpp:537
void configureAlternateFunctionPin(const Pin pin, const PinAlternateFunction alternateFunction, const bool openDrain={}, const PinOutputSpeed outputSpeed={}, const PinPull pull={})
Configures pin for "alternate function" mode.
Definition: STM32-GPIOv2.hpp:591
void configureInputPin(const Pin pin, const PinPull pull={})
Configures pin for "input" mode.
Definition: STM32-GPIOv2.hpp:636
Pin
identifier of single pin - combined address of GPIO port with pin number
Definition: STM32-GPIOv2.hpp:74
void configurePin(Pin pin, PinMode mode, bool openDrain, PinOutputSpeed outputSpeed, PinPull pull, PinAlternateFunction alternateFunction, bool initialState={})
Configures pin.
Definition: STM32-GPIOv2.cpp:36