Driver for the IICA peripheral on RA MCUs. This module implements the I2C Slave Interface.
Overview
Features
- Supports multiple transmission rates
- Standard Mode Support with up to 100-kHz transaction rate.
- Fast Mode Support with up to 400-kHz transaction rate.
- Fast Mode Plus Support with up to 1-MHz transaction rate.
- Reads data written by master device.
- Write data which is read by master device.
- Clock stretching is supported and can be implemented via callbacks.
- Provides Transmission/Reception transaction size in the callback.
- IICA Slave can notify the following events via callbacks: Transmission/Reception Request, Transmission/Reception Request for more data, Transmission/Reception Completion, Error Condition.
Configuration
Build Time Configurations for r_iica_slave
The following build time configurations are defined in fsp_cfg/r_iica_slave_cfg.h:
Configuration | Options | Default | Description |
Parameter Checking |
-
Default (BSP)
-
Enabled
-
Disabled
| Default (BSP) | If selected code for parameter checking is included in the build. |
10-bit slave addressing |
| Disabled | If enabled, the driver will support 10-bit slave addressing mode. |
General call addressing |
| Disabled | If enabled, the driver will support general call slave addressing mode along with the non-general call slave addressing mode. |
Configurations for Connectivity > IICA Slave (r_iica_slave)
This module can be added to the Stacks tab via New Stack > Connectivity > IICA Slave (r_iica_slave).
Configuration | Options | Default | Description |
Name | Name must be a valid C symbol | g_iica_slave0 | Module name. |
Rate |
-
Standard
-
Fast-mode
-
Fast-mode plus
| Standard | Select the transfer rate.
If the requested transfer rate cannot be achieved, the settings with the largest possible transfer rate that is less than or equal to the requested transfer rate are used. The theoretical calculated transfer rate is printed in a comment in the generated iica_slave_extended_cfg_t structure. |
Internal Reference Clock |
| PCLKB / 1 | Select the internal reference clock for IICA slave. |
Signal Rising Time (us) | Must be a valid value | 0 | Set the SDA and SCL signal rising time in micro-seconds. |
Signal Falling Time (us) | Must be a valid value | 0 | Set the SDA and SCL signal falling time in micro-seconds. |
Duty Cycle (%) | Value must be an integer between 0 and 100 | 53 | Set SCL high duty cycle. |
Digital Filter |
| Disabled | Configure digital filter. |
Slave Address | Value must be non-negative | 0x08 | Specify the slave address. |
Callback | Name must be a valid C symbol | iica_slave_callback | A user callback function must be provided. This will be called from the interrupt service routine (ISR) to report IICA Slave transaction events and status. |
IICA0 communication interrupt priority | MCU Specific Options | | Select end of IICA0 communication interrupt priority. |
SCLA Pin |
-
Disabled
-
P100
-
P110
-
P212
-
P914
| Disabled | Specify SCLA pin setting for the MCU. |
SDAA Pin |
-
Disabled
-
P101
-
P109
-
P213
-
P913
| Disabled | Specify SDAA pin setting for the MCU. |
Clock Configuration
The IICA peripheral module uses the PCLKB as its clock source. The actual I2C transfer rate will be calculated and set by the tooling depending on the selected transfer rate. If the PCLKB is configured in such a manner that the selected transfer rate cannot be achieved, an error will be returned.
Pin Configuration
The IICA peripheral module uses pins on the MCU to communicate to external devices. I/O pins must be selected and configured as required by the external device. An IICA channel would consist of two pins - SDAA and SCLA for data/address and clock respectively.
Usage Notes
Interrupt Configuration
- IICA0 communication interrupt must be enabled in the properties of the selected device.
- Note
- : During master-write slave-read type of operations if the slave device requires to perform clock stretching after the last data byte is received.
-
: To support clock stretching (Holding SCL low after the falling edge of the 9th clock cycle), 'Clock Stretching' configuration must be enabled.
Callback
- A callback function must be provided which will be invoked for the cases below:
- An I2C Master initiates a transmission or reception: I2C_SLAVE_EVENT_TX_REQUEST; I2C_SLAVE_EVENT_RX_REQUEST
- A Transmission or reception has been completed: I2C_SLAVE_EVENT_TX_COMPLETE; I2C_SLAVE_EVENT_RX_COMPLETE
- An I2C Master is requesting to read or write more data: I2C_SLAVE_EVENT_TX_MORE_REQUEST; I2C_SLAVE_EVENT_RX_MORE_REQUEST
- Error conditions: I2C_SLAVE_EVENT_ABORTED
- The callback arguments will contain information about the transaction status/events, bytes transferred and a pointer to the user defined context.
- Clock stretching is enabled by the use of callbacks. This means that the IICA slave can hold the clock line SCL LOW to force the I2C Master into a wait state.
- The table below shows I2C Slave event handling expected in user code:
IICA Slave Callback Event | IICA Slave API expected to be called |
I2C_SLAVE_EVENT_ABORTED | Handle event based on application |
I2C_SLAVE_EVENT_RX_COMPLETE | Handle event based on application |
I2C_SLAVE_EVENT_TX_COMPLETE | Handle event based on application |
I2C_SLAVE_EVENT_RX_REQUEST | R_IICA_SLAVE_Read API. If the slave is a Write Only device call this API with 0 bytes to send a NACK to the master. |
I2C_SLAVE_EVENT_TX_REQUEST | R_IICA_SLAVE_Write API |
I2C_SLAVE_EVENT_RX_MORE_REQUEST | R_IICA_SLAVE_Read API. If the slave cannot read any more data call this API with 0 bytes to send a NACK to the master. |
I2C_SLAVE_EVENT_TX_MORE_REQUEST | R_IICA_SLAVE_Write API |
IICA Slave Rate Calculation
- The RA Configuration editor calculates the internal baud-rate setting based on the configured transfer rate. The closest possible baud-rate that can be achieved (less than or equal to the requested rate) at the current PCLKB settings is calculated and used.
Limitations
- DTC doesn't support IICA
- Extension code is not supported
- Please configure SDAA and SCLA pins in the IICA module. IICA pins must be set after IICA is enabled.
Examples
Basic Example
This is a basic example of minimal use of the R_IICA_SLAVE in an application. This example shows how this driver can be used for basic read and write operations.
{
.slave = I2C_7BIT_ADDR_IIC_SLAVE,
.p_callback = iica_master_callback,
.p_context = &g_i2c_master_ctrl,
.p_transfer_tx = NULL,
.p_transfer_rx = NULL,
.p_extend = &g_iic_master_cfg_extend_standard_mode
};
iica_slave_instance_ctrl_t g_i2c_slave_ctrl;
{
.slave = I2C_7BIT_ADDR_IIC_SLAVE,
.p_callback = iica_slave_callback,
.p_context = &g_i2c_slave_ctrl,
.p_extend = &g_iic_slave_cfg_extend_standard_mode
};
{
g_i2c_master_callback_event = p_args->
event;
}
{
g_i2c_slave_callback_event = p_args->
event;
{
}
{
err =
R_IICA_SLAVE_Read(&g_i2c_slave_ctrl, g_i2c_slave_buffer, g_slave_transfer_length);
assert(FSP_SUCCESS == err);
}
{
assert(FSP_SUCCESS == err);
}
else
{
}
}
void basic_example (void)
{
uint32_t i;
uint32_t timeout_ms = I2C_TRANSACTION_BUSY_DELAY;
g_slave_transfer_length = I2C_BUFFER_SIZE_BYTES;
assert(FSP_SUCCESS == err);
assert(FSP_SUCCESS == err);
for (i = 0; i < I2C_BUFFER_SIZE_BYTES; i++)
{
g_i2c_master_tx_buffer[i] = (uint8_t) i;
}
err =
R_SAU_I2C_Write(&g_i2c_master_ctrl, &g_i2c_master_tx_buffer[0], I2C_BUFFER_SIZE_BYTES,
false);
assert(FSP_SUCCESS == err);
{
timeout_ms--;
}
{
__BKPT(0);
}
timeout_ms = I2C_TRANSACTION_BUSY_DELAY;
err =
R_SAU_I2C_Read(&g_i2c_master_ctrl, &g_i2c_master_rx_buffer[0], I2C_BUFFER_SIZE_BYTES,
false);
assert(FSP_SUCCESS == err);
{
timeout_ms--;
}
{
__BKPT(0);
}
if (0U != memcmp(g_i2c_master_tx_buffer, g_i2c_master_rx_buffer, I2C_BUFFER_SIZE_BYTES))
{
__BKPT(0);
}
}
◆ iica_slave_clock_settings_t
struct iica_slave_clock_settings_t |
◆ iica_slave_pin_settings_t
struct iica_slave_pin_settings_t |
Configuration settings for IICA pins
◆ iica_slave_extended_cfg_t
struct iica_slave_extended_cfg_t |
R_IICA_SLAVE extended configuration
◆ R_IICA_SLAVE_Open()
Opens the IICA slave device.
- Return values
-
FSP_SUCCESS | IICA slave device opened successfully. |
FSP_ERR_ALREADY_OPEN | Module is already open. |
FSP_ERR_IP_CHANNEL_NOT_PRESENT | Channel is not available on this MCU. |
FSP_ERR_ASSERTION | Parameter check failure due to one or more reasons below:
- p_api_ctrl or p_cfg is NULL.
- extended parameter is NULL.
- Callback parameter is NULL.
- Invalid IRQ number assigned
|
◆ R_IICA_SLAVE_Read()
Performs a read from the IICA Master device.
This function will fail if there is already an in-progress IICA transfer on the associated channel. Otherwise, the IICA slave read operation will begin. The caller will be notified when the operation has finished by an I2C_SLAVE_EVENT_RX_COMPLETE in the callback. In case the master continues to write more data, an I2C_SLAVE_EVENT_RX_MORE_REQUEST will be issued via callback. In case of errors, an I2C_SLAVE_EVENT_ABORTED will be issued via callback.
- Return values
-
FSP_SUCCESS | Function executed without issue |
FSP_ERR_ASSERTION | p_api_ctrl, bytes or p_dest is NULL. |
FSP_ERR_IN_USE | Another transfer was in progress. |
FSP_ERR_NOT_OPEN | Device is not open. |
◆ R_IICA_SLAVE_Write()
Performs a write to the IICA Master device.
This function will fail if there is already an in-progress IICA transfer on the associated channel. Otherwise, the IICA slave write operation will begin. The caller will be notified when the operation has finished by an I2C_SLAVE_EVENT_TX_COMPLETE in the callback. In case the master continues to read more data, an I2C_SLAVE_EVENT_TX_MORE_REQUEST will be issued via callback. In case of errors, an I2C_SLAVE_EVENT_ABORTED will be issued via callback.
- Return values
-
FSP_SUCCESS | Function executed without issue. |
FSP_ERR_ASSERTION | p_api_ctrl or p_src is NULL. |
FSP_ERR_IN_USE | Another transfer was in progress. |
FSP_ERR_NOT_OPEN | Device is not open. |
◆ R_IICA_SLAVE_CallbackSet()
Updates the user callback and has option of providing memory for callback structure. Implements i2c_slave_api_t::callbackSet
- Return values
-
FSP_SUCCESS | Callback updated successfully. |
FSP_ERR_ASSERTION | A required pointer is NULL. |
FSP_ERR_NOT_OPEN | The control block has not been opened. |
◆ R_IICA_SLAVE_Close()
Closes the IICA device.
- Return values
-
FSP_SUCCESS | Device closed successfully. |
FSP_ERR_NOT_OPEN | Device not opened. |
FSP_ERR_ASSERTION | p_api_ctrl is NULL. |