{"id":667,"date":"2021-12-30T03:46:53","date_gmt":"2021-12-30T03:46:53","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=667"},"modified":"2021-12-30T03:48:07","modified_gmt":"2021-12-30T03:48:07","slug":"working-with-stm32f7-and-uart-part-2-receiving-a-single-character","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=667","title":{"rendered":"Working with STM32F7 and UART part 2: Receiving a single character"},"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=640\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=640\" target=\"_blank\">here<\/a>), we took a look how to configure the PD8 to work as USART3 TX. In this guide, we shall use PD9 to receive characters over UART and echo back the received characters.<\/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 pin and UART<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 Configure pin and UART:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>From the pervious, we enabled UART and GPIOD clock access. In order to add the RX functionality, we should set PD9.<\/p>\n\n\n\n<p>In order to set PD9 to act as UART3_RX, we need to set it as alternate function and select alternate function 7 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 AF07 0x07\n\nGPIOD-&gt;MODER|=GPIO_MODER_MODER9_1;\/\/set PD9 as alternate function\n\t\t\nGPIOD-&gt;AFR[1]|=(AF07&lt;&lt;4); \/\/selecting the alternate fuction<\/pre><\/div>\n\n\n\n<p>Now, for configuring the UART, we need to enable RX part from control register 1 (CR1)<\/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;}\">USART3-&gt;CR1|=USART_CR1_RE; \/\/enbale RX<\/pre><\/div>\n\n\n\n<p>Hence the initializing sequence is as follow:<\/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\t#define AF07 0x07\n\t\t\/\/Configure GPIOD PD8 as TX pin\n\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIODEN;\n\t\tGPIOD-&gt;MODER|=GPIO_MODER_MODER8_1;\n\t\t\n\t\tGPIOD-&gt;AFR[1]|=(AF07&lt;&lt;0);\n\t\t\/\/configure GPIOD PD9 as RX pin\n\t\tGPIOD-&gt;MODER|=GPIO_MODER_MODER9_1;\n\t\tGPIOD-&gt;AFR[1]|=(AF07&lt;&lt;4);\n\t\t\n\t\t\/\/Configure the UART\n\t\t\n\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART3EN;\n\t\tUSART3-&gt;BRR    = 0x008B; \/\/ baud rate 115200 @16MHz\n\t\tUSART3-&gt;CR1    = 0; \/\/ Enable Tx and Rx and Enable USART2\n\t\tUSART3-&gt;CR1|=USART_CR1_TE; \/\/enbale TX\n\t\tUSART3-&gt;CR1|=USART_CR1_RE; \/\/enbale RX\n\t\tUSART3-&gt;CR1|=USART_CR1_UE; \/\/enable UART<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 Checking for availability of the data:<\/h2>\n\n\n\n<p>We can check for the data availability bit 5 (RXNE) from Interrupt and status register (USART_ISR) as following<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"362\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-1024x362.png\" alt=\"\" class=\"wp-image-668\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-1024x362.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-300x106.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-768x272.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-1536x544.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-2048x725.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-1150x407.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-750x265.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-400x142.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-30-at-6.40.14-AM-250x88.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;}\">int uart3_available(void)\n{\nif((USART3-&gt;ISR)&amp;USART_ISR_RXNE){return 1;}\nelse {return 0;}\n}<\/pre><\/div>\n\n\n\n<p>if the serial has new data, we can read it 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;}\">\/\/read the RX buffer\nunsigned char uart3_read(void)\n{\nreturn USART3-&gt;RDR;\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>The code for receiving a character and echo back<\/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;stm32f7xx.h&quot;                  \/\/ Device header\n\n\/\/PD8 for USART3 TX\n\nvoid uart3_write(unsigned char x)\n{\n\nUSART3-&gt;TDR =(x);\nwhile(!((USART3-&gt;ISR)&amp;USART_ISR_TC)){;}\n}\n\/\/if there is data in the buffer\nint uart3_available(void)\n{\nif((USART3-&gt;ISR)&amp;USART_ISR_RXNE){return 1;}\nelse {return 0;}\n\n}\n\/\/read the RX buffer\nunsigned char uart3_read(void)\n{\nreturn USART3-&gt;RDR;\n}\n\n\nint main(void)\n\t{\n\t\t#define AF07 0x07\n\t\t\/\/Configure GPIOD PD8 as TX pin\n\t\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIODEN;\n\t\tGPIOD-&gt;MODER|=GPIO_MODER_MODER8_1;\n\t\t\n\t\tGPIOD-&gt;AFR[1]|=(AF07&lt;&lt;0);\n\t\t\/\/configure GPIOD PD9 as RX pin\n\t\tGPIOD-&gt;MODER|=GPIO_MODER_MODER9_1;\n\t\tGPIOD-&gt;AFR[1]|=(AF07&lt;&lt;4);\n\t\t\n\t\t\/\/Configure the UART\n\t\t\n\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART3EN;\n\t\tUSART3-&gt;BRR    = 0x008B; \/\/ baud rate 115200 @16MHz\n\t\tUSART3-&gt;CR1    = 0; \/\/ Enable Tx and Rx and Enable USART2\n\t\tUSART3-&gt;CR1|=USART_CR1_TE; \/\/enbale TX\n\t\tUSART3-&gt;CR1|=USART_CR1_RE; \/\/enbale RX\n\t\tUSART3-&gt;CR1|=USART_CR1_UE; \/\/enable UART\n\t\t\n\twhile(1)\n\t{\n\t\tif(uart3_available()) \/\/if there is new data \n\t\t\t\t{\n\t\t\t\tuart3_write(uart3_read()); \/\/read and send it back\n\t\t\t\t}\n\t}\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Recording-2021-12-30-at-6.25.22-AM.mov\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to configure the PD8 to work as USART3 TX. In this guide, we shall use PD9 to receive characters over UART and echo back the received characters. In this guide, we shall cover the following: Configure pin and UART Code Demo 1.1 Configure pin and [&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-667","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\/667"}],"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=667"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":671,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions\/671"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}