Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1
5#ifndef PLATFORM_GPIO_H
6#define PLATFORM_GPIO_H
7
8#include <set>
9#include <memory>
10#include <stdint.h>
11
12namespace lampda {
13namespace platform {
15namespace gpio {
16
17class DigitalPinImpl;
18
20{
21public:
22 enum class GPIO
23 {
24 gpio0,
25 gpio1,
26 gpio2,
27 gpio3,
28 gpio4,
29 gpio5,
30 gpio6,
31 gpio7,
32
33 Input_isChargeOk,
34
35 Signal_PowerDelivery,
36 Signal_UsbProtectionFault,
37 Signal_VbusGateFault,
38 Signal_ChargerProcHot,
39 Signal_BatteryBalancerAlert,
40 Signal_ImuInterrupt1,
41 Signal_ImuInterrupt2,
42
43 Output_EnableExternalPeripherals,
44 Output_EnableMicrophone,
45 Output_VbusFastRoleSwap,
46 Output_VbusDirection,
47 Output_EnableOnTheGo,
48 // danger zone: only one of the next 3 signals should be active at a time
49 Output_DischargeVbus,
50 Output_EnableVbusGate,
51 Output_EnableOutputGate
52 };
53 enum class Mode
54 {
55 kDefault = 0,
56 kInput,
57 kOutput,
58
59 kInputPullUp,
60 kInputPullUpSense,
61 kOutputHighCurrent,
62 };
63 enum class Interrupt
64 {
65 kChange,
66 kRisingEdge,
67 kFallingEdge,
68 };
69
70 DigitalPin(GPIO pin);
71
72 DigitalPin(DigitalPin&& other) = delete;
73
74 // reinit
75 void set(GPIO pin);
76
77 void set_pin_mode(Mode mode) const;
78 bool is_high() const; // true if high, false if low
79 void set_high(bool isHigh) const;
80
81 void write(uint16_t value) const;
82 uint16_t read() const;
83
84 int pin() const; // get the raw pin id
85
86 using voidFuncPtr = void (*)(void);
87 void attach_callback(voidFuncPtr func, Interrupt mode) const;
88 void detach_callbacks() const;
89
90 // if called, this pin will be physically disconnected from any power/gnd
91 // if will need to be reinitialized
92 void disconnect() const;
93
94 static void detach_all()
95 {
96 // detach all set interrupts
97 for (DigitalPin::GPIO pin: DigitalPin::s_gpiosWithInterrupts)
98 {
99 DigitalPin(pin).detach_callbacks();
100 }
101 DigitalPin::s_gpiosWithInterrupts.clear();
102 }
103
104 // call this when the gpios needs to be deactivated
105 static void deactivate_gpios()
106 {
107 // loop though all gpios, deactivate them all
108 for (uint32_t pin = static_cast<uint32_t>(GPIO::gpio0); pin != static_cast<uint32_t>(GPIO::Output_EnableOutputGate);
109 ++pin)
110 {
111 DigitalPin((GPIO)pin).disconnect();
112 }
113 }
114
115private:
116 inline static std::set<GPIO> s_gpiosWithInterrupts;
117
118 GPIO mGpio;
119 std::shared_ptr<DigitalPinImpl> mImpl;
120};
121
122} // namespace gpio
123} // namespace platform
124} // namespace lampda
125
126#endif
Program scope.
Definition: control_fixed_modes.hpp:12