{"id":696,"date":"2022-01-14T03:32:25","date_gmt":"2022-01-14T03:32:25","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=696"},"modified":"2022-01-14T03:32:28","modified_gmt":"2022-01-14T03:32:28","slug":"working-with-stm32f7-and-dma-memory-to-memory-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=696","title":{"rendered":"Working with STM32F7 and DMA: Memory to Memory Mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><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><\/p>\n\n\n\n<p>In this guide, we shall take a look at the DMA which called Direct Memory Access of STM32F7 and how to use it in memory to memory to transfer 4-bytes from 1 memory location to another using DMA.<\/p>\n\n\n\n<p><\/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>What is DMA<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 What is DMA:<\/h2>\n\n\n\n<p>Direct memory access (DMA) is used in order to provide high-speed data transfer between peripherals and memory and between memory and memory. Data can be quickly moved by DMA without any CPU action. This keeps CPU resources free for other operations.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>DMA main features<\/p>\n\n\n\n<p>The main DMA features are:<\/p>\n\n\n\n<p>\u2022 Dual AHB master bus architecture, one dedicated to memory accesses and one dedicated to peripheral accesses<\/p>\n\n\n\n<p>\u2022 AHB slave programming interface supporting only 32-bit accesses<\/p>\n\n\n\n<p>\u2022 8 streams for each DMA controller, up to 16 channels (requests) per stream<\/p>\n\n\n\n<p>\u2022 Four-word depth 32 first-in, first-out memory buffers (FIFOs) per stream, that can be used in FIFO mode or direct mode:<\/p>\n\n\n\n<p>\u2013 FIFO mode: with threshold level software selectable between 1\/4, 1\/2 or 3\/4 of the FIFO size<\/p>\n\n\n\n<p>\u2013 Direct mode: each DMA request immediately initiates a transfer from\/to the memory. When it is configured in direct mode (FIFO disabled), to transfer data in memory-to-peripheral mode, the DMA preloads only one data from the memory to the internal FIFO to ensure an immediate data transfer as soon as a DMA request is triggered by a peripheral.<\/p>\n\n\n\n<p>\u2022 Each stream can be configured to be:<\/p>\n\n\n\n<p>\u2013 a regular channel that supports peripheral-to-memory, memory-to-peripheral and memory-to-memory transfers<\/p>\n\n\n\n<p>\u2013 a double buffer channel that also supports double buffering on the memory side<\/p>\n\n\n\n<p>\u2022 Priorities between DMA stream requests are software-programmable (4 levels consisting of very high, high, medium, low) or hardware in case of equality (for example, request 0 has priority over request 1)<\/p>\n\n\n\n<p>\u2022 Each stream also supports software trigger for memory-to-memory transfers (only available for the DMA2 controller)<\/p>\n\n\n\n<p>\u2022 Each stream request can be selected among up to 16 possible channel requests. This selection is software-configurable and allows several peripherals to initiate DMA requests<\/p>\n\n\n\n<p>\u2022 The number of data items to be transferred can be managed either by the DMA controller or by the peripheral:<\/p>\n\n\n\n<p>\u2013 DMA flow controller: the number of data items to be transferred is softwareprogrammable from 1 to 65535<\/p>\n\n\n\n<p>\u2013 Peripheral flow controller: the number of data items to be transferred is unknown and controlled by the source or the destination peripheral that signals the end of the transfer by hardware<\/p>\n\n\n\n<p>\u2022 Independent source and destination transfer width (byte, half-word, word): when the data widths of the source and destination are not equal, the DMA automatically packs\/unpacks the necessary transfers to optimize the bandwidth. This feature is only available in FIFO mode<\/p>\n\n\n\n<p>\u2022 Incrementing or non-incrementing addressing for source and destination<\/p>\n\n\n\n<p>\u2022 Supports incremental burst transfers of 4, 8 or 16 beats. The size of the burst is software-configurable, usually equal to half the FIFO size of the peripheral<\/p>\n\n\n\n<p>\u2022 Each stream supports circular buffer management<\/p>\n\n\n\n<p>\u2022 5 event flags (DMA half transfer, DMA transfer complete, DMA transfer error, DMA FIFO error, direct mode error) logically ORed together in a single interrupt request for each stream<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 DMA Block Diagram:<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"787\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-1024x787.png\" alt=\"\" class=\"wp-image-699\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-1024x787.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-300x231.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-768x590.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-1536x1180.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-2048x1574.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-1150x884.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-750x576.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-400x307.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.54.04-AM-250x192.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">1.3 DMA Map:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"491\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-1024x491.png\" alt=\"\" class=\"wp-image-700\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-1024x491.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-300x144.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-768x368.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-1536x737.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-2048x982.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-1150x552.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-750x360.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-400x192.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.27-AM-250x120.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"630\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-1024x630.png\" alt=\"\" class=\"wp-image-701\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-1024x630.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-768x472.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-1536x945.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-2048x1260.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-1150x707.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-750x461.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-400x246.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-5.56.58-AM-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">1.4 DMA Operation Modes:<\/h2>\n\n\n\n<p>The DMA can operate in the different modes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Memory to peripheral<\/li><li>Peripheral to memory <\/li><li>Memory to Memory <\/li><\/ul>\n\n\n\n<p>These modes can be selected by during the configuration of the DMA:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"298\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-1024x298.png\" alt=\"\" class=\"wp-image-702\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-1024x298.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-300x87.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-768x224.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-1536x448.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-2048x597.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-1150x335.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-750x219.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-400x117.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.00.04-AM-250x73.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You may refer to reference manual for more details about DMA<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Code:<\/h2>\n\n\n\n<p>According to the reference manual, only DMA2 is able to work in Memory to Memory mode<\/p>\n\n\n\n<p>&#8220;Only the DMA2 controller is able to perform memory-to-memory transfers.&#8221;<\/p>\n\n\n\n<p>First we need to find which bus is DMA2 connected and we can get this from the datasheet of STM32F767.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"575\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-1024x575.png\" alt=\"\" class=\"wp-image-703\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-1024x575.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-300x168.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-768x431.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-1536x862.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-2048x1150.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-1150x647.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-750x422.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-400x225.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.07.01-AM-250x141.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence we can start of by enabling clock access to DMA2<\/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;}\">\t\/\/Enable DMA2 clock access\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA2EN;<\/pre><\/div>\n\n\n\n<p>Disable DMA2_Stream0 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;}\">\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 we can configure the DMA stream with the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Memory and peripheral size to 16-bit<\/li><li>Memory and peripheral increment<\/li><li>Direct is memory to memory<\/li><li>Transfer complete interrupt<\/li><li>Set the FIFO to full threshold<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"208\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-1024x208.png\" alt=\"\" class=\"wp-image-704\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-1024x208.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-300x61.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-768x156.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-1536x312.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-2048x416.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-1150x234.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-750x152.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-400x81.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Shot-2022-01-14-at-6.11.43-AM-250x51.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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|=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);<\/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\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\n\t\t\t}<\/pre><\/div>\n\n\n\n<p>For data 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;}\">void 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\n\t}<\/pre><\/div>\n\n\n\n<p>in while loop we can transfer the data 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;}\">\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++;\n\t\t\twhile(finished==0){;}\n\t\t\tfinished=0;\n\t\t\t\tfor (int j=0;j&lt;400000;j++);<\/pre><\/div>\n\n\n\n<p>Hence, the entire code is 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;}\">\/**\n ******************************************************************************\n * @file           : main.c\n * @author         : Auto-generated by STM32CubeIDE\n * @brief          : Main program body\n ******************************************************************************\n * @attention\n *\n * Copyright (c) 2022 STMicroelectronics.\n * All rights reserved.\n *\n * This software is licensed under terms that can be found in the LICENSE file\n * in the root directory of this software component.\n * If no LICENSE file comes with this software, it is provided AS-IS.\n *\n ******************************************************************************\n *\/\n#include &quot;stm32f7xx.h&quot;\n\n\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\n\t}\n\n\n\nint main(void)\n\t{\n\t\/\/Enable DMA2 clock access\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA2EN;\n\t\/\/reset DMA2_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\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++;\n\t\t\twhile(finished==0){;}\n\t\t\tfinished=0;\n\t\t\t\tfor (int j=0;j&lt;400000;j++);\n\n\t\t}\n\n\n\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\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\n\t\t\t}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/01\/Screen-Recording-2022-01-14-at-6.22.27-AM.mov\"><\/video><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at the DMA which called Direct Memory Access of STM32F7 and how to use it in memory to memory to transfer 4-bytes from 1 memory location to another using DMA. In this guide, we will cover the following: What is DMA Code Demo 1.1 What is 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":[2,11,12],"tags":[],"class_list":["post-696","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/696"}],"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=696"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/696\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/696\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}