libyggdrasil  v1.0.0
gpio.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Midgard - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 
30 namespace bsp::mid::drv {
31 
32  namespace {
33 
37  enum class RegisterMap {
38  MODER = 0x00,
39  OTYPER = 0x04,
40  IDR = 0x10,
41  ODR = 0x14,
42  };
43 
44  }
45 
46 
47  // Forward declaring
48  template<addr_t BaseAddress>
49  struct GPIOPort;
50 
58  template<addr_t BaseAddress, u8 Pin, bsp::drv::Active LogicActive>
59  struct GPIOPin {
60  GPIOPin(const GPIOPin&) = delete;
61  GPIOPin(GPIOPin&&) = delete;
62 
63  static_assert(Pin <= 15, "Pin out of range");
64 
65  GPIOPin& operator=(const GPIOPin&) = delete;
66 
73  ALWAYS_INLINE constexpr auto& operator=(bool state) const noexcept {
74  ODRx = LogicActive == bsp::drv::Active::High ? state : !state;
75 
76  return *this;
77  }
78 
84  [[nodiscard]] ALWAYS_INLINE constexpr operator u8() const noexcept {
85  return LogicActive == bsp::drv::Active::High ? IDRx : !IDRx;
86  }
87 
93  bool init() const noexcept {
94  return true;
95  }
96 
102  bool deinit() const noexcept {
103  return true;
104  }
105 
109  void makeOutput() const noexcept {
110  MODERx = 0b01;
111  OTYPERx = 0b0;
112  }
113 
117  void makeInput() const noexcept {
118  MODERx = 0b00;
119  }
120 
121  private:
122  GPIOPin() = default;
123 
128  static inline auto MODERx = typename MODER::template Field<Pin * 2, Pin * 2 + 1>();
129 
134  static inline auto OTYPERx = typename OTYPER::template Field<Pin, Pin>();
135 
140  static inline auto IDRx = typename IDR::template Field<Pin, Pin>();
141 
146  static inline auto ODRx = typename ODR::template Field<Pin, Pin>();
147 
151  template<addr_t, template<addr_t,u8> typename>
152  friend struct bsp::mid::drv::GPIOPort;
153  };
154 
161  template<addr_t BaseAddress>
162  struct GPIOPort {
163  private:
164  GPIOPort() = default;
165 
168 
169  public:
170  GPIOPort(const GPIOPort&) = delete;
171  GPIOPort(GPIOPort&&) = delete;
172 
178  static bool init() {
179  return true;
180  }
181 
187  static bool deinit() {
188  return true;
189  }
190 
197  template<u8 Pin, bsp::drv::Active LogicActive>
198  static constexpr auto Pin = GPIOPin<BaseAddress, Pin, LogicActive>();
199 
207  template<u8 From, u8 To>
208  static constexpr auto In = typename IDR::template Field<From, To>();
209 
217  template<u8 From, u8 To>
218  static constexpr auto Out = typename ODR::template Field<From, To>();
219 
220  };
221 
222 }
bsp::mid::drv::GPIOPin::GPIOPin
GPIOPin(const GPIOPin &)=delete
bsp::mid::drv::GPIOPin::makeOutput
void makeOutput() const noexcept
Turn pin into an output.
Definition: gpio.hpp:109
bsp::Register
MMIO Register abstraction. Gives access to bitfields within the register as well as a reference to th...
Definition: registers.hpp:90
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::mid::drv::GPIOPin::operator=
constexpr ALWAYS_INLINE auto & operator=(bool state) const noexcept
Assignment operator overload.
Definition: gpio.hpp:73
bsp::mid::drv::GPIOPort::Out
static constexpr auto Out
Write multiple GPIO Pins.
Definition: gpio.hpp:218
bsp::mid::drv::GPIOPort
GPIOPort implementation for Midgard.
Definition: gpio.hpp:162
bsp::mid::drv::GPIOPort::GPIOPort
GPIOPort(GPIOPort &&)=delete
bsp::mid::drv::GPIOPin
GPIOPin implementation for Midgard.
Definition: gpio.hpp:59
ALWAYS_INLINE
#define ALWAYS_INLINE
Definition: attributes.h:34
bsp::mid::drv::GPIOPort::In
static constexpr auto In
Read multiple GPIO Pins.
Definition: gpio.hpp:208
RegisterMap
RegisterMap
Register map.
Definition: hash.cpp:42
bsp::mid::drv::GPIOPort::init
static bool init()
Init function.
Definition: gpio.hpp:178
bsp::mid::drv::GPIOPin::deinit
bool deinit() const noexcept
Deinit function.
Definition: gpio.hpp:102
bsp::drv::Active::High
@ High
bsp::mid::drv::GPIOPort::deinit
static bool deinit()
Deinit function.
Definition: gpio.hpp:187
bsp::mid::drv::GPIOPin::operator=
GPIOPin & operator=(const GPIOPin &)=delete
gpio.hpp
Frontend for the GPIO abstraction.
bsp::mid::drv::GPIOPin::makeInput
void makeInput() const noexcept
Turn pin into an input.
Definition: gpio.hpp:117
bsp::mid::drv::GPIOPort::Pin
static constexpr auto Pin
GPIO Pin.
Definition: gpio.hpp:198
bsp::mid::drv::GPIOPin::GPIOPin
GPIOPin(GPIOPin &&)=delete
bsp::mid::drv::GPIOPin::init
bool init() const noexcept
Init function.
Definition: gpio.hpp:93
bsp::mid::drv
Definition: adc.hpp:32
bsp::mid::drv::GPIOPort::GPIOPort
GPIOPort(const GPIOPort &)=delete