手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

用QT210 LDD平台运行《Linux设备驱动开发详解》实例

[日期:2012-01-27] 来源:Linux社区  作者:21cnbao [字体:大 中 小]

编译上述程序有2种方法,一种是使用标准的针对arm的gcc,编译时将上述程序静态编译,这样编译出来的可执行文件不再依赖于glibc动态库;一种是编译针对Android的Android.mk,使用Android的编译器编译,这样编译出来的可执行文件依赖于Android Bionic C动态库,也可以在板子上执行。假设使用前一种方法:

[plain]
  1. arm-unknown-linux-gnueabi-gcc -o fb_test fb_test.c -static  

我们通过adb的push功能把fb_test push到电路板上执行。


由于默认情况下,Android本身绘占据framebuffer,因此,为了保证本实例的运行,可暂停Android的zygote服务:

运行native下的测试案例,需要停止Android本身,我们可以在登录adb shell后,运行:

[plain]
  1. # stop zygote  
停止zygote服务,这样Android本身的刷屏功能将停止工作。

运行

[plain]
  1. # fb_test  
  2. The framebuffer device was opened successfully.  
  3. 800x480, 32bpp  
  4. The framebuffer device was mapped to memory successfully.  
我们将在屏幕上看到3色彩带。

如果我们想恢复Android的运行,只需要启动zygote即可。

[plain]
  1. # start zygote  

2. key test程序

每次按下电路板左侧的任何一个按键,相应的input event事件即被打印,程序代码如下:

[cpp]
  1. /* 
  2.  * LDD6410 key test programs 
  3.  * Copyright 2011 www.linuxdriver.cn 
  4.  */  
  5. #include <unistd.h>   
  6. #include <stdlib.h>   
  7. #include <stdio.h>   
  8. #include <fcntl.h>   
  9. #include <linux/input.h>   
  10.   
  11. int main()  
  12. {  
  13.     int keyfd = 0;  
  14.     char name[50];  
  15.     struct input_event event;  
  16.   
  17.     keyfd = open("/dev/input/event0", O_RDWR);  
  18.     if (!keyfd) {  
  19.         printf("Error: cannot open key input device.\n");  
  20.         exit(1);  
  21.     }  
  22.   
  23.     printf("The input device was opened successfully, name:\n");  
  24.     system("cat /sys/class/input/event0/device/name");  
  25.   
  26.     while(1) {  
  27.         int ret = read(keyfd, &event, sizeof(struct input_event));  
  28.         if (ret == sizeof(struct input_event))  
  29.             printf("input event type:%d code:%d value:%d\n", event.type, event.code, event.value);  
  30.         else  
  31.             continue;  
  32.     }  
  33.     return 0;  
  34. }  
通过adb shell运行该程序,按键后,shell打印如下:

[plain]
  1.  # key_test   
  2. The input device was opened successfully, name:  
  3. s3c-keypad  
  4. input event type:1 code:25 value:1  
  5. input event type:1 code:25 value:0  
  6. input event type:1 code:41 value:1  
  7. input event type:1 code:41 value:0  
  8. input event type:1 code:49 value:1  
  9. input event type:1 code:49 value:0  

3. i2c rw

这个程序可以用userspace读写板子上任何一个i2c client设备上的任何一个寄存器,程序的代码如下:

[cpp]
  1. #include <stdio.h>   
  2. #include <linux/types.h>   
  3. #include <fcntl.h>   
  4. #include <unistd.h>   
  5. #include <stdlib.h>   
  6. #include <sys/types.h>   
  7. #include <sys/ioctl.h>   
  8. #include <errno.h>   
  9. #include <assert.h>   
  10. #include <string.h>   
  11. #include <linux/i2c.h>   
  12. #include <linux/i2c-dev.h>   
  13.   
  14. int i2c_read_reg(char *dev, char *buf, unsigned slave_address, unsigned reg_address, int len)  
  15. {  
  16.     struct i2c_rdwr_ioctl_data work_queue;  
  17.     unsigned char w_val = reg_address;  
  18.     int ret;  
  19.   
  20.     int fd = open(dev, O_RDWR);  
  21.     if (!fd) {  
  22.         printf("Error on opening the device file\n");  
  23.         return 0;  
  24.     }  
  25.   
  26.     work_queue.nmsgs = 2; /* 消息数量 */  
  27.     work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct  
  28.             i2c_msg));  
  29.     if (!work_queue.msgs) {  
  30.         printf("Memory alloc error\n");  
  31.         close(fd);  
  32.         return 0;  
  33.     }  
  34.   
  35.     ioctl(fd, I2C_TIMEOUT, 2); /* 设置超时 */  
  36.     ioctl(fd, I2C_RETRIES, 1); /* 设置重试次数 */  
  37.   
  38.     (work_queue.msgs[0]).len = 1;  
  39.     (work_queue.msgs[0]).addr = slave_address;  
  40.     (work_queue.msgs[0]).buf = &w_val;  
  41.   
  42.     (work_queue.msgs[1]).len = len;  
  43.     (work_queue.msgs[1]).flags = I2C_M_RD;  
  44.     (work_queue.msgs[1]).addr = slave_address;  
  45.     (work_queue.msgs[1]).buf = buf;  
  46.   
  47.     ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);  
  48.     if (ret < 0) {  
  49.         printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);  
  50.         close(fd);  
  51.         free(work_queue.msgs);  
  52.         return 0;  
  53.     } else {  
  54.         printf("read salve:%02x reg:%02x value:%02x\n", slave_address, reg_address, buf[0]);  
  55.         close(fd);  
  56.         free(work_queue.msgs);  
  57.         return len;  
  58.     }  
  59. }  
  60.   
  61. int i2c_write_reg(char *dev, char *buf, unsigned slave_address, unsigned reg_address, int len)  
  62. {  
  63.     struct i2c_rdwr_ioctl_data work_queue;  
  64.     unsigned char w_val = reg_address;  
  65.     unsigned char w_buf[len+1];  
  66.     int ret;  
  67.   
  68.     w_buf[0] = reg_address;  
  69.   
  70.     int fd = open(dev, O_RDWR);  
  71.     if (!fd) {  
  72.         printf("Error on opening the device file\n");  
  73.         return 0;  
  74.     }  
  75.   
  76.     work_queue.nmsgs = 1; /* 消息数量 */  
  77.     work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct  
  78.             i2c_msg));  
  79.     if (!work_queue.msgs) {  
  80.         printf("Memory alloc error\n");  
  81.         close(fd);  
  82.         return 0;  
  83.     }  
  84.   
  85.     ioctl(fd, I2C_TIMEOUT, 2); /* 设置超时 */  
  86.     ioctl(fd, I2C_RETRIES, 1); /* 设置重试次数 */  
  87.   
  88.     (work_queue.msgs[0]).len = 1 + len;  
  89.     (work_queue.msgs[0]).addr = slave_address;  
  90.     (work_queue.msgs[0]).buf = w_buf;  
  91.   
  92.     memcpy(w_buf + 1, buf, len);  
  93.   
  94.     ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue);  
  95.     if (ret < 0) {  
  96.         printf("Error during I2C_RDWR ioctl with error code: %d\n", ret);  
  97.         close(fd);  
  98.         free(work_queue.msgs);  
  99.         return 0;  
  100.     } else {  
  101.         printf("write salve:%02x reg:%02x\n", slave_address, reg_address);  
  102.         close(fd);  
  103.         free(work_queue.msgs);  
  104.         return len;  
  105.     }  
  106. }  
  107.   
  108. int main(int argc, char **argv)  
  109. {  
  110.     unsigned int fd;  
  111.     unsigned int slave_address, reg_address;  
  112.     unsigned r_w;  
  113.     char rw_val;  
  114.   
  115.     if (argc < 5) {  
  116.         printf("Usage:\n%s /dev/i2c-x start_addr reg_addr rw[0|1] [write_val]\n", argv[0]);  
  117.         return 0;  
  118.     }  
  119.   
  120.     fd = open(argv[1], O_RDWR);  
  121.   
  122.     if (!fd) {  
  123.         printf("Error on opening the device file %s\n", argv[1]);  
  124.         return 0;  
  125.     }  
  126.   
  127.     sscanf(argv[2], "%x", &slave_address);  
  128.     sscanf(argv[3], "%x", ?_address);  
  129.     sscanf(argv[4], "%d", &r_w);  
  130.   
  131.     if (r_w == 0) {  
  132.         i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1);  
  133.     } else {  
  134.         if (argc < 6) {  
  135.             printf("Usage:\n%s /dev/i2c-x start_addr reg_addr r|w[0|1] [write_val]\n", argv[0]);  
  136.             return 0;  
  137.         }  
  138.         sscanf(argv[5], "%d", &rw_val);  
  139.         i2c_write_reg(argv[1], &rw_val, slave_address, reg_address, 1);  
  140.     }  
  141. }  
程序的用法为:

[plain]
  1. 读寄存器:  
  2. /system/bin/i2c-rw /dev/i2c-2(挂的总线,可以是i2c-0, i2c-1....)  0x11(i2c地址)  
  3.     0x22(寄存器地址) 0  
  4. 写寄存器:  
  5. /system/bin/i2c-rw /dev/i2c-2(挂的总线,可以是i2c-0, i2c-1....)  0x11(i2c地址)  
  6. 0x22(寄存器地址) 1 0xff(写入寄存器的值)  
在QT210 LDD平台上运行结果如下:

[plain]
  1. / #  i2c-rw /dev/i2c-2 0x38 0x1 0  
  2. read salve:38 reg:01 value:12  
  3. / #   
  4. / #  i2c-rw /dev/i2c-2 0x38 0x2 0  
  5. read salve:38 reg:02 value:81  
linux
相关资讯       Qt  Linux驱动开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款