/workspaces/astro/sol3-sdk/cpp/sol3/gpio/gpio.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/gpio/gpio.h Source File
Space-ng SDK
gpio.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 
3 #pragma once
4 
5 #include <fcntl.h>
6 #include <unistd.h>
7 
8 #include <mutex>
9 #include <unordered_set>
10 
11 namespace sol3::gpio {
12 
13 using GpioPinId = uint32_t;
14 using GpioPinValue = uint32_t;
15 
17 enum class GpioEdge {
18  RISING,
19  FALLING,
20  BOTH
21 };
22 
28  bool active_low = false;
29 };
30 
33  bool timeout = false;
34  bool error = false;
35  bool interrupted = false;
37 };
38 
39 class GpioPinManager;
40 
41 namespace impl {
50 class GpioPinBase {
51  public:
52  // Move-only semantics.
53  GpioPinBase(GpioPinBase&& other) noexcept;
54  GpioPinBase& operator=(GpioPinBase&& other) noexcept;
55  GpioPinBase(GpioPinBase const&) = delete;
56  GpioPinBase& operator=(GpioPinBase const&) = delete;
57 
58  protected:
59  friend class GpioPinManager;
60 
62 
64  GpioPinBase(GpioPinId pin, GpioPinManager* manager, int value_fd);
65 
68  int value_fd_;
69 };
70 } // namespace impl
71 
74  public:
75  ~GpioInputPin() = default;
76 
78  GpioPinValue read() const;
79 
81  GpioEdgeEventResult waitForEdgeEvent(std::chrono::milliseconds timeout) const;
82 
84  GpioInputPinOptions const& getOptions() const { return options_; }
85 
86  // Move-only semantics.
87  GpioInputPin(GpioInputPin&& other) noexcept = default;
88  GpioInputPin& operator=(GpioInputPin&& other) noexcept = default;
89  GpioInputPin(GpioInputPin const& other) noexcept = delete;
90  GpioInputPin& operator=(GpioInputPin& other) noexcept = delete;
91 
92  private:
93  friend class GpioPinManager;
94 
97  GpioPinId pin,
98  GpioPinManager* manager,
99  GpioInputPinOptions const& options);
100 
101  GpioInputPinOptions options_;
102 };
103 
106  public:
107  ~GpioOutputPin() = default;
108 
110  void write(GpioPinValue value);
111 
112  // Move-only semantics.
113  GpioOutputPin(GpioOutputPin&& other) noexcept = default;
114  GpioOutputPin& operator=(GpioOutputPin&& other) noexcept = default;
115  GpioOutputPin(GpioOutputPin const& other) noexcept = delete;
116  GpioOutputPin& operator=(GpioOutputPin const& other) noexcept = delete;
117 
118  private:
119  friend class GpioPinManager;
120 
122  GpioOutputPin(GpioPinId pin, GpioPinManager* manager);
123 };
124 
129  public:
132  static GpioPinManager s_instance;
133  return s_instance;
134  }
135 
139  template <typename TPin>
141  TPin pin, GpioInputPinOptions const& options = {}) {
142  return GpioInputPin(static_cast<GpioPinId>(pin), this, options);
143  }
144 
148  template <typename TPin>
150  return GpioOutputPin(static_cast<GpioPinId>(pin), this);
151  }
152 
153  // Non-copyable, non-movable
154  GpioPinManager(GpioPinManager const&) = delete;
158 
159  private:
160  friend class impl::GpioPinBase;
161 
162  GpioPinManager() = default;
163  ~GpioPinManager() = default;
164 
165  // Reserve the pin for configuration. Called in the constructor of GpioPinBase
166  void reservePin(GpioPinId pin);
167 
168  // Release a reserved pin. Called in the destructor of GpioPinBase.
169  void releasePin(GpioPinId pin);
170 
171  mutable std::mutex mutex_;
172  std::unordered_set<GpioPinId> reserved_pins_;
173 };
174 
176 enum class Pin : GpioPinId {
177  // Carrier ADC Chipselects
178  CARRIER_ADC1_CS = 20,
179  CARRIER_ADC2_CS = 21,
180  CARRIER_ADC3_CS = 32,
181  // SPI
182  MISO = 52,
183  MOSI = 53,
184  SCLK = 54,
185  // Power ADC Chipselect
186  POWER_ADC_CS = 55,
187  // External trigger
188  EXTTRIG = 60,
189  // Self reset
190  SELF_RESET = 122,
191  // Shift Register
192  SER_FB = 137,
193  SER = 142,
194  SRCLK = 143,
195  RCLK = 144,
196  SR_OE = 145,
197  // Deserializer PMIC Enable
198  DES_PMIC_EN = 152,
199 };
200 
201 } // namespace sol3::gpio
RAII wrapper for GPIO input pins.
Definition: gpio.h:73
GpioEdgeEventResult waitForEdgeEvent(std::chrono::milliseconds timeout) const
Wait for an edge event on the input pin with the specified timeout.
GpioInputPin(GpioInputPin const &other) noexcept=delete
GpioInputPin(GpioInputPin &&other) noexcept=default
GpioInputPin & operator=(GpioInputPin &&other) noexcept=default
GpioPinValue read() const
Read current pin value.
GpioInputPinOptions const & getOptions() const
Get the configured options for this input pin.
Definition: gpio.h:84
GpioInputPin & operator=(GpioInputPin &other) noexcept=delete
RAII wrapper for GPIO output pins.
Definition: gpio.h:105
GpioOutputPin(GpioOutputPin const &other) noexcept=delete
GpioOutputPin(GpioOutputPin &&other) noexcept=default
GpioOutputPin & operator=(GpioOutputPin const &other) noexcept=delete
GpioOutputPin & operator=(GpioOutputPin &&other) noexcept=default
void write(GpioPinValue value)
Write pin value.
Definition: gpio.h:128
GpioPinManager(GpioPinManager const &)=delete
GpioInputPin acquireInputPin(TPin pin, GpioInputPinOptions const &options={})
Definition: gpio.h:140
GpioPinManager & operator=(GpioPinManager const &)=delete
GpioPinManager(GpioPinManager &&)=delete
GpioOutputPin acquireOutputPin(TPin pin)
Definition: gpio.h:149
GpioPinManager & operator=(GpioPinManager &&)=delete
static GpioPinManager & getInstance()
Get the singleton instance.
Definition: gpio.h:131
Definition: gpio.h:50
GpioPinId pin_
Definition: gpio.h:66
GpioPinBase(GpioPinId pin, GpioPinManager *manager, int value_fd)
Protected constructor - only derived classes can create.
GpioPinManager * manager_
Definition: gpio.h:67
GpioPinBase(GpioPinBase &&other) noexcept
GpioPinBase & operator=(GpioPinBase &&other) noexcept
GpioPinBase & operator=(GpioPinBase const &)=delete
GpioPinBase(GpioPinBase const &)=delete
int value_fd_
Definition: gpio.h:68
Definition: gpio.h:11
Pin
Current pinout for the Rev1 Sol3/HDK hardware.
Definition: gpio.h:176
GpioEdge
Edge trigger modes for GPIO input event detection.
Definition: gpio.h:17
@ FALLING
Trigger on high-to-low transition.
@ BOTH
Trigger on both transitions.
@ RISING
Trigger on low-to-high transition.
uint32_t GpioPinId
Definition: gpio.h:13
uint32_t GpioPinValue
Definition: gpio.h:14
Return value struct for GpioInputPin::waitForEdgeEvent().
Definition: gpio.h:32
bool interrupted
Definition: gpio.h:35
bool timeout
Definition: gpio.h:33
bool error
Definition: gpio.h:34
GpioEdge edge
Definition: gpio.h:36
Options for configuring a Gpio input pin.
Definition: gpio.h:24
GpioEdge edge
Edge detection mode for interrupt-based monitoring.
Definition: gpio.h:26
bool active_low
Invert pin logic (true = active-low)
Definition: gpio.h:28