2015-07-21 100 views
1

我想使用民意調查()函數得到基於GPIO的中斷在BeagleBone GPIO上工作。下面是我的代碼,但總之:使用民意調查BeagleBone黑色GPIO中斷()

雖然我有針連接到地面,沒有任何反應(如預期)。

當我將引腳連接到高電平時,會產生中斷。然而,它永遠不會清除或以極快的速度再生。我實施了一個計數器,當它升高時,計數器會增加,在幾秒鐘內,計數器就超過10,000。我在設置中丟失了什麼?這個問題可能在我的main()函數中,但我已經包含了其他函數的完整性。

感謝您的任何援助,

查爾斯

#define SYSFS_GPIO_DIR "/sys/class/gpio" 
#define MAX_BUF 64 

int gpio_export(unsigned int gpio) 
{ 
    int fd, len; 
    char buf[MAX_BUF]; 

    fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY); 
    if (fd < 0) { 
     perror("gpio/export"); 
     return fd; 
    } 

    len = snprintf(buf, sizeof(buf), "%d", gpio); 
    write(fd, buf, len); 
    close(fd); 

    return 0; 
} 

int gpio_set_dir(unsigned int gpio, unsigned int direction) 
{ 
    int fd, len; 
    char buf[MAX_BUF]; 

    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio); 

    fd = open(buf, O_WRONLY); 
    if (fd < 0) { 
     perror("gpio/direction"); 
     return fd; 
    } 

    if (direction) 
     write(fd, "out", 4); 
    else 
     write(fd, "in", 3); 

    close(fd); 
    return 0; 
} 

int gpio_set_edge(unsigned int gpio, char *edge) 
{ 
    int fd, len; 
    char buf[MAX_BUF]; 

    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio); 

    fd = open(buf, O_WRONLY); 
    if (fd < 0) { 
     perror("gpio/set-edge"); 
     return fd; 
    } 

    write(fd, edge, strlen(edge) + 1); 
    close(fd); 
    return 0; 
} 

int gpio_fd_open(unsigned int gpio) 
{ 
    int fd, len; 
    char buf[MAX_BUF]; 

    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); 

    fd = open(buf, O_RDONLY | O_NONBLOCK); 
    if (fd < 0) { 
     perror("gpio/fd_open"); 
    } 
    return fd; 
} 

int gpio_fd_close(int fd) 
{ 
    return close(fd); 
} 

int main() 
{ 
    struct pollfd fdset; 
    int nfds = 1; 
    int gpio_fd, timeout, rc; 
    char *buf[MAX_BUF]; 
    unsigned int gpio; 
    int len; 
    char* c; 


    gpio = 26; 

    gpio_export(gpio); 
    gpio_set_dir(gpio, 0); 
    gpio_set_edge(gpio, "rising"); 
    gpio_fd = gpio_fd_open(gpio); 

    timeout = 1; 

    while (1) { 
     memset((void*)&fdset, 0, sizeof(fdset)); 

     fdset.fd = gpio_fd; 
     fdset.events = POLLPRI | POLLERR; 

     len = read(fdset.fd, c, 1); 

     rc = poll(&fdset, nfds, timeout); 

     //printf("POLLPRI is %02x, POLLERR is %02x\r\n", POLLPRI, POLLERR); 

     if (rc < 0) { 
      printf("\npoll() failed!\n"); 
      return -1; 
     } 

     if (rc == 0) { 
      printf("."); 
     } 

     if (fdset.revents & POLLPRI) { 
      if (len==-1) 
      { 
       printf("Hex of revents is %02x, return code is %02x\r\n", fdset.revents, rc); 
      } 
      len = read(fdset.fd, buf, MAX_BUF); 
     } 

     if (fdset.revents & POLLERR) 
     { 
      printf("errno: %d", errno); 
     } 
    } 

    gpio_fd_close(gpio_fd); 
    return 0; 
} 
+0

我沒有讀過你的代碼,但是如果你通過機械開關或類似的(開路線)「連接」,你必須反彈(只搜索術語)。 – Olaf

回答

0

我重新安裝的BeagleBone黑Debian的形象,它的工作原理就像現在的預期。我不確定爲什麼它以前不工作,但它看起來像是一個操作系統問題。