{"id":1250,"date":"2022-10-09T04:40:30","date_gmt":"2022-10-09T04:40:30","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1250"},"modified":"2022-10-09T11:17:03","modified_gmt":"2022-10-09T11:17:03","slug":"working-with-stm32f7-and-uart-part-4-full-duplex-using-dma-with-idle","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1250","title":{"rendered":"Working with STM32F7 and UART part 5: Full Duplex using DMA with IDLE"},"content":{"rendered":"\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.jpg\" alt=\"\" class=\"wp-image-330\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices.jpg 441w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-300x176.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-400x235.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/08\/UART-BUS-between-two-devices-250x147.jpg 250w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1241\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1241\">here<\/a>), we took a look how to configure the UART to transmit and receive data to\/from PC using DMA. The limitation that we shall wait until the buffer is filled before we print the the received characters. In this guide, we shall use IDLE Line interrupt to receive unknown length of characters.<\/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>What is IDLE line.<\/li><li>Configure UART for IDLE interrupt.<\/li><li>Support functions.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is IDLE Line:<\/h2>\n\n\n\n<p>The use of IDLE  is mostly related to&nbsp;<em>synchronous<\/em>&nbsp;communications (although you could use this concepts in asynchronous mode as well).<\/p>\n\n\n\n<p>Because there is a continuous bitstream flowing on the line in synchronous mode, there is no way to send&nbsp;<em>nothing<\/em>&nbsp;&#8211; To be able to do that, the IDLE pattern (normally all ones) is often used. It simply means there is no data on the line.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Configure UART for IDLE interrupt:<\/h2>\n\n\n\n<p>In order to enable IDLE line interrupt for UART, we need to set the IDLEIE bit in CR1:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"352\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-1024x352.png\" alt=\"\" class=\"wp-image-1251\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-1024x352.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-300x103.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-768x264.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-1536x529.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-2048x705.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-1150x396.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-750x258.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-400x138.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-09-at-7.25.06-AM-250x86.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;}\">USART3-&gt;CR1|=USART_CR1_IDLEIE;<\/pre><\/div>\n\n\n\n<p>Then enable USART interrupt in NVIC:<\/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;}\">NVIC_EnableIRQ(USART3_IRQn);<\/pre><\/div>\n\n\n\n<p>For the interrupt handler:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Check if the source is IDLE line.<\/li><li>If it is, set idle_detected to 1.<\/li><li>Clear the pending flag.<\/li><\/ul>\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 USART3_IRQHandler(void)\n\t{\n\t\tif(USART3-&gt;ISR &amp;USART_ISR_IDLE)\n\t\t\t{\n\t\t\t\tidle_detected_var=1;\n\t\t\t\tUSART3-&gt;ICR=USART_ICR_IDLECF;\n\t\t\t}\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>Also, within uart header, we shall declare the following enums:<\/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;}\">typedef enum\n\t{\n\t\tnoIDLE=0,\n\t\tIDLE=1\n\t}idle_enum;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Support functions:<\/h2>\n\n\n\n<p>The following functions are introduced:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>idle_detected<\/strong><\/li><li><strong>flush_buffer<\/strong><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>For idle_detected:<\/p>\n\n\n\n<p>Check the idle_detected, if no idle is detected, return noIDLE which is 0. Else, set the variable to 0 and return IDLE which is 1.<\/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;}\">uint8_t idle_detected()\n\t{\n\t\tif(idle_detected_var==noIDLE){return noIDLE;}\n\t\telse {idle_detected_var=noIDLE; return IDLE;}\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>For flush_buffer:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Disable the DMA Stream.<\/li><li>Clear the buffer.<\/li><li>Relaunch the DMA.<\/li><\/ul>\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 flush_buffer(uint8_t *buffer, uint16_t length)\n\t{\n\t\t\/*Disable the DMA*\/\n\t\tDMA1_Stream1-&gt;CR &amp;=~(DMA_SxCR_EN);\n\t\twhile((DMA1_Stream1-&gt;CR &amp;(DMA_SxCR_EN)));\n\t\t\/*Clear the buffer*\/\n\t\tmemset(buffer,'\\0',length);\n\t\t\/*Relaunch DMA*\/\n\t\tlaunch_rx_dma(buffer,length);\n\t}<\/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>In main.c:<\/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;uart.h&quot;\nchar uart_data_tx[40], uart_data_rx[200];\n\nuint8_t length;\n\nint main(void)\n\t{\n\t\tuart_tx_rx_dma_init();\n\t\tlaunch_rx_dma(&amp;uart_data_rx, 200);\n\t\twhile(1)\n\t\t\t{\n\t\t\t\tif(idle_detected()==IDLE){\n\t\t\t\tlength=sprintf(uart_data_tx,&quot;Received characters are: %s\\r\\n&quot;,uart_data_rx);\n\t\t\t\tusart_dma_write(uart_data_tx,length);\n\t\t\t\tflush_buffer(uart_data_rx, 200);\n\t\t\t\t}\n\t\t\t}\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\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\/2022\/10\/UART_RX_TX_DMA_IDLE.zip\">UART_RX_TX_DMA_IDLE<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/UART_RX_TX_DMA_IDLE.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Demo:<\/h2>\n\n\n\n<p>Open serial terminal and set the baudrate to 115200 and type any characters you need with any length and you should get something similar to this:<\/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=\"IDLE interrupt with DMA in STM32L053\" width=\"1170\" height=\"878\" src=\"https:\/\/www.youtube.com\/embed\/mS7swQjWRF4?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><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to configure the UART to transmit and receive data to\/from PC using DMA. The limitation that we shall wait until the buffer is filled before we print the the received characters. In this guide, we shall use IDLE Line interrupt to receive unknown length of [&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-1250","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\/1250"}],"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=1250"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1250\/revisions"}],"predecessor-version":[{"id":1254,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1250\/revisions\/1254"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}