# How to use the I2C ports

<abstract>
This article illustrates how to use the I2C ports on Acme Systems Road Runner SOM based on Microchip SAMA5D27 MPU
</abstract>

The I2C bus lines are located on the following pins:

| Signal |     | Per A | Per B | Per C | Per D | Per E | Per F |
|--------|-----|-------|-------|-------|-------|-------|-------|
| TWD0   | SDA |       | PD21  |       | PB31  | PC27  | PC29  |
| TWCK0  | SCL |       | PD22  |       | PC0   | PC28  | PC30  |
| TWD1   | SDA | PD4   | PD19  | PC6   |       |       |       |
| TWCK1  | SCL | PD5   | PD20  | PC7   |       |       |       |

* [Roadrunner pinout](/pinout_roadrunner)	

## Kernel configuration

	Device Drivers  --->
	  I2C support  ---> 
	    <*>   I2C device interface
	          I2C Hardware Bus support  ---> 
	          <*> Atmel AT91 I2C Two-Wire interface (TWI) 
	          
## Device tree

	i2c0: i2c@f8028000 {
		dmas = <0>, <0>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_i2c0_default>;
		i2c-sda-hold-time-ns = <350>;
		status = "okay";
	};

	i2c1: i2c@fc028000 {
		dmas = <0>, <0>;
		pinctrl-names = "default";
		pinctrl-0 = <&pinctrl_i2c1_default>;
		status = "okay";
	};

	pinctrl_i2c0_default: i2c0_default {
		pinmux = <PIN_PD21__TWD0>,
			 <PIN_PD22__TWCK0>;
		bias-disable;
	};

	pinctrl_i2c1_default: i2c1_default {
		pinmux = <PIN_PD4__TWD1>,
			 <PIN_PD5__TWCK1>;
		bias-disable;
	};

## Using I2C from user space

### i2c-tools

The faster way to do the first experiments with this board is by 
installing and using the [i2c-tools](http://packages.debian.org/squeeze/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: 

	apt-get update
	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.

*  [i2cdetect man page](http://man.cx/i2cdetect)

Example of use:

	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. 

### i2cset and i2cset

[i2cset](http://man.cx/i2cset) is a small helper program to set 
registers visible through the I2C bus.

The follow simple command writes the byte value 255 to the I2C 
device at address 20 hex on the i2c bus 0 (<b>/dev/i2c-0</b>). 

	i2cset -y 0 0x20 255

If for example you are using a [DAISY-22](/DAISY-22) module with a 
PCF8574 I2C I/O expander this command will set all the GPIO lines to 1.

[i2cget](http://man.cx/i2cset) i2cget is a small helper program to 
read registers visible through the I2C bus.

The follow simple command read a byte from an I2C 
device at address 20 hex on the i2c bus 0 (<b>/dev/i2c-0</b>). 

	i2cget -y 0 0x20 0x01


## Links

* [How to Use the SAMA5D2 I2C Under Linux](https://ww1.microchip.com/downloads/en/Appnotes/AN_3252-How-to-Use-SAMA5D2-I2C-underLinux-00003252a.pdf)
* [Microchip SAMA5D2 Series datasheet](http://ww1.microchip.com/downloads/en/devicedoc/ds60001476b.pdf)

@include='adv_roadrunner'

