Linux驱动第五篇-----驱动注册和生成设备节点

加载驱动的指令是:insmod xx.ko
查看驱动的指令是: lsmod
卸载驱动的指令是:rmmod xx

在include/linux/platform_device.h这个文件中定义了平台驱动注册和卸载文件函数
platform_driver_register 和 platform_driver_unregister 函数
在这里插入图片描述
这个两个函数参数调用了结构体platform_driver
在这里插入图片描述

该结构中包含了一组操作函数和一个 struct device_driver 的对像。在驱动中首先要做的
就是定义 platform_driver 中的函数,并创建这个结构的一个对象实例, 然后在 init()函数中调用
platform_driver_register()向系统注册驱动。

  1. 函数 int (*probe)(struct platform_device *);
    主要是进行设备的探测和初始化。例如想调用一个 GPIO,那么首先需要探测这个 GPIO 是
    否被占用了,如果被占用了那么初始化失败,驱动注册也就失败了;如果没有被占用,那么就
    申明要占用它。该函数中一般还会添加生成设备节点的函数,如果初始化成功,那么就会需要添加设备节点。
  2. 函数 int (*remove)(struct platform_device *);
    移除驱动,该函数中一般用于去掉设备节点或者释放软硬件资源
  3. void (*shutdown)(struct platform_device *);
    int (*suspend)(struct platform_device *, pm_message_t state);
    int (*resume)(struct platform_device *);
    从字面上就很好理解了,关闭驱动,悬挂(休眠)驱动以及恢复的时候该驱动要做什么

接着的结构体 struct device_driver driver;
主要包含两个参数,一个是 name 参数,驱动名称(需要和设备驱动结构体中的 name 参
数一样,这点很重要);一个是 owner,一般是 THIS_MODULE。

接下来编写驱动代码:

#include <linux/init.h>
#include <linux/module.h>

/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>

#define DRIVER_NAME "hello_ctl"  //这个和前面设备的注册的hello结构体里面的名字相同

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static int hello_probe(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tinitialized\n");
	
	return 0;
}

static int hello_remove(struct platform_device *pdv){
	
	return 0;
}

static void hello_shutdown(struct platform_device *pdv){
	
	;
}

static int hello_suspend(struct platform_device *pdv){
	
	return 0;
}

static int hello_resume(struct platform_device *pdv){
	
	return 0;
}

struct platform_driver hello_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.shutdown = hello_shutdown,
	.suspend = hello_suspend,
	.resume = hello_resume,
	.driver = {
		.name = DRIVER_NAME,      //和devices名称相同
		.owner = THIS_MODULE,
	}
};


static int hello_init(void)
{
	int DriverState;
	
	printk(KERN_EMERG "HELLO WORLD enter!\n");
	DriverState = platform_driver_register(&hello_driver); //然后在模块入口调用platform_driver_register
	
	printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_EMERG "HELLO WORLD exit!\n");
	
	platform_driver_unregister(&hello_driver);	//在函数的出口调用platform_driver_unregister
}

module_init(hello_init);
module_exit(hello_exit);

如果设备和驱动匹配成功就会进入函数 hello_probe 打印“initialized
接着需要编写一下 Makefile 文件

#!/bin/bash
obj-m += probe_linux_module.o    //文件名

#源码目录变量,这里用户需要根据实际情况选择路径

KDIR := /home/birate/topeet/iTop4412_Kernel_3.0  #linux源码目录

#当前目录变量
PWD ?= $(shell pwd)

#make命名默认寻找第一个目标
#make -C就是指调用执行的路径
#$(KDIR)Linux源码目录
#$(PWD)当前目录变量
#modules要执行的操作
all:
	make -C $(KDIR) M=$(PWD) modules
		
#make clean执行的操作是删除后缀为o的文件
clean:
	rm -rf *.o

使用make编译,生成模块文件probe_linux_module.ko
然后使用adb push probe_linux_module.ko /data 放到arm系统的/data目录下

之后加载insmod probe_linux_module.ko
如果没有错误将会打印initialized等内容
在这里插入图片描述
之后查看设备
在这里插入图片描述
卸载设备
在这里插入图片描述

后面我们生成杂项设备节点

杂项设备节点在include/linux/miscdevice.h这个文件中,我们也可以使用cat /proc/misc查看对应的杂项设备

在这里插入图片描述
extern int misc_register(struct miscdevice * misc);
杂项设备注册函数;一般在 probe 中调用,参数是 miscdevice
extern int misc_deregister(struct miscdevice *misc);
杂项设备卸载函数;一般是在 hello_remove 中用于卸载驱动

结构体 miscdevice 中参数很多,下面几个是常用的。
int .minor;设备号,赋值为 MISC_DYNAMIC_MINOR,这个宏定义可以查到为 10
const char *name;设备名称
const struct file_operations *fops

file_operations 结构体在头文件“include/linux/fs.h”中,如下图所示,使用命令“vim
include/linux/fs.h”打开头文件。
查找file_operations
在这里插入图片描述
struct module *owner;一般是 THIS_MODULE。
int (*open) (struct inode *, struct file *);对应上层的 open 函数,打开文件。
int (*release) (struct inode *, struct file *);对应上层的 close 函数,打开文件操作之后一
般需要关闭。
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);读函数,上层应用从底层读取
函数。
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);写函数,上层应用向底
层传输数据。
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);这个函数功能和写
函数稍微有点重合,但是这个函数占用的内存非常小,主要针对 IO 口的控制

