eLua platform interface - I2C

Overview

This part of the platform interface groups functions related to the I2C interface(s) of the MCU. Currently only the I2C master mode is supported by eLua.

Data structures, constants and types

// I2C speed
enum
{
  PLATFORM_I2C_SPEED_SLOW = 100000,
  PLATFORM_I2C_SPEED_FAST = 400000
};

Constants used to configure the speed of the I2C interface.

// I2C direction
enum
{
  PLATFORM_I2C_DIRECTION_TRANSMITTER,
  PLATFORM_I2C_DIRECTION_RECEIVER
};

Constants used to select the I2C master transfer direction (transmitter or receiver).

Functions

int platform_i2c_exists( unsigned id );

Checks if the platform has the hardware I2C specified as argument. Implemented in src/common.c, it uses the NUM_I2C macro that must be defined in the platform's platform_conf.h file (see here for details). For example:

#define NUM_I2C   1      // The platform has one I2C interface

Arguments: id - I2C interface ID.

Returns: 1 if the I2C interface exists, 0 otherwise

void platform_i2c_send_start( unsigned id );

Send an I2C START condition on the specified interface.

Arguments: id - I2C interface ID.

Returns: nothing.

void platform_i2c_send_stop( unsigned id );

Send an I2C STOP condition on the specified interface.

Arguments: id - I2C interface ID.

Returns: nothing.

int platform_i2c_send_byte( unsigned id, u8 data );

Send a byte on the I2C interface.

Arguments:

  • id - I2C interface ID.
  • data - the byte to send.

Returns: 1 for success, 0 for error.

int platform_i2c_recv_byte( unsigned id, int ack );

Receive a byte from the I2C interface and send a positive (ACK) or negative (NAK) acknowledgement.

Arguments:

  • id - I2C interface ID.
  • ack - 1 to send ACK, 0 to send NAK. If ACK is 0 a STOP condition will automatically be generated after the NAK.

Returns: 1 for success, 0 for error.