OpenMPTL - STM32F4
C++ Microprocessor Template Library
gpio.hpp
Go to the documentation of this file.
1 /*
2  * OpenMPTL - C++ Microprocessor Template Library
3  *
4  * Copyright (C) 2012-2017 Axel Burri <axel@tty0.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 /*
22  * This program contains derivative representations of CMSIS System
23  * View Description (SVD) files, and is subject to the "End User
24  * License Agreement for STMicroelectronics" (see "STM_License.html"
25  * in the containing directory).
26  */
27 
28 #ifndef ARCH_REG_GPIO_HPP_INCLUDED
29 #define ARCH_REG_GPIO_HPP_INCLUDED
30 
31 #include <register.hpp>
32 #include <type_traits>
33 
34 namespace mptl {
35 
36 /**
37  * General-purpose and alternate-function I/Os (GPIOs and AFIOs)
38  */
39 template< char port >
40 struct GPIO
41 {
42  static_assert((port >= 'A') && (port <= 'I'), "invalid index for GPIO register");
43 
44  static constexpr unsigned gpio_no = port - 'A';
45  static constexpr reg_addr_t base_addr = 0x40020000 + (gpio_no * 0x0400);
46 
47  static constexpr uint32_t moder_reset = ( port == 'A' ? 0xA8000000 :
48  port == 'B' ? 0x00000280 :
49  0x00000000 );
50 
51  static constexpr uint32_t ospeedr_reset = ( port == 'B' ? 0x000000C0 :
52  0x00000000 );
53 
54  static constexpr uint32_t pupdr_reset = ( port == 'A' ? 0x64000000 :
55  port == 'B' ? 0x00000100 :
56  0x00000000 );
57 
58  using MODER = reg< uint32_t, base_addr + 0x00, rw, moder_reset >; /**< GPIO port mode register */
59  using OTYPER = reg< uint32_t, base_addr + 0x04, rw >; /**< GPIO port output type register */
60  using OSPEEDR = reg< uint32_t, base_addr + 0x08, rw, ospeedr_reset >; /**< GPIO port output speed register */
61  using PUPDR = reg< uint32_t, base_addr + 0x0c, rw, pupdr_reset >; /**< GPIO port pull-up/pull-down register */
62  using IDR = reg< uint32_t, base_addr + 0x10, ro /*0x0000XXXX*/ >; /**< GPIO port input data register */
63  using ODR = reg< uint32_t, base_addr + 0x14, rw >; /**< GPIO port output data register */
64  using BSRR = reg< uint32_t, base_addr + 0x18, wo >; /**< GPIO port bit set/reset register */
65  using LCKR = reg< uint32_t, base_addr + 0x1c, rw >; /**< GPIO port configuration lock register */
66  using AFRL = reg< uint32_t, base_addr + 0x20, rw >; /**< GPIO alternate function low register */
67  using AFRH = reg< uint32_t, base_addr + 0x24, rw >; /**< GPIO alternate function high register */
68 
69  template< unsigned pin_no > using MODERx = regbits< MODER , pin_no * 2 , 2 >;
70  template< unsigned pin_no > using OTYPERx = regbits< OTYPER , pin_no , 1 >;
71  template< unsigned pin_no > using OSPEEDRx = regbits< OSPEEDR, pin_no * 2 , 2 >;
72  template< unsigned pin_no > using PUPDRx = regbits< PUPDR , pin_no * 2 , 2 >;
73  template< unsigned pin_no > using IDRx = regbits< IDR , pin_no , 1 >;
74  template< unsigned pin_no > using ODRx = regbits< ODR , pin_no , 1 >;
75  template< unsigned pin_no > using AFRLx = regbits< AFRL , pin_no * 4 , 4 >; /** pin_no = [0..7] */
76  template< unsigned pin_no > using AFRHx = regbits< AFRH , (pin_no % 8) * 4, 4 >; /** pin_no = [8..15] */
77 
78  /**
79  * GPIO alternate function: returns AFRLx or AFRHx type dependent on pin_no.
80  *
81  * NOTE: this is not from the reference manual
82  */
83  template< unsigned pin_no > using AFRx =
84  typename std::conditional<
85  (pin_no < 8),
88  >::type;
89 };
90 
91 } // namespace mptl
92 
93 #endif // ARCH_REG_GPIO_HPP_INCLUDED
static constexpr uint32_t pupdr_reset
Definition: gpio.hpp:54
uintptr_t reg_addr_t
static constexpr uint32_t ospeedr_reset
Definition: gpio.hpp:51
static constexpr reg_addr_t base_addr
Definition: gpio.hpp:45
static constexpr unsigned gpio_no
Definition: gpio.hpp:44
static constexpr uint32_t moder_reset
Definition: gpio.hpp:47
typename std::conditional<(pin_no< 8), AFRLx< pin_no >, AFRHx< pin_no > >::type AFRx
pin_no = [8..15]
Definition: gpio.hpp:88
General-purpose and alternate-function I/Os (GPIOs and AFIOs)
Definition: gpio.hpp:40