{"id":1182,"date":"2022-09-01T05:44:03","date_gmt":"2022-09-01T05:44:03","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1182"},"modified":"2022-09-01T05:44:06","modified_gmt":"2022-09-01T05:44:06","slug":"getting-started-with-stm32l053-uart-full-duplex-in-dma-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1182","title":{"rendered":"Getting started with STM32L053: UART Full duplex in DMA Mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.st.com\/content\/dam\/arm-cortex-m\/M0-Corenew.jpg\" alt=\"\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1175\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1175\" target=\"_blank\">here<\/a>), we saw how to configure the UART and DMA to work in transmit mode. In this guide, we shall see how to configure the DMA and UART to work in full duplex.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Configure UART in DMA Full duplex.<\/li><li>Configure the DMA to receive data from UART.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Configure UART in DMA Full Duplex:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>First thing first, enable clock access to GPIOA and set PA2 and PA3 to alternate function and set the alternate function to AF4 (from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=894\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=894\" target=\"_blank\">here<\/a>):<\/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;IOPENR |= RCC_IOPENR_GPIOAEN;\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE2_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE2_0;\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE3_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE3_0;\n\n\t#define AF04 0x04\n\tGPIOA-&gt;AFR[0]|=(AF04&lt;&lt;8);\n\tGPIOA-&gt;AFR[0]|=(AF04&lt;&lt;12);<\/pre><\/div>\n\n\n\n<p>Then enable clock access to USART2 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;APB1ENR |= RCC_APB1ENR_USART2EN;<\/pre><\/div>\n\n\n\n<p>Set the desired baudrate:<\/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;}\">USART2-&gt;BRR=compute_uart_bd(APB1_CLK,UART_BAUDRATE);<\/pre><\/div>\n\n\n\n<p>Enable both direction for UART:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"377\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-1024x377.png\" alt=\"\" class=\"wp-image-1183\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-1024x377.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-300x110.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-768x283.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-1536x566.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-2048x754.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-1150x423.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-750x276.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-400x147.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.17.12-AM-250x92.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;}\">\t\/*Configure transfer direction*\/\n\tUSART2-&gt;CR1 = USART_CR1_TE|USART_CR1_RE;<\/pre><\/div>\n\n\n\n<p>Enable DMAT and DMAR in CR3 (Control Register 3):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"401\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-1024x401.png\" alt=\"\" class=\"wp-image-1184\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-1024x401.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-300x117.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-768x300.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-1536x601.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-2048x801.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-1150x450.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-750x293.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-400x156.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.21.38-AM-250x98.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;}\"> \t\/*Enable DMA TX &amp; RX for UART*\/\n\tUSART2-&gt;CR3|=USART_CR3_DMAT|USART_CR3_DMAR;<\/pre><\/div>\n\n\n\n<p>Finally enable the UART moduel:<\/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 uart module*\/\n\tUSART2-&gt;CR1 |= USART_CR1_UE;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure the DMA to Receive Data from UART:<\/h2>\n\n\n\n<p>We start off by enabling clock access to DMA1:<\/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 DMA1*\/\n\tRCC-&gt;AHBENR|=RCC_AHBENR_DMA1EN;<\/pre><\/div>\n\n\n\n<p>From the previous guide, we noticed that DMA1_Channel6 is for USART2_RX. Hence, we can this for the USART2_RX.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by disabling the DMA1_Channel6 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;}\">\tDMA1_Channel6-&gt;CCR&amp;=~DMA_CCR_EN;\n\twhile((DMA1_Channel6-&gt;CCR &amp;DMA_CCR_EN));<\/pre><\/div>\n\n\n\n<p>Then configure the DMA as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Memory increment.<\/li><li>Circular mode.<\/li><li>Transfer completed interrupt.<\/li><li>Enable the interrupt in NVIC.<\/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=\"495\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-1024x495.png\" alt=\"\" class=\"wp-image-1185\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-1024x495.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-300x145.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-768x372.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-1536x743.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-2048x991.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-1150x556.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-750x363.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-400x194.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-01-at-8.33.01-AM-250x121.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;}\">\tDMA1_Channel6-&gt;CCR|=DMA_CCR_MINC|DMA_CCR_CIRC|DMA_CCR_TCIE;\n\tNVIC_EnableIRQ(DMA1_Channel4_5_6_7_IRQn);<\/pre><\/div>\n\n\n\n<p>Set the peripheral address to USART2-&gt;RDR:<\/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;}\">DMA1_Channel6-&gt;CPAR=(uint32_t)(&amp;USART2-&gt;RDR);<\/pre><\/div>\n\n\n\n<p>Set channel 6 to USART2:<\/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_CSELR|=(0x04&lt;&lt;20);<\/pre><\/div>\n\n\n\n<p>Set the memory address to the 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;}\">DMA1_Channel6-&gt;CMAR=(uint32_t)data_rx;<\/pre><\/div>\n\n\n\n<p>Set number of transfer to 5:<\/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;}\">\tDMA1_Channel6-&gt;CNDTR=5;<\/pre><\/div>\n\n\n\n<p>Enable the DMA_Channel6:<\/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;}\">DMA1_Channel6-&gt;CCR|=DMA_CCR_EN;<\/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;}\">\nvoid DMA1_Channel4_7_IRQHandler(void)\n\t{\n\n\t\tif(DMA1-&gt;ISR &amp; DMA_ISR_TCIF7)\n\t\t\t{\n\t\t\t\tinprogress=0;\n\t\t\t\tDMA1-&gt;IFCR|=DMA_IFCR_CTCIF7;\n\t\t\t}\n\t\tif(DMA1-&gt;ISR &amp; DMA_ISR_TCIF6)\n\t\t\t{\n\t\t\t\treceived=1;\n\t\t\t\tDMA1-&gt;IFCR|=DMA_IFCR_CTCIF6;\n\t\t\t}\n\t\tNVIC_ClearPendingIRQ(DMA1_Channel4_5_6_7_IRQn);\n\t}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire Full-Duplex 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;stm32l0xx.h&quot;\n#include &quot;stdio.h&quot;\n#define SYS_FREQ\t\t2097000\n#define APB1_CLK\t\tSYS_FREQ\n\n#define UART_BAUDRATE\t115200\n\nvolatile uint8_t received=0, inprogress=0;\n\n\nchar data_tx[40];\nchar data_rx[5];\nuint16_t length;\n\n\n#define DMA_CSELR  (*(volatile unsigned int *)(0x400200a8))\n\n\nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate)\n{\n\treturn ((PeriphClk  +  (BaudRate\/2U))\/BaudRate);\n}\n\n\nvoid uart_transmit_dma(char *message, uint16_t len)\n\t{\n\n\t\tif(inprogress==0)\n\t\t{\n\t\t\t\/*DMA1 Channel 7 is for USART_TX*\/\n\t\t\tDMA1_Channel7-&gt;CCR &amp;=~DMA_CCR_EN;\n\n\t\t\twhile((DMA1_Channel7-&gt;CCR &amp;DMA_CCR_EN));\n\t\t\tDMA1_Channel7-&gt;CCR|=DMA_CCR_MINC|DMA_CCR_DIR|DMA_CCR_TCIE;\n\t\t\tNVIC_EnableIRQ(DMA1_Channel4_5_6_7_IRQn);\n\t\t\tDMA1_Channel7-&gt;CPAR=(uint32_t)(&amp;USART2-&gt;TDR);\n\t\t\tDMA_CSELR|=(0x04&lt;&lt;24);\n\t\t\tDMA1-&gt;IFCR|=DMA_IFCR_CTCIF7;\n\t\t\tDMA1_Channel7-&gt;CMAR=(uint32_t)message;\n\t\t\tDMA1_Channel7-&gt;CNDTR=(uint16_t)len;\n\t\t\tDMA1_Channel7-&gt;CCR|=DMA_CCR_EN;\n\t\t\tinprogress=1;\n\t\t}\n\t\telse return;\n\t}\n\n\n\n\nint main(void)\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;IOPENR |= RCC_IOPENR_GPIOAEN;\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE2_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE2_0;\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE3_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE3_0;\n\n\t#define AF04 0x04\n\tGPIOA-&gt;AFR[0]|=(AF04&lt;&lt;8);\n\tGPIOA-&gt;AFR[0]|=(AF04&lt;&lt;12);\n\n\n\tRCC-&gt;APB1ENR |= RCC_APB1ENR_USART2EN;\n\tUSART2-&gt;BRR=compute_uart_bd(APB1_CLK,UART_BAUDRATE);\n\t\/*Configure transfer direction*\/\n\tUSART2-&gt;CR1 = USART_CR1_TE|USART_CR1_RE;\n\t\/*Enable DMA TX &amp; RX for UART*\/\n\tUSART2-&gt;CR3|=USART_CR3_DMAT|USART_CR3_DMAR;\n\t\/*Enable uart module*\/\n\tUSART2-&gt;CR1 |= USART_CR1_UE;\n\n\t\/*Enable Clock access to DMA1*\/\n\tRCC-&gt;AHBENR|=RCC_AHBENR_DMA1EN;\n\n\t\/*Configure DMA1_Channel6 for USART2_RX*\/\n\tDMA1_Channel6-&gt;CCR&amp;=~DMA_CCR_EN;\n\twhile((DMA1_Channel6-&gt;CCR &amp;DMA_CCR_EN));\n\tDMA1_Channel6-&gt;CCR|=DMA_CCR_MINC|DMA_CCR_CIRC|DMA_CCR_TCIE;\n\tNVIC_EnableIRQ(DMA1_Channel4_5_6_7_IRQn);\n\tDMA1_Channel6-&gt;CPAR=(uint32_t)(&amp;USART2-&gt;RDR);\n\tDMA_CSELR|=(0x04&lt;&lt;20);\n\tDMA1_Channel6-&gt;CMAR=(uint32_t)data_rx;\n\tDMA1_Channel6-&gt;CNDTR=5;\n\tDMA1_Channel6-&gt;CCR|=DMA_CCR_EN;\n\n\n\tlength=sprintf(data_tx,&quot;Hello from UART DMA\\r\\n&quot;);\n\tuart_transmit_dma(data_tx,length);\n\twhile(inprogress==1);\n\tinprogress=0;\n\n\n\twhile(1)\n\t{\n\t\tif(received==1)\n\t\t\t{\n\t\t\t\tlength=sprintf(data_tx,&quot;Received data\\&quot; %s \\&quot;\\r\\n&quot;,data_rx);\n\t\t\t\tuart_transmit_dma(data_tx,length);\n\t\t\t\twhile(inprogress==1);\n\t\t\t\tinprogress=0;\n\t\t\t\treceived=0;\n\t\t\t}\n\n\t}\n\n}\n\n\n\nvoid DMA1_Channel4_7_IRQHandler(void)\n\t{\n\n\t\tif(DMA1-&gt;ISR &amp; DMA_ISR_TCIF7)\n\t\t\t{\n\t\t\t\tinprogress=0;\n\t\t\t\tDMA1-&gt;IFCR|=DMA_IFCR_CTCIF7;\n\t\t\t}\n\t\tif(DMA1-&gt;ISR &amp; DMA_ISR_TCIF6)\n\t\t\t{\n\t\t\t\treceived=1;\n\t\t\t\tDMA1-&gt;IFCR|=DMA_IFCR_CTCIF6;\n\t\t\t}\n\t\tNVIC_ClearPendingIRQ(DMA1_Channel4_5_6_7_IRQn);\n\t}\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. Demo:<\/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-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Full Duplex UART using DMA on STM32L053\" width=\"1170\" height=\"878\" src=\"https:\/\/www.youtube.com\/embed\/vp9bwbstzps?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we saw how to configure the UART and DMA to work in transmit mode. In this guide, we shall see how to configure the DMA and UART to work in full duplex. In this guide, we shall cover the following: Configure UART in DMA Full duplex. Configure the DMA to [&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-1182","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\/1182"}],"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=1182"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1182\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1182\/revisions\/1186"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}