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, len) != len) {
83 len = snprintf(buf,
sizeof(buf),
"%u", gpio);
84 if(write(fd, buf, 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";
119 if (write(fd, dir, 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 = strlen(edge) + 1;
203 if(write(fd, edge, 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 ::
312 if (-1 == this->m_fd) {
324 void LinuxGpioDriverComponentImpl ::
325 intTaskEntry(
void * ptr) {
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, 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::TaskStatus stat = this->m_intTask.
start(name, LinuxGpioDriverComponentImpl::intTaskEntry,
this, priority, stackSize, cpuAffinity);
415 this->m_quitThread =
true;
423 if (this->m_fd != -1) {
424 DEBUG_PRINT(
"Closing GPIO %d fd %d\n",this->m_gpio, this->m_fd);
PlatformIntType NATIVE_INT_TYPE
PlatformUIntType NATIVE_UINT_TYPE
C++-compatible configuration header for fprime configuration.
void log_WARNING_HI_GP_IntStartError(I32 gpio)
NATIVE_INT_TYPE getNum_intOut_OutputPorts() const
void log_WARNING_HI_GP_ReadError(I32 gpio, I32 error)
void log_WARNING_HI_GP_IntWaitError(I32 gpio)
bool isConnected_intOut_OutputPort(NATIVE_INT_TYPE portNum)
void log_WARNING_HI_GP_WriteError(I32 gpio, I32 error)
void intOut_out(NATIVE_INT_TYPE portNum, Svc::TimerVal &cycleStart)
Invoke output port intOut.
void log_WARNING_HI_GP_OpenError(I32 gpio, I32 error, const Fw::LogStringArg &msg)
bool open(NATIVE_INT_TYPE gpio, GpioDirection direction)
open GPIO
void exitThread()
exit thread
~LinuxGpioDriverComponentImpl()
Os::Task::TaskStatus startIntTask(NATIVE_UINT_TYPE priority=Os::Task::TASK_DEFAULT, NATIVE_UINT_TYPE stackSize=Os::Task::TASK_DEFAULT, NATIVE_UINT_TYPE cpuAffinity=Os::Task::TASK_DEFAULT)
Start interrupt task.
GpioDirection
configure GPIO
void format(const CHAR *formatString,...)
write formatted string to buffer
@ TASK_OK
message sent/received okay
TaskStatus start(const Fw::StringBase &name, taskRoutine routine, void *arg, NATIVE_UINT_TYPE priority=TASK_DEFAULT, NATIVE_UINT_TYPE stackSize=TASK_DEFAULT, NATIVE_UINT_TYPE cpuAffinity=TASK_DEFAULT, NATIVE_UINT_TYPE identifier=TASK_DEFAULT)
start the task
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)