19 #define SYSFS_GPIO_DIR "/sys/class/gpio"
31 #define DEBUG_PRINT(...)
52 len = snprintf(buf,
sizeof(buf),
"%u", gpio);
53 if(write(fd, buf,
static_cast<size_t>(len)) != len) {
83 len = snprintf(buf,
sizeof(buf),
"%u", gpio);
84 if(write(fd, buf,
static_cast<size_t>(len)) != len) {
107 len = snprintf(buf,
sizeof(buf),
SYSFS_GPIO_DIR "/gpio%u/direction", gpio);
110 fd = open(buf, O_WRONLY);
116 const char *dir = out_flag ?
"out" :
"in";
117 len =
static_cast<int>(strlen(dir));
119 if (write(fd, dir,
static_cast<size_t>(len)) != len) {
139 const char *val = value ?
"1" :
"0";
142 if(write(fd, val, len) != len) {
147 DEBUG_PRINT(
"GPIO fd %d value %d written\n",fd,value);
163 if (stat1 == -1 || stat2 != 1) {
164 DEBUG_PRINT(
"GPIO read failure: %d %d!\n",stat1,stat2);
193 len = snprintf(buf,
sizeof(buf),
SYSFS_GPIO_DIR "/gpio%u/edge", gpio);
196 fd = open(buf, O_WRONLY);
202 len =
static_cast<int>(strlen(edge) + 1);
203 if(write(fd, edge,
static_cast<size_t>(len)) != len) {
222 len = snprintf(buf,
sizeof(buf),
SYSFS_GPIO_DIR "/gpio%u/value", gpio);
225 fd = open(buf, O_RDWR | O_NONBLOCK );
251 void LinuxGpioDriverComponentImpl ::
270 void LinuxGpioDriverComponentImpl ::
295 stat =
gpio_export(
static_cast<unsigned int>(gpio));
311 this->m_fd =
gpio_fd_open(
static_cast<unsigned int>(gpio));
312 if (-1 == this->m_fd) {
324 void LinuxGpioDriverComponentImpl ::
325 intTaskEntry(
void * ptr) {
333 stat =
gpio_set_edge(
static_cast<unsigned int>(compPtr->m_gpio),
"rising");
340 while(not compPtr->m_quitThread) {
345 memset(fdset, 0,
sizeof(fdset));
347 fdset[0].fd = compPtr->m_fd;
348 fdset[0].events = POLLPRI;
349 stat = poll(fdset,
static_cast<nfds_t
>(nfds), timeout);
357 DEBUG_PRINT(
"stat: %d, revents: 0x%x, POLLERR: 0x%x, POLLIN: 0x%x, POLLPRI: 0x%x\n",
358 stat, fdset[0].revents, POLLERR, POLLIN, POLLPRI);
365 DEBUG_PRINT(
"Krait timed out waiting for GPIO interrupt\n");
376 if (fdset[0].revents & POLLPRI) {
379 (void) lseek(fdset[0].fd, 0, SEEK_SET);
380 if(read(fdset[0].fd, buf,
MAX_BUF) > 0) {
381 DEBUG_PRINT(
"\npoll() GPIO interrupt occurred w/ value: %c\n", buf[0]);
402 name.
format(
"GPINT_%s",this->getObjName());
403 Os::Task::Arguments arguments(name, LinuxGpioDriverComponentImpl::intTaskEntry,
this, priority, stackSize, cpuAffinity);
416 this->m_quitThread =
true;
424 if (this->m_fd != -1) {
425 DEBUG_PRINT(
"Closing GPIO %d fd %d\n",this->m_gpio, this->m_fd);
426 (void)
gpio_fd_close(this->m_fd,
static_cast<unsigned int>(this->m_gpio));
PlatformIntType NATIVE_INT_TYPE
PlatformUIntType NATIVE_UINT_TYPE
C++-compatible configuration header for fprime configuration.
void intOut_out(FwIndexType portNum, Svc::TimerVal &cycleStart)
Invoke output port intOut.
void log_WARNING_HI_GP_IntStartError(I32 gpio)
bool isConnected_intOut_OutputPort(FwIndexType portNum)
FwIndexType getNum_intOut_OutputPorts() const
void log_WARNING_HI_GP_ReadError(I32 gpio, I32 error)
void log_WARNING_HI_GP_IntWaitError(I32 gpio)
void log_WARNING_HI_GP_OpenError(I32 gpio, I32 error, const Fw::StringBase &msg)
void log_WARNING_HI_GP_WriteError(I32 gpio, I32 error)
bool open(NATIVE_INT_TYPE gpio, GpioDirection direction)
open GPIO
void exitThread()
exit thread
Os::Task::Status startIntTask(Os::Task::ParamType priority=Os::Task::TASK_DEFAULT, Os::Task::ParamType stackSize=Os::Task::TASK_DEFAULT, Os::Task::ParamType cpuAffinity=Os::Task::TASK_DEFAULT)
Start interrupt task.
~LinuxGpioDriverComponentImpl()
GpioDirection
configure GPIO
void format(const CHAR *formatString,...)
write formatted string to buffer
FwSizeType ParamType
backwards-compatible parameter type
Status start(const Arguments &arguments) override
start the task
@ OP_OK
message sent/received okay
Serializable class for carrying timer values.
void take()
Function to store a timer value.
int gpio_fd_open(unsigned int gpio)
int gpio_set_edge(unsigned int gpio, const char *edge)
int gpio_fd_close(int fd, unsigned int gpio)
int gpio_unexport(unsigned int gpio)
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
int gpio_get_value(int fd, unsigned int *value)
int gpio_export(unsigned int gpio)
int gpio_set_value(int fd, unsigned int value)