Main Content

SPI Receive

Receive data through Serial Peripheral Interface (SPI) on target

Since R2023a

Add-On Required: This feature requires the Embedded Coder Support Package for STMicroelectronics STM32 Processors add-on.

  • SPI Receive

Libraries:
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32F2xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32F3xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32F4xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32F7xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32G4xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32H7xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32L4xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32L5xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32U5xx Based Boards
Embedded Coder Support Package for STMicroelectronics STM32 Processors / STM32WBxx Based Boards

Description

The SPI Receive block outputs the values received as an [Nx1] array of the uint8, uint16 or uint32 data types.

The SPI Receive block supports synchronous, serial peripheral input/output port communications between the processor and external peripherals or other controllers. The block can run in either peripheral or controller mode. In controller mode, the SPI_MOSI pin transmits data, and the SPI_MISO pin receives the data. When controller mode is selected, the SPI initiates the data transfer by sending a serial clock signal (SPICLK), which is used for the entire serial communications link. Data transfers are synchronized to this SPICLK, which enables both controller and peripheral to send and receive data simultaneously. The maximum frequency for the clock is one quarter of the processor clock frequency.

The SPI device receives data and places the data in the receive buffer. The SPI Receive block reads the data from the receive buffer. In controller mode, SPI Transmit the block initiates SPI transmission by writing data to the transmit buffer. Then, the data received in the receive buffer is read by the SPI Receive block. In peripheral mode, the SPI Receive block is used to read the data in the receive buffer, which is received from the controller. Then, the data is written into the transmit buffer using the SPI Transmit block. From the transmit buffer, the data is sent to the controller.

The SPI receive interrupt is configured for a specific hardware board by navigating to Configuration Parameters > Hardware Implementation > Target hardware resources.

SPI Module is configured in STM32CubeMX project.

Verify that these settings meet the requirements of your application.

Note

The following are the limitations for SPI block:

  • Number of bytes of data to be sent to the transmit block should be less than the FIFO length.

  • On running SPI Controller Transfer block in Monitor & Tune, you might observe the junk data. To resolve, reconnect and start Monitor & Tune.

  • If SPI Controller Transfer block is used in a model don’t use the SPI Transmit and SPI Receive blocks for the same module.

  • Avoid using same modules of SPI in multiple tasks, which can lead to issues.

  • SPI blocks do not support Master Receive Only, Slave Receive Only, Master Transmit Only, and Slave Transmit only for SPI mode configuration in STM32CubeMX file.

Examples

Ports

Output

expand all

The data read from the device over the SPI interface.

Data Types: uint8 | uint16 | uint32

Status of receipt of data. Error status values indicate:

  • 0 — No errors.

  • 1 — Data loss occurred because of overflow.

  • 2 — Data not ready. A time out occurred while the block was waiting to receive data.

Dependencies

To enable this port, select Enable status output parameter.

Data Types: uint8 | uint16 | uint32

Parameters

expand all

Main

The SPI module to which the SPI peripheral device is connected. Each processor has a different number of modules.

The clock polarity used for SPI communication mode. This parameter must be the same for both transmit and receive blocks.

The clock phase used for SPI communication mode. This parameter must be the same for both transmit and receive blocks.

The received data is a vector of type uint8, uint16 or uint32 and the data length is as specified in this parameter (not bytes).

When you select the Enable status output parameter, the block configures an output port, Status. The port outputs the SPI status message.

When this option is selected, the algorithm waits until data is received before continuing processing.

Sample time for the block in seconds. To execute this block asynchronously, set this parameter to -1.

Advanced

Length in bits of each transmitted or received character. For example, if you select 8, the maximum value that can be transmitted using SPI is 28–1. If you send data greater than this value, the buffer overflows. This parameter must be the same for both transmit and receive blocks.

The SPI controller uses the following methods to select SPI peripheral devices:

  • Provided by the SPI peripheral — The SPI controller uses the pin provided by the SPI peripheral. Peripheral select and deselect are handled by the SPI peripheral.

  • Explicit GPIO calls — The SPI controller uses the general purpose input/output pins instead of the STE pin of the SPI peripheral to select/deselect SPI peripheral devices. The SPI Receive block deselects the peripheral using GPIO pins after receiving data. To select the peripheral, the SPI Transmit block must be used along with the SPI Receive block. Use this option only in controller mode. Select the Enable blocking mode option to ensure that the SPI transmission is complete before the peripheral is deselected.

The logic levels supported by the chip select pin to select the SPI peripheral device.

  • Active low — The device is enabled on logic low. The SPI peripheral device is enabled when its chip select pin is set to low.

  • Active high — The device is enabled on logic high. The SPI peripheral device is enabled when its chip select pin is set to high.

Dependencies

This option appears only when Chip select calling method is set to Explicit GPIO calls.

Select the general purpose port name that serves as the chip select for SPI.

Dependencies

This option appears only when Chip select calling method is set to Explicit GPIO calls.

The general purpose input/output pin that serves as the peripheral select for SPI.

Dependencies

This option appears only when Chip select calling method is set to Explicit GPIO calls.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Version History

Introduced in R2023a