eLua reference manual - I2C module

Overview

This module contains functions for accessing the I2C interfaces of the eLua CPU.

IMPORTANT: right now, only master I2C mode is implemented in eLua.

Functions

speed = i2c.setup( id, speed )

Setup the I2C interface.

Arguments:

  • id - the ID of the I2C interface.
  • speed - the speed of the I2C interface. It can be either i2c.FAST (400KHz) or i2c.SLOW (100KHz).

Returns: the actual speed of the I2C interface.

i2c.start( id )

Send a START on the specified I2C interface.

Arguments: id - the ID of the I2C interface.

Returns: nothing.

i2c.stop( id )

Send a STOP on the specified I2C interface.

Arguments: id - the ID of the I2C interface.

Returns: nothing.

acked = i2c.address( id, address, direction )

Send an address on the I2C interface.

Arguments:

  • id - the ID of the I2C interface.
  • address - the address.
  • direction - i2c.TRANSMITTER if the master wants to send data, or i2c.RECEIVER if the master wants to receive data.

Returns: true if the address was acknowledged by an I2C slave, false otherwise.

wrote = i2c.write( id, data1, [data2], ..., [datan] )

Writes data to a slave that already acknowledged an i2c.address call.

Arguments:

  • id - the ID of the I2C interface.
  • data1 - the data to send. It can be either a number between 0 and 255, a string or a table (array).
  • data2 (optional) - the second data to send.
  • datan (optional) - the n-th data to send.

Returns: the number of bytes actually wrote.

data = i2c.read( id, numbytes )

Reads a number of bytes from a slave that already acknowledged an i2c.address call. It acknowledges all the bytes received, except for the last one.

Arguments:

  • id - the ID os the SPI interface.
  • numbytes - the number of bytes to read.

Returns: a string with all the data read from the I2C interface.