{"id":1889,"date":"2023-07-11T13:27:35","date_gmt":"2023-07-11T13:27:35","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1889"},"modified":"2023-07-11T13:27:40","modified_gmt":"2023-07-11T13:27:40","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part5-uart-in-tx-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1889","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part5: UART in TX 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-3-1024x683.jpeg\" alt=\"\" class=\"wp-image-1890\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-3-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1883\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1883\" target=\"_blank\">here<\/a>), we took a look how to configure a pin to work in alternate function. In this guide, we shall take this further by connecting the configured GPIO in alternate function to UART.<\/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>Creating UART Board Support Package header file.<\/li><li>Creating UART Board Support Package source file.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Creating UART Board Support Package header file:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Before going into the creating the header file, refer to this topic about uart <a href=\"https:\/\/blog.embeddedexpert.io\/?p=329\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=329\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by creating new header file with name of uart_bsp.h <\/p>\n\n\n\n<p>After creating the header file, include the following header files:<\/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;stdint.h&quot;\n\n#include &quot;stm32f4xx.h&quot;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>We shall create enum to hold the status of UART for the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>UART_SUCCESS<\/em>.<\/li><li><em>UART_FAILED<\/em>.<\/li><li><em>UART_TIMEOUT<\/em>.<\/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;}\">typedef enum\n{\n\tUART_SUCCESS=0,\n\tUART_FAILED =-1,\n\tUART_TIMEOUT=-2\n}UART_StatusTypedef;<\/pre><\/div>\n\n\n\n<p>We shall also declare enum to hold the mode of UART:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>TX only.<\/li><li>RX only.<\/li><li>TX and RX.<\/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;}\">typedef enum\n{\n\n\tUART_TX_Only=0,\n\tUART_RX_Only=1,\n\tUART_TX_RX=2\n\n}UART_DirectionTypedef;<\/pre><\/div>\n\n\n\n<p>Create new struct to hold the configuration parameters for the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Bus Speed.<\/li><li>Buad Rate.<\/li><li>Mode.<\/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;}\">typedef struct\n{\n\tuint32_t busSpeed;\n\tuint32_t buadRate;\n\tuint8_t mode;\n\n}UART_ConfigTypedef;<\/pre><\/div>\n\n\n\n<p>Declare the following three functions:<\/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 UART2_CLOCK_ENABLE(void);\n\nvoid BSP_UART_Init(USART_TypeDef *uart, UART_ConfigTypedef *config);\n\nUART_StatusTypedef BSP_UART_Write_Char(USART_TypeDef *uart,char ch, uint32_t timeout);\n<\/pre><\/div>\n\n\n\n<p>First one to enable clock access to UART2.<\/p>\n\n\n\n<p>The second one to configure the UART.<\/p>\n\n\n\n<p>Third one to write a single character to UART.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire header file 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;}\">#ifndef UART_BSP_H_\n#define UART_BSP_H_\n\n#include &quot;stdint.h&quot;\n\n#include &quot;stm32f4xx.h&quot;\n\n\ntypedef enum\n{\n\tUART_SUCCESS=0,\n\tUART_FAILED =-1,\n\tUART_TIMEOUT=-2\n}UART_StatusTypedef;\n\n\/**\n * @brief This enum represents the direction of UART\n *\/\n\ntypedef enum\n{\n\n\tUART_TX_Only=0,\n\tUART_RX_Only=1,\n\tUART_TX_RX=2\n\n}UART_DirectionTypedef;\n\n\ntypedef struct\n{\n\tuint32_t busSpeed;\n\tuint32_t buadRate;\n\tuint8_t mode;\n\n}UART_ConfigTypedef;\n\nvoid UART2_CLOCK_ENABLE(void);\n\nvoid BSP_UART_Init(USART_TypeDef *uart, UART_ConfigTypedef *config);\n\nUART_StatusTypedef BSP_UART_Write_Char(USART_TypeDef *uart,char ch, uint32_t timeout);\n\n#endif \/* UART_BSP_H_ *\/\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Creating UART Board Support Package source code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Create new source file with name of uart_bsp.c <\/p>\n\n\n\n<p>Within the source file, include the 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;}\">#include &quot;uart_bsp.h&quot;\n#include &quot;bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the following prototypes of static functions:<\/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;}\">static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,  uint32_t BaudRate);\nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate);<\/pre><\/div>\n\n\n\n<p>Those function will set the baudrate.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For enabling clock access to UART2 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;}\">void UART2_CLOCK_ENABLE(void)\n{\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\n}<\/pre><\/div>\n\n\n\n<p>For board support package uart intiailization 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;}\">void BSP_UART_Init(USART_TypeDef *uart, UART_ConfigTypedef *config)\n{\n\tswitch (config-&gt;mode)\n\t{\n\t\tcase UART_TX_Only: \tuart-&gt;CR1=USART_CR1_TE; break;\n\n\t\tcase UART_RX_Only:  uart-&gt;CR1=USART_CR1_RE; break;\n\n\t\tcase UART_TX_RX:\tuart-&gt;CR1=USART_CR1_RE|USART_CR1_TE; break;\n\t\t\n\t\tdefault : break;\n\t}\n\n\tuart_set_baudrate(uart, config-&gt;busSpeed, config-&gt;buadRate);\n\n\tuart-&gt;CR1|= USART_CR1_UE;\n\n}<\/pre><\/div>\n\n\n\n<p>For writing a single character 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_Write_Char(USART_TypeDef *uart,char ch, uint32_t timeout)\n{\n\tuint32_t start_ticks= BSP_Get_Ticks();\n\n\t\/*Make sure the transmit data register is empty*\/\n\twhile(!(uart-&gt;SR &amp; USART_SR_TXE))\n\t{\n\t\tif(BSP_Get_Ticks()-start_ticks&gt;timeout)\n\t\t{\n\t\t\treturn UART_TIMEOUT;\n\t\t}\n\t}\n\n\t\/*Write to transmit data register*\/\n\tuart-&gt;DR\t=  (ch &amp; 0xFF);\n\n\treturn UART_SUCCESS;\n}\n<\/pre><\/div>\n\n\n\n<p>For setting the baud rate functions:<\/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;}\">static void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,  uint32_t BaudRate)\n{\n\tUSARTx-&gt;BRR =  compute_uart_bd(PeriphClk,BaudRate);\n}\n\nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate)\n{\n\treturn ((PeriphClk + (BaudRate\/2U))\/BaudRate);\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Note: all those steps have been mentioned <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=329\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>Hence, the entire source code 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;}\">#include &quot;uart_bsp.h&quot;\n#include &quot;bsp.h&quot;\n\n\nstatic void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,  uint32_t BaudRate);\nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate);\n\n\n\nvoid UART2_CLOCK_ENABLE(void)\n{\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_USART2EN;\n}\n\nvoid BSP_UART_Init(USART_TypeDef *uart, UART_ConfigTypedef *config)\n{\n\tswitch (config-&gt;mode)\n\t{\n\t\tcase UART_TX_Only: \tuart-&gt;CR1=USART_CR1_TE; break;\n\n\t\tcase UART_RX_Only:  uart-&gt;CR1=USART_CR1_RE; break;\n\n\t\tcase UART_TX_RX:\tuart-&gt;CR1=USART_CR1_RE|USART_CR1_TE; break;\n\t\t\n\t\tdefault : break;\n\t}\n\n\tuart_set_baudrate(uart, config-&gt;busSpeed, config-&gt;buadRate);\n\n\tuart-&gt;CR1|= USART_CR1_UE;\n\n}\n\n\n\nUART_StatusTypedef BSP_UART_Write_Char(USART_TypeDef *uart,char ch, uint32_t timeout)\n{\n\tuint32_t start_ticks= BSP_Get_Ticks();\n\n\t\/*Make sure the transmit data register is empty*\/\n\twhile(!(uart-&gt;SR &amp; USART_SR_TXE))\n\t{\n\t\tif(BSP_Get_Ticks()-start_ticks&gt;timeout)\n\t\t{\n\t\t\treturn UART_TIMEOUT;\n\t\t}\n\t}\n\n\t\/*Write to transmit data register*\/\n\tuart-&gt;DR\t=  (ch &amp; 0xFF);\n\n\treturn UART_SUCCESS;\n}\n\n\n\n\nstatic void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t PeriphClk,  uint32_t BaudRate)\n{\n\tUSARTx-&gt;BRR =  compute_uart_bd(PeriphClk,BaudRate);\n}\n\nstatic uint16_t compute_uart_bd(uint32_t PeriphClk, uint32_t BaudRate)\n{\n\treturn ((PeriphClk + (BaudRate\/2U))\/BaudRate);\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. Code:<\/h2>\n\n\n\n<p>You may download the entire source 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-2.zip\">BSP-STM32F411_Nucleo64-2<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/BSP-STM32F411_Nucleo64-2.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\">4. Results:<\/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\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\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_Only;\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\tBSP_UART_Write_Char(USART2,'A',10);\n\t\tBSP_UART_Write_Char(USART2,'\\r',10);\n\t\tBSP_UART_Write_Char(USART2,'\\n',10);\n\t\tBSP_Delay(1000);\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>You should get the following results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"967\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-1024x967.png\" alt=\"\" class=\"wp-image-1892\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-1024x967.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-300x283.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-768x726.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-1150x1086.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-750x709.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-400x378.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM-250x236.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-11-at-4.01.48-PM.png 1340w\" 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 (here), we took a look how to configure a pin to work in alternate function. In this guide, we shall take this further by connecting the configured GPIO in alternate function to UART. In this guide, we shall cover the following: Creating UART Board Support Package header file. Creating UART Board [&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-1889","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\/1889"}],"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=1889"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1889\/revisions"}],"predecessor-version":[{"id":1893,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1889\/revisions\/1893"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}