18 #include <linux/gpio.h> 20 #include <sys/ioctl.h> 23 #include <type_traits> 30 switch (errno_input) {
35 status = Os::File::Status::NOT_OPENED;
38 status = Os::File::Status::INVALID_ARGUMENT;
41 status = Os::File::Status::DOESNT_EXIST;
47 status = Os::File::Status::NO_PERMISSION;
50 status = Os::File::Status::INVALID_MODE;
66 switch (errno_input) {
68 status = Drv::GpioStatus::T::NOT_OPENED;
80 status = Drv::GpioStatus::T::UNKNOWN_ERROR;
88 switch (configuration) {
90 flags = GPIOHANDLE_REQUEST_OUTPUT;
97 flags = GPIOHANDLE_REQUEST_INPUT;
100 FW_ASSERT(0, static_cast<FwAssertArgType>(configuration));
108 switch (configuration) {
110 flags = GPIOEVENT_REQUEST_RISING_EDGE;
113 flags = GPIOEVENT_REQUEST_FALLING_EDGE;
116 flags = GPIOEVENT_REQUEST_RISING_EDGE | GPIOEVENT_REQUEST_FALLING_EDGE;
119 FW_ASSERT(0, static_cast<FwAssertArgType>(configuration));
126 (void) ::close(this->m_fd);
135 const GpioConfiguration& configuration,
140 struct gpiohandle_request request;
141 (void) ::memset(&request, 0,
sizeof request);
142 request.lineoffsets[0] = gpio;
144 static_cast<FwSizeType>(
sizeof request.consumer_label));
145 request.default_values[0] = (default_state ==
Fw::Logic::HIGH) ? 1 : 0;
151 PlatformIntType return_value = ioctl(chip_descriptor, GPIO_GET_LINEHANDLE_IOCTL, &request);
153 if (return_value != 0) {
162 const GpioConfiguration& configuration,
166 struct gpioevent_request event;
167 (void) ::memset(&event, 0,
sizeof event);
168 event.lineoffset = gpio;
170 static_cast<FwSizeType>(
sizeof event.consumer_label));
175 PlatformIntType return_value = ioctl(chip_descriptor, GPIO_GET_LINEEVENT_IOCTL, &event);
177 if (return_value != 0) {
191 FW_ASSERT(configuration < MAX_GPIO_CONFIGURATION and configuration >= 0, static_cast<FwAssertArgType>(configuration));
194 status = chip_file.
open(device, Os::File::Mode::OPEN_WRITE);
202 struct gpiochip_info chip_info;
203 (void) ::memset(&chip_info, 0,
sizeof chip_info);
204 PlatformIntType return_value = ioctl(chip_descriptor, GPIO_GET_CHIPINFO_IOCTL, &chip_info);
205 if (return_value != 0) {
211 if (gpio >= chip_info.lines) {
217 struct gpioline_info pin_info;
218 (void) ::memset(&pin_info, 0,
sizeof pin_info);
219 pin_info.line_offset = gpio;
220 return_value = ioctl(chip_descriptor, GPIO_GET_LINEINFO_IOCTL, &pin_info);
221 if (return_value == 0) {
222 const bool has_consumer = pin_info.consumer[0] !=
'\0';
223 pin_message.
format(
"%s%s%s", pin_info.name, has_consumer ?
" with current consumer " :
"",
224 has_consumer ? pin_info.consumer :
"");
229 switch (configuration) {
233 status = this->setupLineHandle(chip_descriptor, gpio, configuration, default_state, pin_fd);
239 status = this->setupLineEvent(chip_descriptor, gpio, configuration, pin_fd);
253 this->m_configuration = configuration;
260 if (this->m_configuration == GpioConfiguration::GPIO_INPUT) {
261 struct gpiohandle_data values;
262 (void) ::memset(&values, 0,
sizeof values);
263 PlatformIntType return_value = ioctl(this->m_fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &values);
264 if (return_value != 0) {
276 if (this->m_configuration == GpioConfiguration::GPIO_OUTPUT) {
277 struct gpiohandle_data values;
278 (void) ::memset(&values, 0,
sizeof values);
280 PlatformIntType return_value = ioctl(this->m_fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &values);
281 if (return_value != 0) {
290 void LinuxGpioDriver ::pollLoop() {
294 static_assert(
GPIO_POLL_TIMEOUT < static_cast<unsigned int>(std::numeric_limits<int>::max()),
"Poll timeout would overflow");
295 static_assert(
sizeof(
struct gpioevent_data) < std::numeric_limits<FwSizeType>::max(),
"FwSizeType too small");
296 using unsigned_ssize_t = std::make_unsigned<ssize_t>::type;
297 static_assert(static_cast<unsigned_ssize_t>(std::numeric_limits<ssize_t>::max()) <= std::numeric_limits<FwSizeType>::max(),
"FwSizeType too small");
299 pollfd file_descriptors[1];
301 while (this->getRunning()) {
303 (void) ::memset(file_descriptors, 0,
sizeof file_descriptors);
304 file_descriptors[0].fd = this->m_fd;
305 file_descriptors[0].events = POLLIN;
310 struct gpioevent_data event_data;
311 FwSizeType read_bytes =
static_cast<FwSizeType>(::read(this->m_fd, &event_data,
sizeof event_data));
312 if (read_bytes ==
sizeof event_data) {
320 static_cast<U32>(read_bytes));
324 else if (status < 0) {
A catch-all for other errors. Have to look in implementation-specific code.
Drv::GpioStatus errno_to_gpio_status(PlatformIntType errno_input)
Input GPIO pin triggers interrupt port on falling edge.
void log_WARNING_HI_InterruptReadError(U32 expected, U32 got) const
Log event InterruptReadError.
PlatformIntType NATIVE_INT_TYPE
PlatformSizeType FwSizeType
Status now() override
Get the current time.
void log_WARNING_HI_OpenPinError(const Fw::StringBase &chip, U32 pin, const Fw::StringBase &pinMessage, Os::FileStatus status) const
Log event OpenPinError.
Os::FileInterface::Status open(const char *path, Mode mode)
open file with supplied path and mode
int PlatformIntType
DefaultTypes.hpp provides fallback defaults for the platform types.
Os::File::Status errno_to_file_status(PlatformIntType errno_input)
Operation not permitted with current configuration.
#define FW_OPTIONAL_NAME(name)
void log_WARNING_HI_OpenChipError(const Fw::StringBase &chip, Os::FileStatus status) const
Log event OpenChipError.
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
void log_DIAGNOSTIC_OpenChip(const Fw::StringBase &chip, const Fw::StringBase &chipLabel, U32 pin, const Fw::StringBase &pinMessage) const
Log event OpenChip.
FileHandle * getHandle() override
returns the raw file handle
void gpioInterrupt_out(FwIndexType portNum, Os::RawTime &cycleStart)
Invoke output port gpioInterrupt.
GpioConfiguration
configure the GPIO pin
Input GPIO pin triggers interrupt port on both edges.
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
C++-compatible configuration header for fprime configuration.
No space left on the device for writing.
Os::File::Status open(const char *device, const U32 gpio, const GpioConfiguration &configuration, const Fw::Logic &default_state=Fw::Logic::LOW)
open a GPIO pin for use in the system
Operation was successful.
void log_WARNING_HI_PollingError(I32 error_number) const
Log event PollingError.
Output GPIO pin for direct writing.
U32 configuration_to_event_flags(Drv::LinuxGpioDriver::GpioConfiguration configuration)
Input GPIO pin for direct reading.
Input GPIO pin triggers interrupt port on rising edge.
U32 configuration_to_handler_flags(Drv::LinuxGpioDriver::GpioConfiguration configuration)
FPP shadow-enum representing Os::File::Status.
static constexpr FwSizeType GPIO_POLL_TIMEOUT