I2c协议介绍

Key to symbols

==============

S(1 bit) : Start bit开始位

P(1 bit) : Stop bit结束位

Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.

A, NA (1 bit) : Accept and reverse accept bit.应答位和不应答位

Addr(7 bits): I2C 7 bit address. Note that this can be expanded as usual to

get a 10 bit I2C address.

Comm(8 bits): Command byte, a data byte which often selects a register on

the device.

Data(8 bits): A plain data byte. Sometimes, I write DataLow, DataHigh

for 16 bit data.

Count (8 bits): A data byte containing the length of a block operation.

[..]: Data sent by I2C device, as opposed to data sent by the host adapter.

Simple send transaction简单发送传输

======================

This corresponds to i2c_master_send.

S Addr Wr [A] Data [A] Data [A] ... [A] Data [A] P

Simple receive transaction简单接收传输

===========================

This corresponds to i2c_master_recv

S Addr Rd[A] [Data] A [Data] A ... A [Data] NA P

Combined transactions复合传输

====================

This corresponds to i2c_transfer.

They are just like the above transactions, but instead of a stop bit P,a start bit S is sent and the transaction continues. An example of a byte read, followed by a byte write:

S Addr Rd [A] [Data] NA S Addr Wr [A] Data [A] P

Modified transactions

=====================

We have found some I2C devices that needs the following modifications:

FlagI2C_M_NOSTART:

In a combined transaction, no 'S Addr Wr/Rd [A]' is generated at somepoint. For example, setting I2C_M_NOSTART on the second partial message generates something like:

S Addr Rd[A] [Data] NA Data [A] P

If you set the I2C_M_NOSTART variable for the first partial message, we do not generate Addr, but we do generate the startbitS. This will probably confuse all other clients on your bus, so don't try this.

Flags I2C_M_REV_DIR_ADDR

This toggles theRd/Wrflag. That is, if you want to do a write, but Normally message is interrupted immediately if there is[NA]from the client. Setting this flag treats any[NA]as[A], and all of message is sent.

These messages may still fail to SCL lo->hi timeout.

Flags I2C_M_NO_RD_ACK

In a read message, master A/NA bit is skipped.

Terminology术语

===========

When we talk about I2C, we use the following terms:

Bus-> Algorithm

Adapter

Device -> Driver

Client

An Algorithm driver contains general code that can be used for a whole class of I2C adapters. Each specific adapter driver either depends on one algorithm driver, or includes its own implementation.

A Driver driver (yes, this sounds ridiculous, sorry) contains the general code to access some type of device. Each detected device gets its own data in the Client structure. Usually, Driver and Client are more closely integrated than Algorithm and Adapter.

For a given configuration, you will need a driver for your I2C bus, and drivers for your I2C devices (usually one driver for each device).

At this time, Linux only operates I2C (or SMBus) in master mode; you can't use these APIs to make a Linux system behave as a slave/device, either to speak a custom protocol or to emulate some other device.

I2c-dev.c文件完全可以被看作一个i2c设备驱动,它可以被用于用户空间访问adapter.它实现的一个i2c_client是虚拟、临时的,随着设备文件的打开而产生,并随设备文件的关闭而撤销。

I2c-dev.c提供以下几个函数,如下

static const struct file_operations i2cdev_fops = {

.owner= THIS_MODULE,

.llseek= no_llseek,

.read= i2cdev_read,

.write= i2cdev_write,

.unlocked_ioctl= i2cdev_ioctl,

.open= i2cdev_open,

.release= i2cdev_release,

};

实际使用时, i2cdev_read和i2cdev_write使用比较少,不建议使用,因为他们不具有太强的通用性。使用的时候一般就使用i2cdev_ioctl,当然i2cdev_open与i2cdev_release也是需要使用的。

要使用i2c-dev,首先必须把I2C support里面的I2C device interface选上。

下面是一个例子,参考的华清远见的刘老师的,如下

/*i2c_test.c

* hongtao_liu <>

*/

#include #include #include #include #include #include #include #include #define I2C_RETRIES 0x0701

#define I2C_TIMEOUT 0x0702

#define I2C_SLAVE   0x0703

#define I2C_RDWR    0x0707

/*********定义struct i2c_rdwr_ioctl_data和struct i2c_msg,要和内核一致*******/