#include <linux/init.h>
#include <linux/module.h>

/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>
/*注册杂项设备头文件*/
#include <linux/miscdevice.h>
/*注册设备节点的文件结构体*/
#include <linux/fs.h>

#define DRIVER_NAME "hello_ctl"
#define DEVICE_NAME "hello_ctl123"  //杂项节点名称
MODULE_LICENSE("Dual BSD/GPL");

static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){

	
	printk("cmd is %d,arg is %d\n",cmd,arg);
	return 0;
}

static int hello_release(struct inode *inode, struct file *file){
	printk(KERN_EMERG "hello release\n");
	return 0;
}

static int hello_open(struct inode *inode, struct file *file){
	printk(KERN_EMERG "hello open\n");
	return 0;
}

static struct file_operations hello_ops = {   //定义file_operations 参数
	.owner = THIS_MODULE,
	.open = hello_open,
	.release = hello_release,
	.unlocked_ioctl = hello_ioctl,
};

static  struct miscdevice hello_dev = {
	.minor = MISC_DYNAMIC_MINOR,  //参数 minor 为 MISC_DYNAMIC_MINOR,也就是 10																													
	.name = DEVICE_NAME,					//参数 name 为 DEVICE_NAME,也就是 hello_ctl123	
	.fops = &hello_ops,								//参数 fops 为“hello_ops”
};


static int hello_probe(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tinitialized\n"); 
	misc_register(&hello_dev); 向添加 hello_probe 中添加注册杂项设备的函数 misc_register,如下图所示,
将 miscdevice 参数定义为 hello_dev
	
	return 0;
}

static int hello_remove(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tremove\n");
	misc_deregister(&hello_dev);
	return 0;
}

static void hello_shutdown(struct platform_device *pdv){
	
	;
}

static int hello_suspend(struct platform_device *pdv,pm_message_t pmt){
	
	return 0;
}

static int hello_resume(struct platform_device *pdv){
	
	return 0;
}

struct platform_driver hello_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.shutdown = hello_shutdown,
	.suspend = hello_suspend,
	.resume = hello_resume,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	}
};


static int hello_init(void)
{
	int DriverState;
	
	printk(KERN_EMERG "HELLO WORLD enter!\n");
	DriverState = platform_driver_register(&hello_driver);
	
	printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
	return 0;
}


static void hello_exit(void)
{
	printk(KERN_EMERG "HELLO WORLD exit!\n");
	
	platform_driver_unregister(&hello_driver);	
}

module_init(hello_init);
module_exit(hello_exit);

修改makefile函数

#!/bin/bash
obj-m += devicenode_linux_module.o 
#源码目录变量,这里用户需要根据实际情况选择路径
KDIR := /home/topeet/android4.0/iTop4412_Kernel_3.0

#当前目录变量
PWD ?= $(shell pwd)

#make命名默认寻找第一个目标
#make -C就是指调用执行的路径
#$(PWD)当前目录变量
#modules要执行的操作
all:
	make -C $(KDIR) M=$(PWD) modules
		
#make clean执行的操作是删除后缀为o的文件
clean:
	rm -rf *.o

生成.ko文件
传到 /data目录中
在这里插入图片描述
加载驱动 在/dev查看设备节点
在这里插入图片描述
已经生成了设备节点

