RTLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
gpio.h
Go to the documentation of this file.
1 /*
2  * This file is part of RTLib.
3  *
4  * Copyright (C) 2018 Derppening <david.18.19.21@gmail.com>
5  *
6  * RTLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser 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  * RTLib 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with RTLib. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef RTLIB_CORE_STM32F4_GPIO_H_
21 #define RTLIB_CORE_STM32F4_GPIO_H_
22 
23 #if defined(STM32F4)
24 
25 #include <cstdint>
26 #include <utility>
27 
28 #include <libopencm3/stm32/gpio.h>
29 
30 #include "core/util.h"
31 
32 namespace core {
33 namespace stm32f4 {
34 
41 class GPIO final {
42  public:
48  using AltFn = uint8_t;
49 
57  enum struct Mode : uint8_t {
63  kInput = 0x0,
69  kOutput,
75  kAF,
81  kAnalog
82  };
83 
91  enum struct Speed : uint8_t {
97  k2MHz = 0x0,
103  k25MHz,
109  k50MHz,
115  k100MHz
116  };
117 
125  enum struct Pullup : uint8_t {
131  kNone = 0x0,
137  kPullup,
143  kPulldown
144  };
145 
153  enum struct DriverType : uint8_t {
157  kPushPull = 0x0,
161  kOpenDrain
162  };
163 
167  struct Config {
173  Pinout pin = {};
174 
181 
188 
195 
202 
208  AltFn altfn = GPIO_AF0;
209  };
210 
216  explicit GPIO(const Config& config);
217 
231  GPIO(Pinout pin,
232  Mode mode,
233  Pullup pullup,
234  Speed speed = Speed::k2MHz,
236  AltFn altfn = 0x0);
237 
241  ~GPIO() = default;
242 
248  GPIO(GPIO&& other) noexcept = default;
255  GPIO& operator=(GPIO&& other) noexcept = default;
256 
263  GPIO(const GPIO&) = delete;
270  GPIO& operator=(const GPIO&) = delete;
271 
277  bool Read() const;
283  void Set(bool state) const;
287  void Toggle() const;
288 
292  void Reset() const;
293 
294  private:
303  void Init(Mode mode, Pullup pullup, Speed speed = Speed::k2MHz, DriverType driver = DriverType::kPushPull) const;
311  void InitRcc(Port port) const;
312 
320  void SetAltFn(uint8_t altfn);
321 
329  const Pinout& GetPinout() const { return pin_; }
330 
334  Pinout pin_;
335 };
336 
337 } // namespace stm32f4
338 } // namespace core
339 
340 #endif // defined(STM32F4)
341 
342 #endif // RTLIB_CORE_STM32F4_GPIO_H_
unsigned Port
Type definition for MCU Port.
Definition: util.h:41
Drive GPIO output using open-drain (i.e. only pull to GND).
Speed
Enumeration for different GPIO output speeds.
Definition: gpio.h:91
AltFn altfn
GPIO Alternate Function Selection.
Definition: gpio.h:208
Pullup
Enumeration for different internal pull-up states.
Definition: gpio.h:125
uint8_t AltFn
Type definition for GPIO alternate function selection.
Definition: gpio.h:48
Drive GPIO output using push-pull (i.e. actively pushed to VCC/pulled to GND).
Use this GPIO as digital input.
std::pair< Port, Pin > Pinout
Type definition for MCU Pinout.
Definition: util.h:49
Use this GPIO as analog I/O.
GPIO(const Config &config)
Conversion constructor.
Definition: gpio.cpp:32
Pullup pullup
Whether to use MCU's internal pull-up/down resistor.
Definition: gpio.h:194
Use the internal pull-up resistor.
DriverType driver
GPIO Output Driver Type.
Definition: gpio.h:201
Use this GPIO as an alternate function.
bool Read() const
Reads the current logic state of the managed GPIO.
Definition: gpio.cpp:96
void Toggle() const
Toggles GPIO state. Logic High -> Logic Low and vice versa.
Definition: gpio.cpp:108
Configuration for GPIO.
Definition: gpio.h:167
Mode
Enumeration for different GPIO modes.
Definition: gpio.h:57
Speed speed
GPIO Output Speed.
Definition: gpio.h:187
Utility helpers for core library.
Mode mode
GPIO Mode.
Definition: gpio.h:180
void Set(bool state) const
Sets new GPIO state.
Definition: gpio.cpp:100
STM32F4xx-specific HAL implementation for GPIO pins.
Definition: gpio.h:41
Do not use the internal pull-up/pull-down resistor (i.e. floating mode).
Use this GPIO as digital output.
Pinout pin
GPIO Pin to enable and initialize.
Definition: gpio.h:173
Use the internal pull-down resistor.
void Reset() const
Resets the GPIO to its original configuration, i.e. when the RESET button is first pushed...
Definition: gpio.cpp:112
DriverType
Enumeration for GPIO output driver modes.
Definition: gpio.h:153