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 #ifndef ARCH_GPIO_HPP_INCLUDED
22 #define ARCH_GPIO_HPP_INCLUDED
23 
24 #include <arch/rcc.hpp>
25 #include <arch/reg/gpio.hpp>
26 #include <gpio_base.hpp>
27 #include <type_traits>
28 
29 namespace mptl {
30 
31 
32 //////////////////// gpio ////////////////////
33 
34 
35 template< char port, unsigned pin_no >
36 class gpio
37 {
38  static_assert((port >= 'A') && (port <= 'I'), "invalid GPIO port");
39  static_assert(pin_no < 16, "invalid GPIO pin-no");
40 
41 public:
42 
43  using GPIOx = GPIO<port>;
44 
46 
47 protected:
48 
49  static constexpr uint32_t pin_mask = (uint32_t)0x1 << pin_no;
50 
51 private:
52 
53  template<freq_t value>
54  struct speed_impl
55  {
56  static_assert((value == mhz(2)) ||
57  (value == mhz(25)) ||
58  (value == mhz(50)) ||
59  (value == mhz(100)),
60  "Illegal frequency for gpio output speed (allowed: mhz(2), mhz(25), mhz(50), mhz(100))");
61 
62  using type = regval<
63  typename GPIOx::template OSPEEDRx<pin_no>,
64  (value == mhz(25) ? 1 :
65  value == mhz(50) ? 2 :
66  value == mhz(100) ? 3 :
67  0)
68  >;
69  };
70 
71  template<unsigned value>
72  struct alt_func_num_impl
73  {
74  static_assert(value < 16, "illegal alternate function number");
75 
76  using type = typename std::conditional<
77  pin_no < 8,
80  >::type;
81  };
82 
83 
84 public: /* ------ configuration resources ------ */
85 
86  struct mode
87  {
90 
91  /**
92  * Alternate function mode selection
93  *
94  * NOTE: usually in combination with "alt_func_num<>".
95  */
98  };
99 
100  struct output_type
101  {
102  /** General purpose output push-pull (e.g. LED's) */
104 
105  /** General purpose output open-drain */
107  };
108 
109 
110  /** I/O output speed */
111  template<freq_t value>
112  using speed = typename speed_impl<value>::type;
113 
114 
115  struct resistor
116  {
117  /** Floating input */
119 
120  /** Input with pull-up */
122 
123  /** Input with pull-down */
125  };
126 
127  /**
128  * Alternate function selection
129  *
130  * NOTE: usually in combination with "mode::alternate_function".
131  */
132  template<unsigned value>
133  using alt_func_num = typename alt_func_num_impl<value>::type;
134 
135 
136 public: /* ------ static member functions ------ */
137 
138  static void set() {
139  GPIOx::BSRR::store(pin_mask);
140  }
141  static void reset() {
142  GPIOx::BSRR::store(pin_mask << 16);
143  }
144 
145  static uint32_t read_input_bit() {
146  return GPIOx::IDR::test(pin_mask);
147  }
148 
149  static uint32_t read_output_bit() {
150  return GPIOx::ODR::test(pin_mask);
151  }
152 };
153 
154 
155 //////////////////// gpio_input ////////////////////
156 
157 
158 template< char port, unsigned pin_no, gpio_active_state active_state = gpio_active_state::high >
160 : public gpio_input_base< gpio< port, pin_no >, active_state >
161 {
163 public:
164  using resources = typelist<
165  typename gpio_type::resources,
166  typename gpio_type::mode::input
167  >;
168 };
169 
170 
171 //////////////////// gpio_output ////////////////////
172 
173 
174 template< char port, unsigned pin_no, gpio_active_state active_state = gpio_active_state::high >
176 : public gpio_output_base< gpio< port, pin_no >, active_state >
177 {
179 public:
180  using resources = typelist<
181  typename gpio_type::resources,
182  typename gpio_type::mode::output
183  >;
184 };
185 
186 
187 //////////////////// gpio_analog_io ////////////////////
188 
189 
190 template<char port, unsigned pin_no, freq_t speed = mhz(2)>
192 : public gpio_analog_io_base< gpio< port, pin_no > >
193 {
195 public:
196  using resources = typelist<
197  typename gpio_type::resources,
198  typename gpio_type::mode::analog,
199  typename gpio_type::output_type::push_pull,
200  typename gpio_type::template speed< speed >,
201  typename gpio_type::resistor::floating
202  >;
203 };
204 
205 
206 //////////////////// gpio_led ////////////////////
207 
208 
209 template<char port, unsigned pin_no, gpio_active_state active_state = gpio_active_state::high>
210 class gpio_led
211 : public gpio_led_base< gpio_output< port, pin_no, active_state > >
212 {
214 public:
215  using resources = typelist<
216  typename gpio_type::resources,
217  typename gpio_type::output_type::push_pull
218  >;
219 };
220 
221 } // namespace mptl
222 
223 #endif // ARCH_GPIO_HPP_INCLUDED
Definition: gpio.hpp:86
typename speed_impl< value >::type speed
I/O output speed.
Definition: gpio.hpp:112
typename alt_func_num_impl< value >::type alt_func_num
Alternate function selection.
Definition: gpio.hpp:133
static uint32_t read_output_bit()
Definition: gpio.hpp:149
Definition: gpio.hpp:100
Definition: rcc.hpp:198
Definition: gpio.hpp:36
static constexpr uint32_t pin_mask
Definition: gpio.hpp:49
Definition: gpio.hpp:159
typename mpl::make_typelist< sane_typelist<>, Tp... >::type typelist
static constexpr freq_t mhz(unsigned long long x)
Definition: gpio.hpp:191
Definition: gpio.hpp:210
typelist< typename gpio_type::resources, typename gpio_type::mode::input > resources
Definition: gpio.hpp:167
static uint32_t read_input_bit()
Definition: gpio.hpp:145
static void reset()
Definition: gpio.hpp:141
Definition: gpio.hpp:115
typelist< typename gpio_type::resources, typename gpio_type::output_type::push_pull > resources
Definition: gpio.hpp:218
static __always_inline void store(Tp const value)
General-purpose and alternate-function I/Os (GPIOs and AFIOs)
Definition: gpio.hpp:40
typelist< typename gpio_type::resources, typename gpio_type::mode::analog, typename gpio_type::output_type::push_pull, typename gpio_type::template speed< speed >, typename gpio_type::resistor::floating > resources
Definition: gpio.hpp:202
typelist< typename gpio_type::resources, typename gpio_type::mode::output > resources
Definition: gpio.hpp:183
Definition: gpio.hpp:175
static __always_inline value_type test(value_type const value)