{"id":1947,"date":"2023-08-26T13:17:36","date_gmt":"2023-08-26T13:17:36","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1947"},"modified":"2023-08-26T13:17:38","modified_gmt":"2023-08-26T13:17:38","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part13-4-dma-peripheral-to-memory-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1947","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part13.4: DMA Peripheral to Memory Mode"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-1024x683.jpeg\" alt=\"\" class=\"wp-image-1948\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-5-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this fourth section of the thirteenth part of board support package, we shall use the BSP of DMA to transfer data from the peripheral to memory using SPI and MPU9250.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Configure the DMA.<\/li><li>Transfer data from MPU9250 to STM32.<\/li><li>MPU9250 connection.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Configure the DMA:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In order to transfer the data from MPU9250 to the STM32, SPI bus shall be used.<\/p>\n\n\n\n<p>In the third section (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1940\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1940\" target=\"_blank\">here<\/a>), we saw how to configure the DMA to transfer data from memory to SPI.<\/p>\n\n\n\n<p>To configure the DMA to transfer data from peripheral to memory, we shall set the direction to be peripheral to memory.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Since we require both TX and RX part, we shall use DMA2 Stream 2 and Stream 3 and channel 3 to handle the SPI communication<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"554\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-1024x554.png\" alt=\"\" class=\"wp-image-1949\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-1024x554.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-300x162.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-768x415.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-1536x830.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-2048x1107.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-1150x622.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-750x405.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-400x216.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-26-at-3.52.19-PM-250x135.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence, the DMA SPI Configuration as following:<\/p>\n\n\n\n<p>Declare DMA_ConfigTypedef struct for SPI_TX as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DMA_ConfigTypedef SPI_TX_DMA;<\/pre><\/div>\n\n\n\n<p>Same thing for SPI_RX as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DMA_ConfigTypedef SPI_RX_DMA;<\/pre><\/div>\n\n\n\n<p>Declare two volatile variables for the interrupts which will works as flags in our application :<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">volatile uint8_t tx_finished=0,rx_finished;<\/pre><\/div>\n\n\n\n<p>Declare two arrays of size 7 to handle the two way communication:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t SPI_TX[7]={0x3B|0x80,0x00,0x00,0x00,0x00,0x00,0x00};\n\nuint8_t SPI_RX[7];<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In main function:<\/p>\n\n\n\n<p>Configure the DMA_SPI_TX as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tSPI_TX_DMA.DMA_Channel=Channel3;\n\tSPI_TX_DMA.CircularMode=SingleTransfer;\n\tSPI_TX_DMA.MemorySize=byte;\n\tSPI_TX_DMA.PerpheralSize=byte;\n\tSPI_TX_DMA.MemoryIncMode=incMode;\n\tSPI_TX_DMA.PeripheralIncMode=SingleTransfer;\n\tSPI_TX_DMA.Direction=memory2Peripheral;\n\tSPI_TX_DMA.InterruptEnable=InterruptEnbaled;\n\tSPI_TX_DMA.TransferCompleteInterrupt=TransferComplet;\n\tSPI_TX_DMA.DMAStream=dma2stream3;\n\tBSP_DMA_Init(DMA2_Stream3, &amp;SPI_TX_DMA);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>For SPI_RX_DMA:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\tSPI_TX_DMA.DMA_Channel=Channel3;\n\tSPI_TX_DMA.CircularMode=SingleTransfer;\n\tSPI_TX_DMA.MemorySize=byte;\n\tSPI_TX_DMA.PerpheralSize=byte;\n\tSPI_TX_DMA.MemoryIncMode=incMode;\n\tSPI_TX_DMA.PeripheralIncMode=SingleTransfer;\n\tSPI_TX_DMA.Direction=memory2Peripheral;\n\tSPI_TX_DMA.InterruptEnable=InterruptEnbaled;\n\tSPI_TX_DMA.TransferCompleteInterrupt=TransferComplet;\n\tSPI_TX_DMA.DMAStream=dma2stream3;\n\tBSP_DMA_Init(DMA2_Stream3, &amp;SPI_TX_DMA);<\/pre><\/div>\n\n\n\n<p>They are identical except for the direction, the TX is memory to peripheral and RX peripheral to memory.<\/p>\n\n\n\n<p>For interrupt handler:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void DMA2_Stream2_handler(void)\n{\n\tif((DMA2-&gt;LISR &amp; DMA_LISR_TCIF2)==DMA_LISR_TCIF2)\n\t{\n\t\trx_finished=1;\n\t}\n\n}\n\n\nvoid DMA2_Stream3_handler(void)\n{\n\tif((DMA2-&gt;LISR &amp; DMA_LISR_TCIF3)==DMA_LISR_TCIF3)\n\t{\n\t\ttx_finished=1;\n\t}\n\n}<\/pre><\/div>\n\n\n\n<p>Thats all for the DMA configuration.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Transfer Data from MPU9250 to STM32:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In while loop, we shall first prepare the RX buffer to hold the data to be received as following:<\/p>\n\n\n\n<p>Start with setting cs pin to low:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIO_WritePin(GPIOA,&amp;CS_Pin,Reset);<\/pre><\/div>\n\n\n\n<p>Prepare the rx buffer as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">BSP_DMA_Transfer_Single_Buffer(DMA2_Stream2, (uint32_t)&amp;SPI1-&gt;DR, (uint32_t)&amp;SPI_RX,7);<\/pre><\/div>\n\n\n\n<p>Transmit data over SPI:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">BSP_DMA_Transfer_Single_Buffer(DMA2_Stream3, (uint32_t)&amp;SPI1-&gt;DR,(uint32_t)&amp;SPI_TX,7);<\/pre><\/div>\n\n\n\n<p>Wait until the receive buffer is filled:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while(rx_finished ==0){;}\t<\/pre><\/div>\n\n\n\n<p>Reset the rx flag:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">rx_finished=0;<\/pre><\/div>\n\n\n\n<p>also, reset the tx flag:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">if(tx_finished==1)tx_finished=0;<\/pre><\/div>\n\n\n\n<p>Set the cs pin to high:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIO_WritePin(GPIOA,&amp;CS_Pin,Set);<\/pre><\/div>\n\n\n\n<p>Delay a little bit:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">BSP_Delay(200);<\/pre><\/div>\n\n\n\n<p>Hence, the main.c code as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;bsp.h&quot;\n#include &quot;uart_bsp.h&quot;\n#include &quot;exti_bsp.h&quot;\n#include &quot;bsp_debug.h&quot;\n#include &quot;spi_bsp.h&quot;\n#include &quot;dma_bsp.h&quot;\n\n#include &quot;stdlib.h&quot;\n\nvoid clock_config(void);\n\nvolatile uint8_t tx_finished=0,rx_finished;\n\n\nuint8_t SPI_TX[7]={0x3B|0x80,0x00,0x00,0x00,0x00,0x00,0x00};\n\nuint8_t SPI_RX[7];\n\n\nDMA_ConfigTypedef SPI_TX_DMA;\n\nDMA_ConfigTypedef SPI_RX_DMA;\n\n\n\nGPIO_Output_Typedef CS_Pin;\n\n\nint main()\n{\n\n\n\t#if FPU_EN\n\t\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n\t#endif\n\n\tclock_config();\n\tBSP_Ticks_Init(100000000);\n\n\tGPIO_Configure_Typedef PA5_SPI,PA6_SPI,PA7_SPI;\n\n\tGPIOA_CLOCK_ENABLE();\n\n\tPA5_SPI.PinNumber=5;\n\tPA5_SPI.Mode=Alternate_function;\n\tPA5_SPI.AlternateType=AF5;\n\tPA5_SPI.OutPutspeed=High_Speed;\n\n\n\tPA6_SPI.PinNumber=6;\n\tPA6_SPI.Mode=Alternate_function;\n\tPA6_SPI.AlternateType=AF5;\n\tPA6_SPI.OutPutspeed=High_Speed;\n\n\tPA7_SPI.PinNumber=7;\n\tPA7_SPI.Mode=Alternate_function;\n\tPA7_SPI.AlternateType=AF5;\n\tPA6_SPI.OutPutspeed=High_Speed;\n\n\tGPIO_Initialization(GPIOA,&amp;PA5_SPI);\n\tGPIO_Initialization(GPIOA,&amp;PA6_SPI);\n\tGPIO_Initialization(GPIOA,&amp;PA7_SPI);\n\n\n\tGPIO_Configure_Typedef CS_Pin_Config;\n\n\tCS_Pin_Config.PinNumber=pin0;\n\tCS_Pin_Config.Mode=OUTPUT;\n\tCS_Pin_Config.OutPutspeed=High_Speed;\n\n\tGPIO_Initialization(GPIOA,&amp;CS_Pin_Config);\n\n\tGPIO_WritePin(GPIOA,&amp;CS_Pin,Set);\n\n\n\tSPI_Enable_Clock(spi1);\n\n\tSPI_ConfigTypedef cSPI1;\n\n\tcSPI1.mode=Master_mode;\n\tcSPI1.slaveManagement=software_slave;\n\tcSPI1.baud=FCLK_32;\n\tcSPI1.CPHA=CPHA0;\n\tcSPI1.CPOL=CPOL0;\n\tcSPI1.LSBFirst=MSB_First;\n\tcSPI1.DMA_Enable=SPI_DMA_Enable;\n\n\tBSP_SPI_Config(SPI1,&amp;cSPI1);\n\n\n\t__BSP_DMA2__CLOCK_ENABLE();\n\n\tSPI_TX_DMA.DMA_Channel=Channel3;\n\tSPI_TX_DMA.CircularMode=SingleTransfer;\n\tSPI_TX_DMA.MemorySize=byte;\n\tSPI_TX_DMA.PerpheralSize=byte;\n\tSPI_TX_DMA.MemoryIncMode=incMode;\n\tSPI_TX_DMA.PeripheralIncMode=SingleTransfer;\n\tSPI_TX_DMA.Direction=memory2Peripheral;\n\tSPI_TX_DMA.InterruptEnable=InterruptEnbaled;\n\tSPI_TX_DMA.TransferCompleteInterrupt=TransferComplet;\n\tSPI_TX_DMA.DMAStream=dma2stream3;\n\tBSP_DMA_Init(DMA2_Stream3, &amp;SPI_TX_DMA);\n\n\n\tSPI_RX_DMA.DMA_Channel=Channel3;\n\tSPI_RX_DMA.CircularMode=SingleTransfer;\n\tSPI_RX_DMA.MemorySize=byte;\n\tSPI_RX_DMA.PerpheralSize=byte;\n\tSPI_RX_DMA.MemoryIncMode=incMode;\n\tSPI_RX_DMA.PeripheralIncMode=SingleTransfer;\n\tSPI_RX_DMA.Direction=peripheral2Memory;\n\tSPI_RX_DMA.InterruptEnable=InterruptEnbaled;\n\tSPI_RX_DMA.TransferCompleteInterrupt=TransferComplet;\n\tSPI_RX_DMA.DMAStream=dma2stream2;\n\tBSP_DMA_Init(DMA2_Stream2, &amp;SPI_RX_DMA);\n\n\n\n\twhile(1)\n\t{\n\t\tGPIO_WritePin(GPIOA,&amp;CS_Pin,Reset);\n\n\t\tBSP_DMA_Transfer_Single_Buffer(DMA2_Stream2, (uint32_t)&amp;SPI1-&gt;DR, (uint32_t)&amp;SPI_RX,7);\n\n\t\tBSP_DMA_Transfer_Single_Buffer(DMA2_Stream3, (uint32_t)&amp;SPI1-&gt;DR,(uint32_t)&amp;SPI_TX,7);\n\n\t\twhile(rx_finished ==0){;}\n\n\t\trx_finished=0;\n\n\t\tif(tx_finished==1)tx_finished=0;\n\n\t\tGPIO_WritePin(GPIOA,&amp;CS_Pin,Set);\n\n\t\tBSP_Delay(200);\n\n\t}\n}\n\nvoid clock_config(void)\n{\n\tClock_Config_Typedef clockConfig;\n\n\tclockConfig.PLL_M= 4;\n\tclockConfig.PLL_N= 200;\n\tclockConfig.PLL_P= 4;\n\n\tclockConfig.AHB1Prescaler=AHB1_Prescaler1;\n\tclockConfig.APB1Prescaler=APB1_Prescaler2;\n\tclockConfig.APB2Prescaler=APB2_Prescaler1;\n\n\tclockConfig.clockSourc=External_Oscillator;\n\tclockConfig.flash_latency= Three_wait_state;\n\n\tClock_Configuration(&amp;clockConfig);\n}\n\nvoid DMA2_Stream2_handler(void)\n{\n\tif((DMA2-&gt;LISR &amp; DMA_LISR_TCIF2)==DMA_LISR_TCIF2)\n\t{\n\t\trx_finished=1;\n\t}\n\n}\n\n\nvoid DMA2_Stream3_handler(void)\n{\n\tif((DMA2-&gt;LISR &amp; DMA_LISR_TCIF3)==DMA_LISR_TCIF3)\n\t{\n\t\ttx_finished=1;\n\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. MPU9250 Connection:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The connection as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"774\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-1024x774.png\" alt=\"\" class=\"wp-image-1950\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-1024x774.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-300x227.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-768x581.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-1536x1161.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-1150x869.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-750x567.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-400x302.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2-250x189.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/Screen-Shot-2022-01-17-at-6.39.56-AM-2048x1548-2.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>By adding SPI_RX buffer to live expression, we shall get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"747\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/ezgif-4-854679b4cd.gif\" alt=\"\" class=\"wp-image-1951\" \/><\/figure>\n\n\n\n<p>That means, we have successfully transferred and received data over SPI bus using DMA.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this fourth section of the thirteenth part of board support package, we shall use the BSP of DMA to transfer data from the peripheral to memory using SPI and MPU9250. In this guide, we shall cover the following: Configure the DMA. Transfer data from MPU9250 to STM32. MPU9250 connection. Results. 1. Configure the DMA: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,12],"tags":[],"class_list":["post-1947","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1947"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1947"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1947\/revisions"}],"predecessor-version":[{"id":1952,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1947\/revisions\/1952"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}