设置IO设备的回调函数,以便在IO事件发生时获取数据

时间:2016-09-06 11:28:34

标签: c++ c i2c

我正在尝试使用I2C接口从陀螺仪获取数据,我无法弄清楚如何设置事件处理程序来检查IO而不是在无限循环中读取FIFO缓冲区。

我使用UART做了类似的事情。

struct sigaction saio;
struct termios options;

uartFilestream = open(UART_DEV, O_RDWR | O_NOCTTY | O_NDELAY);    //Open in non blocking read/write mode

saio.sa_handler = HandleIO;
saio.sa_flags = 0;
saio.sa_restorer = NULL; 
sigaction(SIGIO,&saio,NULL);

fcntl(uartFilestream, F_SETFL, FNDELAY);
fcntl(uartFilestream, F_SETOWN, getpid());
fcntl(uartFilestream, F_SETFL,  O_ASYNC );

tcgetattr(uartFilestream, &options);

options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;   //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;

tcflush(uartFilestream, TCIFLUSH);
tcsetattr(uartFilestream, TCSANOW, &options);
只要串行接口上​​发生IO,就会调用

HandleIO。请告诉我如何使用I2C做同样的事情。

这是我用I2C做的,但它不起作用。

fd = open("/dev/i2c-1", O_RDWR | O_NOCTTY | O_NDELAY);    //Open in non blocking read/write mode

saio.sa_handler = test_func;
saio.sa_flags = 0;
saio.sa_restorer = NULL; 
sigaction(SIGIO,&saio,NULL);

fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL,  O_ASYNC );

tcgetattr(fd, &options);

options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;   //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;

tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);

0 个答案:

没有答案