struct i2c_msg

{

unsigned short addr;

unsigned short flags;

#define I2C_M_TEN 0x0010

#define I2C_M_RD 0x0001

unsigned short len;

unsigned char *buf;

};

struct i2c_rdwr_ioctl_data

{

struct i2c_msg *msgs;

int nmsgs;

/* nmsgs这个数量决定了有多少开始信号,对于“单开始时序”,取1*/

};

#define SLAVE_ADD  0x50

/***********主程序***********/

int main()

{

int fd,ret;

struct i2c_rdwr_ioctl_data e2prom_data;

fd=open("/dev/i2c0",O_RDWR);

/*

dev/i2c-0是在注册i2c-dev.c后产生的,代表一个可操作的适配器。如果不使用i2c-dev.c

的方式,就没有,也不需要这个节点。

*/

if(fd<0)

{

perror("open error");

}

e2prom_data.nmsgs=2;

/*因为操作时序中,最多是用到2个开始信号(字节读操作中),所以此将e2prom_data.nmsgs配置为2 */

e2prom_data.msgs=(struct i2c_msg*)malloc(e2prom_data.nmsgs*sizeof(struct i2c_msg));

if(!e2prom_data.msgs)

{

perror("malloc error");

exit(1);

}

ioctl(fd,I2C_TIMEOUT,1);/*超时时间*/

ioctl(fd,I2C_RETRIES,2);/*重复次数*/

/***write data to e2prom**/

e2prom_data.nmsgs=1;

(e2prom_data.msgs[0]).len=3; //1个 e2prom 写入目标的地址和1个数据

(e2prom_data.msgs[0]).addr=SLAVE_ADD;//e2prom 设备地址

(e2prom_data.msgs[0]).flags=0; //write

(e2prom_data.msgs[0]).buf=(unsigned char*)malloc(3);

(e2prom_data.msgs[0]).buf[0]=0x10;// e2prom 写入目标的地址

(e2prom_data.msgs[0]).buf[1]=0x55;//the data to write

(e2prom_data.msgs[0]).buf[2]=0xaa;//the data to write

ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);

if(ret<0)

{

perror("ioctl error1");

}

sleep(1);

/******read data from e2prom*******/

e2prom_data.nmsgs=2;

(e2prom_data.msgs[0]).len=1; //e2prom 目标数据的地址

(e2prom_data.msgs[0]).addr=SLAVE_ADD; // e2prom 设备地址

(e2prom_data.msgs[0]).flags=0;//write

(e2prom_data.msgs[0]).buf[0]=0x10;//e2prom数据地址

(e2prom_data.msgs[1]).len=2;//读出的数据

(e2prom_data.msgs[1]).addr=SLAVE_ADD;// e2prom 设备地址

(e2prom_data.msgs[1]).flags=I2C_M_RD;//read

(e2prom_data.msgs[1]).buf=(unsigned char*)malloc(2);//存放返回值的地址。

(e2prom_data.msgs[1]).buf[0]=0;//初始化读缓冲

(e2prom_data.msgs[1]).buf[1]=0;//初始化读缓冲

ret=ioctl(fd,I2C_RDWR,(unsigned long)&e2prom_data);

if(ret<0)

{

perror("ioctl error2");

}

printf("buff[0]=0x%x,buff[1]=0x%x\n",(e2prom_data.msgs[1]).buf[0],(e2prom_data.msgs[1]).buf[1]);

close(fd);

return 0;

}

