{"id":930,"date":"2022-05-06T16:57:38","date_gmt":"2022-05-06T16:57:38","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=930"},"modified":"2022-05-06T16:59:42","modified_gmt":"2022-05-06T16:59:42","slug":"working-with-stm32-and-uart-part-6-idle-line-interrupt-with-dma","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=930","title":{"rendered":"Working with STM32 and UART part 6: IDLE Line interrupt with DMA"},"content":{"rendered":"\n<p>In pervious guides(<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=442\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=442\" target=\"_blank\">here<\/a>), we took look how to receive 5 characters from DMA and echo back the sent characters. However, the limitation is to need the number of received character in advanced which will limit the capability of the firmware utilitized the driver. In this guide, we shall see how configure the UART to detect idle line and generate interrupt to indicate the the last character is received.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"259\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2.jpg\" alt=\"\" class=\"wp-image-348\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2.jpg 441w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2-300x176.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2-400x235.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-2-250x147.jpg 250w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/figure><\/div>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is IDLE line detection.<\/li><li>IDLE line configuration.<\/li><li>DMA configuration.<\/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. What is IDLE line detection:<\/h2>\n\n\n\n<p>IDLE line event is triggered if UART RX line is inactive for one frame. The frame time depends on the baudrate. Higher baudrate, lower frame.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"433\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-1024x433.png\" alt=\"\" class=\"wp-image-931\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-1024x433.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-300x127.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-768x325.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-1536x650.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-2048x867.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-1150x487.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-750x317.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-400x169.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.43.22-PM-250x106.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<h2 class=\"wp-block-heading\">2. IDLE Line configuration:<\/h2>\n\n\n\n<p>We start of by enabling clock access to Port A and USART2 and configure PA2 and PA3 to work in USART 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;}\">*Enable clock access to GPIOA and USART2*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\t\/*Configure the GPIO for UART Mode*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE2_1;\/\/set bit5\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE2_0;\/\/reset bit4\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE3_1;\/\/set bit7\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE3_0;\/\/reset bit6\n\t#define AF07 0x07\n\tGPIOA-&gt;AFR[0]|=(AF07&lt;&lt;GPIO_AFRL_AFSEL2_Pos)|(AF07&lt;&lt;GPIO_AFRL_AFSEL3_Pos); \/\/ALT7 for UART2 (PA2 and PA3)<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Then we configure UART as following:<\/p>\n\n\n\n<p>Set baud rate to 9600 and enable TX\/RT for the UART:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"286\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-1024x286.png\" alt=\"\" class=\"wp-image-932\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-1024x286.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-300x84.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-768x215.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-1536x429.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-2048x572.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-1150x321.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-750x210.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-400x112.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-06-at-7.47.34-PM-250x70.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;}\">\/*Configure UART*\/\n\tUSART2-&gt;BRR  = 0x0681;    \/\/9600 @16MHz\n\tUSART2-&gt;CR1|=USART_CR1_TE|USART_CR1_RE;<\/pre><\/div>\n\n\n\n<p>Enable ILDE line 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;}\">USART2-&gt;CR1|=USART_CR1_IDLEIE;<\/pre><\/div>\n\n\n\n<p>Enable DMA for RX in CR3 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;}\">USART2-&gt;CR3|=USART_CR3_DMAR;<\/pre><\/div>\n\n\n\n<p>Enable interrupt and UART 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;}\">\tNVIC_EnableIRQ(USART2_IRQn);\n\tUSART2-&gt;CR1|=USART_CR1_UE;<\/pre><\/div>\n\n\n\n<p>For uart 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 USART2_IRQHandler(void)\n{\n    if (USART2-&gt;SR &amp; USART_SR_IDLE) {\n\n    done =1;\n    (void)USART2-&gt;DR;\n\tDMA1_Stream5-&gt;CR &amp;= ~DMA_SxCR_EN;         \/* disable DMA1 Stream 5 *\/\n\twhile (DMA1_Stream5-&gt;CR &amp; DMA_SxCR_EN) {} \/* wait until DMA1 Stream 5 is disabled *\/\n\n    }\n\n}<\/pre><\/div>\n\n\n\n<p>In order to clear the IDLE line interrupt, we need to read the SR followed by reading the DR register.<\/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>The DMA configuration is similar to the previous guide and the difference is we shall not use circular 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;}\">RCC-&gt;AHB1ENR |= RCC_AHB1ENR_DMA1EN;\n\tDMA1_Stream5-&gt;CR &amp;= DMA_SxCR_EN;         \/* disable DMA1 Stream 5 *\/\n\twhile (DMA1_Stream5-&gt;CR &amp; DMA_SxCR_EN) {} \/* wait until DMA1 Stream 5 is disabled *\/\n\n\tDMA1-&gt;HIFCR|=DMA_HIFCR_CDMEIF5|DMA_HIFCR_CFEIF5|DMA_HIFCR_CTCIF5|DMA_HIFCR_CHTIF5|DMA_HIFCR_CTCIF5;\n\n\tDMA1_Stream5-&gt;PAR =(uint32_t)&amp;USART2-&gt;DR;\n\tDMA1_Stream5-&gt;M0AR = (uint32_t)(&amp;char_data[0]);\n\tDMA1_Stream5-&gt;NDTR = buffer_size;\n\n\tDMA1_Stream5-&gt;CR =(1&lt;&lt;27);\n\tDMA1_Stream5-&gt;CR|=(1&lt;&lt;10)|(1&lt;&lt;4);\n\tDMA1_Stream5-&gt;CR|=(1&lt;&lt;0);<\/pre><\/div>\n\n\n\n<p>DMA 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;}\">\n\nvoid DMA1_Stream5_IRQHandler(void)\n\t\t{\n\t\tif((DMA1-&gt;HISR)&amp;(1&lt;&lt;11)) \/\/DMA receive complete\n\t\t\t{\n\t\t\tDMA1-&gt;HIFCR|=(1&lt;&lt;11);\n\t\t\t}\n\t\t}\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>The entire code can be found here:<\/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 &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdio.h&gt;\n\n#define buffer_size 100\nuint8_t char_data[buffer_size];\nvolatile uint8_t done=0,done_rec;\n#define AF07 0x07\n\nvoid UART_Write_String(char *p);\nvoid flush_buffer();\nvoid reload();\n\nint main(void)\n{\n\t\/*Enable clock access to GPIOA and USART2*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\t\/*Configure the GPIO for UART Mode*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE2_1;\/\/set bit5\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE2_0;\/\/reset bit4\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE3_1;\/\/set bit7\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE3_0;\/\/reset bit6\n\tGPIOA-&gt;AFR[0]|=(AF07&lt;&lt;GPIO_AFRL_AFSEL2_Pos)|(AF07&lt;&lt;GPIO_AFRL_AFSEL3_Pos); \/\/ALT7 for UART2 (PA2 and PA3)\n\n\t\/*Configure UART*\/\n\tUSART2-&gt;BRR  = 0x0681;    \/\/9600 @16MHz\n\tUSART2-&gt;CR1|=USART_CR1_TE|USART_CR1_RE;\n\tUSART2-&gt;CR1|=USART_CR1_IDLEIE;\n\tUSART2-&gt;CR3|=USART_CR3_DMAR;\n\tNVIC_EnableIRQ(USART2_IRQn);\n\tUSART2-&gt;CR1|=USART_CR1_UE;\n\t\/*Configure the DMA*\/\n\tRCC-&gt;AHB1ENR |= RCC_AHB1ENR_DMA1EN;\n\tDMA1_Stream5-&gt;CR &amp;= DMA_SxCR_EN;         \/* disable DMA1 Stream 5 *\/\n\twhile (DMA1_Stream5-&gt;CR &amp; DMA_SxCR_EN) {} \/* wait until DMA1 Stream 5 is disabled *\/\n\t\/*Clear DMA1_Stream5 interrupts*\/\n\tDMA1-&gt;HIFCR|=DMA_HIFCR_CDMEIF5|DMA_HIFCR_CFEIF5|DMA_HIFCR_CTCIF5|DMA_HIFCR_CHTIF5|DMA_HIFCR_CTCIF5;\n\t\/*Memory and peripheral address and number of transfers*\/\n\tDMA1_Stream5-&gt;PAR =(uint32_t)&amp;USART2-&gt;DR;\n\tDMA1_Stream5-&gt;M0AR = (uint32_t)(&amp;char_data[0]);\n\tDMA1_Stream5-&gt;NDTR = buffer_size;\n\t\/*Confgiure the DMA*\/\n\tDMA1_Stream5-&gt;CR =(1&lt;&lt;27);\n\tDMA1_Stream5-&gt;CR|=(1&lt;&lt;10)|(1&lt;&lt;4);\n\tDMA1_Stream5-&gt;CR|=(1&lt;&lt;0);\n\tUART_Write_String(&quot;hello world\\r\\n&quot;);\n\twhile(1)\n\t\t{\n\n\t\twhile (done==0);\n\t\tdone=0;\n\t\tUART_Write_String((char*)char_data);\n\t\tflush_buffer();\n\t\treload();\n\n\n\t\t}\n\n}\n\n\nvoid USART_write(int ch){\nwhile(!(USART2-&gt;SR&amp;0x0080)){\n}\nUSART2-&gt;DR=(ch);\n}\n\nvoid UART_Write_String(char *p)\n\t{\n\twhile(*p!='\\0')\n{\nUSART_write(*p);\np++;\n}\n}\n\nvoid flush_buffer()\n\t{\n\tmemset(char_data, 0, buffer_size);\n\t}\n\nvoid reload()\n\t{\n\n\tDMA1-&gt;HIFCR|=DMA_HIFCR_CDMEIF5|DMA_HIFCR_CFEIF5|DMA_HIFCR_CTCIF5|DMA_HIFCR_CHTIF5|DMA_HIFCR_CTCIF5;\n\tDMA1_Stream5-&gt;PAR =(uint32_t)&amp;USART2-&gt;DR;\n\tDMA1_Stream5-&gt;M0AR = (uint32_t)(&amp;char_data[0]);\n\tDMA1_Stream5-&gt;NDTR = buffer_size;\n\tDMA1_Stream5-&gt;CR |= DMA_SxCR_EN;\n\t}\n\n\nvoid USART2_IRQHandler(void)\n{\n    if (USART2-&gt;SR &amp; USART_SR_IDLE) {\n\n    done =1;\n    (void)USART2-&gt;DR;\n\tDMA1_Stream5-&gt;CR &amp;= ~DMA_SxCR_EN;         \/* disable DMA1 Stream 5 *\/\n\twhile (DMA1_Stream5-&gt;CR &amp; DMA_SxCR_EN) {} \/* wait until DMA1 Stream 5 is disabled *\/\n\n    }\n\n}\n\n\n\nvoid DMA1_Stream5_IRQHandler(void)\n\t\t{\n\t\tif((DMA1-&gt;HISR)&amp;(1&lt;&lt;11)) \/\/DMA receive complete\n\t\t\t{\n\t\t\tDMA1-&gt;HIFCR|=(1&lt;&lt;11);\n\t\t\t}\n\t\t}\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<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=\"IDLE line on STM32F4\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/ykhglXzElnY?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 pervious guides(here), we took look how to receive 5 characters from DMA and echo back the sent characters. However, the limitation is to need the number of received character in advanced which will limit the capability of the firmware utilitized the driver. In this guide, we shall see how configure the UART to detect [&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-930","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\/930"}],"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=930"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/930\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/930\/revisions\/934"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}