编译上述程序有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
运行
- # fb_test
- The framebuffer device was opened successfully.
- 800x480, 32bpp
- The framebuffer device was mapped to memory successfully.
如果我们想恢复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;
- }
- # 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(写入寄存器的值)
- / # 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
