I2C Serial Port in Squeak

Ned Konz ned at squeakland.org
Fri Oct 29 20:57:55 UTC 2004


On Wednesday 27 October 2004 8:45 am, Jon Hylands wrote:
> Hi everyone,
>
> I have a question for the VM guys -- I'm planning on using an I2C bus on my
> next autonomous robot, and the Squeak machine I'm going to use
> (www.gumstix.com) has a hardware I2C interface, with a driver for it in
> Linux. As far as I can tell, the I2C port looks just like another device in
> Linux, so I'm hoping I can just use the serial port with a different com
> port #.

Ah yes, I remember some more...

You can use open/close/read/write/ioctl

However, I don't know if we have an interface to ioctl (this would be a good 
thing to have, though!). Perhaps it's in OSProcess. Or you could use FFI.

From the Documentation/i2c/dev-interface file:

Usually, i2c devices are controlled by a kernel driver. But it is also
possible to access all devices on an adapter from userspace, through
the /dev interface. You need to load module i2c-dev for this.

Each registered i2c adapter gets a number, counting from 0. You can
examine /proc/bus/i2c to see what number corresponds to which adapter.
I2C device files are character device files with major device number 89
and a minor device number corresponding to the number assigned as 
explained above. They should be called "i2c-%d" (i2c-0, i2c-1, ..., 
i2c-10, ...). All 256 minor device numbers are reserved for i2c.

[snip C example]

The following IOCTLs are defined and fully supported 
(see also i2c-dev.h and i2c.h):

ioctl(file,I2C_SLAVE,long addr)
  Change slave address. The address is passed in the 7 lower bits of the
  argument (except for 10 bit addresses, passed in the 10 lower bits in this
  case).

ioctl(file,I2C_TENBIT,long select)
  Selects ten bit addresses if select not equals 0, selects normal 7 bit
  addresses if select equals 0. Default 0.

ioctl(file,I2C_PEC,long select)
  Selects SMBus PEC (packet error checking) generation and verification
  if select not equals 0, disables if select equals 0. Default 0.
  Used only for SMBus transactions.

ioctl(file,I2C_FUNCS,unsigned long *funcs)
  Gets the adapter functionality and puts it in *funcs.

ioctl(file,I2C_RDWR,struct i2c_ioctl_rdwr_data *msgset)

  Do combined read/write transaction without stop in between.
  The argument is a pointer to a struct i2c_ioctl_rdwr_data {

      struct i2c_msg *msgs;  /* ptr to array of simple messages */
      int nmsgs;             /* number of messages to exchange */
  }

  The msgs[] themselves contain further pointers into data buffers.
  The function will write or read data to or from that buffers depending
  on whether the I2C_M_RD flag is set in a particular message or not.
  The slave address and whether to use ten bit address mode has to be
  set in each message, overriding the values set with the above ioctl's.

Other values are NOT supported at this moment, except for I2C_SMBUS,
which you should never directly call; instead, use the access functions
below.

You can do plain i2c transactions by using read(2) and write(2) calls.
You do not need to pass the address byte; instead, set it through
ioctl I2C_SLAVE before you try to access the device.

-- 
Ned Konz
http://bike-nomad.com/squeak/



More information about the Squeak-dev mailing list