HAL Library 33- DMA extension for SPI on STM32Fxxx

SPI DMA library allows you to send and receive data via SPI in non-blocking mode. It can be use for TX only, RX only or both (transmissing mode).

This library is extension for my SPI library. It requires SPI library as basic to make this to work.

Library

Read more about HAL libraries

Features

  • SPI extension for non-blocking TX, RX or both modes
  • Supports all SPI peripherals on STM32F4 and STM32F7 device
  • Supports interrupt processing
  • Allows you to start DMA transmissing and wait till done
    • This is handy when working with RTOS for example and when you have to wait transmissing

Dependencies

  • HAL
  • TM
    • STM32Fxxx HAL
    • defines.h configuration file
    • TM DMA
    • TM SPI

Pinout, functions and enumerations

Pinout, list of functions and its enumerations can be found on SPI library API and SPI DMA library API.

Example

SPI DMA example is available on my Github account.

Icon
TM STM32 Libraries

STM32 libraries based on STM32Fxxx HAL drivers.

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Imran

    Hi there!
    I am using STM32F7 Discovery board (STM32F746NGH6 to be exact). I downloaded your libraries and example code. I was particularly interested in SPI with DMA extension. So I loaded your example no. 33 titled 33-STM32Fxxx_DMA_SPI. I selected STM32F7_DISCOVERY as the target and ran your program without any modification. I shorted the MOSI and MISO pins which are Pin no. 4 and 5 on Arduino header for the sample given. But everytime I run the project I get “SPI Memory Fail” on the serial port. So I tried debugging it and by setting a breakpoint at
    if (memcmp(SPI_TX, SPI_RX, TX_SIZE) == 0)

    I can see that SPI_TX contains the valid data but SPI_RX is all zeroes.

    So I hook up an oscilloscope to the pins. I can see the clock on the SPI2_SCK pin which is pin no. 6 on the Arduino header. But when I hook the scope on the MOSI pin I get nothing. i.e no pulse or anything. Then I removed the connection between MOSI and MISO and then connected MISO to 3.3V so that it is always high. Then I ran the program but again at the breakpoint SPI_RX is all zeroes. Can you help me what am I doing wrong? the only modification that I did to your program was to Define USART as USART6 for it to be used with the Serial Port on the Arduino Header.
    Imran
    Islamabad, Pakistan.

    • Did you short MISO and MOSI pins? You have to read something, in example you read directly MOSI output on MISO input. That’s the idea.

      • Imran

        Thanks for the reply….. Yes this is the first thing that I did…. I connected the MISO and MOSI pins together. When I set breakpoint at

        if (memcmp(SPI_TX, SPI_RX, TX_SIZE) == 0)

        I can see that SPI_TX contains the valid data but SPI_RX is all zeroes.

        So for the example titled 33-STM32Fxxx_DMA_SPI I donot get any reply at all. In fact the code never outputs anything on the MOSI line (Verified by scope). The clock is outputed on the SPI2_SCK pin though.

        When I tried your Library No. 8 titled 08-STM32Fxxx_SPI I get the following response

        Transmit (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E)

        and in the receive I get

        Transmit (0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B)

        So again I don’t receive what I transmitted. But at least I get something. For the DMA extension for SPI I don’t get anything back.

        • Faisal Khan

          I am facing the same problem. In example 33 no data is received in Rx buffer. Plz help.

  • Imran

    So no further update about this problem?

    • I don’t see a reson for your problem. I would see a problem is:
      1. DMA wouldn’t start at all
      2. Data were not even sent.

      Your problem is totally something else. I have no idea right now what is.

      My test show again that sent data were also received back.
      Check if you have hardware problems, maybe anything connected to your SPI pins.

      • Imran

        Thanks for the reply…. I don’t have any problem with my hardware… If I try the code provided by DFP( Device Family Pack) version 2.8 labeled “SPI_FullDuplex_ComDMA” provided by Keil I get the data back perfectly fine. Even with your example 08-STM32Fxxx_SPI (with slight modification i.e using TM_SPI_SendMulti(SPI2,Transmit,Receive,15); instead of Receive[i] = TM_SPI_Send(SPI2, Transmit[i]);
        I get back data perfectly fine. So the only problem is with the DMA extension example for STM32F7 devices.

        I am pretty sure that the data on MOSI line is never sent in your example. I have hooked Oscilloscope and can confirm that there is absolutely no activity on the MOSI line . Do I need to change some setting in the code for it to work? I have made absolutely no change to your code and am running it as it is….

        So when you say that in your test you receive back the data did you try it with STM32F7 discovery board?

        Also I would like to see if this is some problem with the HAL Libraries? I saw that in the changelog of DFP 2.8.0 Following is mentioned.

        Version: 2.8.0
        Keil.STM32F7xx_DFP.2.8.0.pack

        Updated to STM32Cube_FW_F7 Firmware Package version V1.5.1

        Updated RTE_Device config file:
        Added “Not Used” support for SPI MOSI/MISO pins and USART TX/RX pins.
        Added SDMMC2 and CAN pin configurations

        Updated CMSIS drivers:

        SPI:
        Added “Not Used” support for MISO and MOSI pins
        Corrected transfer problem, when DMA is used

        So there is mention of some problem with DMA and SPI in older versions?

        • Copy/paste content from link below to file.hex, use ST-Link utility and upload program.

          https://raw.githubusercontent.com/MaJerle/stm32fxxx_hal_libraries/master/33-STM32Fxxx_DMA_SPI/Targets/STM32F7_DISCOVERY/project.hex

          Report what is received on UART status.
          SPI2, PinsPack4 (My SPI lib for HAL for pin reference) is used.

          • Imran

            So I downloaded ST Link utility and downloaded the hex file… But I can’t see anything on the com port. What baud rate, pinpack and UART are you using for the UART? If you are using the code from example 33-STM32Fxxx_DMA_SPI then one need to change the following in the code for the UART to work.

            #define USART USART6 /* USART 6 on STM32F7-Discovery Arduino header */

            Also the initialization should be

            TM_USART_Init(USART, TM_USART_PinsPack_1, 115200);

            for it to work while communicating with the PC.

          • Download it again. 921600 bauds.
            Don’t know why initialization should be what you wrote but anyway. Who knows.

          • Imran

            But my PC don’t support 921600 baud rate… It supports 115200 max baud rate… The reason of why using USART6 is because it is on the Arduino header….

          • What are you talking? Why it would go higher than 115200?

          • Imran

            The Port on PC side doesn’t support baud rate of 921600. If you initialize the UART at 115200 using
            TM_USART_Init(USART, TM_USART_PinsPack_1, 115200);
            then I will be able to get the data on the PC…. At the baud rate of 921600 the STM32F7 is outputting data but my PC can’t read it….

          • Imran, please don’t talk stupid things. PC works via USB so real issue in speed is USB -> UART converter, in your case ST-Link on Discovery board and it can handle that speed.

          • Imran

            But I was not using the ST-Link Serial port…. I am using the UART6 that is on the Arduino header of the discovery board. I have hooked it to my PC serial port. This way I can only use max. baud rate of 115200.

          • Is there a reason why are you screwing with me? Use VCP UART!
            I’ve added back to original so grab latest project.hex version from Github (link above) and try again at 921600 bauds! Or don’t, it is up to you.

          • Imran

            I don’t know what I have done to offend you. All the terminal applications have limitation of maximum baud rate of 115200. So I downloaded the old and tested hyper terminal and it let me set the baud rate to 921600. Here is what I get on the serial port…

            This is SPI DMA demo. It sends data on MOSI and reads the same data on MISO.
            Connect MOSI on MISO pins together for testing.
            DMA received data!
            SPI memory FAIL

          • Imran

            Hi Tilen,
            There is no response from your side about this problem now for 7 days? At least you can tell me what could be wrong? Is it the HAL libraries or your wrapper on top of those libraries that is screwing up the SPI-DMA code or there is something wrong with the example. I have already posted the reply that you asked on the serial port. Anyways here it is again.
            This is SPI DMA demo. It sends data on MOSI and reads the same data on MISO.
            Connect MOSI on MISO pins together for testing.
            DMA received data!
            SPI memory FAIL

          • Now the fun fact since I updated HAL drivers.
            for example, 15 bytes should be clocked out and they are clocked only SCK pin.

            Actual data on SPI register does not seem to work, data from memory is not transferred to SPI->DR register at all.

            Now discoverying starts 😉

          • Imran

            OK… Will be waiting for the updates.

          • Imran

            Now I am getting some garbage data on the Com Port because of 921600 baud rate. The PC don’t support such high baud rate. Can you trim it down to 115200?