User Tools

Site Tools


en:i2c

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
en:i2c [2013/11/27 12:35] – created kakliken:i2c [2014/12/21 10:12] fluktuacia
Line 1: Line 1:
-====== I2C in MLAB system ====== 
  
-===== Activating The I2C Port =====+====== I²C bus as an MLAB's component ======
  
 +[[http://en.wikipedia.org/wiki/I²C|I²C bus]], also called TWI is a popular electronic bus designed for low-speed communication between different peripherals. As an MLAB's component it is mostly used to ensure communication between microprocessor and sensors. I²C derivative [[http://en.wikipedia.org/wiki/System_Management_Bus|SMBus]] differs from it in that it contains timeouts' definitions and Packet Error Checking and therefor is, in sense, more robust than the original I²C. 
 +   
 +Thanks to an advancement in computer technologies [[en:arm|new possibilities]] arise allowing the use of the bus for direct communication between sensors and Linux systems. Furthermore, with the help of [[en:usbi2c|I²C adapter]] the bus can be connected to PC as well.  
  
-The I2C port needs to be enabled in kernel before you can use it. This can be achieved by +===== Linking of I²C devices =====
  
-  sudo modprobe i2c-dev  +Although the I²C bus was originally designed to link devices on small scales (within the dimensions of PCB), using certain techniques it is possible to enlarge its range to tens of meters (( [[http://cladlab.com/electronics/circuit-design/communication-protocols/i2c-protocol#gallery|I2C Transmission Distances And Buffer’s]] )). However, it must be noted that such approach may not be safe because over long wires a high voltage can be electrostaticilly and electormagnetically induced and can cause damage to connected devices. The exact distance over which it is safe to use the I²C bus depends on the surrounding environment and the required reliability of the system.    
-   +
-Then I²C bus interfaces can be listed by+
  
-  sudo i2cdetect -l+Linking of I²C devices within MLAB is implemented on several levels according to extent of a network. Locally (on scale of several meters), the modules are connected by MLAB cables and separated by [[en:i2chub]]. This module also solves the conversion between logic 3.3V and +5V.
  
-===== Checking For Connected Devices =====+Over more extensive distances the bus is linked through shielded signal cables ending in female connectors and connected to [[en:uniserial|UniSerial]] module. This module also contains a basic surge protection and most importantly acts as a mechanical bridge between 'heavy external wiring' and fine internal linking of devices. 
  
-At the command prompt type one of these depending on whether you are using the I2C-X port:+In the case of extensive networks I²C is replaced by other physical layer (CAN, Ethernet) that interconnects individual I²C network segments.
  
-sudo i2cdetect -y X 
  
-The 7 bit I2C address of all found devices will be shown (ignoring the R/W bit, so I2C address 0000 0110 is displayed as hex 03). 
  
 +==== Pymlab package ====
 +
 +Pymlab represents a software package that enables a creation of network made of I²C devices and modules, which serve as routing points. It defines the structure of a network and addresses of individual nodes in a source file of a control program. Individual points in the network can than be accessed using simple python commands. 
    
-===== SMBus (System Management Bus) Functions =====+The installation of the [[en:pymlab|pymlab]] packed can easily be done using following command:  
 + 
 +  sudo pip install pymlab
  
-SMBus (System Management Bus) is a subset from the I2C protocol +In case of having some older version of pymlab already installed, it is enough to run:
-When writing a driver for an I2C device try to use the SMBus commands only (if it possible). It makes  possible to use the device driver on both SMBus adapters and I2C adapters.+
  
-Note address is the 7 bit address excluding the read / write bit (it will be shifted left 1 bit whne added to the read/write bit)+  sudo pip install --upgrade pymlab
  
-long write_quick(int addr)+Both commands are equivalent and use the python package manager that contains [[https://pypi.python.org/pypi/pymlab/0.2|the pymlab]]. 
 +This procedure should also download other packages used by pymlab such as cython-hidapi (reffered only as hid module in python). However it does not install alelibusb, which must still be done manually using:
  
-Send only the read / write bit+  sudo apt-get install libudev-dev libusb-1.0-0-dev libhidapi-dev python-setuptools python-smbus cython 
  
  
-long read_byte(int addr)+===== Convertors to connect I²C to PC =====
  
-Read a single byte from a devicewithout specifying a device register.+Although I²C is quite popular bus suitable to connect different sensors over short distancesit is not usually found and readily available on regular computers with exception of [[http://www.paintyourdragon.com/?p=43|DCC leading out of free display port.]]
  
 +==== USB interface ====
  
-long write_byte(int addr,char val)+Proprietary designs:
  
-Send a single byte to a device+  * [[http://www.robot-electronics.co.uk/acatalog/USB_I2C.html|USB-I2C  USB to I2C Communications Module]] 
 +  * [[http://www.harbaum.org/till/i2c_tiny_usb/index.shtml|i2c-tiny-usb]] 
 +  * [[http://www.headfuzz.co.uk/?q=usbtoi2c|OpenServo interface]]
  
 +These share a common problem with a quality of drivers. Furthermore, i2c-tiny-usb does not fully meet the I²C specification, completely fails to comply with USB physical layer specification and only has maximal communication speed of 50Kbps. MLAB replaces these constructions with [[en:i2c_avr_usb|I2C AVR USB]] device that is equivalent to i2c-tiny-usb design, but made of MLAB modules.  [[en:i2c-pic-usb| I²C to USB with PIC connector]] represent another, yet unfinished, alternative.
  
-long read_byte_data(int addr,char cmd) 
  
-Read Byte Data transaction.+==== USB HID ====
  
 +For connections without the need for drivers designed for specific hardware [[http://en.wikipedia.org/wiki/USB_human_interface_device_class|USB HID]] specification can be used. MLAB has only one module that complies with this particular specification - [[cs:usbi2c|USBI2C01A]]. It can be operated using python or HIDAPI - see below.
  
-long write_byte_data(int addr,char cmd,char val) 
  
-Write Byte Data transaction.+==== UART/RS232 to I²C  ====
  
 +NXP manufactures [[http://www.nxp.com/products/interface_and_connectivity/bridges/uart_to_i2c_master_gpio_bridges/SC18IM700IPW.html|konvertor UART na I²C SC18IM700IPW]], which can possibly become the basis for construction of another conversion module.
  
-long read_word_data(int addr,char cmd)+==== Ethernet to I²C  ====
  
-Read Word Data transaction.+This type of convertor can be constructed using [[cs:eth|ETH01A]] and [[cs:stm32f10xrxt|STM32F10xRxT01A]] modules and controlled by Pymlab software package (see below).
  
 +===== I²C implementation in Linux systems =====
  
-long write_word_data(int addr,char cmd,int val)+There exists a package of tools for working with I²C bus for Ubuntu that can be installed through:  
 +  sudo apt-get install i2c-tools
  
-Write Word Data transaction.+Due to the usual absence of //i2c-dev// (([[https://i2c.wiki.kernel.org/index.php/Main_Page| Linux I²C Subsystem]])) module in kernel, we need to add it manually: 
 +  sudo modprobe i2c-dev
  
 +<WRAP tip>  
 +Modprobe command adds the module only to the kernel instance that is already running. Therefore, after reboot, the module will probably still be missing. If we do not want to use modprobe command on next computer start, we need to add following line to  /etc/modules:
 +  i2c-dev  
 +</WRAP>
  
-long process_call(int addr,char cmd,int val)+It is now possible to list available I²C buses:
  
-Process Call transaction.+  $ sudo i2cdetect -l 
 +  i2c-0 i2c        i915 gmbus ssc                  I2C adapter 
 +  i2c-1 i2c        i915 gmbus vga                  I2C adapter 
 +  i2c-2 i2c        i915 gmbus panel                I2C adapter 
 +  i2c-3 i2c        i915 gmbus dpc                  I2C adapter 
 +  i2c-4 i2c        i915 gmbus dpb                  I2C adapter 
 +  i2c-5 i2c        i915 gmbus dpd                  I2C adapter 
 +  i2c-6 i2c        i2c-tiny-usb at bus 001 device 030 I2C adapter
  
 +Using //i2cdetect// software we can further search for I²C buses for example on [[cs:odroid-x2|Odroid-X2]], which has I²C output through GPIO.
  
-long[] read_block_data(int addr,char cmd)+  kaklik@radio-arm-0:~$ sudo i2cdetect -y 1 
 +        1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
 +  00:          -- -- -- -- -- -- -- -- -- -- -- -- --  
 +  10: UU -- -- -- -- -- -- -- -- -- -- -- -- -- 1e --  
 +  20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 +  30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 +  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 +  50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 +  60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  
 +  70: 70 -- -- -- -- -- -- --                          
 +  kaklik@radio-arm-0:~$
  
-Read Block Data transaction    +The output above shows, that apart from system device with 0x10 address, there are to additional devices connected to the bus: 0x1e ([[cs:mag|MAG01A]]) and 0x70 ([[cs:i2chub|I2CHUB02A]]).
  
 +==== Communication with I²C devices ====
  
-write_block_data(int addr,char cmd,long vals[])+The bus can be controlled either by using system kernel interface or a service utility that is a part of i2c-tools package:
  
-Write up to 32 bytes to a device.  This fucntion adds an initial byte indicating the length of the vals array before the valls array.  Use write_i2c_block_data instead!+  $ apropos i2c 
 +  i2cdetect (8)        - detect I2C chips 
 +  i2cdump (8)          - examine I2C registers 
 +  i2cget (8)           - read from I2C/SMBus chip registers 
 +  i2cset (8)           - set I2C registers
  
 +=== Writing ===
  
-long[] block_process_call(int addr,char cmd,long vals[])+Writing is executed by i2cset command. A following [[cs:i2chub]setting provides a specific example as the IO has only one register that is not addressed. Data is therefore written directly: 
  
-Block Process Call transaction.      +  ~$ sudo i2cset -y -r  1 0x70 0xff 
-I2C Access Functions+  Value 0xff written, readback matched
  
 +The command triggers all I²C channels on I²CHub ((caution is necessary as having the same addresses on several channels or devices will cause a collision and locking of the bus. In this event the bus has to be reset by disconnecting the power supply or through RESET pin on I2CHUB module)).
  
-long[] read_i2c_block_data(int addr,char cmd)+=== Reading ===
  
-Block Read transaction.+Reading from I²CHub is atypical as well, because the data registry is not addressed: 
  
 +  :~$ sudo i2cget -y 1 0x70 
 +  0xff
  
-write_i2c_block_data(int addr,char cmd,long vals[]) 
  
-Block Write transaction. +===== Other operating systems =====
  
 +On other OS (like Windows or Mac OS), where we cannot or do not want to use kernel I²C interface support, we may use Python programming environment that is multi-platform.
  
-===== References =====+===== Related sites =====
  
-  * [[http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2|Using the I2C Interface]] +  * [[cs:usbi2c]] 
-  * [[http://wiki.erazor-zone.de/wiki%3alinux%3apython%3asmbus%3adoc|python-smbus documentation]]+  * [[cs:i2c-pic-usb|I2C-PIC-USB]] Konvertor I²C na USB s PIC 
 +  * [[cs:i2c_avr_usb|I2C-AVR-USB]] - Konvertor I²C na USB s AVR 
 +  * [[cs:arm|Procesory architektury ARM ve stavebnici MLAB]] 
 +  * [[cs:odroid-x2]] 
 +  * [[cs:friendlyarm]] 
 +  * [[cs:i2chub]] 
 +  * [[cs:pymlab]]
en/i2c.txt · Last modified: 2014/12/21 10:42 (external edit)