I2C is a multi-master serial single-ended bus invented by Philips that is used to attach low-speed peripherals to an embedded system. SMBus, defined by Intel in 1995, is a subset of I2C. This article is a practical guide to use the I2C bus on the CORE9G25 Systems Linux embedded boards
To have a more in depth idea of how the I2C works under Linux please read the Linux Kernel documentation on:
Using i2c-tools
The faster way to do the first experiments with this board is by installing and using the i2c-tools.
i2c-tools is a package contains a heterogeneous set of I2C tools for Linux such as:
- a bus probing tool
- a chip dumper
- a register-level access helpers
- an EEPROM decoding scripts
- ...and more
To install i2c-tools on the FOX Board just type:
debarm:~# apt-get update debarm:~# apt-get install i2c-tools
Using i2cdetect
i2cdetect is an userspace program to scan an I2C bus for devices. It outputs a table with the list of detected devices on the specified bus.
Example:
debarm:~# i2cdetect -y 0 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
In this case a device has been detected on address 20 hex.
Using i2cset
i2cset is a small helper program to set registers visible through the I2C bus.
The following simple command writes the byte value 255 to the I2C device at address 20 hex on the i2c bus 0 (/dev/i2c-0).
debarm:~# i2cset -y 0 0x20 255
If for example you are using a DAISY-22 module with a PCF8574 I2C I/O expander this command will set all the GPIO lines to 1.
Python code example
python-smbus is a Python module allows SMBus access through the I2C /dev interface on Linux hosts. The host kernel must have I2C support, I2C device interface support, and a bus adapter driver.
The following example sends a sequence of values from 0 to 255 to the PCF8574 I2C I/O expander at address 0x20.
- write.py This code example is downloadable from the CD. File: CD://debian/playground/python/i2c/write.py
import smbus import time bus = smbus.SMBus(0) for a in range(0,256): bus.write_byte(0x20,a) time.sleep(0.1)
C code example
The following example sends a sequence of values from 0 to 255 to the PCF8574 I2C I/O expander at address 0x20 in C language.
- write.c This code example is downloadable from the CD. File: CD: /debian/playground/i2c/write.c
#include <stdio.h> #include <fcntl.h> #include <linux/i2c-dev.h> #include <errno.h> #define I2C_ADDR 0x20 int main (void) { int value; int fd; fd = open("/dev/i2c-0", O_RDWR); if (fd < 0) { printf("Error opening file: %s\n", strerror(errno)); return 1; } if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) { printf("ioctl error: %s\n", strerror(errno)); return 1; } for (value=0; value<=255; value++) { if (write(fd, &value, 1) != 1) { printf("Error writing file: %s\n", strerror(errno)); } usleep(100000); } return 0; }
Related links
Documentation Terms of Use
The Acme Systems srl provides this Debian system development and user manual.
The origin of these doc came from the website: http://www.acmesystems.it
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.