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
u32 platform_i2c_setup( unsigned id, u23 speed );
This function is used to initialize the parameters of the I2C interface. NOTE: currently, only master I2C mode is implemented in eLua.
Arguments:
- id - I2C interface ID.
- speed - the speed of the interface, can be either PLATFORM_I2C_SPEED_SLOW or PLATFORM_I2C_SPEED_FAST as defined here
Returns: the actual speed set for the I2C interface. Depending on the hardware, this may have a different value than the speed argument.
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_address( unsigned id, u16 address, int direction );
Send an address on the I2C with the specified transfer direction (transmitter or receiver).
Arguments:
- id - I2C interface ID.
- address - I2C peripheral address.
- direction - transfer direction, either PLATFORM_I2C_DIRECTION_TRANSMITTER or PLATFORM_I2C_DIRECTION_RECEIVER as defined here.
Returns: 1 for success, 0 for error.
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.