{"id":1680,"date":"2023-04-12T08:04:05","date_gmt":"2023-04-12T08:04:05","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1680"},"modified":"2023-04-12T08:04:08","modified_gmt":"2023-04-12T08:04:08","slug":"republishing-toggling-led-using-dma-and-timer-on-stm32f4xx","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1680","title":{"rendered":"[Republishing] Toggling LED using DMA and Timer on STM32F4xx"},"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\/04\/AdobeStock_111938559-1024x683.jpeg\" alt=\"\" class=\"wp-image-1681\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_111938559-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall use DMA to toggle LED with a help of a timer to achieve the toggle rate of 1Hz.<\/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>GPIO Configuration.<\/li><li>Timer configuration.<\/li><li>DMA Configuration.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. GPIO Configuration:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Since this guide, the Nucleo-64 F411 is used which has built-in LED which is connected to PA5, hence we shall configure PA5 as output as following:<\/p>\n\n\n\n<p>Enable Clock access to GPIOA:<\/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 clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Set pin 5 as output:<\/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\/*Set PA5 as output*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE5_1;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>For more details how, please refer to this topic <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=246\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=246\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>Thats all for GPIO configuration.<\/p>\n\n\n\n<p>Next, timer configuration:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Timer configuration:<\/h2>\n\n\n\n<p>Since timer1 is used for this operation, we need first to enable clock access to it. <br>First, we need to find which bus timer1 is connected to.<\/p>\n\n\n\n<p>To find which bus, we need to investigate STM32F411xC\/xE block diagram in the datasheet:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-916x1024.png\" alt=\"\" class=\"wp-image-1682\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-916x1024.png 916w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-268x300.png 268w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-768x859.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-1150x1286.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-750x839.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-400x447.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM-250x280.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.45.03-AM.png 1338w\" sizes=\"(max-width: 916px) 100vw, 916px\" \/><\/figure>\n\n\n\n<p>From the block diagram, we can find that TIM1 is connected to APB2 bus. Hence, we can enable clock access to TIM1 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;}\">\tRCC-&gt;APB2ENR  |=  RCC_APB2ENR_TIM1EN;\t<\/pre><\/div>\n\n\n\n<p>Enable DMA request when update occurs:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"236\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-1024x236.png\" alt=\"\" class=\"wp-image-1683\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-1024x236.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-300x69.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-768x177.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-1536x354.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-1150x265.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-750x173.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-400x92.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM-250x58.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.48.28-AM.png 1934w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, set prescaler and ARR to 16000-1 and 1000-1 respectively to achieve 1Hz:<\/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;}\">\tTIM1-&gt;PSC=16000-1; \/*16 000 000 \/ 16 000 =1 000*\/\n\tTIM1-&gt;ARR=1000-1; \/*1 000 \/ 1 000 = 1HZ*\/<\/pre><\/div>\n\n\n\n<p>Don&#8217;t enable the timer yet.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, we shall configure DMA.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. DMA configuration:<\/h2>\n\n\n\n<p>Before configuring the DMA, we need to know which DMA and DMA stream is related to TIM1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"531\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-1024x531.png\" alt=\"\" class=\"wp-image-1684\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-1024x531.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-300x155.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-768x398.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-1536x796.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-2048x1061.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-1150x596.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-750x389.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-400x207.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.53.34-AM-250x130.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We can find that DMA2 Stream 5 Channel 6 is responsible for TIM1_UP. Hence, we shall enable 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>Disable the Stream and make sure it 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_Stream5-&gt;CR&amp;=DMA_SxCR_EN;\n\twhile((DMA2_Stream5-&gt;CR)&amp;DMA_SxCR_EN){;}<\/pre><\/div>\n\n\n\n<p>Configure the DMA with the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Channel 6.<\/li><li>Memory and Peripheral size to be word size (32-bit).<\/li><li>Memory increment mode.<\/li><li>Circular mode.<\/li><li>Direction: Memory to peripheral.<\/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=\"393\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-1024x393.png\" alt=\"\" class=\"wp-image-1685\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-1024x393.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-300x115.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-768x295.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-1536x590.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-2048x786.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-1150x442.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-750x288.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-400x154.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-12-at-10.57.26-AM-250x96.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;}\">#define CH6 \t\t0x06\n\n\tDMA2_Stream5-&gt;CR|=(CH6&lt;&lt;DMA_SxCR_CHSEL_Pos)|DMA_SxCR_MSIZE|DMA_SxCR_PSIZE|\n\t\t\tDMA_SxCR_MINC|DMA_SxCR_CIRC|DMA_SxCR_DIR_0;<\/pre><\/div>\n\n\n\n<p>Set the number of transfers to be  data_size (for this guide, it is 2):<\/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_Stream5-&gt;NDTR=(uint32_t)data_size;<\/pre><\/div>\n\n\n\n<p>Peripheral address to be GPIOA-&gt;BSRR 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;}\">DMA2_Stream5-&gt;PAR=(uint32_t)(&amp;GPIOA-&gt;BSRR);<\/pre><\/div>\n\n\n\n<p>Set memory address to be the pattern required:<\/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_Stream5-&gt;M0AR=(uint32_t)(gpio_pattern);<\/pre><\/div>\n\n\n\n<p>The used pattern 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;}\">#define data_size \t2\n\nuint32_t gpio_pattern[data_size]={GPIO_BSRR_BS5,GPIO_BSRR_BR5};<\/pre><\/div>\n\n\n\n<p>Enable DMA stream:<\/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\/*Launch DMA*\/\n\tDMA2_Stream5-&gt;CR|=DMA_SxCR_EN;<\/pre><\/div>\n\n\n\n<p>Finally enable the timer:<\/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\/*Launch Timer*\/\n\tTIM1-&gt;CR1     |= TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>The entire 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;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;\n\n\n#define data_size \t2\n#define CH6 \t\t0x06\n\nuint32_t gpio_pattern[data_size]={GPIO_BSRR_BS5,GPIO_BSRR_BR5};\n\n\n\nint main(void)\n{\n\t\/*GPIO Configuration*\/\n\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\n\t\/*Set PA5 as output*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE5_1;\n\n\t\/*Timer configuration*\/\n\n\tRCC-&gt;APB2ENR  |=  RCC_APB2ENR_TIM1EN;\n\t\n\tTIM1-&gt;DIER=TIM_DIER_UDE;\n\t\n\tTIM1-&gt;PSC=16000-1; \/*16 000 000 \/ 16 000 =1 000*\/\n\tTIM1-&gt;ARR=1000-1; \/*1 000 \/ 1 000 = 1HZ*\/\n\n\n\t\/*DMA configuration*\/\n\t\n\t\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_DMA2EN;\n\tDMA2_Stream5-&gt;CR&amp;=DMA_SxCR_EN;\n\twhile((DMA2_Stream5-&gt;CR)&amp;DMA_SxCR_EN){;}\n\tDMA2_Stream5-&gt;CR|=(CH6&lt;&lt;DMA_SxCR_CHSEL_Pos)|DMA_SxCR_MSIZE|DMA_SxCR_PSIZE|\n\t\t\tDMA_SxCR_MINC|DMA_SxCR_CIRC|DMA_SxCR_DIR_0;\n\n\tDMA2_Stream5-&gt;NDTR=(uint32_t)data_size;\n\n\tDMA2_Stream5-&gt;PAR=(uint32_t)(&amp;GPIOA-&gt;BSRR);\n\n\tDMA2_Stream5-&gt;M0AR=(uint32_t)(gpio_pattern);\n\n\t\n\t\/*Launch DMA*\/\n\tDMA2_Stream5-&gt;CR|=DMA_SxCR_EN;\n\n\t\/*Launch Timer*\/\n\tTIM1-&gt;CR1     |= TIM_CR1_CEN;\n\n\twhile(1)\n\t{\n\n\t}\n\n\n\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"LED toggle using DMA and timer\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/xFh9q-DyUX4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall use DMA to toggle LED with a help of a timer to achieve the toggle rate of 1Hz. In this guide, we shall cover the following : GPIO Configuration. Timer configuration. DMA Configuration. Code. Results. 1. GPIO Configuration: Since this guide, the Nucleo-64 F411 is used which has built-in LED [&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-1680","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\/1680"}],"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=1680"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1680\/revisions"}],"predecessor-version":[{"id":1686,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1680\/revisions\/1686"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}