Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
MmapGpio.hxx
Go to the documentation of this file.
1
35#ifndef _OS_MMAPGPIO_HXX_
36#define _OS_MMAPGPIO_HXX_
37
38#include "os/Gpio.hxx"
39
51template<typename STORAGE>
52class MmapGpioTemplate : public Gpio
53{
54public:
64 STORAGE *const ptr, const unsigned bit_ofs, const bool is_output)
65 : ptr_(ptr + (bit_ofs >> 5))
66 , bit_(bit_ofs & 31)
67 , isOutput_(is_output ? 1 : 0)
68 {
69 }
70
71 void write(Value new_state) const OVERRIDE
72 {
73 if (new_state == Value::SET) {
74 *ptr_ |= (1<<bit_);
75 } else {
76 *ptr_ &= ~(1<<bit_);
77 }
78 }
79
80 void set() const OVERRIDE
81 {
82 *ptr_ |= (1<<bit_);
83 }
84
85 void clr() const OVERRIDE
86 {
87 *ptr_ &= ~(1<<bit_);
88 }
89
91 {
92 return ((*ptr_) & (1<<bit_)) != 0 ? Value::SET : Value::CLR;
93 }
94
96 {
97 // Mmapped GPIO cannot change direction.
98 HASSERT(dir == direction());
99 }
100
102 {
103 return isOutput_ ? Direction::DOUTPUT : Direction::DINPUT;
104 }
105
106private:
108 STORAGE * const ptr_;
110 const unsigned bit_ : 5;
112 const unsigned isOutput_ : 1;
113};
114
117
118#endif // _OS_MMAPGPIO_HXX_
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
Value
Defines the options for GPIO level.
Definition Gpio.hxx:62
Direction
Defines the options for GPIO direction.
Definition Gpio.hxx:73
Gpio wrapper for a single bit in memory.
Definition MmapGpio.hxx:53
void clr() const OVERRIDE
Clears the GPIO output pin to low.
Definition MmapGpio.hxx:85
const unsigned bit_
Which bit. 0 == LSB, from there it goes up towards MSB.
Definition MmapGpio.hxx:110
void set_direction(Direction dir) const OVERRIDE
Sets the GPIO direction.
Definition MmapGpio.hxx:95
void write(Value new_state) const OVERRIDE
Writes a GPIO output pin (set or clear to a specific state).
Definition MmapGpio.hxx:71
Value read() const OVERRIDE
Retrieves the current Value of a GPIO input pin.
Definition MmapGpio.hxx:90
Direction direction() const OVERRIDE
Gets the GPIO direction.
Definition MmapGpio.hxx:101
const unsigned isOutput_
1 if this GPIO is an output, 0 if it's an input.
Definition MmapGpio.hxx:112
STORAGE *const ptr_
Pointer to storage.
Definition MmapGpio.hxx:108
constexpr MmapGpioTemplate(STORAGE *const ptr, const unsigned bit_ofs, const bool is_output)
Constructor.
Definition MmapGpio.hxx:63
void set() const OVERRIDE
Sets the GPIO output pin to high.
Definition MmapGpio.hxx:80
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138