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

QT210 LDD开发平台采用Samsung S5PV210,基于CortexTM-A8,运行主频1GHz,内置PowerVR SGX540高性能图形引擎,最高可支持1080p@30fps硬件解码视频流畅播放,格式可为MPEG4, H.263, H.264等。

QT210 LDD S5PV210平台地址系: http://item.taobao.com/item.htm?id=13261044573



QT210默认运行Android 2.3,是LDD6410硬件软件的全面升级。下面我们以3个case为例看看如何以QT210 LDD平台运行《Linux设备驱动开发详解》的实例。


1. framebuffer测试程序

该测试程序在lcd上绘制r,g,b3个逐渐变化的彩带,程序源代码如下:

/* * LDD6410 framebuffer test programs * Copyright 2011 www.linuxdriver.cn */ #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <linux/fb.h> #include <sys/mman.h> int main() { int fbfd = 0; struct fb_var_screeninfo vinfo; unsigned long screensize = 0; char *fbp = 0; int x = 0, y = 0; int i = 0; // Open the file for reading and writing fbfd = open("/dev/graphics/fb0", O_RDWR); if (!fbfd) { printf("Error: cannot open framebuffer device.\n"); exit(1); } printf("The framebuffer device was opened successfully.\n"); // Get variable screen information if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) { printf("Error reading variable information.\n"); exit(1); } printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel); if (vinfo.bits_per_pixel != 16 && vinfo.bits_per_pixel != 32) { printf("Error: not supported bits_per_pixel, it only supports 16/32 bit color\n"); } // Figure out the size of the screen in bytes screensize = vinfo.xres * vinfo.yres * (vinfo.bits_per_pixel / 8); // Map the device to memory fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0); if ((int)fbp == -1) { printf("Error: failed to map framebuffer device to memory.\n"); exit(4); } printf("The framebuffer device was mapped to memory successfully.\n"); // Draw 3 rect with graduated RED/GREEN/BLUE for (i = 0; i < 3; i++) { for (y = i * (vinfo.yres / 3); y < (i + 1) * (vinfo.yres / 3); y++) { for (x = 0; x < vinfo.xres; x++) { long location = x * 2 + y * vinfo.xres * 2; int r = 0, g = 0, b = 0; if (vinfo.bits_per_pixel == 16) { unsigned short rgb; if (i == 0) r = ((x * 1.0) / vinfo.xres) * 32; if (i == 1) g = ((x * 1.0) / vinfo.xres) * 64; if (i == 2) b = ((x * 1.0) / vinfo.xres) * 32; rgb = (r << 11) | (g << 5) | b; *((unsigned short*)(fbp + location)) = rgb; } else { location = location * 2; unsigned int rgb; if (i == 0) r = ((x * 1.0) / vinfo.xres) * 256; if (i == 1) g = ((x * 1.0) / vinfo.xres) * 256; if (i == 2) b = ((x * 1.0) / vinfo.xres) * 256; rgb = (r << 16) | (g << 8) | b; *((unsigned int*)(fbp + location)) = rgb; } } } } munmap(fbp, screensize); close(fbfd); return 0; }

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

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后,运行:

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

运行

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

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

# start zygote

2. key test程序

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

/* * LDD6410 key test programs * Copyright 2011 www.linuxdriver.cn */ #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <linux/input.h> int main() { int keyfd = 0; char name[50]; struct input_event event; keyfd = open("/dev/input/event0", O_RDWR); if (!keyfd) { printf("Error: cannot open key input device.\n"); exit(1); } printf("The input device was opened successfully, name:\n"); system("cat /sys/class/input/event0/device/name"); while(1) { int ret = read(keyfd, &event, sizeof(struct input_event)); if (ret == sizeof(struct input_event)) printf("input event type:%d code:%d value:%d\n", event.type, event.code, event.value); else continue; } return 0; }通过adb shell运行该程序,按键后,shell打印如下:

# key_test The input device was opened successfully, name: s3c-keypad input event type:1 code:25 value:1 input event type:1 code:25 value:0 input event type:1 code:41 value:1 input event type:1 code:41 value:0 input event type:1 code:49 value:1 input event type:1 code:49 value:0


