主题 : 简单请教个驱动问题 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 60244
精华: 0
发帖: 20
金钱: 100 两
威望: 20 点
综合积分: 40 分
注册时间: 2011-12-07
最后登录: 2019-06-17
楼主  发表于: 2012-03-12 15:35

 简单请教个驱动问题

友善三星2440的buttons_test程序,里面是这样的
for (;;) {
        char current_buttons[3];
        int count_of_changed_key;
        int i;
        
        if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
            perror("read buttons:");
            exit(1);
        }

        for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
            if (buttons != current_buttons) {
                buttons = current_buttons;
                printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons == '0' ? "up" : "down");
                count_of_changed_key++;
            }
        }
        if (count_of_changed_key) {
            printf("\n");
        }
        printf("ok\n");    //这是我添加的,为什么不是一直打印OK?????
        
    }

只打印了一次,然后按键才打印一次???不是死循环来的吗?
级别: 侠客
UID: 63650
精华: 0
发帖: 64
金钱: 330 两
威望: 66 点
综合积分: 128 分
注册时间: 2012-02-22
最后登录: 2016-12-03
1楼  发表于: 2012-03-13 19:17
>read(buttons_fd, current_buttons, sizeof current_buttons)
这个读取按键的接口可能会阻塞等待按键输入?(猜测)
级别: 新手上路
UID: 60244
精华: 0
发帖: 20
金钱: 100 两
威望: 20 点
综合积分: 40 分
注册时间: 2011-12-07
最后登录: 2019-06-17
2楼  发表于: 2012-11-07 11:03
就是那里的read问题,等待中断发生。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
3楼  发表于: 2012-11-07 14:07

 回 2楼(liyk) 的帖子

这是阻塞型IO,参考《Linux Device Driver》第三版 6.2节"阻塞I/O"的描述
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."