I2C “WIRES” (JSAPI_I2C_WIRE)

The I2C_WIRE class represents the physical I2C (Inter-Integrated Circuit) peripheral units available on the MCU. The physical I2C interface is a 2-wire multiple master/slave synchronous serial interface. It is used to connect devices over short distances. I2C is also called TWI (Twin Wire Interface) due to legal requirements. A synchronous interface means that there is a common clock signal between the communicating devices.

The I2C signals are:

·         SCL (synchronous clock) is the clock source from the master

·         SDA (data) is the wire that carries data between the master and the slaves

https://lh3.googleusercontent.com/i8_Wz1m1b7qVZ1PSna81vjyGjaxe6yz9Wu6ezspTGf5rysaHUpDGCAOMJlbIq5z1fNSZwIy7w0nxdNC2pHno_PPNw-HqGY29RDLVHCJ2MQUBcyPCAPyN82bwvwnr3MCMorVZb_U

The I2C class (see later section) represents an I2C object (e.g. a LCD hooked up to a MCU using the I2C bus) that builds on top of the I2C_WIRE. Separating the two classes (I2C and I2C wire) corresponds to the fact that a single I2C bus can support multiple I2C devices.

In the I2C protocol, a master device initiates communication by creating a START condition on the bus. To terminate communication, the master device creates a STOP condition on the bus.

The STM32F4xx series uses a different I2C peripheral implementation than the other ST MCU series, hence some API functions are slightly different between these two sets. However, except in advanced use scenarios, the only I2C_WIRE function a user needs to call is the SetPins function to associate the GPIO pins to a hardware I2C unit. Once done, then the user only needs to use the JSAPI_I2C class to work with I2C devices.

Object

extern JSAPI_I2C_WIRE i2c1, i2c2;

Associate GPIO pins with I2C functions

void SetPins(

    JSAPI_GPIO *scl_port,

    unsigned scl_pin_no,

    unsigned scl_af,

    JSAPI_GPIO *sda_port,

    unsigned sda_pin_no,

    unsigned sda_af

);

 

Again, the following functions should normally not be used by users, and thus they will not be described further.

STM32F4xx Series

Receive a byte

int Receive(_Bool is_last_byte);

Enable I2C hardware

int Enable(unsigned CCR_TRISE, void *p);

STM32F0xx/STM32F3xx/STM32F7xx Series

Receive a byte

int Receive(void);

Enable I2C hardware

int Enable(unsigned timing_register, void *p);

Generic Common Routines

Send a byte

int Send(unsigned char data);

Generate a START condition

void Start(unsigned CR2);

Generate a STOP condition

void Stop(void);

Set timeout value

void SetTimeout(int timeout);

Disable I2C unit

int Disable(void *p);

Reset I2C unit

void Reset(void);

 

Examples of using JSAPI_I2C_WIRE are given in the JSAPI_I2C section below.