{"id":2351,"date":"2024-02-15T13:38:11","date_gmt":"2024-02-15T13:38:11","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2351"},"modified":"2024-02-15T13:38:13","modified_gmt":"2024-02-15T13:38:13","slug":"getting-started-with-stm32g0-and-stm32cubeide-uart-interrupt-receiver","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2351","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: UART Interrupt Receiver"},"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-2.webp\" alt=\"\" class=\"wp-image-2352\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\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=2316\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2316\" target=\"_blank\">here<\/a>), we took a look at the UART and we were able to configure the STM32G070 to transmit data. Now, we shall receive data using UART in interrupt mode and echo back the received data.<\/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>Enable USART2 interrupt.<\/li><li>Code developing.<\/li><li>Results. <\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Enable USART2 Interrupt:<\/h2>\n\n\n\n<p>By continue from the previous guide, open UART.ioc (depending on the name of the project name), Connectivity, then USART2, NVIC Settings and enable the USART2 Global interrupt as shown in the figure below<\/p>\n\n\n\n<p><\/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-15_15-40-20-1024x567.jpg\" alt=\"\" class=\"wp-image-2353\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-1024x567.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-300x166.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-768x425.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-1536x851.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-2048x1134.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-1150x637.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-750x415.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-400x221.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-40-20-250x138.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Save the project to generate the code.<\/p>\n\n\n\n<p>Now, the USART2 is configured with interrupt capability.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Code Developing:<\/h2>\n\n\n\n<p>Since we are dealing with interrupt, HAL offers some interrupt drivers for UART interrupt. <\/p>\n\n\n\n<p>From project tree, open drivers, STM32G0xx_HAL_DRIVER, then SRC then stm32g0xx_hal_uart.c<\/p>\n\n\n\n<p>At line 2665, you will find this 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;}\">__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)\n{\n  \/* Prevent unused argument(s) compilation warning *\/\n  UNUSED(huart);\n\n  \/* NOTE : This function should not be modified, when the callback is needed,\n            the HAL_UART_RxCpltCallback can be implemented in the user file.\n   *\/\n}<\/pre><\/div>\n\n\n\n<p><\/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-15_15-42-50-1024x567.jpg\" alt=\"\" class=\"wp-image-2354\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-1024x567.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-300x166.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-768x425.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-1536x851.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-2048x1134.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-1150x637.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-750x415.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-400x221.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_15-42-50-250x138.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This function is defined as weak which can be overridden by the user. The purpose of this function is when the UART received the characters, this function shall be called.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, in main.c, the section between:<\/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;}\">\/* USER CODE BEGIN 0 *\/<\/pre><\/div>\n\n\n\n<p>and<\/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;}\">\/* USER CODE END 0 *\/<\/pre><\/div>\n\n\n\n<p>Declare the following variable:<\/p>\n\n\n\n<p>First:<\/p>\n\n\n\n<p>Buffer to hold the received characters:<\/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;}\">uint8_t RxData[5];<\/pre><\/div>\n\n\n\n<p>In this example, we shall receive 5 characters.<\/p>\n\n\n\n<p>Buffer to hold the data to be transmitted:<\/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;}\">uint8_t TxData[40];<\/pre><\/div>\n\n\n\n<p>Adapt the size according to your application.<\/p>\n\n\n\n<p>Declare a volatile variable which acts as flag:<\/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;}\">volatile uint8_t finished;<\/pre><\/div>\n\n\n\n<p>Now, redeclare the weak 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;}\">void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)\n{\n\tfinished=1;\n\n\tHAL_UART_Receive_IT(&amp;huart2, RxData, 5);\n}<\/pre><\/div>\n\n\n\n<p>In the function, we shall set the flag to 1 and start the interrupt again.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In User code begin 2 in the main function:<\/p>\n\n\n\n<p>Start the UART to receive in interrupt mode:<\/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_Receive_IT(&amp;huart2, RxData, 5);<\/pre><\/div>\n\n\n\n<p>In user code begin 3:<\/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  if(finished==1)\n\t  {\n\t\t   len=sprintf((char*)TxData,&quot;Received Data are %c %c %c %c %c \\r\\n&quot;,\n\t\t\t\t  RxData[0],RxData[1],RxData[2],RxData[3],RxData[4]);\n\n\t\t  HAL_UART_Transmit(&amp;huart2, TxData, len, 100);\n\t\t  finished=0;\n\n\t  }<\/pre><\/div>\n\n\n\n<p>Check if the flag is set, if yes, print the received characters and set the flag back to 0.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Using any serial port program, set the baud rate of 115200, send any 5 characters without \\r\\n and see the echoed characters.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"738\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-1024x738.jpg\" alt=\"\" class=\"wp-image-2355\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-1024x738.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-300x216.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-768x553.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-1536x1106.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-1150x828.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-750x540.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-400x288.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28-250x180.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/2024-02-15_16-11-28.jpg 1866w\" 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 at the UART and we were able to configure the STM32G070 to transmit data. Now, we shall receive data using UART in interrupt mode and echo back the received data. In this guide, we shall cover the following: Enable USART2 interrupt. Code developing. Results. 1. Enable [&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-2351","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\/2351"}],"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=2351"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2351\/revisions"}],"predecessor-version":[{"id":2356,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2351\/revisions\/2356"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}