libyggdrasil  v1.0.0
gpio.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Asgard - *
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 #include <unistd.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <string>
35 
36 namespace bsp::asg::drv {
37 
38  // Forward declaring
39  template<u8 GPIOBaseNumber>
40  struct GPIOPort;
41 
49  template<u8 GPIOBaseNumber, u8 Pin, bsp::drv::Active LogicActive>
50  struct GPIOPin {
51  GPIOPin(const GPIOPin&) = delete;
52  GPIOPin(GPIOPin&&) = delete;
53 
54  static_assert(Pin <= 15, "Pin out of range");
55 
56  GPIOPin& operator=(const GPIOPin&) = delete;
57 
64  auto& operator=(bool state) const noexcept {
65  state = LogicActive == bsp::drv::Active::High ? state : !state;
66 
67  int fd = open(("/sys/class/gpio/gpio" + std::to_string(GPIOBaseNumber + Pin) + "/value").c_str(), O_WRONLY);
68  if (fd == -1) return *this;
69 
70  write(fd, state ? "1" : "0", 1);
71  close(fd);
72 
73  return *this;
74  }
75 
81  [[nodiscard]] operator u8() const noexcept {
82  int fd = open(("/sys/class/gpio/gpio" + std::to_string(GPIOBaseNumber + Pin) + "/value").c_str(), O_RDONLY);
83  if (fd == -1) return 0;
84 
85  char buffer[2] = { 0 };
86  read(fd, buffer, sizeof(buffer));
87  close(fd);
88 
89  bool result = buffer[0] == '1';
90 
91  return LogicActive == bsp::drv::Active::High ? result : !result;
92  }
93 
99  bool init() const noexcept {
100  int fd = open("/sys/class/gpio/export", O_WRONLY);
101  if (fd == -1) return false;
102 
103  auto value = std::to_string(GPIOBaseNumber + Pin);
104  write(fd, value.c_str(), value.length());
105  close(fd);
106 
107  return true;
108  }
109 
115  bool deinit() const noexcept {
116  int fd = open("/sys/class/gpio/unexport", O_WRONLY);
117  if (fd == -1) return false;
118 
119  auto value = std::to_string(GPIOBaseNumber + Pin);
120  write(fd, value.c_str(), value.length());
121  close(fd);
122 
123  return true;
124  }
125 
129  void makeOutput() const noexcept {
130  int fd = open(("/sys/class/gpio/gpio" + std::to_string(GPIOBaseNumber + Pin) + "/direction").c_str(), O_WRONLY);
131  if (fd == -1) return;
132 
133  write(fd, "out", 3);
134  close(fd);
135  }
136 
140  void makeInput() const noexcept {
141  int fd = open(("/sys/class/gpio/gpio" + std::to_string(GPIOBaseNumber + Pin) + "/direction").c_str(), O_WRONLY);
142  if (fd == -1) return;
143 
144  write(fd, "in", 2);
145  close(fd);
146  }
147 
148  private:
149  GPIOPin() = default;
153  template<addr_t, template<addr_t,u8> typename>
154  friend struct bsp::asg::drv::GPIOPort;
155  };
156 
163  template<u8 GPIOBaseNumber>
164  struct GPIOPort {
165  private:
166  GPIOPort() = default;
167 
168  public:
174  static bool init() {
175  return true;
176  }
177 
183  static bool deinit() {
184  return true;
185  }
186 
187  GPIOPort(const GPIOPort&) = delete;
188  GPIOPort(GPIOPort&&) = delete;
189 
196  template<u8 Pin, bsp::drv::Active LogicActive>
198  };
199 
200 }
bsp::asg::drv
Definition: display.hpp:60
bsp::asg::drv::GPIOPin::makeInput
void makeInput() const noexcept
Turn pin into an input.
Definition: gpio.hpp:140
bsp::asg::drv::GPIOPort::GPIOPort
GPIOPort(const GPIOPort &)=delete
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::asg::drv::GPIOPin::operator=
GPIOPin & operator=(const GPIOPin &)=delete
bsp::asg::drv::GPIOPin
GPIOPin implementation for Asgard.
Definition: gpio.hpp:50
bsp::asg::drv::GPIOPort::deinit
static bool deinit()
Deinit function.
Definition: gpio.hpp:183
bsp::asg::drv::GPIOPort::GPIOPort
GPIOPort(GPIOPort &&)=delete
bsp::asg::drv::GPIOPort
GPIOPort implementation for Asgard.
Definition: gpio.hpp:164
bsp::asg::drv::GPIOPin::deinit
bool deinit() const noexcept
Deinit function.
Definition: gpio.hpp:115
bsp::asg::drv::GPIOPin::init
bool init() const noexcept
Init function.
Definition: gpio.hpp:99
bsp::drv::Active::High
@ High
bsp::asg::drv::GPIOPort::Pin
static constexpr auto Pin
GPIO Pin definition.
Definition: gpio.hpp:197
bsp::asg::drv::GPIOPin::GPIOPin
GPIOPin(GPIOPin &&)=delete
bsp::asg::drv::GPIOPort::init
static bool init()
Init function.
Definition: gpio.hpp:174
gpio.hpp
Frontend for the GPIO abstraction.
bsp::asg::drv::GPIOPin::GPIOPin
GPIOPin(const GPIOPin &)=delete
bsp::asg::drv::GPIOPin::operator=
auto & operator=(bool state) const noexcept
Assignment operator overload.
Definition: gpio.hpp:64
bsp::asg::drv::GPIOPin::makeOutput
void makeOutput() const noexcept
Turn pin into an output.
Definition: gpio.hpp:129