📄 the i2c layer ex.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0063)http://www.geocities.com/marco_corvi/games/lkpe/i2c/example.htm -->
<HTML><HEAD><TITLE>The I2C layer</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"><LINK
href="The I2C layer ex_file/style.css" rel=stylesheet>
<META content="MSHTML 6.00.2800.1170" name=GENERATOR></HEAD>
<BODY>
<H2>The I2C layer - Example</H2>
<DIV>References:<BR></DIV><BR clear=all><BR clear=all><BR clear=all>
<DIV>Some of the theory behind I2C has been discussed in the introduction. Now
we come to a working example, ... something you can do even if you do not have
any special i2c device to play with. Of course the first thing to do is to read
the kernel code for the I2C core and supported devices. </DIV>
<DIV>To be independent from any hardware i decided to write an I2C adapter for a
virtual bus that exiest only in memory, and a driver for virtual devices that
sit on that bus. I use the I2C core functionalities and structures to manage
adapter and devices and the data transfer. The figure below shows the main
relations between the I2C core and the driver/adapter callback functions (shown
in red). </DIV><BR clear=all><IMG height=600
src="The I2C layer ex_file/callback.gif" width=560> <BR clear=all>
<H3>The i2c_adapter i2c_mem_adpt.c</H3>
<DIV>The adapter i2c_mem_adapter does not have to do anything special; the list
of clients[] is already managed by the i2c_core, it must specify that it is
using the algorithm i2c_mem_algorithm (about which i will say shortly), and
declare an id that is not used by other adapters (so check include/linux/i2c.h).
All the other functons may be NULL. Here i put some printk to see when they are
invoked. </DIV>
<DIV>The algorithm i2c_mem_algorithm implements two important functions. First
functionality() that returns a functionality flag that must be chosen so that
clients that are not "memory" virtual devices do not get attached to the
adapter. I chose a flag that is not already used by the I2C core (again see
include/linux/i2c.h). Second master_xfer() must be implemented to code the
actual data transfer. If smbus_xfer() is NULL, the algorithm implements a pure
"i2c protocol". In the example i did not conecrn with protocol issues and did a
simple byte copy (after all everything is in memory, and i am more concerned
with the dynamics of the intercations among the I2C layer components). </DIV>
<DIV>The adapter i2c_mem_adapter and the algorithm i2c_mem_algorithm are coded
in the file <A
href="http://www.geocities.com/marco_corvi/games/lkpe/i2c/i2c_mem_adpt.c">i2c_mem_adpt.c</A>.
You need also the header file <A
href="http://www.geocities.com/marco_corvi/games/lkpe/i2c/i2c_mem_h.txt">i2c_mem.h</A>.
<BR>Compile with the <A
href="http://www.geocities.com/marco_corvi/games/lkpe/i2c/makefile.txt">Makefile</A>
and insert the module with insmod. You should see the kernel messages that tell
that the adapter has registerd with the I2C core: <PRE> i2c-core.o: i2c core module
i2c_mem version 0001
i2c-core.o: adapter mem adapter registered as adapter 0.
i2c_mem_adpt: initialized 1
</PRE></DIV>
<DIV>Check the /proc/bus/i2c interface. You should find <PRE> i2c-0 smbus/i2c mem adapter mem bus algorithm
</PRE>where the fields are the device name (as assigned by the I2c core), the
bus, the adapter name, and its description. /proc/bus/i2c-0 is still empty
because there is no client on the adapter. </DIV>
<H3>The i2c_driver i2c_mem_drv.c</H3>
<DIV>Once the memory virtual adapter is in place, you can go on to the memory
virtual clients and driver. This driver should manage mamory-based virtual
clients, that rely on the adapter i2c_mem_adapter to transfer data. </DIV>
<DIV>The driver and the client are coded in the file <A
href="http://www.geocities.com/marco_corvi/games/lkpe/i2c/i2c_mem_drv.c">i2c_mem_drv.c</A>.
Compile it with the same makefile, and install it. </DIV>
<DIV>After installing check /proc/bus/i2c-0 again and you should see that the
client is now there, <PRE> 37 i2c_mem_client 0xp001 i2c mem driver
</PRE>where the fields are the address, the client name, and its driver name.
</DIV>
<H3>Accessing the I2C memory client from user space</H3>
<DIV>Our last effort consists in accessing the client from user space. We need
the i2c-dev.c module which provides the /dev interface to I2C adapters. </DIV>
<DIV>After opening the device, the program should bound to the adapter with an
address (not already taken by any client). This is done with the ioctl
I2C_SLAVE, with argument the address. Next it can transfer data with the ioctl
I2C_RDWR; the argument is a pointer to a i2c_rdwr_ioctl_data structure. This
structure contains
<UL>
<LI>msgs, a pointer to i2c_msg struct;
<LI>nmsgs, the number of messagges. </LI></UL>Each i2c_msg struct has the
following fields
<UL>
<LI>addr (unsigned short) address of the slave;
<LI>flags (unsigned short), can have I2C_M_TEN, I2C_M_RD, I2C_M_NOSTART,
I2C_M_REV_DIR_ADDR;
<LI>len (short) the message length;
<LI>buf (char *) pointer to the message data. </LI></UL></DIV>
<DIV><A
href="http://www.geocities.com/marco_corvi/games/lkpe/i2c/i2c_test.c">i2c_test.c</A>
is a simple user application that shows how to read/write to the memory virtual
client, through the i2c_mem_adapter. </DIV><BR clear=all><BR clear=all><FONT
size=-1>Marco Corvi - 2003</FONT> <!-- text below generated by server. PLEASE REMOVE --></OBJECT></LAYER>
<DIV></DIV></SPAN></STYLE></NOSCRIPT></TABLE></SCRIPT></APPLET>
<SCRIPT
language=JavaScript>var PUpage="76001084"; var PUprop="geocities"; </SCRIPT>
<SCRIPT language=JavaScript src="The I2C layer ex_file/pu5geo.js"></SCRIPT>
<SCRIPT language=JavaScript src="The I2C layer ex_file/ygIELib9.js"></SCRIPT>
<SCRIPT language=JavaScript>var yviContents='http://us.toto.geo.yahoo.com/toto?s=76001084&l=NE&b=1&t=1057746763';yviR='us';yfiEA(0);</SCRIPT>
<SCRIPT language=JavaScript src="The I2C layer ex_file/mc.js"></SCRIPT>
<SCRIPT language=JavaScript src="The I2C layer ex_file/geov2.js"></SCRIPT>
<SCRIPT language=javascript>geovisit();</SCRIPT>
<NOSCRIPT><IMG height=1 alt=setstats src="The I2C layer ex_file/visit.gif"
width=1 border=0></NOSCRIPT> <IMG height=1 alt=1
src="The I2C layer ex_file/serv.gif" width=1> <!-- w36.geo.scd.yahoo.com compressed/chunked Wed Jul 9 03:32:43 PDT 2003 --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -