The following build time configurations are defined in fsp_cfg/r_iic_slave_cfg.h:
This module can be added to the Stacks tab via New Stack > Connectivity > I2C Slave (r_iic_slave).
Configuration | Options | Default | Description |
Interrupt Priority Level > Transmit, Receive, and Transmit End | MCU Specific Options | | Select the interrupt priority level. This is set for TXI, RXI, and TEI interrupts. |
Interrupt Priority Level > Error | MCU Specific Options | | Select the interrupt priority level. This is set for ERI interrupt. |
Name | Name must be a valid C symbol | g_i2c_slave0 | Module name. |
Channel | Value must be a non-negative integer | 0 | Specify the IIC channel. |
Rate |
| Standard | Select the transfer rate.
If the delay for 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 delay is printed in a comment in the generated iic_slave_extended_cfg_t structure. |
Internal Reference Clock |
-
PCLKL / 1
-
PCLKL / 2
-
PCLKL / 4
-
PCLKL / 8
-
PCLKL / 16
-
PCLKL / 32
-
PCLKL / 64
-
PCLKL / 128
| PCLKL / 1 | Select the internal reference clock for IIC slave. The internal reference clock is used only to determine the clock frequency of the noise filter samples. |
Digital Noise Filter Stage Select |
-
Disabled
-
Single-stage filter
-
2-stage filter
-
3-stage filter
-
4-stage filter
| 3-stage filter | Select the number of digital filter stages for IIC Slave. |
Slave Address | Value must be non-negative | 0x08 | Specify the slave address. |
General Call |
| Disabled | Allows the slave to respond to general call address: 0x00. |
Address Mode |
| 7-Bit | Select the slave address mode. |
Clock Stretching |
| Disabled | Configure Clock Stretching. |
Callback | Name must be a valid C symbol | NULL | A user callback function must be provided. This will be called from the interrupt service routine (ISR) to report I2C Slave transaction events and status. |
The IIC peripheral module uses the PCLKL 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 PCLKL is configured in such a manner that the selected transfer rate cannot be achieved, an error will be returned.
The IIC 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 I2C channel would consist of two pins - SDA and SCL for data/address and clock respectively.
This is a basic example of minimal use of the R_IIC_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,
.ipl = 12,
.rxi_irq = VECTOR_NUMBER_IIC2_RXI,
.txi_irq = VECTOR_NUMBER_IIC2_TXI,
.tei_irq = VECTOR_NUMBER_IIC2_TEI,
.eri_irq = VECTOR_NUMBER_IIC2_EEI,
.p_callback = i2c_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
};
iic_slave_instance_ctrl_t g_i2c_slave_ctrl;
{
.slave = I2C_7BIT_ADDR_IIC_SLAVE,
.ipl = 12,
.eri_ipl = 12,
.rxi_irq = VECTOR_NUMBER_IIC1_RXI,
.txi_irq = VECTOR_NUMBER_IIC1_TXI,
.tei_irq = VECTOR_NUMBER_IIC1_TEI,
.eri_irq = VECTOR_NUMBER_IIC1_EEI,
.p_callback = i2c_slave_callback,
.p_context = &g_i2c_slave_ctrl,
.clock_stretching_enable = false,
.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_IIC_SLAVE_Read(&g_i2c_slave_ctrl, g_i2c_slave_buffer, g_slave_transfer_length);
handle_error(err);
}
{
err =
R_IIC_SLAVE_Write(&g_i2c_slave_ctrl, g_i2c_slave_buffer, g_slave_transfer_length);
handle_error(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;
handle_error(err);
handle_error(err);
for (i = 0; i < I2C_BUFFER_SIZE_BYTES; i++)
{
g_i2c_master_tx_buffer[i] = (uint8_t) i;
}
err =
R_IIC_MASTER_Write(&g_i2c_master_ctrl, &g_i2c_master_tx_buffer[0], I2C_BUFFER_SIZE_BYTES,
false);
handle_error(err);
{
timeout_ms--;
}
{
__BKPT(0);
}
timeout_ms = I2C_TRANSACTION_BUSY_DELAY;
err =
R_IIC_MASTER_Read(&g_i2c_master_ctrl, &g_i2c_master_rx_buffer[0], I2C_BUFFER_SIZE_BYTES,
false);
handle_error(err);
{
timeout_ms--;
}
{
__BKPT(0);
}
if (0U != memcmp(g_i2c_master_tx_buffer, g_i2c_master_rx_buffer, I2C_BUFFER_SIZE_BYTES))
{
__BKPT(0);
}
}