{"id":1894,"date":"2023-07-15T16:58:32","date_gmt":"2023-07-15T16:58:32","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1894"},"modified":"2023-07-15T16:58:34","modified_gmt":"2023-07-15T16:58:34","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part6-uart-in-tx-rx-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1894","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part6: UART in TX-RX Mode"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-1024x683.jpeg\" alt=\"\" class=\"wp-image-1895\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-4-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide, we took a look at building BSP for UART in TX mode. Now, we shall develop the BSP for UART receiver and use UART2 in full duplex mode.<\/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>Develop the header file.<\/li><li>Develop the source file.<\/li><li>Main.c <\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Develop the header file.<\/h2>\n\n\n\n<p>From the previous guide, open uart_bsp.h file and declare the following function:<\/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;}\">UART_StatusTypedef BSP_UART_Read_Char(USART_TypeDef *uart,char *recChar, uint32_t timeout);<\/pre><\/div>\n\n\n\n<p>The function will take three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pointer USART_Typedef to handle which UART to be used.<\/li><li>Pointer to hold the data to be read.<\/li><li>timeout to handle the time in the firmware.<\/li><li>Return UART_StatusTypedef.<\/li><\/ul>\n\n\n\n<p>Thats all for the header file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Develop the source file.<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open uart_bsp.c source file.<\/p>\n\n\n\n<p>Include the function:<\/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;}\">UART_StatusTypedef BSP_UART_Read_Char(USART_TypeDef *uart,char *recChar, uint32_t timeout)<\/pre><\/div>\n\n\n\n<p>In order to read a single character, the following steps are required:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Wait until the receiver buffer is filled.<\/li><li>Read the data register.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<p>Get the current tick and store it to indicate the start of the time:<\/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;}\">uint32_t start_ticks= BSP_Get_Ticks();<\/pre><\/div>\n\n\n\n<p>Wait until the buffer is filled using while loop:<\/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;}\">while(!(uart-&gt;SR &amp; USART_SR_RXNE))<\/pre><\/div>\n\n\n\n<p>Since we don&#8217;t want to get stuck here forever, we shall check for timeout, if timeout occurred, we shall return timeout 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;}\">\/*Time out management*\/\nif(BSP_Get_Ticks()-start_ticks&gt;timeout)\n{\t\/*Return if timedout, return timeout*\/\n\treturn UART_TIMEOUT;\n}<\/pre><\/div>\n\n\n\n<p>If the character is received before timeout, get the new data and store in the pointe 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 data*\/\n*recChar= uart-&gt;DR;<\/pre><\/div>\n\n\n\n<p>Return success:<\/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;}\">return UART_SUCCESS;<\/pre><\/div>\n\n\n\n<p>Hence the entire 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;}\">UART_StatusTypedef BSP_UART_Read_Char(USART_TypeDef *uart,char *recChar, uint32_t timeout)\n{\n\tuint32_t start_ticks= BSP_Get_Ticks();\n\n\t  \/*Make sure the receive data register is not empty*\/\n\t\twhile(!(uart-&gt;SR &amp; USART_SR_RXNE))\n\t\t{\n\t\t\t\/*Time out management*\/\n\t\t\tif(BSP_Get_Ticks()-start_ticks&gt;timeout)\n\t\t\t{\t\/*Return if timedout, return timeout*\/\n\t\t\t\treturn UART_TIMEOUT;\n\t\t\t}\n\t\t}\n\n\t\t\/*Read data*\/\n\t\t*recChar= uart-&gt;DR;\n\n\t\treturn UART_SUCCESS;\n\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\">3. Main.c:<\/h2>\n\n\n\n<p><\/p>\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;bsp.h&quot;\n#include &quot;uart_bsp.h&quot;\nvoid clock_config(void);\n\nchar uart_char;\n\n\n\nint main()\n{\n\n\tclock_config();\n\n\tGPIOA_CLOCK_ENABLE();\n\n\tGPIO_Configure_Typedef PA2_ALT;\n\n\tPA2_ALT.PinNumber=pin2;\n\tPA2_ALT.Mode=Alternate_function;\n\tPA2_ALT.AlternateType=AF7;\n\tGPIO_Initialization(GPIOA,&amp;PA2_ALT);\n\n\n\tGPIO_Configure_Typedef PA3_ALT;\n\n\tPA3_ALT.PinNumber=pin3;\n\tPA3_ALT.Mode=Alternate_function;\n\tPA3_ALT.AlternateType=AF7;\n\tGPIO_Initialization(GPIOA,&amp;PA3_ALT);\n\n\n\n\tUART2_CLOCK_ENABLE();\n\n\tUART_ConfigTypedef uart_config;\n\n\tuart_config.buadRate=115200;\n\tuart_config.busSpeed=50000000;\n\tuart_config.mode=UART_TX_RX;\n\n\tBSP_UART_Init(USART2, &amp;uart_config);\n\n\tBSP_Ticks_Init(100000000);\n\n\n\twhile(1)\n\t{\n\n\t\tif (BSP_UART_Read_Char(USART2, &amp;uart_char, 100)==UART_SUCCESS)\n\t\t{\n\t\tBSP_UART_Write_Char(USART2,uart_char,100);\n\t\tBSP_UART_Write_Char(USART2,'\\r',100);\n\t\tBSP_UART_Write_Char(USART2,'\\n',100);\n\t\t}\n\n\t}\n\n}\n\nvoid clock_config(void)\n{\n\tClock_Config_Typedef clockConfig;\n\n\tclockConfig.PLL_M= 4;\n\tclockConfig.PLL_N= 200;\n\tclockConfig.PLL_P= 4;\n\n\tclockConfig.AHB1Prescaler=AHB1_Prescaler1;\n\tclockConfig.APB1Prescaler=APB1_Prescaler2;\n\tclockConfig.APB2Prescaler=APB2_Prescaler1;\n\n\tclockConfig.clockSourc=External_Oscillator;\n\tclockConfig.flash_latency= Three_wait_state;\n\n\tClock_Configuration(&amp;clockConfig);\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. Code:<\/h2>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/BSP-STM32F411_Nucleo64-3.zip\">BSP-STM32F411_Nucleo64-3<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/BSP-STM32F411_Nucleo64-3.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\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open your serial terminal, set the baud rate to 115200 and send any character you will get the send character back:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"880\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-1024x880.png\" alt=\"\" class=\"wp-image-1897\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-1024x880.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-300x258.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-768x660.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-1150x988.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-750x645.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-400x344.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM-250x215.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-15-at-4.14.42-PM.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide, we took a look at building BSP for UART in TX mode. Now, we shall develop the BSP for UART receiver and use UART2 in full duplex mode. In this guide, we shall cover the following: Develop the header file. Develop the source file. Main.c Code. Results. 1. Develop the header [&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-1894","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\/1894"}],"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=1894"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions"}],"predecessor-version":[{"id":1898,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions\/1898"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}