|
fsp_err_t | R_SCI_B_SPI_Open (spi_ctrl_t *p_api_ctrl, spi_cfg_t const *const p_cfg) |
|
fsp_err_t | R_SCI_B_SPI_Read (spi_ctrl_t *const p_api_ctrl, void *p_dest, uint32_t const length, spi_bit_width_t const bit_width) |
|
fsp_err_t | R_SCI_B_SPI_Write (spi_ctrl_t *const p_api_ctrl, void const *p_src, uint32_t const length, spi_bit_width_t const bit_width) |
|
fsp_err_t | R_SCI_B_SPI_WriteRead (spi_ctrl_t *const p_api_ctrl, void const *p_src, void *p_dest, uint32_t const length, spi_bit_width_t const bit_width) |
|
fsp_err_t | R_SCI_B_SPI_CallbackSet (spi_ctrl_t *const p_api_ctrl, void(*p_callback)(spi_callback_args_t *), void const *const p_context, spi_callback_args_t *const p_callback_memory) |
|
fsp_err_t | R_SCI_B_SPI_Close (spi_ctrl_t *const p_api_ctrl) |
|
fsp_err_t | R_SCI_B_SPI_CalculateBitrate (uint32_t bitrate, sci_b_spi_clock_source_t clock_source, sci_b_spi_div_setting_t *sclk_div) |
|
Driver for the SCI peripheral on RZ MPUs. This module implements the SPI Interface.
Overview
Features
- Standard SPI Modes
- Master or Slave Mode
- Clock Polarity (CPOL)
- CPOL=0 SCLK is low when idle
- CPOL=1 SCLK is high when idle
- Clock Phase (CPHA)
- CPHA=0 Select data sampling on leading edge, data change on trailing edge
- CPHA=1 Select data change on leading edge, data sampling on trailing edge
- MSB/LSB first
- Configurable bit rate
- DMAC Support
- Callback Events
- Transfer Complete
- RX Overflow Error (The SCI shift register is copied to the data register before previous data was read)
Configuration
Build Time Configurations for r_sci_b_spi
The following build time configurations are defined in fsp_cfg/r_sci_b_spi_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. |
DMAC Support |
| Enabled | If support for transferring data using the DMAC will be compiled in. |
Configurations for Connectivity > SPI (r_sci_b_spi)
This module can be added to the Stacks tab via New Stack > Connectivity > SPI (r_sci_b_spi). Non-secure callable guard functions can be generated for this module by right clicking the module in the RA Configuration tool and checking the "Non-secure Callable" box.
Configuration | Options | Default | Description |
Name | Name must be a valid C symbol | g_sci_spi0 | Module name. |
Channel | Value must be a non-negative integer | 0 | Select the SCI channel. |
Operating Mode |
| Master | Select the SPI operating mode. |
Clock Phase |
-
Data sampling on odd edge, data variation on even edge (CPHA=0)
-
Data sampling on even edge, data variation on odd edge (CPHA=1)
| Data sampling on even edge, data variation on odd edge (CPHA=1) | Select the clock edge to sample data. |
Clock Polarity |
-
Low when idle (CPOL=0)
-
High when idle (CPOL=1)
| High when idle (CPOL=1) | Select clock level when idle. |
Bit Order |
| MSB First | Select the data bit order. |
Callback | Name must be a valid C symbol | sci_b_spi_callback | A user callback function that is called from the sci_b_spi interrupts when a transfer is completed or an error has occurred. |
Receive Interrupt Priority | Value must be a non-negative integer | 12 | Set the receive interrupt priority. |
Transmit Interrupt Priority | Value must be a non-negative integer | 12 | Set the transmit interrupt priority. |
Transmit End Interrupt Priority | Value must be a non-negative integer | 12 | Set the transmit end interrupt priority. |
Error Interrupt Priority | Value must be a non-negative integer | 12 | Set the error interrupt priority. |
Bitrate | Value must be an integer greater than 0 | 2500000 | Enter the desired bitrate.
If the requested bitrate cannot be achieved, the settings with the largest possible value that is less than or equal to the requested bitrate is used. The theoretical bitrate is printed in a comment in the generated sci_spi_extended_cfg_t structure. |
Clock Configuration
The clock for this module is derived from the following peripheral clock for each MCU group:
MCU Group | Peripheral Clock |
RZV2H | P5CLK |
RZV2N | P5CLK |
Pin Configuration
This module uses SCIn_MOSI, SCIn_MISO, SCIn_SPCK, and SCIn_SS pins to communicate with on board devices.
- Note
- At high bit rates, it might be necessary to configure the pins with IOPORT_CFG_DRIVE_HIGH.
Usage Notes
Transfer Complete Event
The transfer complete event is triggered when all of the data has been transfered. In slave mode if the SS pin is de-asserted then no transfer complete event is generated until the SS pin is asserted and the remaining data is transferred.
Performance
At high bit rates, interrupts may not be able to service transfers fast enough. In master mode this means there will be a delay between each data frame. In slave mode this could result in RX Overflow errors.
In order to improve performance at high bit rates, it is recommended that the instance be configured to service transfers using the DMAC.
Transmit From RXI Interrupt
After every byte, the SCI SPI peripheral generates a transmit buffer empty interrupt and a receive buffer full interrupt. Whenever possible, the SCI SPI module handles both interrupts in the receive buffer full interrupt.
Slave Select Pin
- In master mode the slave select pin must be driven in software.
- In slave mode the hardware handles the slave select pin and will only transfer data when the SS pin is low.
Examples
Basic Example
This is a basic example of minimal use of the SCI_B_SPI module in an application.
static volatile bool g_transfer_complete = false;
{
{
g_transfer_complete = true;
}
}
void sci_b_spi_basic_example (void)
{
uint8_t tx_buffer[TRANSFER_SIZE];
uint8_t rx_buffer[TRANSFER_SIZE];
assert(FSP_SUCCESS == err);
g_transfer_complete = false;
assert(FSP_SUCCESS == err);
while (false == g_transfer_complete)
{
;
}
g_transfer_complete = false;
assert(FSP_SUCCESS == err);
while (false == g_transfer_complete)
{
;
}
}
◆ sci_b_spi_div_setting_t
struct sci_b_spi_div_setting_t |
Settings for adjusting the SPI CLK.
◆ R_SCI_B_SPI_Open()
Initialize a channel for SPI communication mode. Implements spi_api_t::open.
This function performs the following tasks:
- Performs parameter checking and processes error conditions.
- Enables the clock for the SCI channel.
- Initializes the associated registers with default value and the user-configurable options.
- Provides the channel handle for use with other API functions.
- Parameters
-
p_api_ctrl | Pointer to the control structure. |
p_cfg | Pointer to a configuration structure. |
- Return values
-
FSP_SUCCESS | Channel initialized successfully. |
FSP_ERR_ASSERTION | An input parameter is invalid or NULL. |
FSP_ERR_ALREADY_OPEN | The instance has already been opened. |
FSP_ERR_IP_CHANNEL_NOT_PRESENT | The channel number is invalid. |
◆ R_SCI_B_SPI_Read()
Receive data from an SPI device. Implements spi_api_t::read.
The function performs the following tasks:
- Performs parameter checking and processes error conditions.
- Enable transmitter.
- Enable receiver.
- Enable interrupts.
- Start data transmission by writing data to the TXD register.
- Receive data from receive buffer full interrupt occurs and copy data to the buffer of destination.
- Complete data reception via receive buffer full interrupt and transmitting dummy data.
- Disable transmitter.
- Disable receiver.
- Disable interrupts.
- Parameters
-
| p_api_ctrl | Pointer to the control structure. |
| p_dest | Pointer to the destination buffer. |
[in] | length | The number of bytes to transfer. |
[in] | bit_width | Invalid for SCI_B_SPI (Set to SPI_BIT_WIDTH_8_BITS). |
- Return values
-
FSP_SUCCESS | Read operation successfully completed. |
FSP_ERR_ASSERTION | One of the following invalid parameters passed:
- Pointer p_api_ctrl is NULL
- Bit width is not 8 bits
- Length is equal to 0
- Pointer to destination is NULL
|
FSP_ERR_NOT_OPEN | The channel has not been opened. Open the channel first. |
FSP_ERR_UNSUPPORTED | The given bit_width is not supported. |
FSP_ERR_IN_USE | A transfer is already in progress. |
- Returns
- See Common Error Codes or functions called by this function for other possible return codes. This function calls:
◆ R_SCI_B_SPI_Write()
Transmit data to a SPI device. Implements spi_api_t::write.
The function performs the following tasks:
- Performs parameter checking and processes error conditions.
- Enable transmitter.
- Enable interrupts.
- Start data transmission with data via transmit buffer empty interrupt.
- Copy data from source buffer to the SPI data register for transmission.
- Complete data transmission via transmit buffer empty interrupt.
- Disable transmitter.
- Disable receiver.
- Disable interrupts.
- Parameters
-
| p_api_ctrl | Pointer to the control structure. |
| p_src | Pointer to the source buffer. |
[in] | length | The number of bytes to transfer. |
[in] | bit_width | Invalid for SCI_B_SPI (Set to SPI_BIT_WIDTH_8_BITS). |
- Return values
-
FSP_SUCCESS | Write operation successfully completed. |
FSP_ERR_ASSERTION | One of the following invalid parameters passed:
- Pointer p_api_ctrl is NULL
- Pointer to source is NULL
- Length is equal to 0
- Bit width is not equal to 8 bits
|
FSP_ERR_NOT_OPEN | The channel has not been opened. Open the channel first. |
FSP_ERR_UNSUPPORTED | The given bit_width is not supported. |
FSP_ERR_IN_USE | A transfer is already in progress. |
- Returns
- See Common Error Codes or functions called by this function for other possible return codes. This function calls:
◆ R_SCI_B_SPI_WriteRead()
Simultaneously transmit data to SPI device while receiving data from SPI device (full duplex). Implements spi_api_t::writeRead.
The function performs the following tasks:
- Performs parameter checking and processes error conditions.
- Enable transmitter.
- Enable receiver.
- Enable interrupts.
- Start data transmission using transmit buffer empty interrupt (or by writing to the TDR register).
- Copy data from source buffer to the SPI data register for transmission.
- Receive data from receive buffer full interrupt and copy data to the destination buffer.
- Complete data transmission and reception via transmit end interrupt.
- Disable transmitter.
- Disable receiver.
- Disable interrupts.
- Parameters
-
| p_api_ctrl | Pointer to the control structure. |
| p_src | Pointer to the source buffer. |
| p_dest | Pointer to the destination buffer. |
[in] | length | The number of bytes to transfer. |
[in] | bit_width | Invalid for SCI_B_SPI (Set to SPI_BIT_WIDTH_8_BITS). |
- Return values
-
FSP_SUCCESS | Write operation successfully completed. |
FSP_ERR_ASSERTION | One of the following invalid parameters passed:
- Pointer p_api_ctrl is NULL
- Pointer to source is NULL
- Pointer to destination is NULL
- Length is equal to 0
- Bit width is not equal to 8 bits
|
FSP_ERR_NOT_OPEN | The channel has not been opened. Open the channel first. |
FSP_ERR_UNSUPPORTED | The given bit_width is not supported. |
FSP_ERR_IN_USE | A transfer is already in progress. |
- Returns
- See Common Error Codes or functions called by this function for other possible return codes. This function calls:
◆ R_SCI_B_SPI_CallbackSet()
Updates the user callback and has option of providing memory for callback structure. Implements spi_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. |
FSP_ERR_NO_CALLBACK_MEMORY | p_callback is non-secure and p_callback_memory is either secure or NULL. |
◆ R_SCI_B_SPI_Close()
Disable the SCI channel and set the instance as not open. Implements spi_api_t::close.
- Parameters
-
p_api_ctrl | Pointer to an opened instance. |
- Return values
-
FSP_SUCCESS | Channel successfully closed. |
FSP_ERR_ASSERTION | The parameter p_api_ctrl is NULL. |
FSP_ERR_NOT_OPEN | The channel has not been opened. Open the channel first. |
◆ R_SCI_B_SPI_CalculateBitrate()
Calculate the register settings required to achieve the desired bitrate.
- Parameters
-
[in] | bitrate | bitrate [bps]. For example, 250,000; 500,00; 2,500,000 (max), etc. |
| clock_source | clock source (PCLKA or SCISPICLK) used for bit rate calculation. |
| sclk_div | Pointer to sci_b_spi_div_setting_t used to configure baudrate settings. |
- Return values
-
FSP_SUCCESS | Baud rate is set successfully. |
FSP_ERR_ASSERTION | Baud rate is not achievable. |
FSP_ERR_INVALID_ARGUMENT | Clock source is invalid. |
- Note
- The application must pause for 1 bit time after the BRR register is loaded before transmitting/receiving to allow time for the clock to settle.