8. I2C Interface
In this document, we will exclusively focus on initiating I2C communication using SDK6. For a more comprehensive understanding of the I2C interface, please refer to the relevant sections covering I2C Architecture, Timing, and I2C Controller Enhancements in the DA1453x or DA1458x datasheets.
8.1. Features
Two-wire I2C serial interface consisting of a serial data line (SDA) and a serial clock (SCL)
Two speeds are supported:
Standard mode (0 to 100 kbit/s)
Fast mode (≤ 400 kbit/s)
Clock synchronization
7-bit or 10-bit addressing
7-bit or 10-bit combined format transfers
Default slave address of
0x055
Programmable SDA hold time
DMA support`
8.2. I2C configuration in SDK6:
To start any peripheral project we suggest to start with empty_peripheral_template
from SDK6 projects here we will only show how to configure I2C interface.
SPI examples
- There are two examples in BLE SDK6 examples , Please take a look to see how to use I2C in action:
I2C Master Slave example .This example describes how to perform I2C data buffer transmission/reception between two boards in asynchronous mode (non-blocking communication)
If you want to use a sensor and receive data on your mobile phone these examples can be a good choice:
In SDK6 the peripheral configuration can be done in user_periph_setup module. Make sure to include SPI header in your project:
#include "i2c.h"
In user_periph_setup.h
the I2C Configuration for master is as follow:
#define I2C_SCL_PORT GPIO_PORT_0
#define I2C_SCL_PIN GPIO_PIN_6
#define I2C_SDA_PORT GPIO_PORT_0
#define I2C_SDA_PIN GPIO_PIN_7
#define I2C_FLAGS I2C_F_WAIT_FOR_STOP | I2C_F_ADD_STOP
#define I2C_SLAVE_ADDRESS (0x30 >> 1)
#define I2C_TARGET_ADDRESS I2C_SLAVE_ADDRESS
#define I2C_SPEED_MODE I2C_SPEED_STANDARD
#define I2C_ADDRESS_MODE I2C_ADDRESSING_7B
#define I2C_ADDRESS_SIZE I2C_1BYTES_ADDR
- brief explanation for each line of the provided C code:
I2C_SCL_PORT
: Defines the GPIO port for the I2C serial clock (SCL) pin.I2C_SCL_PIN
: Defines the GPIO pin number for the I2C serial clock (SCL) pin .I2C_SDA_PORT
: Defines the GPIO port for the I2C serial data (SDA) pin as .I2C_SDA_PIN
: Defines the GPIO pin number for the I2C serial data (SDA) pin .I2C_FLAGS
: Defines the flags for the I2C communication, indicating waiting for stop and adding a stop condition.I2C_SLAVE_ADDRESS
: Defines the slave device address for the I2C communication. The address is shifted right by 1 bit (which is equivalent to dividing by 2), resulting in the address 0x18.I2C_TARGET_ADDRESS
: Defines the target address for the I2C communication, which is set to the same value as the slave address.I2C_SPEED_MODE
: Defines the speed mode for the I2C communication as standard speed, which operates at 100 kbits/s ,I2C_SPEED_FAST (400 kbits/s)I2C_ADDRESS_MODE
: Defines the addressing mode for the I2C communication as 7-bit addressing mode.I2C_ADDRESS_SIZE
: Defines the address width for the I2C communication as 1-byte address.
To customize the desired parameters , you can change macros in user_periph_setup.h
:
Warning
The above configuration is based on DA14531 I2C Master Slave example You can change based on your requirements.
In
user_periph_setup.c
invoid set_pad_functions(void)
add these two lines:
GPIO_ConfigurePin(I2C_SCL_PORT, I2C_SCL_PIN, INPUT_PULLUP, PID_I2C_SCL, false);
GPIO_ConfigurePin(I2C_SDA_PORT, I2C_SDA_PIN, INPUT_PULLUP, PID_I2C_SDA, false);
The initialization of the I2C is performed in the
i2c_init(&i2c_cfg
function call within theperiph_init
function. This function is the combination of the steps we explained before:
i2c_init(&i2c_cfg);
i2c_cfg
- This structure will pass the I2C configuration to
i2c_init
: //i2c configuration i2c_cfg_t i2c_cfg = { .clock_cfg.ss_hcnt = I2C_SS_SCL_HCNT_REG_RESET, .clock_cfg.ss_lcnt = I2C_SS_SCL_LCNT_REG_RESET, .clock_cfg.fs_hcnt = I2C_FS_SCL_HCNT_REG_RESET, .clock_cfg.fs_lcnt = I2C_FS_SCL_LCNT_REG_RESET, .restart_en = I2C_RESTART_ENABLE, .speed = I2C_SPEED_MODE, .mode = I2C_MODE_MASTER, .addr_mode = I2C_ADDRESS_MODE, .address = I2C_SLAVE_ADDRESS, .tx_fifo_level = 16, .rx_fifo_level = 16,};
This configuration structure captures the necessary parameters for setting up and initializing an I2C interface like Clock Configuration, Restart Enable, Speed, Mode, Addressing Mode, Slave Address, and FIFO Levels.