{"id":924,"date":"2022-05-04T05:06:01","date_gmt":"2022-05-04T05:06:01","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=924"},"modified":"2022-05-04T05:07:02","modified_gmt":"2022-05-04T05:07:02","slug":"getting-started-with-stm32l053-uart-receiver-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=924","title":{"rendered":"Getting started with STM32L053: UART Receiver 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>In the previous guide (<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>), we took a look how to configure the UART of STM32L051 in transmit mode. Now we shall see how to configure the UART to receive data and echo back the received data (Transmit receive 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>Configure the UART in RX mode.<\/li><li>Code.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Configure the UART in RX mode:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>From the previous guide, we noticed that PA2 and PA3 are connected to UART of ST-Link and since PA2 and PA3 are USART2 of STM32L053, we can configure the PA2 to work in alternate function as following:<\/p>\n\n\n\n<p><\/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;}\">\tGPIOA-&gt;MODER|=GPIO_MODER_MODE3_1;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE3_0;<\/pre><\/div>\n\n\n\n<p>Then we can select which alternate 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;}\">#define AF04 0x04\nGPIOA-&gt;AFR[0]|=(AF04&lt;&lt;12);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Now we can enable the RX part in CR1 of USART2:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"360\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-1024x360.png\" alt=\"\" class=\"wp-image-925\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-1024x360.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-300x105.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-768x270.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-1536x540.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-2048x720.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-1150x404.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-750x264.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-400x141.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.47.19-AM-250x88.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/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_TE|USART_CR1_RE; \/*TE from previous guide*\/<\/pre><\/div>\n\n\n\n<p>And finally, enable UART <\/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    USART2-&gt;CR1 |= USART_CR1_UE;<\/pre><\/div>\n\n\n\n<p>Now we need to check if there is new data as following:<\/p>\n\n\n\n<p>We can check the RXNE (Read data register not empty) bit in USART interrupt and status register (USART_ISR) <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"360\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-1024x360.png\" alt=\"\" class=\"wp-image-926\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-1024x360.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-300x105.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-768x270.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-1536x540.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-2048x720.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-1150x404.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-750x264.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-400x141.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/Screen-Shot-2022-05-04-at-7.52.50-AM-250x88.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We can create a function that allows us to check this bit and return 1 if the receiver buffer is not empty 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;}\">\nint8_t uart2_data_available(void)\n{\n\t\/*Wait for char to arrive*\/\n\tif((USART2-&gt;ISR &amp; USART_ISR_RXNE)){return 1;}\n\telse return 0;\n\n}<\/pre><\/div>\n\n\n\n<p>And finally a function that allows us to read the content of the UART receiver 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;}\">char uart2_read(void)\n\t{\n\treturn(USART2-&gt;RDR);\n\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\">2. Code: <\/h2>\n\n\n\n<p>In the main.c file, we can check for the data is available and echo back the received character:<\/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;\n\n\nint main(void)\n{\n\tuart_init();\n\n\twhile(1)\n\t{\n\tif(uart2_data_available()==1)\n\t\t{\n\t\tuart2_write(uart2_read());\n\t\tuart2_write('\\r');uart2_write('\\n');\n\t\t}\n\n\n\t}\n\n}\n<\/pre><\/div>\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\/05\/UART_RX.zip\">UART_RX<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/UART_RX.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\">3. 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-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"UART Demo on STM32L051 Nucleo-64 Board\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/3c74-oQ8Zvw?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 took a look how to configure the UART of STM32L051 in transmit mode. Now we shall see how to configure the UART to receive data and echo back the received data (Transmit receive mode). In this guide, we shall cover the following: Configure the UART in RX mode. Code. [&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-924","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\/924"}],"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=924"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/924\/revisions"}],"predecessor-version":[{"id":929,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/924\/revisions\/929"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}