int event_send(int id, int event) { if (event < 0 || event >= EV_COUNT) { return 1; } if (evqueue[event].front_thread) { // handle event now struct thread *thread = evqueue[event].front_thread; evqueue[event].front_thread = thread->next_evqueue; if (!thread->next_evqueue) evqueue[event].back_thread = NULL; thread->eax = event; thread->event = -1; if (thread->state == TS_RUNNING) { thread_save(thread); } schedule_push(thread); thread->state = TS_QUEUED; } if (event < 240) { irq_mask(event); irq_reset(event); irqstate[event] = 1; } return 0; }
int irq_read(void) { struct timeval tv; gettimeofday(&tv,NULL); irq_reset(&pfd.fd, rdbuf); if(irq_poll(&pfd.fd, rdbuf, &pfd)==0) { timestamp.first = timestamp.second; timestamp.second = 1000000 * (unsigned int)tv.tv_sec + (unsigned int)tv.tv_usec; return (int)timestamp.second-(int)timestamp.first; } return EXIT_FAILURE; }
/* Attaches an interrupt handler to a specific GPIO pin Whenever an rising, falling or changing interrupt occurs the function given as the last argument will be called */ int irq_attach(int gpio_pin, int mode) { gc_attach(irq_free); char command[50], folder[32]; int err; struct stat s; sprintf(folder, "/sys/class/gpio/gpio%d", gpio_wiringPi2BCM(gpio_pin)); if((err = stat(folder, &s)) != -1 && S_ISDIR(s.st_mode)) { if(mode == CHANGE) sprintf(command, "echo falling > /sys/class/gpio/gpio%d/edge", gpio_wiringPi2BCM(gpio_pin)); else if(mode == RISING) sprintf(command, "echo rising > /sys/class/gpio/gpio%d/edge", gpio_wiringPi2BCM(gpio_pin)); else if(mode == FALLING) sprintf(command, "echo both > /sys/class/gpio/gpio%d/edge", gpio_wiringPi2BCM(gpio_pin)); } else { logprintf(LOG_ERR, "can't claim gpio pin %d", gpio_pin); return EXIT_FAILURE; } system(command); irq_reset(&fd, rdbuf); memset(fn, 0x00, GPIO_FN_MAXLEN); snprintf(fn, GPIO_FN_MAXLEN-1, "/sys/class/gpio/gpio%d/value", gpio_wiringPi2BCM(gpio_pin)); fd=open(fn, O_RDONLY); if(fd < 0) { return EXIT_FAILURE; } pfd.fd=fd; pfd.events=POLLPRI; if(read(fd, rdbuf, RDBUF_LEN-1)<0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }