Home page > mini2440 > Windows CE > Tutorials > How to use i2c driver in an application

How to use i2c driver in an application

Sunday 14 February 2010, by Dom

There’s a i2c driver in mini2440 BSP that’s no difficult to use. In addition, there is an i2c eeprom on the mini2440, then no additional hard is required to test i2c.

I2c driver is a "stream" driver. We can interface i2c driver from an application with CreateFile function that returns us a handle. This handle is then used to send IOCTL command to i2c driver with DeviceIoControl function.

One of the CreateFile function parametes is the name of the driver. To find this name, we just have to open C:\WINCE600\PLATFORM\Mini2440\FILES\platform.reg file, where we can find i2c driver configuration :

"Mode"=dword:1 ; Mode: 0 = POLLING, 1 = INTERRUPT
"SlaveAddress"=dword:0 ; Bus Driver’s Slave Address
"FriendlyName"="I2C Bus Driver"

The Prefix key defines the name of the driver. A driver can manage several chanels, then we have to add a chanel number after the driver’s name, followed by ":". For i2c driver, it’s "I2C0:".

Now we know the name of the driver, we need to find IOCTL commands that we can send to it. We can find them in C:\WINCE600\PLATFORM\Mini2440\SRC\INC\i2c.h file :

DeviceIoControl function needs the driver handle, an input structure and an output strcuture.
We can find in driver source code how a IOCTL command is processed. When we send a IOTCL command to the driver (with DeviceIoControl function), the I2C_IOControl is called by the system. Source code of this function show us the type of the input structure, defined in .h file of the driver :

typedef struct _I2C_IO_DESC
DWORD SlaveAddr; // Target Slave Address
UCHAR WordAddr; // Starting Slave Word Address
PUCHAR Data; // pBuffer
DWORD Count; // nBytes to read/write

The members of this structure are :
- address of the i2c device we want to access
- address in the device we want access
- pointer on a buffer
- size of the buffer

To read or write in eeprom, we just have to send IOCTL commands by giving this structure in parameter. For reading, buffer will contain read data, for writing, buffer will contain data to be writen.

I quickly wrote a small application as an example.

You can download source code of this sample here

Note : the speed of I2C clock is configured in the driver at the very low value. :
PCLK / 512 / 17 = 5OOOO / 512 / 17 = 5.74kHz.
I2C bus can commonly works at 100kHz, it is then possible to replace, in the file C:\WINCE600\PLATFORM\Mini2440\SRC\DRIVERS\IIC\drv.h, the lines :

#define RESUME_ACK      0xEF
#define RESUME_NO_ACK   0x6F

by :

#define RESUME_ACK      0xE0
#define RESUME_NO_ACK   0x60

1 Message

SPIP | Contact me | | Site Map | Follow-up of the site's activity RSS 2.0      Version Française | English version