I2C

I2C driver provide access to hardware I2C bus controller and currently only support bus communication in master mode.

Note

MT2503/MT6261 has a buffer limitation of maximum 8 byte read/write in single transaction. Writing more than 8 byte require multiple read/write operation.

I2C Driver Usage

#include <hw/i2c.h>

/* initialize I2C port */
i2c_hw_init(I2C_PORT_0, 100);

/* i2c write */
i2c_hw_write(I2C_PORT_0, 0x20, write_buf, length);

/* i2c read */
i2c_hw_read(I2C_PORT_0, 0x20, read_buf, length);

/* i2c write then read */
i2c_hw_writeread(I2C_PORT_0, 0x20, write_buf, wr_len, read_buf, rd_len);

/* release i2c bus */
i2c_hw_free(I2C_PORT_0);

Application Example

waybyte/example-i2c

4G LTE Module I2C Port Mapping

I2C Port 0

I2C Pin

EC600U

EC200U

N58

N716

L610

EG915U

SCL

Pin 11

Pin 41

Pin 59

Pin 122

Pin 41

Pin 103

SDA

Pin 12

Pin 42

Pin 58

Pin 121

Pin 42

Pin 114

I2C Port 1

I2C Pin

EC600U

EC200U

N58

N716

L610

EG915U

SCL

Pin 57

Pin 141

Pin 82

Pin 71

Pin 141

Pin 40

SDA

Pin 56

Pin 142

Pin 81

Pin 70

Pin 142

Pin 41

GSM/NB-IoT Module I2C Port Mapping

RDA8955 based modules provide two I2C ports. Following is the pin mapping:

I2C Port 0

I2C Pin

M590 Pin

MC65 Pin

A9 Pin

SCL

Pin 23

Pin 56

Pin 1

SDA

Pin 24

Pin 65

Pin 2

I2C Port 1

I2C Pin

M590 Pin

MC65 Pin

A9 Pin

SCL

Pin 60

Pin 35

Pin 48

SDA

Pin 61

Pin 36

Pin 47

GSM (MT2503, MT6261) and NB-IoT (MT2625) chipset supported modules expose only one I2C port. Following is the pin mapping for SCL and SDA:

I2C Pin

M56 Pin

MC60 Pin
MC20(U) Pin

M66 Pin

BC20 Pin

SIM868 Pin

SCL

Pin 24

Pin 35

Pin 20

Pin 58

Pin 65

SDA

Pin 23

Pin 36

Pin 21

Pin 63

Pin 64

API Reference

Header File

Source: include/hw/i2c.h

#include <hw/i2c.h>

Functions

int i2c_hw_init(int port, int speed_khz)

Initialize I2C hardware and setup pin-mux. Please check hardware guide for I2C pin details.

Parameters:
  • port – [in] I2C Port (i2cport_e)

  • speed_khz – [in] I2C Speed in Khz (<= 400)

Returns:

0 on success, negative value on error

int i2c_hw_setspeed(int port, int speed_khz)

Set I2C master clock speed

Parameters:
  • port – [in] I2C Port (i2cport_e)

  • speed_khz – [in] I2C Speed in Khz (<= 400)

Returns:

0 on success, negative value on error

int i2c_hw_write(int port, unsigned char address, const void *buffer, int len)

Master send data over I2C slave device Hardware FIFO only support upto 8 bytes at one time for transfer

Parameters:
  • port – [in] I2C Port (i2cport_e)

  • address – [in] 7-bit Slave address

  • buffer – [in] Pointer to buffer containing data

  • len – [in] Length of data to send (must be less than or equal to 8)

Returns:

Length of bytes written on success, negative value on error

int i2c_hw_read(int port, unsigned char address, void *buffer, int len)

Master read data from I2C slave device

Parameters:
  • port – [in] I2C Port (i2cport_e)

  • address – [in] 7-bit slave address

  • buffer – [out] Pointer to buffer to store data

  • len – [in] No. of bytes to read

Returns:

Length of data read from slave on success, negative value on error

int i2c_hw_writeread(int port, unsigned char address, const void *wrbuf, int wrlen, void *rdbuf, int rdlen)

Write and Read operation with repeated start. Operation is performed in two transactions separated by repeated start, Each transaction is limited to max fifo length of 8 bytes.

Parameters:
  • port – [in] I2C Port (i2cport_e)

  • address – [in] 7-bit slave address

  • wrbuf – [in] Pointer to buffer for write operation

  • wrlen – [in] Length of data in wrbuf

  • rdbuf – [out] Pointer to buffer for storing data

  • rdlen – [in] No. of bytes to read

Returns:

Length of data read from slave on success, negative value on error

int i2c_hw_free(int port)

De-Init I2C bus and release GPIO

Parameters:

port – [in] I2C Port (i2cport_e)

Returns:

0 on success, negative value on error

Header File

Source: include/plat/def_i2c.h

#include <plat/def_i2c.h>

Enumerations

enum i2cport_e

I2C port list

Values:

enumerator I2C_PORT_0

I2C Port 0

enumerator I2C_PORT_1

I2C Port 1

Note

Avaiable on RDA8910 and RDA8955 chipset based module only