linux怎么查看i2c设备,Linux 下I2c设备分析相关推荐

  1. Linux命令查看子进程命令,Linux基础命令——查看进程命令

    linux是一个 多进程   多用户的操作系统 ps(显示当前进程的状态) ps -ef  查看当前linux 进程 ps -ef | grep 'mysqld'  过滤mysql的进程 (grep  ...

  2. Linux端口查看及常见Linux端口说明

    Linux端口查看及常见Linux端口说明 现在各行业对服务器安全管理特别严格,不管是内网服务还是公网对外开放端口,都要经过严格的安全扫描. 常见linux服务器端口启用查询命令: 该命令可列出系统正 ...

  3. 设备树下字符设备驱动

    设备树下字符设备驱动 一.在设备树里添加自己的节点 二.驱动代码 三.makefile 四.应用层代码 运行测试 总结 一.在设备树里添加自己的节点 alphaled { 2 #address-cel ...

  4. linux mount 查看挂载目录,Linux下使用mount来挂载设备到目录

    一般情况下直接mount 设备路径 目录路径,就可以了.umount 设备名,就可以卸载这个设备了 使用lsblk -f可以查看挂载的设备,以及这些设备的文件系统. root@tao-PC:/boot ...

  5. linux系统查看usb口,Linux运维知识之Linux系统下查看USB设备名及使用USB设备

    本文主要向大家介绍了Linux运维知识之Linux系统下查看USB设备名及使用USB设备,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. 1.系统插入USB设备后,从控制台界面有 ...

  6. linux命令查看cpu序列号,Linux下用命令查看CPU ID以及厂家等信息

    Linux下用命令查看CPU ID // 获得CPU ID dmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}' // 获得磁盘ID f ...

  7. linux服务器查看wwn号,linux 下查看wwn号

    PC server主机与FC存储进行连接时,一般需要加装HBA卡,两者之间衔接的一个重要参数就是wwn号.redhat或suse下查看wwn号的方法如下. 一.SuSE Linux 9 查看 /pro ...

  8. linux如何查看zst文件,Linux下如何查看共享文件夹

    一般情况,我们用到smbclient,常用方法所如下: #smbclient -L //IP地址或计算机名 smbclient是samba的Linux客户端,在Linux机器上用来查看服务器上的共享资 ...

  9. linux命令查看几位,Linux每周几个命令(一)--查找篇

    Linux每周几个命令--查找篇 标签(空格分隔): linux 当要查找某一个文件位置的时候,在linux下可以使用如下命令: which 查看可执行文件位置 whereis 查看文件位置 loca ...

  10. linux ps查看完整时间,Linux ps 命令查看进程启动及运行时间

    引言 同事问我怎样看一个进程的启动时间和运行时间,我第一反应当然是说用 ps 命令啦. ps aux或ps -ef不就可以看时间吗? ps aux选项及输出说明 我们来重新复习下ps aux的选项,这 ...

最新文章

  1. IBM也要开源机器学习平台
  2. pip virtualenv requirements
  3. Arduino--DHT11温湿度传感器
  4. MFC 基础知识:主对话框与子对话框(二)
  5. 初识ABP vNext(12):模块的独立运行与托管
  6. sqlserver备份还原后数据库关系图无法显示问题
  7. oracle两个数据库之间,如何实现oracle两个数据库之间的同步
  8. Python使用wordnet工具计算词集与词条基本用法(三)
  9. 更名OpenShift容器平台,红帽实现战略性转变
  10. linux redis 安装部署,Linux Redis安装部署
  11. polymorphic-associations 多态关联实例 ruby on rails
  12. c语言程序设计教程 郭浩志,C语言程序设计教程答案杨路明郭浩志.doc
  13. php 微信商城 多级分销,PHP多级分销全解密去后门修复版商城源码+分红系统+微信多级分销...
  14. 实对称矩阵的特征值求法_实对称矩阵、相似、标准型、合同的逻辑网
  15. 【2022 李宏毅】机器学习导论
  16. 百旺如何看是否清卡_百旺开票系统每月清卡怎么操作?
  17. threejs 透视相机参数解析
  18. html5标签思维导图,HTML/HTML5 知识点思维导图
  19. 【简单详细】为Unity游戏制作开场动画video,新手必看
  20. CentOS 5设置千兆网卡

热门文章

  1. ubuntu安装和卸载python3.8
  2. [详解]Linux炫技用命令
  3. 原来是这么做的,新手小白做自媒体,1个视频可以获取10份收益
  4. java tomcat环境变量配置_JDK 和 tomcat 环境变量配置
  5. cad vba 部分中文方法
  6. Apple Watch初体验
  7. QT 多线程创建方法及应用实例
  8. 注册热键 RegisterHotKey
  9. alooa是华为什么型号_alooa是华为什么型号_pot alooa是华为什么型号 pot alooa是华为麦芒8(图文)...
  10. 字体样式,文本样式,背景样式,边框样式,线性渐变,伪类选择器,css权重