CMSIS-Driver_STM32  
CMSIS Drivers for STMicroelectronics STM32 Series devices
 
Loading...
Searching...
No Matches
SPI

Revision History

  • Version 3.0
    • Initial release

Requirements

This driver requires the STM32 device specific HAL and STM32CubeMX (CubeMX) initialization code generator. The driver instance is mapped to hardware as shown in the table below:

CMSIS Driver Instance STM32 Hardware Resource
Driver_SPI1 SPI1
Driver_SPI2 SPI2
Driver_SPI3 SPI3
Driver_SPI4 SPI4
Driver_SPI5 SPI5
Driver_SPI6 SPI6
Driver_SPI7 SPI7
Driver_SPI8 SPI8

Deviations

This driver has the following deviations from the CMSIS-Driver specification:

Conceptual deviations:

  • CubeMX generated initialization code (function MX_SPIn_Init) already configures the peripheral. Power, clocks, pins, and interrupts are enabled after execution of initialization that executes in main.c.

Functional deviations:

  • Initialize:
    • depends on the code generated by CubeMX.
    • does not initialize the pins.
  • Uninitialize:
    • does not de-initialize the pins.
  • PowerControl:
    • depends on the code generated by CubeMX.
    • low-power mode is not supported by HAL.
  • Send:
    • maximum number of data items supported by HAL with a single send request is 65535.
  • Receive:
    • maximum number of data items supported by HAL with a single receive request is 65535.
  • Transfer:
    • maximum number of data items supported by HAL with a single transfer request is 65535.
  • Control:
    • changes are not effective after this function but when data transfer operation is started (for example: changing Phase or Polarity will change clock line state when data operation is started and not after this function finishes).
  • GetStatus:
    • mode fault and data lost cannot be detected until data transfer operation is started.
  • SignalEvent:
    • mode fault and data lost detection does not work until data transfer operation is started.

CubeMX Configuration

This driver requires the following configuration in CubeMX:

  • clock: SPI peripheral clock.
  • peripheral: SPI peripheral configured as Full-Duplex Master/Slave and Parameter Setting configured as desired.
  • pins: SPI MISO, MOSI, SCK and NSS pins.
  • DMA: optional DMA configuration for transfers.
  • interrupts: enabled SPI interrupts and IRQ handlers that Call HAL handlers and enabled DMA interrupts and IRQ handlers that Call HAL handlers if DMA is used.

Notes

  • configuration information in the MX_Device.h file is based on CubeMX configuration.
  • Hardware or Software Slave Select functionality is only available if NSS pin is configured in the CubeMX with alternate function Hardware NSS Signal.
  • DMA configuration can differ between devices series so configure DMA as required by the used device.
  • for DMA usage on devices with cache, ensure that data buffers for Send and Receive functions are in non-cacheable memory, or ensure that memory for send is updated (cache clean) before Send function is called and that memory containing received data is updated after the reception finishes (cache invalidate).
  • some DMA controllers can only access specific memories, so ensure that proper memory is used for the buffers according to the DMA requirement.

Example

Pinout & Configuration tab

  1. In the Pinout view window click on a pin and select it's functionality:
    Pin Functionality
    PF6 SPI5_NSS
    PF7 SPI5_SCK
    PF8 SPI5_MISO
    PF9 SPI5_MOSI

  2. Under Categories: Connectivity select SPI5:

    Mode:

    • Mode: Full-Duplex Master
    • Hardware NSS Signal: Hardware NSS Output Signal

    Configuration:

    • Parameter Settings: as desired
    • GPIO Settings:
      Pin Name Signal on Pin Pin Context.. GPIO output.. GPIO mode GPIO Pull-up/Pull.. Maximum out.. Fast Mode User Label
      PF6 SPI5_NSS n/a n/a Alternate Function Push Pull No pull-up and no.. Medium n/a .
      PF7 SPI5_SCK n/a n/a Alternate Function Push Pull No pull-up and no.. Medium n/a .
      PF8 SPI5_MISO n/a n/a Alternate Function Push Pull No pull-up and no.. Medium n/a .
      PF9 SPI5_MOSI n/a n/a Alternate Function Push Pull No pull-up and no.. Medium n/a .

  3. Under Categories: System Core select DMA (might be different on other device series, or for some peripherals might be BDMA):

    Configuration:

    • DMA1, DMA2:

      Click on Add button and add DMA Requests as in table below:

      DMA Request Stream Direction Priority
      SPI5_RX DMA1 Stream 0 Peripheral to Memory Low
      SPI5_TX DMA1 Stream 1 Memory to Peripheral Low


  4. Under Categories: System Core select NVIC:

    Configuration:

    • NVIC:
      NVIC Interrupt Table Enabled Preemption Priority Sub Priority
      DMA1 stream0 global interrupt checked 0 0
      DMA1 stream1 global interrupt checked 0 0
      SPI5 global interrupt checked 0 0

    • Code generation:
      Enabled interrupt table Select for.. Generate Enable in.. Generate IRQ h.. Call HAL handler
      DMA1 stream0 global interrupt unchecked checked checked checked
      DMA1 stream1 global interrupt unchecked checked checked checked
      SPI5 global interrupt unchecked checked checked checked

Clock Configuration tab

  1. Configure To SPI4,5 (MHz): 100

Project Manager tab

  1. Under Advanced Settings:

    Generated Function Calls:

    Generate Code Function Name Peripheral Inst.. Do not generate .. Visibility (Static)
    checked MX_SPI5_Init SPI5 unchecked checked

Troubleshooting

Compilation errors

Error: SPIn peripheral clock frequency is unknown!

  • Reason: peripheral clock frequency is not determinable from CubeMX configuration.
  • Solution: add User Constant with Name SPIn_PERIPH_CLOCK_FREQ and Value of respective SPIn peripheral clock frequency (in Hz).
  • Example: in the Pinout & Configuration tab, under Categories: Connectivity select SPI1, under Configuration: User Constants add Constant with Name SPI1_PERIPH_CLOCK_FREQ and Value of peripheral clock frequency (in Hz) as determined from the Clock Configuration or device documentation.