Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
GpioWrapper.hxx
Go to the documentation of this file.
1
36#ifndef _FREERTOS_DRIVERS_COMMON_GPIOWRAPPER_HXX_
37#define _FREERTOS_DRIVERS_COMMON_GPIOWRAPPER_HXX_
38
39#include "os/Gpio.hxx"
40
45template <class PIN> class GpioWrapper : public Gpio
46{
47public:
50 constexpr GpioWrapper()
51 {
52 }
53
54 void write(Value new_state) const OVERRIDE
55 {
56 PIN::set(new_state);
57 }
58
59 void set() const OVERRIDE
60 {
61 PIN::set(true);
62 }
63
64 void clr() const OVERRIDE
65 {
66 PIN::set(false);
67 }
68
70 {
71 return PIN::get() ? VHIGH : VLOW;
72 }
73
75 {
76 // We cannot change the direction of a wrapped pin. Crash if the code
77 // attempts to do so.
78 if (dir == Direction::DOUTPUT)
79 {
80 HASSERT(PIN::is_output());
81 }
82 else
83 {
84 HASSERT(!PIN::is_output());
85 }
86 }
87
89 {
90 if (PIN::is_output())
91 {
92 return Direction::DOUTPUT;
93 }
94 else
95 {
96 return Direction::DINPUT;
97 }
98 }
99
101 static constexpr const Gpio *instance()
102 {
103 return &instance_;
104 }
105
106 static const GpioWrapper instance_;
107};
108
110template <class PIN> const GpioWrapper<PIN> GpioWrapper<PIN>::instance_;
111
113template<class PIN> class InvertedGpio {
114public:
115 static void hw_init() { PIN::hw_init(); }
116 static void hw_set_to_safe() { PIN::hw_set_to_safe(); }
117
118 static void set(bool value) { PIN::set(!value); }
119 static bool get() { return !PIN::get(); }
120
121 static void toggle() { PIN::toggle(); }
122 static bool is_output() { return PIN::is_output(); }
123 static void set_output(bool o) { PIN::set_output(o); }
124
126 static constexpr const Gpio *instance()
127 {
129 }
130};
131
132
133#endif // _FREERTOS_DRIVERS_COMMON_GPIOWRAPPER_HXX_
Creates an implementation of an os-independent Gpio object from a hardware-specific static Gpio struc...
void write(Value new_state) const OVERRIDE
Writes a GPIO output pin (set or clear to a specific state).
void set() const OVERRIDE
Sets the GPIO output pin to high.
void clr() const OVERRIDE
Clears the GPIO output pin to low.
Value read() const OVERRIDE
Retrieves the current Value of a GPIO input pin.
void set_direction(Direction dir) const OVERRIDE
Sets the GPIO direction.
static const GpioWrapper instance_
Defines the linker symbol for the wrapped Gpio instance.
constexpr GpioWrapper()
This constructor is constexpr which ensures that the object can be initialized in the data section.
Direction direction() const OVERRIDE
Gets the GPIO direction.
static constexpr const Gpio * instance()
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
Creates a hardware GPIO adaptor that inverts the pin state.
static constexpr const Gpio * instance()
#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