Difference between revisions of "I2c library for C"

From Tuxamito
Jump to: navigation, search
(Combined Write/Read)
Line 5: Line 5:
 
= Usage =
 
= Usage =
  
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="c">
 
int i2c_dev_open(struct i2c_dev *dev, int adapter);
 
int i2c_dev_open(struct i2c_dev *dev, int adapter);
 
int i2c_dev_close(struct i2c_dev *dev);
 
int i2c_dev_close(struct i2c_dev *dev);
Line 15: Line 15:
  
 
=== Set Up I2C device ===
 
=== Set Up I2C device ===
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="c">
 
int i2c_dev_open(struct i2c_dev *dev, int adapter);
 
int i2c_dev_open(struct i2c_dev *dev, int adapter);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== Close I2C device ===
 
=== Close I2C device ===
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="c">
 
int i2c_dev_close(struct i2c_dev *dev);
 
int i2c_dev_close(struct i2c_dev *dev);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== Write to device ===
 
=== Write to device ===
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="c">
 
int i2c_dev_write(struct i2c_dev *dev, uint8_t *buf, int n);
 
int i2c_dev_write(struct i2c_dev *dev, uint8_t *buf, int n);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
=== Read from device ===
 
=== Read from device ===
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="c">
 
int i2c_dev_read(struct i2c_dev *dev, uint8_t *buf, int n);
 
int i2c_dev_read(struct i2c_dev *dev, uint8_t *buf, int n);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== Combined Write/Read ===
 
=== Combined Write/Read ===
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="c">
 
int i2c_dev_wrrs(struct i2c_dev *dev, uint8_t *wbuf, int wn, uint8_t *rbuf, int rn);
 
int i2c_dev_wrrs(struct i2c_dev *dev, uint8_t *wbuf, int wn, uint8_t *rbuf, int rn);
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:44, 11 November 2015

This library was created to make communication with I2C devices from C language. Up to know it has work fine for in Raspberry Pi boards (and similar ones!).

Download: File:I2c-driver.tar.gz

Usage

int i2c_dev_open(struct i2c_dev *dev, int adapter);
int i2c_dev_close(struct i2c_dev *dev);

int i2c_dev_write(struct i2c_dev *dev, uint8_t *buf, int n);
int i2c_dev_read(struct i2c_dev *dev, uint8_t *buf, int n);
int i2c_dev_wrrs(struct i2c_dev *dev, uint8_t *wbuf, int wn, uint8_t *rbuf, int rn);

Set Up I2C device

int i2c_dev_open(struct i2c_dev *dev, int adapter);

Close I2C device

int i2c_dev_close(struct i2c_dev *dev);

Write to device

int i2c_dev_write(struct i2c_dev *dev, uint8_t *buf, int n);

Read from device

int i2c_dev_read(struct i2c_dev *dev, uint8_t *buf, int n);

Combined Write/Read

int i2c_dev_wrrs(struct i2c_dev *dev, uint8_t *wbuf, int wn, uint8_t *rbuf, int rn);

The purpose of this is to allow combined write/read operations to one or more devices without releasing the bus and thus with the guarantee that the operation is not interrupted.

The hardware support for this feature is usually disable in the Rapberry Pi. To activate it execute as root:

echo 1 > /sys/module/i2c_bcm2708/parameters/combined 

Example