ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
gpio.hpp
Go to the documentation of this file.
1
47#ifndef ELEC_C7222_DEVICES_GPIO_H_
48#define ELEC_C7222_DEVICES_GPIO_H_
49
50#include <cstdint>
51#include <functional>
52#include "non_copyable.hpp"
53
60namespace c7222 {
61
65enum class GpioPullMode {
66 None,
67 PullUp,
69};
70
74enum class GpioOutputType {
75 HighZ,
76 PushPull,
78};
79
84 mA2,
85 mA4,
86 mA8,
87 mA12
88};
89
96enum class GpioInputEvent : uint32_t {
97 None = 0x00,
98 LevelLow = 0x01,
99 LevelHigh = 0x02,
100 BothLevels = 0x03,
101 FallingEdge = 0x04,
102 RisingEdge = 0x08,
103 BothEdges = 0x0c,
104 All = 0x0f
105};
106
130 public:
134 struct Config {
135 uint32_t pin_;
138 std::function<void(uint32_t)> irq_handler{};
139
144 explicit Config(uint32_t pin);
149 bool Validate() const;
150
151 bool operator==(const Config& other) const;
152 };
153
157 explicit GpioIn(uint32_t pin) : GpioIn(pin, Config(pin)) {}
163 explicit GpioIn(uint32_t pin, const Config& config);
164
170 virtual ~GpioIn();
171
178 void EnableIrq(GpioInputEvent events, std::function<void(uint32_t)> handler);
179
184
189 void Configure(const Config& config);
194 bool Read() const;
195
199 uint32_t GetPin() const {
200 return pin_;
201 }
206 return config_;
207 }
208
212 bool HasHandler() const{
213 return static_cast<bool>(config_.irq_handler);
214 }
215
221 void CallIrqHandler(uint32_t events) const;
222
223 protected:
230
232 uint32_t pin_;
235};
236
246 public:
270
274 explicit GpioOut(uint32_t pin) : GpioOut(pin, Config(pin)) {}
275
281 virtual ~GpioOut();
287 explicit GpioOut(uint32_t pin, const Config& config);
288
293 void Configure(const Config& config);
298 void Write(bool value);
302 void Toggle();
303
307 uint32_t GetPin() const {
308 return pin_;
309 }
314 return config_;
315 }
316
317 protected:
324
326 uint32_t pin_;
329};
330
331} // namespace c7222
332
333#endif // ELEC_C7222_DEVICES_GPIO_H_
GPIO input-only wrapper with pull configuration and optional IRQ.
Definition gpio.hpp:129
bool Read() const
Read the current GPIO level.
void EnableIrq(GpioInputEvent events, std::function< void(uint32_t)> handler)
Enable GPIO IRQs for the given event set and handler.
void CallIrqHandler(uint32_t events) const
Invoke the registered IRQ handler (if any).
GpioIn(uint32_t pin, const Config &config)
Create a GPIO input with the given configuration.
void ApplyConfig()
Apply internal configuration to the hardware.
uint32_t GetPin() const
Return the GPIO number.
Definition gpio.hpp:199
bool HasHandler() const
Return true if an IRQ handler is currently registered.
Definition gpio.hpp:212
Config config_
Cached configuration used to (re)apply platform settings.
Definition gpio.hpp:234
void DisableIrq()
Disable any IRQs configured for this input.
void Configure(const Config &config)
Apply a new configuration.
virtual ~GpioIn()
Destructor.
Config GetConfig() const
Return the cached configuration.
Definition gpio.hpp:205
GpioIn(uint32_t pin)
Create a GPIO input with default configuration.
Definition gpio.hpp:157
uint32_t pin_
GPIO pin number managed by this instance.
Definition gpio.hpp:232
GPIO output-only wrapper with output configuration and drive strength.
Definition gpio.hpp:245
void ApplyConfig()
Apply internal configuration to the hardware.
GpioOut(uint32_t pin)
Create a GPIO output with default configuration.
Definition gpio.hpp:274
uint32_t pin_
GPIO pin number managed by this instance.
Definition gpio.hpp:326
Config config_
Cached configuration used to (re)apply platform settings.
Definition gpio.hpp:328
virtual ~GpioOut()
Destructor.
void Write(bool value)
Write an output value.
void Configure(const Config &config)
Apply a new configuration.
void Toggle()
Toggle the pin output.
Config GetConfig() const
Return the cached configuration.
Definition gpio.hpp:313
GpioOut(uint32_t pin, const Config &config)
Create a GPIO output with the given configuration.
uint32_t GetPin() const
Return the GPIO number.
Definition gpio.hpp:307
Disable both copy and move operations.
Definition non_copyable.hpp:75
C7222 course abstractions namespace.
Definition ble.hpp:20
GpioPullMode
GPIO pull configuration.
Definition gpio.hpp:65
@ PullDown
Enable pull-down.
@ None
Disable pulls.
@ PullUp
Enable pull-up.
GpioDriveStrength
GPIO drive strength.
Definition gpio.hpp:83
GpioInputEvent
GPIO input event bitmask for IRQ configuration.
Definition gpio.hpp:96
@ LevelLow
Trigger on low level.
@ RisingEdge
Trigger on low-to-high edge.
@ FallingEdge
Trigger on high-to-low edge.
@ All
Trigger on all level and edge events.
@ BothEdges
Trigger on rising or falling edge.
@ BothLevels
Trigger on low or high level.
@ LevelHigh
Trigger on high level.
GpioOutputType
GPIO output type.
Definition gpio.hpp:74
@ HighZ
High-impedance (not driven).
@ OpenDrain
Drive low or float.
@ PushPull
Drive high/low actively.
Base classes to control copy/move semantics.
Configuration structure for input GPIO setup.
Definition gpio.hpp:134
bool operator==(const Config &other) const
GpioInputEvent input_events
Input events to trigger IRQ (default: None).
Definition gpio.hpp:137
bool Validate() const
Validate configuration for basic constraints.
Config(uint32_t pin)
Create config with a pin number.
GpioPullMode pull
Pull mode (default: None).
Definition gpio.hpp:136
uint32_t pin_
GPIO pin number (non-negative integer).
Definition gpio.hpp:135
std::function< void(uint32_t)> irq_handler
Optional IRQ handler callback. The function argument is bit field of events that triggered the IRQ.
Definition gpio.hpp:138
Configuration structure for output GPIO setup.
Definition gpio.hpp:250
bool initial_state
Initial logic state when configured as output.
Definition gpio.hpp:255
GpioDriveStrength drive
Drive strength (default: 4 mA).
Definition gpio.hpp:254
uint32_t pin_
GPIO pin number (non-negative integer).
Definition gpio.hpp:251
GpioOutputType output_type
Output type (default: PushPull).
Definition gpio.hpp:253
bool operator==(const Config &other) const
bool Validate() const
Validate configuration for basic constraints.
GpioPullMode pull
Pull mode (default: None).
Definition gpio.hpp:252
Config(uint32_t pin)
Create config with a pin number.