3. i2c rw

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

#include <stdio.h> #include <linux/types.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ioctl.h> #include <errno.h> #include <assert.h> #include <string.h> #include <linux/i2c.h> #include <linux/i2c-dev.h> int i2c_read_reg(char *dev, char *buf, unsigned slave_address, unsigned reg_address, int len) { struct i2c_rdwr_ioctl_data work_queue; unsigned char w_val = reg_address; int ret; int fd = open(dev, O_RDWR); if (!fd) { printf("Error on opening the device file\n"); return 0; } work_queue.nmsgs = 2; /* 消息数量 */ work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct i2c_msg)); if (!work_queue.msgs) { printf("Memory alloc error\n"); close(fd); return 0; } ioctl(fd, I2C_TIMEOUT, 2); /* 设置超时 */ ioctl(fd, I2C_RETRIES, 1); /* 设置重试次数 */ (work_queue.msgs[0]).len = 1; (work_queue.msgs[0]).addr = slave_address; (work_queue.msgs[0]).buf = &w_val; (work_queue.msgs[1]).len = len; (work_queue.msgs[1]).flags = I2C_M_RD; (work_queue.msgs[1]).addr = slave_address; (work_queue.msgs[1]).buf = buf; ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue); if (ret < 0) { printf("Error during I2C_RDWR ioctl with error code: %d\n", ret); close(fd); free(work_queue.msgs); return 0; } else { printf("read salve:%02x reg:%02x value:%02x\n", slave_address, reg_address, buf[0]); close(fd); free(work_queue.msgs); return len; } } int i2c_write_reg(char *dev, char *buf, unsigned slave_address, unsigned reg_address, int len) { struct i2c_rdwr_ioctl_data work_queue; unsigned char w_val = reg_address; unsigned char w_buf[len+1]; int ret; w_buf[0] = reg_address; int fd = open(dev, O_RDWR); if (!fd) { printf("Error on opening the device file\n"); return 0; } work_queue.nmsgs = 1; /* 消息数量 */ work_queue.msgs = (struct i2c_msg*)malloc(work_queue.nmsgs *sizeof(struct i2c_msg)); if (!work_queue.msgs) { printf("Memory alloc error\n"); close(fd); return 0; } ioctl(fd, I2C_TIMEOUT, 2); /* 设置超时 */ ioctl(fd, I2C_RETRIES, 1); /* 设置重试次数 */ (work_queue.msgs[0]).len = 1 + len; (work_queue.msgs[0]).addr = slave_address; (work_queue.msgs[0]).buf = w_buf; memcpy(w_buf + 1, buf, len); ret = ioctl(fd, I2C_RDWR, (unsigned long) &work_queue); if (ret < 0) { printf("Error during I2C_RDWR ioctl with error code: %d\n", ret); close(fd); free(work_queue.msgs); return 0; } else { printf("write salve:%02x reg:%02x\n", slave_address, reg_address); close(fd); free(work_queue.msgs); return len; } } int main(int argc, char **argv) { unsigned int fd; unsigned int slave_address, reg_address; unsigned r_w; char rw_val; if (argc < 5) { printf("Usage:\n%s /dev/i2c-x start_addr reg_addr rw[0|1] [write_val]\n", argv[0]); return 0; } fd = open(argv[1], O_RDWR); if (!fd) { printf("Error on opening the device file %s\n", argv[1]); return 0; } sscanf(argv[2], "%x", &slave_address); sscanf(argv[3], "%x", ®_address); sscanf(argv[4], "%d", &r_w); if (r_w == 0) { i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1); } else { if (argc < 6) { printf("Usage:\n%s /dev/i2c-x start_addr reg_addr r|w[0|1] [write_val]\n", argv[0]); return 0; } sscanf(argv[5], "%d", &rw_val); i2c_write_reg(argv[1], &rw_val, slave_address, reg_address, 1); } }
程序的用法为:

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

/ # i2c-rw /dev/i2c-2 0x38 0x1 0 read salve:38 reg:01 value:12 / # / # i2c-rw /dev/i2c-2 0x38 0x2 0 read salve:38 reg:02 value:81

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值