{"id":1934,"date":"2023-08-19T05:40:49","date_gmt":"2023-08-19T05:40:49","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1934"},"modified":"2023-08-20T03:43:28","modified_gmt":"2023-08-20T03:43:28","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part13-2-dma-memory-to-memory-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1934","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part13.2: DMA Memory to Memory Mode"},"content":{"rendered":"\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-3-1024x683.jpeg\" alt=\"\" class=\"wp-image-1935\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/AdobeStock_128586024-3-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this second section of the thirteenth part of board support package, we shall develop the source code of the DMA driver and test the driver with Memory to Memory mode.<\/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>Adding the required functions to the header file.<\/li><li>Develop the source code.<\/li><li>DMA Memory to Memory driver.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Adding the required function to the header file:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open dma_bsp.h header file and add the following three functions:<\/p>\n\n\n\n<p>First 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 BSP_DMA_Init(DMA_Stream_TypeDef *dma, DMA_ConfigTypedef *config );<\/pre><\/div>\n\n\n\n<p>This function shall initialize the DMA and takes DMA_Stream_TypeDef and DMA_ConfigTypedef as argument and returns nothing.<\/p>\n\n\n\n<p>Second and third function 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;}\">void BSP_DMA_Transfer_Single_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest, uint16_t len);\n\nvoid BSP_DMA_Transfer_Double_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest1,uint32_t dest2, uint16_t len);\n<\/pre><\/div>\n\n\n\n<p>Those functions will launch DMA transfer for both single and double buffer mode. <\/p>\n\n\n\n<p>Both functions will take DMA_Stream_TypeDef,  length and source as argument.<\/p>\n\n\n\n<p>In double buffer, you will have two destinations rather than single destination for single buffer transfer.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the updated header file 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;}\">#ifndef DMA_BSP_H_\n#define DMA_BSP_H_\n\n#include &quot;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;\n\n\n#define __BSP_DMA1__CLOCK_ENABLE()\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA1EN;\n#define __BSP_DMA2__CLOCK_ENABLE()\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA2EN;\n\n\n\ntypedef enum\n{\n\tdma1stream0=0,\n\tdma1stream1,\n\tdma1stream2,\n\tdma1stream3,\n\tdma1stream4,\n\tdma1stream5,\n\tdma1stream6,\n\tdma1stream7,\n\n\tdma2stream0,\n\tdma2stream1,\n\tdma2stream2,\n\tdma2stream3,\n\tdma2stream4,\n\tdma2stream5,\n\tdma2stream6,\n\tdma2stream7,\n\n}DMAStreamTypedef;\n\n\ntypedef enum\n{\n\tChannel0=0,\n\tChannel1,\n\tChannel2,\n\tChannel3,\n\tChannel4,\n\tChannel5,\n\tChannel6,\n\tChannel7\n}DMA_Stream_ChannelTypedef;\n\n\n\ntypedef enum\n{\n\tsingleTransfer=0,\n\tINCR4,\n\tINCR8,\n\tINCR16,\n}DMA_Stream_BurstTypedef;\n\n\ntypedef enum\n{\n\tsingleBuffer=0,\n\tdoubleBuffer,\n}DMA_StreamDoubleBufferTypedef;\n\ntypedef enum\n{\n\tlow=0,\n\tmedium,\n\thigh,\n\tvery_high\n\n}DMA_StreamPriorityTypedef;\n\ntypedef enum\n{\n\tPsize=0,\n\twordSize,\n}DMA_StreamPrephIncSizeTypedef;\n\n\ntypedef enum\n{\n\tbyte=0,\n\thalfWord,\n\tword\n\n}DMA_SizeTypedef;\n\ntypedef enum\n{\n\tfixedMode=0,\n\tincMode=1\n}DMA_IncModeTypedef;\n\n\ntypedef enum\n{\n\tSingleTransfer=0,\n\tCircular=1\n}DMA_CricularTypedef;\n\n\ntypedef enum\n{\n\tperipheral2Memory=0,\n\tmemory2Peripheral,\n\tmemory2Memory\n\n}DMA_DirectionTypedef;\n\n\ntypedef enum\n{\n\tDMAController=0,\n\tPeripheralController,\n}DMAPeripheralControlTypedef;\n\n\ntypedef enum\n{\n\tInterruptDisabled=0,\n\tInterruptEnbaled=1,\n\n\tTransferComplet=1,\n\tHalfTransfer=1,\n\tTransferError=1,\n\tDirectModeError=1\n}DMA_InterruptTypedef;\n\ntypedef struct\n{\n\n\tuint8_t DMA_Channel;\n\tuint8_t PeripheralBurstMode;\n\tuint8_t MemoryBurstMode;\n\tuint8_t DoubleBuffer;\n\tuint8_t Priority;\n\tuint8_t PeripheralIncSize;\n\tuint8_t MemorySize;\n\tuint8_t PerpheralSize;\n\tuint8_t MemoryIncMode;\n\tuint8_t PeripheralIncMode;\n\tuint8_t CircularMode;\n\tuint8_t Direction;\n\tuint8_t PeripheralController;\n\tuint8_t InterruptEnable;\n\tuint8_t DMAStream;\n\tuint8_t TransferCompleteInterrupt;\n\tuint8_t HalfTransferInterrupt;\n\tuint8_t TransferErrorInterrupt;\n\tuint8_t DirectModeErrorInterrupt;\n}DMA_ConfigTypedef;\n\nvoid BSP_DMA_Init(DMA_Stream_TypeDef *dma, DMA_ConfigTypedef *config );\n\nvoid BSP_DMA_Transfer_Single_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest, uint16_t len);\n\nvoid BSP_DMA_Transfer_Double_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest1,uint32_t dest2, uint16_t len);\n\n\n#endif \/* DMA_BSP_H_ *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Develop the source code:<\/h2>\n\n\n\n<p>We start off by creating new source code with name of dma_bsp.c.<\/p>\n\n\n\n<p>Within the source code, we shall the following header file:<\/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;dma_bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>We start with DMA Initialization:<\/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 BSP_DMA_Init(DMA_Stream_TypeDef *dma, DMA_ConfigTypedef *config )<\/pre><\/div>\n\n\n\n<p>Make sure that the DMA 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;}\">\t\/*Disable the stream*\/\n\tdma-&gt;CR &amp;=~ DMA_SxCR_EN;\n\t\/*Wait until stream is disabled*\/\n\twhile ((dma-&gt;CR&amp; DMA_SxCR_EN));<\/pre><\/div>\n\n\n\n<p>Reset the control register:<\/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-&gt;CR=0;<\/pre><\/div>\n\n\n\n<p>Configure the DMA according to the passed parameters 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-&gt;CR |=(config-&gt;DMA_Channel &lt;&lt; DMA_SxCR_CHSEL_Pos);\n\n\tdma-&gt;CR |=(config-&gt;PeripheralBurstMode&lt;&lt;DMA_SxCR_PBURST_Pos);\n\n\tdma-&gt;CR|=(config-&gt;MemoryBurstMode&lt;&lt;DMA_SxCR_MBURST_Pos);\n\n\tdma-&gt;CR|=(config-&gt;DoubleBuffer&lt;&lt;DMA_SxCR_DBM_Pos);\n\n\tdma-&gt;CR|=(config-&gt;Priority&lt;&lt;DMA_SxCR_PL_Pos);\n\n\tdma-&gt;CR|=(config-&gt;MemorySize&lt;&lt;DMA_SxCR_MSIZE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;PerpheralSize&lt;&lt;DMA_SxCR_PSIZE_Pos);\n\n\tdma-&gt;CR|= (config-&gt;MemoryIncMode&lt;&lt;DMA_SxCR_MINC_Pos);\n\n\tdma-&gt;CR|= (config-&gt;PeripheralIncMode&lt;&lt;DMA_SxCR_PINC_Pos);\n\n\tdma-&gt;CR|=(config-&gt;CircularMode&lt;&lt;DMA_SxCR_CIRC_Pos);\n\n\tdma-&gt;CR|=(config-&gt;Direction&lt;&lt;DMA_SxCR_DIR_Pos);\n\n\tif(config-&gt;Direction==memory2Memory)\n\t{\n\t\tdma-&gt;FCR|=(1U&lt;&lt;DMA_SxFCR_DMDIS_Pos);\n\t}\n\n\tdma-&gt;CR|=(config-&gt;PeripheralController&lt;&lt;DMA_SxCR_PFCTRL_Pos);\n\n\tif(config-&gt;InterruptEnable==InterruptEnbaled)\n\t{\n\n\t\tswitch (config-&gt;DMAStream)\n\t\t{\n\n\t\tcase dma1stream0 : NVIC_EnableIRQ(DMA1_Stream0_IRQn); break;\n\t\tcase dma1stream1 : NVIC_EnableIRQ(DMA1_Stream1_IRQn); break;\n\t\tcase dma1stream2 : NVIC_EnableIRQ(DMA1_Stream2_IRQn); break;\n\t\tcase dma1stream3 : NVIC_EnableIRQ(DMA1_Stream3_IRQn); break;\n\t\tcase dma1stream4 : NVIC_EnableIRQ(DMA1_Stream4_IRQn); break;\n\t\tcase dma1stream5 : NVIC_EnableIRQ(DMA1_Stream5_IRQn); break;\n\t\tcase dma1stream6 : NVIC_EnableIRQ(DMA1_Stream6_IRQn); break;\n\t\tcase dma1stream7 : NVIC_EnableIRQ(DMA1_Stream7_IRQn); break;\n\n\t\tcase dma2stream0 : NVIC_EnableIRQ(DMA2_Stream0_IRQn); break;\n\t\tcase dma2stream1 : NVIC_EnableIRQ(DMA2_Stream1_IRQn); break;\n\t\tcase dma2stream2 : NVIC_EnableIRQ(DMA2_Stream2_IRQn); break;\n\t\tcase dma2stream3 : NVIC_EnableIRQ(DMA2_Stream3_IRQn); break;\n\t\tcase dma2stream4 : NVIC_EnableIRQ(DMA2_Stream4_IRQn); break;\n\t\tcase dma2stream5 : NVIC_EnableIRQ(DMA2_Stream5_IRQn); break;\n\t\tcase dma2stream6 : NVIC_EnableIRQ(DMA2_Stream6_IRQn); break;\n\t\tcase dma2stream7 : NVIC_EnableIRQ(DMA2_Stream7_IRQn); break;\n\n\t\tdefault: break;\n\n\n\t\t}\n\n\t}\n\n\tdma-&gt;CR|=(config-&gt;TransferCompleteInterrupt&lt;&lt;DMA_SxCR_TCIE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;HalfTransferInterrupt&lt;&lt;DMA_SxCR_HTIE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;TransferErrorInterrupt&lt;&lt;DMA_SxCR_TEIE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;DirectModeErrorInterrupt&lt;&lt;DMA_SxCR_DMEIE_Pos);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>For DMA transfer single buffer:<\/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 BSP_DMA_Transfer_Single_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest, uint16_t len)\n{\n\tdma-&gt;M0AR=dest;\n\tdma-&gt;PAR=src;\n\tdma-&gt;NDTR=len;\n\tdma-&gt;CR |=DMA_SxCR_EN;\n}<\/pre><\/div>\n\n\n\n<p>For double buffer 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;}\">void BSP_DMA_Transfer_Double_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest1,uint32_t dest2, uint16_t len)\n{\n\tdma-&gt;M0AR=(uint32_t)dest1;\n\tdma-&gt;M1AR=(uint32_t)dest2;\n\tdma-&gt;PAR=(uint32_t)src;\n\tdma-&gt;NDTR=len;\n\tdma-&gt;CR |=DMA_SxCR_EN;\n}<\/pre><\/div>\n\n\n\n<p>For the 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;}\">__WEAK void DMA1_Stream0_handler(void)\n{}\n__WEAK void DMA1_Stream1_handler(void)\n{}\n__WEAK void DMA1_Stream2_handler(void)\n{}\n__WEAK void DMA1_Stream3_handler(void)\n{}\n__WEAK void DMA1_Stream4_handler(void)\n{}\n__WEAK void DMA1_Stream5_handler(void)\n{}\n__WEAK void DMA1_Stream6_handler(void)\n{}\n__WEAK void DMA1_Stream7_handler(void)\n{}\n\n__WEAK void DMA2_Stream0_handler(void)\n{}\n__WEAK void DMA2_Stream1_handler(void)\n{}\n__WEAK void DMA2_Stream2_handler(void)\n{}\n__WEAK void DMA2_Stream3_handler(void)\n{}\n__WEAK void DMA2_Stream4_handler(void)\n{}\n__WEAK void DMA2_Stream5_handler(void)\n{}\n__WEAK void DMA2_Stream6_handler(void)\n{}\n__WEAK void DMA2_Stream7_handler(void)\n{}\n\n\nvoid DMA1_Stream0_IRQHandler(void)\n{\n\t\/*Call DMA handler*\/\n\tDMA1_Stream0_handler();\n\t\/*Clear interrupt flags*\/\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CFEIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CDMEIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTEIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CHTIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTCIF0;\n\n\n\n}\n\nvoid DMA1_Stream1_IRQHandler(void)\n{\n\tDMA1_Stream1_handler();\n\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CFEIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CDMEIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTEIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CHTIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTCIF1;\n}\nvoid DMA1_Stream2_IRQHandler(void)\n{\n\tDMA1_Stream2_handler();\n\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CFEIF2;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CDMEIF2;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTEIF2;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CHTIF2;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTCIF2;\n}\nvoid DMA1_Stream3_IRQHandler(void)\n{\n\tDMA1_Stream3_handler();\n\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CFEIF3;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CDMEIF3;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTEIF3;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CHTIF3;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTCIF3;\n}\nvoid DMA1_Stream4_IRQHandler(void)\n{\n\tDMA1_Stream4_handler();\n\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CFEIF4;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CDMEIF4;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CTEIF4;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CHTIF4;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CTCIF4;\n}\nvoid DMA1_Stream5_IRQHandler(void)\n{\n\tDMA1_Stream5_handler();\n\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CFEIF5;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CDMEIF5;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CTEIF5;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CHTIF5;\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CTCIF5;\n}\n\nvoid DMA1_Stream6_IRQHandler(void)\n{\n\tDMA1_Stream6_handler();\n\n\tDMA1-&gt;HIFCR=DMA_HIFCR_CFE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A_LIFCR_CHTIF2;\n\tDMA2-&gt;LIFCR=DMA_LIFCR_CTCIF2;\n}\nvoid DMA2_Stream3_IRQHandler(void)\n{\n\tDMA2_Stream3_handler();\n\n\tDMA2-&gt;LIFCR=DMA_LIFCR_CFEIF3;\n\tDMA2-&gt;LIFCR=DMA_LIFCR_CDMEIF3;\n\tDMA2-&gt;LIFCR=DMA_LIFCR_CTEIF3;\n\tDMA2-&gt;LIFCR=DMA_LIFCR_CHTIF3;\n\tDMA2-&gt;LIFCR=DMA_LIFCR_CTCIF3;\n}\nvoid DMA2_Stream4_IRQHandler(void)\n{\n\tDMA2_Stream4_handler();\n\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CFEIF4;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CDMEIF4;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTEIF4;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CHTIF4;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTCIF4;\n}\n\nvoid DMA2_Stream5_IRQHandler(void)\n{\n\tDMA2_Stream5_handler();\n\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CFEIF5;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CDMEIF5;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTEIF5;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CHTIF5;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTCIF5;\n}\n\nvoid DMA2_Stream6_IRQHandler(void)\n{\n\tDMA2_Stream6_handler();\n\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CFEIF6;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CDMEIF6;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTEIF6;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CHTIF6;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTCIF6;\n}\nvoid DMA2_Stream7_IRQHandler(void)\n{\n\tDMA2_Stream7_handler();\n\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CFEIF7;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CDMEIF7;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTEIF7;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CHTIF7;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTCIF7;\n}\n\n<\/pre><\/div>\n\n\n\n<p>The process as following:<\/p>\n\n\n\n<p>Call the interrupt handler which is defined as weak function that will allow the user to overwrite the function then clear the pending flags.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire source 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;dma_bsp.h&quot;\n\nvoid BSP_DMA_Init(DMA_Stream_TypeDef *dma, DMA_ConfigTypedef *config )\n{\n\t\/*Disable the stream*\/\n\tdma-&gt;CR &amp;=~ DMA_SxCR_EN;\n\t\/*Wait until stream is disabled*\/\n\twhile ((dma-&gt;CR&amp; DMA_SxCR_EN));\n\n\tdma-&gt;CR=0;\n\n\tdma-&gt;CR |=(config-&gt;DMA_Channel &lt;&lt; DMA_SxCR_CHSEL_Pos);\n\n\tdma-&gt;CR |=(config-&gt;PeripheralBurstMode&lt;&lt;DMA_SxCR_PBURST_Pos);\n\n\tdma-&gt;CR|=(config-&gt;MemoryBurstMode&lt;&lt;DMA_SxCR_MBURST_Pos);\n\n\tdma-&gt;CR|=(config-&gt;DoubleBuffer&lt;&lt;DMA_SxCR_DBM_Pos);\n\n\tdma-&gt;CR|=(config-&gt;Priority&lt;&lt;DMA_SxCR_PL_Pos);\n\n\tdma-&gt;CR|=(config-&gt;MemorySize&lt;&lt;DMA_SxCR_MSIZE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;PerpheralSize&lt;&lt;DMA_SxCR_PSIZE_Pos);\n\n\tdma-&gt;CR|= (config-&gt;MemoryIncMode&lt;&lt;DMA_SxCR_MINC_Pos);\n\n\tdma-&gt;CR|= (config-&gt;PeripheralIncMode&lt;&lt;DMA_SxCR_PINC_Pos);\n\n\tdma-&gt;CR|=(config-&gt;CircularMode&lt;&lt;DMA_SxCR_CIRC_Pos);\n\n\tdma-&gt;CR|=(config-&gt;Direction&lt;&lt;DMA_SxCR_DIR_Pos);\n\n\tif(config-&gt;Direction==memory2Memory)\n\t{\n\t\tdma-&gt;FCR|=(1U&lt;&lt;DMA_SxFCR_DMDIS_Pos);\n\t}\n\n\tdma-&gt;CR|=(config-&gt;PeripheralController&lt;&lt;DMA_SxCR_PFCTRL_Pos);\n\n\tif(config-&gt;InterruptEnable==InterruptEnbaled)\n\t{\n\n\t\tswitch (config-&gt;DMAStream)\n\t\t{\n\n\t\tcase dma1stream0 : NVIC_EnableIRQ(DMA1_Stream0_IRQn); break;\n\t\tcase dma1stream1 : NVIC_EnableIRQ(DMA1_Stream1_IRQn); break;\n\t\tcase dma1stream2 : NVIC_EnableIRQ(DMA1_Stream2_IRQn); break;\n\t\tcase dma1stream3 : NVIC_EnableIRQ(DMA1_Stream3_IRQn); break;\n\t\tcase dma1stream4 : NVIC_EnableIRQ(DMA1_Stream4_IRQn); break;\n\t\tcase dma1stream5 : NVIC_EnableIRQ(DMA1_Stream5_IRQn); break;\n\t\tcase dma1stream6 : NVIC_EnableIRQ(DMA1_Stream6_IRQn); break;\n\t\tcase dma1stream7 : NVIC_EnableIRQ(DMA1_Stream7_IRQn); break;\n\n\t\tcase dma2stream0 : NVIC_EnableIRQ(DMA2_Stream0_IRQn); break;\n\t\tcase dma2stream1 : NVIC_EnableIRQ(DMA2_Stream1_IRQn); break;\n\t\tcase dma2stream2 : NVIC_EnableIRQ(DMA2_Stream2_IRQn); break;\n\t\tcase dma2stream3 : NVIC_EnableIRQ(DMA2_Stream3_IRQn); break;\n\t\tcase dma2stream4 : NVIC_EnableIRQ(DMA2_Stream4_IRQn); break;\n\t\tcase dma2stream5 : NVIC_EnableIRQ(DMA2_Stream5_IRQn); break;\n\t\tcase dma2stream6 : NVIC_EnableIRQ(DMA2_Stream6_IRQn); break;\n\t\tcase dma2stream7 : NVIC_EnableIRQ(DMA2_Stream7_IRQn); break;\n\n\t\tdefault: break;\n\n\n\t\t}\n\n\t}\n\n\tdma-&gt;CR|=(config-&gt;TransferCompleteInterrupt&lt;&lt;DMA_SxCR_TCIE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;HalfTransferInterrupt&lt;&lt;DMA_SxCR_HTIE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;TransferErrorInterrupt&lt;&lt;DMA_SxCR_TEIE_Pos);\n\n\tdma-&gt;CR|=(config-&gt;DirectModeErrorInterrupt&lt;&lt;DMA_SxCR_DMEIE_Pos);\n\n\n}\n\nvoid BSP_DMA_Transfer_Single_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest, uint16_t len)\n{\n\tdma-&gt;M0AR=dest;\n\tdma-&gt;PAR=src;\n\tdma-&gt;NDTR=len;\n\tdma-&gt;CR |=DMA_SxCR_EN;\n}\n\nvoid BSP_DMA_Transfer_Double_Buffer(DMA_Stream_TypeDef *dma, uint32_t src, uint32_t dest1,uint32_t dest2, uint16_t len)\n{\n\tdma-&gt;M0AR=(uint32_t)dest1;\n\tdma-&gt;M1AR=(uint32_t)dest2;\n\tdma-&gt;PAR=(uint32_t)src;\n\tdma-&gt;NDTR=len;\n\tdma-&gt;CR |=DMA_SxCR_EN;\n}\n\n\n__WEAK void DMA1_Stream0_handler(void)\n{}\n__WEAK void DMA1_Stream1_handler(void)\n{}\n__WEAK void DMA1_Stream2_handler(void)\n{}\n__WEAK void DMA1_Stream3_handler(void)\n{}\n__WEAK void DMA1_Stream4_handler(void)\n{}\n__WEAK void DMA1_Stream5_handler(void)\n{}\n__WEAK void DMA1_Stream6_handler(void)\n{}\n__WEAK void DMA1_Stream7_handler(void)\n{}\n\n__WEAK void DMA2_Stream0_handler(void)\n{}\n__WEAK void DMA2_Stream1_handler(void)\n{}\n__WEAK void DMA2_Stream2_handler(void)\n{}\n__WEAK void DMA2_Stream3_handler(void)\n{}\n__WEAK void DMA2_Stream4_handler(void)\n{}\n__WEAK void DMA2_Stream5_handler(void)\n{}\n__WEAK void DMA2_Stream6_handler(void)\n{}\n__WEAK void DMA2_Stream7_handler(void)\n{}\n\n\nvoid DMA1_Stream0_IRQHandler(void)\n{\n\t\/*Call DMA handler*\/\n\tDMA1_Stream0_handler();\n\t\/*Clear interrupt flags*\/\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CFEIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CDMEIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTEIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CHTIF0;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTCIF0;\n\n\n\n}\n\nvoid DMA1_Stream1_IRQHandler(void)\n{\n\tDMA1_Stream1_handler();\n\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CFEIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CDMEIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTEIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CHTIF1;\n\tDMA1-&gt;LIFCR=DMA_LIFCR_CTCIF1;\n}\nvoid DMA1_Stream2_IRQHandler(void)\n{\n\tDMA1_Stream2_handler();\n\n\tDMA1-&gt;LIFCR=D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R=DMA_HIFCR_CTEIF7;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CHTIF7;\n\tDMA2-&gt;HIFCR=DMA_HIFCR_CTCIF7;\n}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. DMA Memory to Memory driver:<\/h2>\n\n\n\n<p>In main.c:<\/p>\n\n\n\n<p>Include the dma_bsp header file:<\/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;dma_bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>Also, include stdlib for random number generation:<\/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;stdlib.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare a volatile variable to handle transfer complete interrupt:<\/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 finished=0;<\/pre><\/div>\n\n\n\n<p>Declare two arrays to hold the source and destination:<\/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;}\">uint32_t dataSource[5]={0x00,0x22,0xFF,0x55,0x33};\nuint32_t dataDest[5];<\/pre><\/div>\n\n\n\n<p>Declare DMA_ConfigTypedef data structure :<\/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 DMA_Config;<\/pre><\/div>\n\n\n\n<p>DMA2 Channel0 configuration:<\/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_DMA2__CLOCK_ENABLE();\n\n\tDMA_Config.DMA_Channel=Channel0;\n\tDMA_Config.CircularMode=SingleTransfer;\n\tDMA_Config.MemorySize=word;\n\tDMA_Config.PerpheralSize=word;\n\tDMA_Config.MemoryIncMode=incMode;\n\tDMA_Config.PeripheralIncMode=incMode;\n\tDMA_Config.Direction=memory2Memory;\n\tDMA_Config.InterruptEnable=InterruptEnbaled;\n\tDMA_Config.TransferCompleteInterrupt=TransferComplet;\n\tDMA_Config.DMAStream=dma2stream0;<\/pre><\/div>\n\n\n\n<p>Configure the 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;}\">BSP_DMA_Init(DMA2_Stream0, &amp;DMA_Config);<\/pre><\/div>\n\n\n\n<p>In while loop:<\/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\tBSP_DMA_Transfer_Single_Buffer(DMA2_Stream0, &amp;dataSource, &amp;dataDest, 5);\n\n\t\twhile(finished ==0){;}\n\t\tfinished=0;\n\t\tfor (int i=0; i&lt;5;i++)\n\t\t{\n\t\t\tdataSource[i]=random()%256;\n\t\t}\n<\/pre><\/div>\n\n\n\n<p>In 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_Stream0_handler(void)\n{\n\tif((DMA2-&gt;LISR &amp; DMA_LISR_TCIF0)==DMA_LISR_TCIF0)\n\t{\n\t\tfinished=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\">4. Code:<\/h2>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/BSP-STM32F411_Nucleo64.zip\">BSP-STM32F411_Nucleo64<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/BSP-STM32F411_Nucleo64.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p>Using debug session, add both array to live expression and you should get the following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"396\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/08\/ezgif-4-9232fb06ec.gif\" alt=\"\" class=\"wp-image-1938\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>We have successfully transfer 5 word data from one location to another.<\/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 second section of the thirteenth part of board support package, we shall develop the source code of the DMA driver and test the driver with Memory to Memory mode. In this guide, we shall cover the following: Adding the required functions to the header file. Develop the source code. DMA Memory to Memory [&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-1934","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\/1934"}],"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=1934"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1934\/revisions"}],"predecessor-version":[{"id":1939,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1934\/revisions\/1939"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}