{"id":555,"date":"2021-11-08T04:10:26","date_gmt":"2021-11-08T04:10:26","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=555"},"modified":"2021-11-23T18:27:36","modified_gmt":"2021-11-23T18:27:36","slug":"working-with-dma-and-stm32-memory-to-memory-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=555","title":{"rendered":"Working with DMA and STM32: Memory to Memory Mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"291\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1.jpg\" alt=\"\" class=\"wp-image-556\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1-300x85.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1-768x218.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1-750x213.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1-400x114.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/DMA_two_part_transfer-1024x291-1-250x71.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>In pervious multiple guides, we took look at ADC with DMA (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=319\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=319\" target=\"_blank\">here<\/a>), USART with DMA (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=442\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=442\" target=\"_blank\">here<\/a>) and SPI-TX with DMA (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=474\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=474\" target=\"_blank\">here<\/a>). In this guide, we shall use DMA to transfer data from one location to another using DMA.<\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DMA memory to memory initialization<\/li><li>Memory to Memory transfer <\/li><li>Code<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. DMA Memory to Memory Initialization:<\/h2>\n\n\n\n<p>The DMA channels can also work without being triggered by a request from a peripheral. This is the memory-to-memory mode, described in figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"648\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-1024x648.png\" alt=\"\" class=\"wp-image-557\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-1024x648.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-300x190.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-768x486.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-1536x973.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-2048x1297.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-1150x728.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-750x475.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-400x253.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.47.29-AM-250x158.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When the stream is enabled by setting the Enable bit (EN) in the DMA_SxCR register, the stream immediately starts to fill the FIFO up to the threshold level. When the threshold level is reached, the FIFO contents are drained and stored into the destination.The transfer stops once the DMA_SxNDTR register reaches zero or when the EN bit in the DMA_SxCR register is cleared by software.<\/p>\n\n\n\n<p>Hence, we can use any DMA and any stream for memory to memory transfer mode.<\/p>\n\n\n\n<p>In this guide, we shall use DMA2 Stream 0 for this experiment.<\/p>\n\n\n\n<p>We start off by enabling clock access to DMA2 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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA2EN;<\/pre><\/div>\n\n\n\n<p>Reseting DMA2 Stream 0 and wait until the stream is disabled <\/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;}\">\tDMA2_Stream0-&gt;CR=0x00;\n\t\/\/wait until \n\twhile(DMA2_Stream0-&gt;CR &amp; DMA_SxCR_EN){;}<\/pre><\/div>\n\n\n\n<p>Then, the DMA configuration shall be as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set memory and peripheral size to 16-bit.<\/li><li>Memory increment, peripheral increment<\/li><li>direction memory to memory, transfer complete interrupt, transfer error interrupt and direct mode error.<\/li><li><\/li><\/ul>\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;}\">DMA2_Stream0-&gt;CR|=DMA_SxCR_MSIZE_0\n\t|DMA_SxCR_PSIZE_0|DMA_SxCR_MINC|DMA_SxCR_PINC\n\t|DMA_SxCR_DIR_1|DMA_SxCR_TCIE|DMA_SxCR_TEIE|DMA_SxCR_DMEIE;<\/pre><\/div>\n\n\n\n<p>Set FIFO threshold to Full and disable direct mode<\/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;}\">\tDMA2_Stream0-&gt;FCR |= DMA_SxFCR_DMDIS;\n  DMA2_Stream0-&gt;FCR |= (DMA_SxFCR_FTH_0 | DMA_SxFCR_FTH_1);<\/pre><\/div>\n\n\n\n<p>Finally enable interrupt in NVIC <\/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;}\">NVIC_EnableIRQ(DMA2_Stream0_IRQn);<\/pre><\/div>\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;}\">\/\/DMA2 interrupt handler\n\tvoid DMA2_Stream0_IRQHandler(void)\n\t\t\t{\n\t\t\tif((DMA2-&gt;LISR)&amp;DMA_LISR_TCIF0)\n\t\t\t\t\t{\n\t\t\t\t\t\/\/set finished to 1\n\t\t\t\t\tfinished=1;\n\t\t\t\t\t\/\/clear transfer complete interrupt\n\t\t\t\t\tDMA2-&gt;LIFCR=DMA_LIFCR_CTCIF0;\n\t\t\t\t\t}\n\t\t\tif((DMA2-&gt;LISR)&amp;DMA_LISR_TEIF0)\n\t\t\t\t\t{\n\t\t\t\t\t\/\/transfer error interrupt\n\t\t\t\t\tDMA2-&gt;LIFCR=DMA_LIFCR_CTEIF0;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\tif((DMA2-&gt;LISR)&amp;DMA_LISR_DMEIF0)\n\t\t\t\t\t{\n\t\t\t\t\/\/direct mode error\n\t\t\t\t\tDMA2-&gt;LIFCR=DMA_LIFCR_CDMEIF0;\n\t\t\t\t\t}\n\t\t\t\n\t\t\t}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. Memory to Memory transfer<\/h2>\n\n\n\n<p>We start of defining our function<\/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 send_data(uint32_t src, uint32_t des, uint16_t len)<\/pre><\/div>\n\n\n\n<p>It takes three argument:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Source<\/li><li>Destination<\/li><li>Length <\/li><\/ul>\n\n\n\n<p>In order to transfer data from memory location to another, we need to set the source data in the peripheral address as mentioned in the reference manual<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"299\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-1024x299.png\" alt=\"\" class=\"wp-image-558\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-1024x299.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-300x88.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-768x224.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-1536x449.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-2048x598.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-1150x336.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-750x219.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-400x117.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-08-at-6.46.34-AM-250x73.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence, we can set it 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;}\">DMA2_Stream0-&gt;PAR=(uint32_t)src;<\/pre><\/div>\n\n\n\n<p>Note: it is important to typecast it to uint32_t <\/p>\n\n\n\n<p>Now, we can set the destination 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;}\">DMA2_Stream0-&gt;M0AR=(uint32_t)des;<\/pre><\/div>\n\n\n\n<p>The number of transfers<\/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;}\">DMA2_Stream0-&gt;NDTR=len;<\/pre><\/div>\n\n\n\n<p>Finally we can enable the transfer<\/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;}\">DMA2_Stream0-&gt;CR|=DMA_SxCR_EN;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\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;stm32f4xx.h&quot;                  \/\/ Device header\n\n\/\/data to be send\nuint16_t data_s[4];\n\/\/data to be received\nuint16_t data_r[4];\nuint16_t var=2;\n\/\/indicate that DMA finished transfered to out application\nvolatile uint8_t finished=0;\n\n\n\nvoid send_data(uint32_t src, uint32_t des, uint16_t len)\n\t{\n\tDMA2_Stream0-&gt;PAR=(uint32_t)src;\n\tDMA2_Stream0-&gt;M0AR=(uint32_t)des;\n\tDMA2_Stream0-&gt;NDTR=len;\n\tDMA2_Stream0-&gt;CR|=DMA_SxCR_EN;\n\t\n\t}\n\n\n\nint main(void)\n\t{\n\t\/\/Enable DMA1 clock access\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA2EN;\n\t\/\/reset DMA1_stream0 \n\tDMA2_Stream0-&gt;CR=0x00;\n\t\/\/wait until \n\twhile(DMA2_Stream0-&gt;CR &amp; DMA_SxCR_EN){;}\n\t\/*set memory and peripheral size to 16-bit, \n\tMemory increment, peripheral increment,\n\tdirection memory to memory and transfer complete interrupt\n\t*\/\n\tDMA2_Stream0-&gt;CR|=DMA_SxCR_MSIZE_0\n\t|DMA_SxCR_PSIZE_0|DMA_SxCR_MINC|DMA_SxCR_PINC\n\t|DMA_SxCR_DIR_1|DMA_SxCR_TCIE|DMA_SxCR_TEIE|DMA_SxCR_DMEIE;\n\tDMA2_Stream0-&gt;FCR |= DMA_SxFCR_DMDIS;\n  DMA2_Stream0-&gt;FCR |= (DMA_SxFCR_FTH_0 | DMA_SxFCR_FTH_1);\n\tNVIC_EnableIRQ(DMA2_Stream0_IRQn);\n\t\t\n\twhile(1)\n\t\t{\n\t\tfor (int i=0;i&lt;4;i++)\n\t\t\t\t{\n\t\t\t\tdata_s[i]=var;\n\t\t\t\t}\n\t\t\tsend_data((uint32_t)data_s,(uint32_t)data_r,4);\n\t\t\tvar++;\t\n\t\t\twhile(finished==0){;}\n\t\t\tfinished=0;\t\t\n\t\t\t\tfor (int j=0;j&lt;400000;j++);\n\t\t\t\t\n\t\t}\n\t\n\t\n\t\n\t}\n\t\/\/DMA2 interrupt handler\n\tvoid DMA2_Stream0_IRQHandler(void)\n\t\t\t{\n\t\t\tif((DMA2-&gt;LISR)&amp;DMA_LISR_TCIF0)\n\t\t\t\t\t{\n\t\t\t\t\t\/\/set finished to 1\n\t\t\t\t\tfinished=1;\n\t\t\t\t\t\/\/clear transfer complete interrupt\n\t\t\t\t\tDMA2-&gt;LIFCR=DMA_LIFCR_CTCIF0;\n\t\t\t\t\t}\n\t\t\tif((DMA2-&gt;LISR)&amp;DMA_LISR_TEIF0)\n\t\t\t\t\t{\n\t\t\t\t\t\/\/transfer error interrupt\n\t\t\t\t\tDMA2-&gt;LIFCR=DMA_LIFCR_CTEIF0;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\tif((DMA2-&gt;LISR)&amp;DMA_LISR_DMEIF0)\n\t\t\t\t\t{\n\t\t\t\t\/\/direct mode error\n\t\t\t\t\tDMA2-&gt;LIFCR=DMA_LIFCR_CDMEIF0;\n\t\t\t\t\t}\n\t\t\t\n\t\t\t}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>In pervious multiple guides, we took look at ADC with DMA (here), USART with DMA (here) and SPI-TX with DMA (here). In this guide, we shall use DMA to transfer data from one location to another using DMA. In this guide, we will cover the following: DMA memory to memory initialization Memory to Memory transfer [&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-555","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\/555"}],"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=555"}],"version-history":[{"count":4,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":593,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/555\/revisions\/593"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}