{"id":2316,"date":"2024-02-02T04:46:29","date_gmt":"2024-02-02T04:46:29","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2316"},"modified":"2024-02-02T04:46:32","modified_gmt":"2024-02-02T04:46:32","slug":"getting-started-with-stm32g0-and-stm32cubeide-uart-transmit","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2316","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: UART Transmit"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg.webp\" alt=\"\" class=\"wp-image-2317\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall take a look at the UART protocol and how to setup UART using STM32CubeMX.<\/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 UART.<\/li><li>UART Setup using STM32CubeMX.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is UART:<\/h2>\n\n\n\n<p>To transfer data, computers adopt two method: serial and parallel. In serial communication data is sent a bit at a time (1 line)while in parallel communication 8 or more lines are often used to transfer data. The UART is a form of serial communication. When distance is short, digital signal can be transferred as it is on a single wire and require no modulation. This is how PC keyboard transfer data between the keyboard and motherboard. For long distances like telephone lines, serial data communication requires a modem to modulate.We will not go into the details of this in this lesson, this shall be left for subsequent lessons.&nbsp;<\/p>\n\n\n\n<p><strong>Synchronous vs. Asynchronous<\/strong><\/p>\n\n\n\n<p>Serial communication operate in two ways : synchronous and asynchronous. In the synchronous mode, a block of data is sent at a time while in the asynchronous mode, a single byte of data is sent at a time. It is possible to write code to provide both the synchronous and asynchronous serial communication functions however this could turn out tedious and over complicated therefore, special chips are manufactured to perform these functions. When these chips are added to a microcontroller, they become known as Serial Communication Interface (SCI). The chip that provides the the asynchronous communication (<strong>UART<\/strong>) is the main theme of this lesson. The chip for synchronous communication is known as the&nbsp;<strong>USART<\/strong>&nbsp;and shall left for another lesson.&nbsp;<\/p>\n\n\n\n<p><strong>Asynchronous Communication Data Framing<\/strong><\/p>\n\n\n\n<p>The data is received in zeros and ones format, in order to make sense of the data, the sender and the receiver must agree on a set of rules i.e. a protocol, on how data is packed, how many bits constitute a character and when data begins and ends.<\/p>\n\n\n\n<p><strong>The Protocol<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Start and Stop bits<\/strong><\/li><\/ol>\n\n\n\n<p>Asynchronous serial communication is widely used in character transfer. Each character is packed between start and stop bits. This is known as the framing.<\/p>\n\n\n\n<p><strong>start bit<\/strong>&nbsp;: &nbsp;Always&nbsp;<strong>1<\/strong>&nbsp;bit, value is always&nbsp;<strong>0<\/strong><\/p>\n\n\n\n<p><strong>stop bit<\/strong>: &nbsp;<strong>&nbsp;1<\/strong>&nbsp;or&nbsp;<strong>2<\/strong>&nbsp;bits, value is always<strong>&nbsp;1<\/strong><\/p>\n\n\n\n<p>Lets take an example. Lets say we are transferring ASCII character \u2018A\u2019&nbsp;<\/p>\n\n\n\n<p>its binary code is 0100 0001 and 0x41 in hex.Furthermore, it is framed between a start bit and 2 stop bits. This is what its frame will look like.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"297\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Introduction-to-UART-Packet-Frame-and-Bits-2.png\" alt=\"\" class=\"wp-image-2318\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Introduction-to-UART-Packet-Frame-and-Bits-2.png 665w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Introduction-to-UART-Packet-Frame-and-Bits-2-300x134.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Introduction-to-UART-Packet-Frame-and-Bits-2-400x179.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/Introduction-to-UART-Packet-Frame-and-Bits-2-250x112.png 250w\" sizes=\"(max-width: 665px) 100vw, 665px\" \/><\/figure><\/div>\n\n\n\n<p>&nbsp;2.<strong>&nbsp;Parity bit<\/strong><\/p>\n\n\n\n<p>Some systems require parity bit in order to maintain data integrity.The parity bit of a character byte is included in the data frame after the stop bit(s).<\/p>\n\n\n\n<p>&nbsp;3.<strong>Rate of data transfer : Baud rate\/bps<\/strong><\/p>\n\n\n\n<p>We shall explain this concept with an example. Lets say we are asked to calculate the number of bits used &nbsp;and time taken in transferring 50 pages of text, each with 80\u00d725 characters. Assuming 8 bits per character and 1 stop bit<\/p>\n\n\n\n<p><em>solution<\/em><\/p>\n\n\n\n<p>For each character a total of 10 bits is used ( 1 start bit, 8 bits character, 1 stop bit).<\/p>\n\n\n\n<p>Therefore total number of bits &nbsp;=&nbsp;80 x 25 x10 = 20,000 bits per page<\/p>\n\n\n\n<p>50 pages &nbsp;implies,&nbsp;50\u00d720,000 &nbsp;= &nbsp;1,000,000 bits.<\/p>\n\n\n\n<p>Therefore it will take 1 million bits to transfer &nbsp;this information.<\/p>\n\n\n\n<p>The time it takes to transfer the entire data using<\/p>\n\n\n\n<p><strong>a<\/strong>. 9600 baudrate implies, &nbsp;1,000,000 \/ 9600 = 204 seconds<\/p>\n\n\n\n<p><strong>b<\/strong>. 57,600 baudrate implies&nbsp;1,000,000 \/57,600 = 17 seconds<\/p>\n\n\n\n<p>Baudrate simply means the transfer of bits per second. There are various standardized baudrate we can choose from when we program our serial communication devices. \u00a0The key thing is, both communication devices must have the same baudrate. We shall see this later on in our example code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. UART Setup in STM32CubeMX:<\/h2>\n\n\n\n<p>Open STM32CubeIDE and create new project and name it UART. For more information how to setup the project using STM32CubeIDE, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2299\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>After that, Open connectivity and enable UART2 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"567\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-1024x567.jpg\" alt=\"\" class=\"wp-image-2319\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-1024x567.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-300x166.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-768x425.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-1536x851.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-2048x1134.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-1150x637.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-750x415.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-400x221.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-07-25-250x138.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Set the mode to be Asynchronous and leave the parameter to default values.<\/p>\n\n\n\n<p>Yo will notice that PA2 and PA3 has been enabled automatically.<\/p>\n\n\n\n<p>PA2 is the UART TX while PA3 is UART RX.<\/p>\n\n\n\n<p>Now, save the project and this will generate the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>Now within USER CODE BEGIN Includes, include the stdio.h header 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;}\">#include &quot;stdio.h&quot;<\/pre><\/div>\n\n\n\n<p>Now, within USER CODE BEGIN PV, declare an array to hold the characters to be transmitted 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;}\">char uart_data[100];<\/pre><\/div>\n\n\n\n<p>Also, declare the counter value which will be incremented in the while loop 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;}\">uint32_t counter;<\/pre><\/div>\n\n\n\n<p>In USER CODE BEGIN 3, declare a new variable called len 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;}\">uint32_t len;<\/pre><\/div>\n\n\n\n<p>This will hold the length of the string to be transmitted.<\/p>\n\n\n\n<p>Now, we shall use sprintf to create character array to be transmitted over UART 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;}\">len = sprintf(uart_data,&quot;Counter value is %d\\r\\n&quot;,counter++);<\/pre><\/div>\n\n\n\n<p>Since sprintf returns number of characters, we shall use it.<\/p>\n\n\n\n<p>Next, we shall transmit data 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;}\">HAL_UART_Transmit(&amp;huart2, uart_data, len, 100);<\/pre><\/div>\n\n\n\n<p>The function HAL_UART_Transmit will transmit data over uart using blocking mode.<\/p>\n\n\n\n<p>The parameters of the function as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>UART handle typedef is which UART to be used (UART2 in this case).<\/li><li>Pointer to the data array to be transmitted.<\/li><li>Length of the data to be transmitted.<\/li><li>Time out which if the function spend more time that the specified, it will return time out.<\/li><\/ul>\n\n\n\n<p>The function shall return one of these three:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>HAL_OK means everything has been transmitted.<\/li><li>HAL_ERROR, an error occurred like invalid parameter etc.<\/li><li>HAL_BUSY, the peripheral is currently occupied.<\/li><li>HAL_TIMEOUT means the function took longer than expected and exited.<\/li><\/ul>\n\n\n\n<p>Finally, delay by 100ms 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;}\">HAL_Delay(100);<\/pre><\/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>Run the code on your STM32G0x0 and select the baudrate to be 115200, you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"733\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-1024x733.jpg\" alt=\"\" class=\"wp-image-2320\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-1024x733.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-300x215.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-768x550.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-1150x823.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-750x537.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-400x286.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53-250x179.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-02_07-14-53.jpg 1168w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at the UART protocol and how to setup UART using STM32CubeMX. In this guide, we shall cover the following: What is UART. UART Setup using STM32CubeMX. Code. Results. 1. What is UART: To transfer data, computers adopt two method: serial and parallel. In serial communication data is [&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-2316","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\/2316"}],"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=2316"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2316\/revisions"}],"predecessor-version":[{"id":2321,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2316\/revisions\/2321"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}