卸载 完成

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目 录 第1篇 Linux网络开发基础 第1章 Linux操作系统概述 2 1.1 Linux发展历史 2 1.1.1 Linux的诞生和发展 2 1.1.2 Linux名称的由来 1.2 Linux的发展要素 3 1.2.1 UNIX操作系统 4 1.2.2 Minix操作系统 4 1.2.3 POSIX 标准 4 1.3 Linux与UNIX的异同 5 1.4 操作系统类型选择和内核版本的选择 5 1.4.1 常见的不同公司发行的Linux异同 6 1.4.2 内核版本的选择 6 1.5 Linux的系统架构 7 1.5.1 Linux内核的主要模块 7 1.5.2 Linux的文件结构 9 1.6 GNU通用公共许可证 10 1.6.1 GPL许可证的历史 10 1.6.2 GPL的自由理念 10 1.6.3 GPL的基本条款 11 1.6.4 关于GPL许可证的争议 12 1.7 Linux软件开发的可借鉴之处 12 1.8 小结 13 第2章 Linux编程环境 14 2.1 Linux环境下的编辑器 14 2.1.1 vim使用简介 14 2.1.2 使用vim建立文件 15 2.1.3 使用vim编辑文本 16 2.1.4 vim的格式设置 18 2.1.5 vim配置文件.vimrc 19 2.1.6 使用其他编辑器 19 2.2 Linux下的GCC编译器工具集 19 2.2.1 GCC简介 19 2.2.2 编译程序的基本知识 21 2.2.3 单个文件编译成执行文件 22 2.2.4 编译生成目标文件 22 2.2.5 多文件编译 23 2.2.6 预处理 24 2.2.7 编译成汇编语言 24 2.2.8 生成和使用静态链接库 25 2.2.9 生成动态链接库 26 2.2.10 动态加载库 29 2.2.11 GCC常用选项 31 2.2.12 编译环境的搭建 33 2.3 Makefile文件简介 34 2.3.1 一个多文件的工程例子 34 2.3.2 多文件工程的编译 36 2.3.3 Makefile的规则 37 2.3.4 Makefile中使用变量 39 2.3.5 搜索路径 43 2.3.6 自动推导规则 44 2.3.7 递归make 44 2.3.8 Makefile中的函数 46 2.4 用GDB调试程序 47 2.4.1 编译可调试程序 48 2.4.2 使用GDB调试程序 49 2.4.3 GDB常用命令 52 2.4.4 其他的GDB 59 2.5 小结 60 第3章 文件系统简介 61 3.1 Linux下的文件系统 61 3.1.1 Linux下文件的内涵 61 3.1.2 文件系统的创建 62 3.1.3 挂接文件系统 64 3.1.4 索引节点inode 65 3.1.5 普通文件 66 3.1.6 设备文件 66 3.1.7 虚拟文件系统VFS 68 3.2 文件的通用操作方法 72 3.2.1 文件描述符 72 3.2.2 打开创建文件open()、create()函数 72 3.2.3 关闭文件close()函数 76 3.2.4 读取文件read()函数 77 3.2.5 写文件write()函数 79 3.2.6 文件偏移lseek()函数 80 3.2.7 获得文件状态fstat()函数 83 3.2.8 文件空间映射mmap()函数 85 3.2.9 文件属性fcntl()函数 88 3.2.10 文件输入输出控制ioctl()函数 92 3.3 socket文件类型 93 3.4 小结 93 第4章 程序、进程和线程 94 4.1 程序、进程和线程的概念 94 4.1.1 程序和进程的差别 94 4.1.2 Linux环境下的进程 95 4.1.3 进程和线程 96 4.2 进程产生的方式 96 4.2.1 进程号 96 4.2.2 进程复制fork() 97 4.2.3 system()方式 98 4.2.4 进程执行exec()函数系列 99 4.2.5 所有用户态进程的产生进程init 100 4.3 进程间通信和同步 101 4.3.1 半双工管道 101 4.3.2 命名管道 107 4.3.3 消息队列 108 4.3.4 消息队列的一个例子 114 4.3.5 信号量 116 4.3.6 共享内存 121 4.3.7 信号 124 4.4 Linux下的线程 127 4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号量 133 4.5 小结 136 第2篇 Linux用户层网络编程 第5章 TCP/IP协议族简介 138 5.1 OSI网络分层介绍 138 5.1.1 OSI网络分层结构 138 5.1.2 OSI的7层网络结构 139 5.1.3 OSI参考模型中的数据传输 140 5.2 TCP/IP协议栈 141 5.2.1 TCP/IP协议栈参考模型 141 5.2.2 主机到网络层协议 143 5.2.3 IP协议 144 5.2.4 网际控制报文协议(ICMP) 146 5.2.5 传输控制协议(TCP) 150 5.2.6 用户数据报文协议(UDP) 154 5.2.7 地址解析协议(ARP) 156 5.3 IP地址分类与TCP/UDP端口 158 5.3.1 因特网中IP地址的分类 159 5.3.2 子网掩码(subnet mask address) 161 5.3.3 IP地址的配置 162 5.3.4 端口 163 5.4 主机字节序和网络字节序 163 5.4.1 字节序的含义 164 5.4.2 网络字节序的转换 164 5.5 小结 166 第6章 应用层网络服务程序简介 167 6.1 HTTP协议和服务 167 6.1.1 HTTP协议概述 167 6.1.2 HTTP协议的基本过程 168 6.2 FTP协议和服务 170 6.2.1 FTP协议概述 170 6.2.2 FTP协议的工作模式 172 6.2.3 FTP协议的传输方式 172 6.2.4 一个简单的FTP过程 173 6.2.5 常用的FTP工具 173 6.3 TELNET协议和服务 174 6.3.1 远程登录的基本概念 174 6.3.2 使用TELNET协议进行远程登录的工作过程 174 6.3.3 TELNET协议 174 6.4 NFS协议和服务 176 6.4.1 安装NFS服务器和客户端 176 6.4.2 服务器端的设定 176 6.4.3 客户端的操作 177 6.4.4 showmount命令 177 6.5 自定义网络服务 177 6.5.1 xinetd/inetd 178 6.5.2 xinetd服务配置 178 6.5.3 自定义网络服务 179 6.6 小结 180 第7章 TCP网络编程基础 181 7.1 套接字编程基础知识 181 7.1.1 套接字地址结构 181 7.1.2 用户层和内核层交互过程 183 7.2 TCP网络编程流程 184 7.2.1 TCP网络编程架构 184 7.2.2 创建网络插口函数socket() 186 7.2.3 绑定一个地址端口对bind() 189 7.2.4 监听本地端口listen 192 7.2.5 接受一个网络请求accept() 194 7.2.6 连接目标网络服务器connect() 199 7.2.7 写入数据函数write() 200 7.2.8 读取数据函数read() 201 7.2.9 关闭套接字函数close() 201 7.3 服务器/客户端的简单例子 202 7.3.1 例子功能描述 202 7.3.2 服务器网络程序 203 7.3.3 服务器读取和显示字符串 205 7.3.4 客户端的网络程序 205 7.3.5 客户端读取和显示字符串 206 7.3.6 编译运行程序 206 7.4 截取信号的例子 207 7.4.1 信号处理 207 7.4.2 信号SIGPIPE 208 7.4.3 信号SIGINT 208 7.5 小结 208 第8章 服务器和客户端信息的获取 210 8.1 字节序 210 8.1.1 大端字节序和小端字节序 210 8.1.2 字节序转换函数 212 8.1.3 一个字节序转换的例子 214 8.2 字符串IP地址和二进制IP地址的转换 217 8.2.1 inet_xxx()函数 217 8.2.2 inet_pton()和inet_ntop()函数 219 8.2.3 使用8.2.1节地址转换函数的例子 220 8.2.4 使用函数inet_pton()和函数inet_ntop()的例子 223 8.3 套接字描述符判定函数issockettype() 223 8.3.1 进行文件描述符判定的函数issockettype() 224 8.3.2 main()函数 224 8.4 IP地址与域名之间的相互转换 225 8.4.1 DNS原理 225 8.4.2 获取主机信息的函数 226 8.4.3 使用主机名获取主机信息的例子 228 8.4.4 函数gethostbyname()不可重入的例子 230 8.5 协议名称处理函数 232 8.5.1 xxxprotoxxx()函数 232 8.5.2 使用协议族函数的例子 233 8.6 小结 236 第9章 数据的IO和复用 237 9.1 IO函数 237 9.1.1 使用recv()函数接收数据 237 9.1.2 使用send()函数发送数据 239 9.1.3 使用readv()函数接收数据 240 9.1.4 使用writev()函数发送数据 240 9.1.5 使用recvmsg()函数接收数据 242 9.1.6 使用sendmsg()函数发送数据 244 9.1.7 IO函数的比较 246 9.2 使用IO函数的例子 246 9.2.1 客户端处理框架的例子 246 9.2.2 服务器端程序框架 248 9.2.3 使用recv()和send()函数 249 9.2.4 使用readv()和write()函数 251 9.2.5 使用recvmsg()和sendmsg()函数 253 9.3 IO模型 256 9.3.1 阻塞IO模型 256 9.3.2 非阻塞IO模型 257 9.3.3 IO复用 257 9.3.4 信号驱动IO模型 258 9.3.5 异步IO模型 258 9.4 select()函数和pselect()函数 259 9.4.1 select()函数 259 9.4.2 pselect()函数 261 9.5 poll()函数和ppoll()函数 262 9.5.1 poll()函数 263 9.5.2 ppoll()函数 264 9.6 非阻塞编程 264 9.6.1 非阻塞方式程序设计介绍 264 9.6.2 非阻塞程序设计的例子 264 9.7 小结 266 第10章 基于UDP协议的接收和发送 267 10.1 UDP编程框架 267 10.1.1 UDP编程框图 267
pcf8563_i2c1_r8_ruoge_ov2640通过给RTC驱动增加设备节点读取秒钟成功+直接读取I2C1获取秒钟值20160626_2201.7z http://blog.csdn.net/21cnbao/article/details/7919055 在Android源码树中添加userspace I2C读写工具(i2c-util) 本文使用的开发板是:杭州若格科技有限公司的全志R8。CPU:CPUARM Cortex-A8 更多芯片资料请参见全志官网: http://www.allwinnertech.com/clq/r/R8.html 通过/dev/i2c-n节点,用户可以在userspace直接访问板上的i2c外设寄存器,主要是透过I2C_RDWR这个IO控制命令将i2c_msg数组传递给kernel去执行。 开发板的/dev/i2c-1总线下挂有一片I2C的RTC:pcf8563。 root@android:/dev # cd /sys/class/i2c-adapter/ root@android:/sys/class/i2c-adapter # ll lrwxrwxrwx root root 1970-01-02 08:31 i2c-0 -> ../../devices/platform/sun5i-i2c.0/i2c-0 lrwxrwxrwx root root 1970-01-02 08:31 i2c-1 -> ../../devices/platform/sun5i-i2c.1/i2c-1 lrwxrwxrwx root root 1970-01-02 08:31 i2c-2 -> ../../devices/platform/sun5i-i2c.2/i2c-2 root@android:/sys/class/i2c-adapter # cd i2c-1 root@android:/sys/class/i2c-adapter/i2c-1 # ll drwxr-xr-x root root 1970-01-02 08:31 1-0051 --w------- root root 4096 1970-01-02 08:31 delete_device lrwxrwxrwx root root 1970-01-02 08:31 device -> ../../sun5i-i2c.1 drwxr-xr-x root root 1970-01-01 08:00 i2c-dev -r--r--r-- root root 4096 1970-01-02 08:31 name --w------- root root 4096 1970-01-02 08:31 new_device drwxr-xr-x root root 1970-01-01 08:00 power lrwxrwxrwx root root 1970-01-02 08:31 subsystem -> ../../../../bus/i2c -rw-r--r-- root root 4096 1970-01-01 08:00 uevent root@android:/sys/class/i2c-adapter/i2c-1 # root@android:/sys/class/i2c-adapter/i2c-1 # cd 1-0051/ root@android:/sys/class/i2c-adapter/i2c-1/1-0051 # ll lrwxrwxrwx root root 1970-01-02 10:18 driver -> ../../../../../bus/i2c/drivers/pcf8563 -r--r--r-- root root 4096 1970-01-02 10:18 modalias -r--r--r-- root root 4096 1970-01-02 10:18 name drwxr-xr-x root root 1970-01-02 10:18 power drwxr-xr-x root root 1970-01-02 10:18 rtc lrwxrwxrwx root root 1970-01-02 10:18 subsystem -> ../../../../../bus/i2c -rw-r--r-- root root 4096 1970-01-02 10:18 uevent root@android:/sys/class/i2c-adapter/i2c-1/1-0051 # cat name pcf8563 root@android:/sys/class/i2c-adapter/i2c-1/1-0051 # 注释:1-0051 1 表示 i2c-1这条I2C1总线上挂载的设备,如果是I2C2总线上挂载的设备,路径就是2-00XX了。 0051 一般的I2C设备的从机地址都是一个字节的,因为前两位为0x00(16进制的),后两位为pcf8563移位自后的I2C从机地址0x51(也是16进制的) 压缩包中的PCF8563-CN.pdf,datasheet告诉我们:I2C总线从地址:读,0A3H;写,0A2H。右移一位之后正好是0x51。 下面的代码可以完成这个功能: #include #include #include #include #include #include #include #include #include #include #include /* This is the structure as used in the I2C_RDWR ioctl call */ struct i2c_rdwr_ioctl_data { struct i2c_msg __user *msgs; /* pointers to i2c_msgs */ __u32 nmsgs; /* number of i2c_msgs */ }; int i2c_read_reg(char *dev, unsigned 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:x reg:x\n", slave_address, reg_address); close(fd); free(work_queue.msgs); return len; } } int i2c_write_reg(char *dev, unsigned 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:x reg:x\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; unsigned w_val; unsigned 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", &reg_address); sscanf(argv[4], "%d", &r_w); if (r_w == 0) { i2c_read_reg(argv[1], &rw_val, slave_address, reg_address, 1); printf("Read %s-%x reg %x, read value:%x\n", argv[1], slave_address, reg_address, rw_val); } 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", &w_val); if ((w_val & ~0xff) != 0) printf("Error on written value %s\n", argv[5]); rw_val = (unsigned char)w_val; i2c_write_reg(argv[1], &rw_val, slave_address, reg_address, 1); } return 0; } 在android/external/新建i2c-util目录,上述源代码存入android/external/i2c-util/i2c-util.c, R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\i2c-util\i2c-util.c 编写对应的Android.mk: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_MODULE = i2c-util # LOCAL_SRC_FILES := $(call all-subdir-c-files) LOCAL_SRC_FILES := i2c-util.c include $(BUILD_EXECUTABLE) 编译Android后,上述工具会位于/system/bin目录。在电路板上使用它: R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin\i2c-util 如果android已经编译了,只需要执行: rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ source build/envsetup.sh rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ lunch 选择:18. nuclear_evb-eng (注意:不同的ubuntu电脑,序号可能不同,但是只需要选择nuclear_evb-eng编译选项前面的序号既可!!!!) rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ cd i2c-util/ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android/i2c-util$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android/i2c-util$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android/i2c-util$ mm 详细的编译步骤: Connecting to 192.168.1.103:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64) * Documentation: https://help.ubuntu.com/ Last login: Sun Jun 26 19:23:30 2016 from 192.168.1.101 rootroot@rootroot-E400:~$ cd wyb/pcf8563_i2c1_r8_ruoge_ov2640/ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640$ ll total 8606460 drwxr-xr-x 4 rootroot rootroot 4096 Jun 26 20:46 ./ drwxr-xr-x 19 rootroot rootroot 4096 Jun 25 06:50 ../ drwxrwxr-x 29 rootroot rootroot 4096 Jun 26 19:51 android/ drwxrwxr-x 8 rootroot rootroot 4096 Jun 26 16:35 lichee/ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640$ cd android/ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ source build/envsetup.sh including device/asus/grouper/vendorsetup.sh including device/asus/tilapia/vendorsetup.sh including device/generic/armv7-a-neon/vendorsetup.sh including device/generic/armv7-a/vendorsetup.sh including device/generic/mips/vendorsetup.sh including device/generic/x86/vendorsetup.sh including device/samsung/maguro/vendorsetup.sh including device/samsung/manta/vendorsetup.sh including device/samsung/toroplus/vendorsetup.sh including device/samsung/toro/vendorsetup.sh including device/softwinner/common/vendorsetup.sh including device/softwinner/crane-evb/vendorsetup.sh including device/softwinner/nuclear-256m/vendorsetup.sh including device/softwinner/nuclear-evb/vendorsetup.sh including device/softwinner/nuclear-r8m-evb/vendorsetup.sh including device/ti/panda/vendorsetup.sh including sdk/bash_completion/adb.bash rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ lunch You're building on Linux Lunch menu... pick a combo: 1. full-eng 2. full_x86-eng 3. vbox_x86-eng 4. full_mips-eng 5. full_grouper-userdebug 6. full_tilapia-userdebug 7. mini_armv7a_neon-userdebug 8. mini_armv7a-userdebug 9. mini_mips-userdebug 10. mini_x86-userdebug 11. full_maguro-userdebug 12. full_manta-userdebug 13. full_toroplus-userdebug 14. full_toro-userdebug 15. crane_evb-eng 16. nuclear_256m-user 17. nuclear_256m-eng 18. nuclear_evb-eng 19. nuclear_r8m_evb-eng 20. full_panda-userdebug Which would you like? [full-eng] 18 ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=4.2.2 TARGET_PRODUCT=nuclear_evb TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a-neon HOST_ARCH=x86 HOST_OS=linux HOST_OS_EXTRA=Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty HOST_BUILD_TYPE=release BUILD_ID=JDQ39 OUT_DIR=out ============================================ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ ll total 156 drwxrwxr-x 29 rootroot rootroot 4096 Jun 26 19:51 ./ drwxr-xr-x 4 rootroot rootroot 4096 Jun 26 20:46 ../ drwxrwxr-x 3 rootroot rootroot 4096 Jun 13 11:38 abi/ drwxrwxr-x 10 rootroot rootroot 4096 Jun 13 11:38 bionic/ drwxrwxr-x 5 rootroot rootroot 4096 Jun 13 11:38 bootable/ drwxrwxr-x 7 rootroot rootroot 4096 Jun 13 11:38 build/ drwxrwxr-x 11 rootroot rootroot 4096 Jun 13 11:38 cts/ drwxrwxr-x 18 rootroot rootroot 4096 Jun 13 11:38 dalvik/ drwxrwxr-x 18 rootroot rootroot 4096 Jun 13 11:38 development/ drwxrwxr-x 10 rootroot rootroot 4096 Jun 13 11:38 device/ drwxrwxr-x 3 rootroot rootroot 4096 Jun 13 11:38 docs/ drwxrwxr-x 159 rootroot rootroot 4096 Jun 13 11:39 external/ drwxrwxr-x 14 rootroot rootroot 4096 Jun 13 11:40 frameworks/ drwxrwxr-x 10 rootroot rootroot 4096 Jun 13 11:40 gdk/ drwxrwxr-x 10 rootroot rootroot 4096 Jun 13 11:40 hardware/ drwxr-xr-x 2 rootroot rootroot 4096 Jun 26 19:37 i2cscan/ drwxr-xr-x 2 rootroot rootroot 4096 Jun 26 19:52 i2c-util/ drwxrwxr-x 11 rootroot rootroot 4096 Jun 13 11:40 libcore/ drwxrwxr-x 4 rootroot rootroot 4096 Jun 13 11:40 libnativehelper/ -r--r--r-- 1 rootroot rootroot 87 Jun 13 11:38 Makefile drwxrwxr-x 8 rootroot rootroot 4096 Jun 13 11:40 ndk/ drwxrwxr-x 4 rootroot rootroot 4096 Jun 26 16:43 out/ drwxrwxr-x 8 rootroot rootroot 4096 Jun 13 11:40 packages/ drwxrwxr-x 5 rootroot rootroot 4096 Jun 13 11:40 pdk/ drwxrwxr-x 10 rootroot rootroot 4096 Jun 13 11:41 prebuilts/ drwxr-xr-x 2 rootroot rootroot 4096 Jun 25 13:01 read_pcf8563/ drwxrwxr-x 6 rootroot rootroot 4096 Jun 13 11:38 .repo/ drwxrwxr-x 51 rootroot rootroot 4096 Jun 13 11:41 sdk/ drwxrwxr-x 9 rootroot rootroot 4096 Jun 13 11:41 system/ drwxrwxr-x 4 rootroot rootroot 4096 Jun 13 11:41 tools/ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android$ cd i2c-util/ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android/i2c-util$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android/i2c-util$ rootroot@rootroot-E400:~/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android/i2c-util$ mm ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=4.2.2 TARGET_PRODUCT=nuclear_evb TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a-neon HOST_ARCH=x86 HOST_OS=linux HOST_OS_EXTRA=Linux-3.13.0-24-generic-x86_64-with-Ubuntu-14.04-trusty HOST_BUILD_TYPE=release BUILD_ID=JDQ39 OUT_DIR=out ============================================ PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/Effect_Tick.ogg:system/media/audio/ui/Effect_Tick.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressStandard.ogg:system/media/audio/ui/KeypressStandard.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressSpacebar.ogg:system/media/audio/ui/KeypressSpacebar.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressDelete.ogg:system/media/audio/ui/KeypressDelete.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressReturn.ogg:system/media/audio/ui/KeypressReturn.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/VideoRecord.ogg:system/media/audio/ui/VideoRecord.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/camera_click.ogg:system/media/audio/ui/camera_click.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/LowBattery.ogg:system/media/audio/ui/LowBattery.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/Dock.ogg:system/media/audio/ui/Dock.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/Undock.ogg:system/media/audio/ui/Undock.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/Lock.ogg:system/media/audio/ui/Lock.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/Unlock.ogg:system/media/audio/ui/Unlock.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Sceptrum.ogg:system/media/audio/ringtones/Sceptrum.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressStandard_120.ogg:system/media/audio/ui/KeypressStandard.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressSpacebar_120.ogg:system/media/audio/ui/KeypressSpacebar.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressDelete_120.ogg:system/media/audio/ui/KeypressDelete.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressReturn_120.ogg:system/media/audio/ui/KeypressReturn.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/notifications/ogg/Capella.ogg:system/media/audio/notifications/Capella.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/notifications/ogg/CetiAlpha.ogg:system/media/audio/notifications/CetiAlpha.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/notifications/ogg/Polaris.ogg:system/media/audio/notifications/Polaris.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/notifications/ogg/Pollux.ogg:system/media/audio/notifications/Pollux.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/notifications/ogg/Procyon.ogg:system/media/audio/notifications/Procyon.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Aquila.ogg:system/media/audio/ringtones/Aquila.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/ArgoNavis.ogg:system/media/audio/ringtones/ArgoNavis.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Carina.ogg:system/media/audio/ringtones/Carina.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Centaurus.ogg:system/media/audio/ringtones/Centaurus.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Cygnus.ogg:system/media/audio/ringtones/Cygnus.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Draco.ogg:system/media/audio/ringtones/Draco.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Machina.ogg:system/media/audio/ringtones/Machina.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Orion.ogg:system/media/audio/ringtones/Orion.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Pegasus.ogg:system/media/audio/ringtones/Pegasus.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Pyxis.ogg:system/media/audio/ringtones/Pyxis.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Rigel.ogg:system/media/audio/ringtones/Rigel.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Scarabaeus.ogg:system/media/audio/ringtones/Scarabaeus.ogg ignored. PRODUCT_COPY_FILES frameworks/base/data/sounds/ringtones/ogg/Solarium.ogg:system/media/audio/ringtones/Solarium.ogg ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/es-ES_zl0_sg.bin:system/tts/lang_pico/es-ES_zl0_sg.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/es-ES_ta.bin:system/tts/lang_pico/es-ES_ta.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/fr-FR_nk0_sg.bin:system/tts/lang_pico/fr-FR_nk0_sg.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/fr-FR_ta.bin:system/tts/lang_pico/fr-FR_ta.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/de-DE_gl0_sg.bin:system/tts/lang_pico/de-DE_gl0_sg.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/it-IT_cm0_sg.bin:system/tts/lang_pico/it-IT_cm0_sg.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/it-IT_ta.bin:system/tts/lang_pico/it-IT_ta.bin ignored. PRODUCT_COPY_FILES device/softwinner/nuclear-common/googleservice/gapps-jb-20121130-signed/system/tts/lang_pico/de-DE_ta.bin:system/tts/lang_pico/de-DE_ta.bin ignored. No private recovery resources for TARGET_DEVICE nuclear-evb make: Entering directory `/home/rootroot/wyb/pcf8563_i2c1_r8_ruoge_ov2640/android' target thumb C: i2c-util cd R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin C:\Users\Administrator.USER-20150913SZ>r: R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin>adb remount remount succeeded R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin> R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin>dir i2c* 驱动器 R 中的卷是 rootroot 卷的序列号是 1A1C-E71D R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin 的目录 2016/06/26 21:10 5,388 i2c-util 1 个文件 5,388 字节 0 个目录 268,337,782,784 可用字节 R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin> R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin>adb push i2c-util /data/ 29 KB/s (5388 bytes in 0.180s) R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\android\out\target\product\nuclear-evb\system\bin> 将R8的串口连接到windows电脑上。通过串口工具:比如Xshell5(有些人可能喜欢使用SecureCRT)读取R8的串口打印 (ubuntu下请使用minicom,使用方法请自行搜索了) 波特率选择:115200 N 8 1 等R8的android启动完成之后(LCD进android主界面),输入:su [ 37.990060] init: process 'ril-daemon', pid 950 exited [ 38.010049] init: process 'ril-daemon' killing any children in process group su root@android:/ # root@android:/ # root@android:/ # cd /data root@android:/data # root@android:/data # ll drwxrwxr-x system system 1970-01-11 08:58 anr drwxrwx--x system system 1970-01-02 08:01 app drwx------ root root 1980-10-01 11:03 app-asec drwxrwx--x system system 1970-01-02 08:01 app-lib drwxrwx--x system system 1980-10-01 11:03 app-private drwx------ system system 1980-10-01 11:04 backup drwxrwx--x system system 1970-01-02 08:00 dalvik-cache drwxrwx--x system system 2016-06-21 10:11 data drwxr-x--- root log 1980-10-01 11:03 dontpanic drwxrwx--- drm drm 1980-10-01 11:04 drm -rw-rw-rw- root root 5388 2016-06-26 21:10 i2c-util drwxr-x--x root root 1980-10-01 11:03 local drwxrwx--- root root 1970-01-01 08:00 lost+found drwxrwx--- media_rw media_rw 1980-10-01 11:03 media drwxrwx--t system misc 1980-10-01 11:03 misc -rw------- system system 154 1970-01-02 08:02 pointercal drwx------ root root 1970-01-02 08:00 property -rwxrwxrwx root root 5392 2016-06-25 13:01 read_pcf8563 drwxrwx--x system system 1980-10-01 11:03 resource-cache drwxr-x--- root shell 1980-10-01 11:03 ssh drwxrwxr-x system system 1970-01-02 08:00 system drwx------ system system 1970-01-02 08:03 tombstones drwx--x--x system system 1980-10-01 11:03 user root@android:/data # (让i2c-util具有可执行权限:) root@android:/data # chmod 777 i2c-util root@android:/data # root@android:/data # ll i2c* -rwxrwxrwx root root 5388 2016-06-26 21:10 i2c-util root@android:/data # root@android:/data # root@android:/data # (可选执行) root@android:/data # sync root@android:/data # 注意:串口打印会打印很多log信息。上面的步骤中的状态信息已经被过滤了。 如果你的串口打印过量的log信息,属于正常现象! 如果不想要这么多的状态信息,可以考虑使用adb shell。 不过windows命令行中的adb shell不能够按TAB键自动补充,ubuntu的可以。 也许我们可以把windows的命令行特别设计一下(给它修正一下),让它也可以通过按TAB键来自动补全!^_ 读取pcf8563的第2个寄存器(秒钟值): 表 5:BCD 格式寄存器概况 标明“-”的位无效 地址 寄存器名称 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 02h 秒 VL 00~59BCD 码格式数 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:11 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:12 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:12 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:13 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:14 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:14 root@android:/data # ./i2c-util /dev/i2c-1 0x51 0x02 0 read salve:51 reg:02 Read /dev/i2c-1-51 reg 2, read value:15 可以知道秒钟的值是变化的,基本上是一秒钟递增一次,I2C读取成功。 如果感兴趣的话,在执行读取命令的时候通过协议分析仪或者示波器来抓取I2C1的SCL/SDA的波形,来进行更加详尽的分析!^_ 方法二: 给出了一种复杂的解决办法(步骤从简,更多请参考开头的方法): R:\wyb\pcf8563_i2c1_r8_ruoge_ov2640\lichee\linux-3.4\drivers\rtc\rtc-sun5i.c 给这个驱动文件增加设备节点:/dev/a20_r8_pcf8563 增加设备节点的方法请参考: http://blog.csdn.net/mirkerson/article/details/8844997 android驱动学习---led实验 然后在这个驱动文件的pcf8563_probe函数处截获它的client指针(struct i2c_client *client) client2 = client; 用户可以通过在userspace直接访问设备节点:/dev/a20_r8_pcf8563(open) 然后调用ioctl:ioctl(fd, 0x00000001, 0x02); 来获取第2个寄存器:秒钟的值了。 #include #include #include #include #include #include #include #include #include #include #include int fd; int main(int argc, char **argv) { fd = open("/dev/a20_r8_pcf8563", O_RDWR); ioctl(fd, 0x00000001, 0x02); close(fd); return 0; } 执行过程(直接从内核打印秒钟值了,也可以看到秒钟值大概也是一秒钟递增一次!): root@android:/data # root@android:/data # ./read_pcf8563 [ 1397.060015] ****wyb drivers/rtc/rtc-sun5i.c:1169/r8_pcf8563_open()! open init.... [ 1397.071009] ****wyb drivers/rtc/rtc-sun5i.c:1130/r8_pcf8563_ioctl()! cmd=0x00000001 [ 1397.079076] ****wyb drivers/rtc/rtc-sun5i.c:1160/r8_pcf8563_ioctl()! cmd=0x00000001, value=0x00000046 [ 1397.088342] ****wyb drivers/rtc/rtc-sun5i.c:1177/r8_pcf8563_close()! close init root@android:/data # root@android:/data # ./read_pcf8563 [ 1398.409888] ****wyb drivers/rtc/rtc-sun5i.c:1169/r8_pcf8563_open()! open init.... [ 1398.411203] ****wyb drivers/rtc/rtc-sun5i.c:1130/r8_pcf8563_ioctl()! cmd=0x00000001 [ 1398.419273] ****wyb drivers/rtc/rtc-sun5i.c:1160/r8_pcf8563_ioctl()! cmd=0x00000001, value=0x00000047 [ 1398.428546] ****wyb drivers/rtc/rtc-sun5i.c:1177/r8_pcf8563_close()! close init root@android:/data # root@android:/data # root@android:/data # ./read_pcf8563 [ 1399.668173] ****wyb drivers/rtc/rtc-sun5i.c:1169/r8_pcf8563_open()! open init.... [ 1399.670939] ****wyb drivers/rtc/rtc-sun5i.c:1130/r8_pcf8563_ioctl()! cmd=0x00000001 [ 1399.679022] ****wyb drivers/rtc/rtc-sun5i.c:1160/r8_pcf8563_ioctl()! cmd=0x00000001, value=0x00000048 [ 1399.688299] ****wyb drivers/rtc/rtc-sun5i.c:1177/r8_pcf8563_close()! close init root@android:/data #

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值