{"id":3291,"date":"2025-03-15T08:01:13","date_gmt":"2025-03-15T08:01:13","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3291"},"modified":"2025-03-15T08:01:15","modified_gmt":"2025-03-15T08:01:15","slug":"getting-started-with-lin-local-interconnected-network-part-3-receiving-data","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3291","title":{"rendered":"Getting Started with LIN (Local Interconnected Network) Part 3: Receiving Data"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2.webp\" alt=\"\" class=\"wp-image-3292\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2.webp 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2-768x768.webp 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2-750x750.webp 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2-400x400.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-03-01-16.00.55-A-cover-image-for-a-technical-guide-on-LIN-Local-Interconnect-Network-bus-using-STM32F4.-The-image-should-feature-an-STM32F4-microcontroller-with-hi-2-250x250.webp 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this guide, we shall see how to receive data using UART in LIN configuration.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>UART6 configuration.<\/li>\n\n\n\n<li>Modification to source and header file.<\/li>\n\n\n\n<li>Developing the receiver driver.<\/li>\n\n\n\n<li>Hardware connection<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. UART6 Configuration:<\/h2>\n\n\n\n<p>Since STM32F411RE Nucleo has three uarts as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>USART1 which is already used by the transmitter.<\/li>\n\n\n\n<li>USART2 which is used by on board ST-Link V2.<\/li>\n\n\n\n<li>UART6 which will be the receiver of the data over LIN.<\/li>\n<\/ul>\n\n\n\n<p>From the previous part <a href=\"https:\/\/blog.embeddedexpert.io\/?p=3276\" data-type=\"link\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=3276\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>, open the .ioc file as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"482\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-1024x482.jpg\" alt=\"\" class=\"wp-image-3293\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-1024x482.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-300x141.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-768x361.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-1536x723.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-2048x964.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-1150x541.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-750x353.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-400x188.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-22-14-250x118.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Once the CubeMX window is opened, from connectivity, select UART6 and set it to LIN as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"555\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-1024x555.jpg\" alt=\"\" class=\"wp-image-3294\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-1024x555.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-300x163.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-768x417.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-1536x833.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-2048x1111.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-1150x624.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-750x407.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-400x217.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-24-20-250x136.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>This will automatically enabled PC6 and PC7 for UART communication.<\/p>\n\n\n\n<p>Note: Make sure to match the baudrate in USART1 with the one in UART6.<\/p>\n\n\n\n<p>Also, from NVIC Setting, enable UART6 global interrupt as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-765x1024.jpg\" alt=\"\" class=\"wp-image-3295\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-765x1024.jpg 765w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-224x300.jpg 224w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-768x1029.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-1147x1536.jpg 1147w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-1150x1540.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-750x1004.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-400x536.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41-250x335.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-26-41.jpg 1474w\" sizes=\"(max-width: 765px) 100vw, 765px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the UART6 configuration.<\/p>\n\n\n\n<p>Save the project this will generate the new code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Modification to Source and Header File:<\/h2>\n\n\n\n<p>Open LIN.c source file and remove the static from the check sum to become like this:<\/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 checksum_Calc (uint8_t PID, uint8_t *data, int size)<\/pre><\/div>\n\n\n\n<p>Save the source file.<\/p>\n\n\n\n<p>Next, open the header file and add the check_sum to the 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;}\">uint8_t checksum_Calc (uint8_t PID, uint8_t *data, int size);<\/pre><\/div>\n\n\n\n<p>Hence, the new header file will be 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 INC_LIN_H_\n#define INC_LIN_H_\n\n#include &quot;main.h&quot;\n#include &quot;stdint.h&quot;\n\nuint8_t LIN_Transmit_Data(uint8_t ID, uint8_t *data, uint16_t len);\nuint8_t checksum_Calc (uint8_t PID, uint8_t *data, int size);\n\n#endif \/* INC_LIN_H_ *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Save the header file.<\/p>\n\n\n\n<p>That&#8217;s all for the modification.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Developing the Receiver Driver:<\/h2>\n\n\n\n<p>In main.c, we start by including the stdlib header file which will allow us to generate random numbers 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;stdlib.h&quot;<\/pre><\/div>\n\n\n\n<p>Next, in user code begin PV, declare the following array:<\/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 LINDataRX[20];<\/pre><\/div>\n\n\n\n<p>This array shall hold the entire data packet from the LIN bus.<\/p>\n\n\n\n<p>Next, the array to hold the actual received data:<\/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 LINDataReceived[8];<\/pre><\/div>\n\n\n\n<p>Next, some auxiliary variables needed as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Number of bytes received<\/li>\n<\/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;}\">int numDataBytes = 0;<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The ID:<\/li>\n<\/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;}\">uint8_t ID = 0;<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Is data valid:<\/li>\n<\/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;}\">int isDataValid = 0;<\/pre><\/div>\n\n\n\n<p>Next, a variable to store the previous ticks which will be used to store the ticks for sending purposes 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 previousTicks=0;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, in user code begin 2 in the main function we shall fill the sending array with random 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;}\">for (int i=0; i&lt;8; i++)\n\t{\n\t\tLINData[i] = rand() % 255;\n\t}<\/pre><\/div>\n\n\n\n<p>This will fill the array with random data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we shall start UART6 to receive data in interrupt mode until IDLE line is detected 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_UARTEx_ReceiveToIdle_IT(&amp;huart6, LINDataRX, 20);<\/pre><\/div>\n\n\n\n<p>This will trigger interrupt either the full 20 characters have received or IDLE line is detected. We know the maximum length for the data is 12 byte, but we gave extra bytes just in case.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 3 in while 1 loop:<\/p>\n\n\n\n<p>First, check if there is a valid 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;}\">if(isDataValid==1)\n\t  {\n\t\t  for (int i=0; i&lt;numDataBytes; i++)\n\t\t  {\n\t\t\t  LINDataReceived[i] = LINDataRX[i+3];\n\t\t  }\n\t\t  isDataValid = 0;\n\t  }<\/pre><\/div>\n\n\n\n<p>If yes, fill the Received buffer with the actual data and reset isDataValided back to 0.<\/p>\n\n\n\n<p>Next, we shall transmit the data each 300ms 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;}\">\t  if(HAL_GetTick()-previousTicks&gt;300)\n\t  {\n\n\t\tLIN_Transmit_Data(0x34, LINData, 8);\n\t\tfor (int i=0; i&lt;8; i++)\n\t\t{\n\t\t\tLINData[i] = rand() % 255;\n\t\t}\n\t\tpreviousTicks=HAL_GetTick();\n\t  }<\/pre><\/div>\n\n\n\n<p>This function will allow us to send the data without using any delays.<\/p>\n\n\n\n<p>In user code begin 4 at near the end of 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;}\">void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)\n{\n\tnumDataBytes = Size - 4;\n\tuint8_t checksum = checksum_Calc(LINDataRX[2], LINDataRX+3, numDataBytes);\n\tID = LINDataRX[2]&amp;0x3F;\n\tif (checksum != LINDataRX[Size-1])\n\t{\n\t\tisDataVa\u00c7lid = 0;\n\t\t\/\/ call error handler\n\t}\n\telse isDataValid = 1;\n\tHAL_UARTEx_ReceiveToIdle_IT(&amp;huart6, LINDataRX, 20);\n}\n<\/pre><\/div>\n\n\n\n<p>The function:<\/p>\n\n\n\n<p><strong>void<\/strong> <strong>HAL_UARTEx_RxEventCallback<\/strong>(<strong>UART_HandleTypeDef<\/strong> *huart, <strong>uint16_t<\/strong> Size) will be called every time when either 20 bytes have been received or IDL line has been detected.<\/p>\n\n\n\n<p>Once the function is called, first we get the number of data bytes 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;}\">numDataBytes = Size - 4;<\/pre><\/div>\n\n\n\n<p>Then we shall calculate the check sum 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;}\">uint8_t checksum = checksum_Calc(LINDataRX[2], LINDataRX+3, numDataBytes);<\/pre><\/div>\n\n\n\n<p>Since the checksum takes first the PID which is always at 3rd position of the array (index 2), the actual data which start at the fourth position and the data length.<\/p>\n\n\n\n<p>Next, calculate the ID 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;}\">ID = LINDataRX[2]&amp;0x3F;<\/pre><\/div>\n\n\n\n<p>This will give us the ID of the data packet.<\/p>\n\n\n\n<p>Next, check if the checksum is matched or not, if matched, set isDataValid to 1 and if it is not, reset it to zero and restart the UART reception until IDLE in interrupt mode 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;}\">if (checksum != LINDataRX[Size-1])\n\t{\n\t\tisDataValid = 0;\n\t\t\/\/ call error handler\n\t}\n\telse isDataValid = 1;\n\tHAL_UARTEx_ReceiveToIdle_IT(&amp;huart6, LINDataRX, 20);<\/pre><\/div>\n\n\n\n<p>Thats all for the main.c<\/p>\n\n\n\n<p>Save the project, build it and run it in debugging mode on your STM32F4 board.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"38\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-1024x38.jpg\" alt=\"\" class=\"wp-image-3296\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-1024x38.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-300x11.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-768x29.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-1536x57.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-1150x43.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-750x28.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-400x15.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27-250x9.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-56-27.jpg 1878w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. Hardware Connection:<\/h2>\n\n\n\n<p>The connection is as simple as connecting PC7 to PA9 using jumper wire as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"826\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-1024x826.jpg\" alt=\"\" class=\"wp-image-3297\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-1024x826.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-300x242.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-768x619.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-1536x1239.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-2048x1652.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-1150x928.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-750x605.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-400x323.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/2025-03-15_10-58-46-250x202.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Simply, connect a wire from D8 to D9 of the arduino header.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Add both LINData and LINDataReceived to live expression and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-726x1024.gif\" alt=\"\" class=\"wp-image-3298\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-726x1024.gif 726w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-213x300.gif 213w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-768x1083.gif 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-750x1058.gif 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-400x564.gif 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/03\/ezgif-1d5d6b6b75b373-250x353.gif 250w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/><\/figure>\n\n\n\n<p>As you can see, the data do match.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to receive data using UART in LIN configuration. In this guide, we shall cover the following: 1. UART6 Configuration: Since STM32F411RE Nucleo has three uarts as following: From the previous part here, open the .ioc file as following: Once the CubeMX window is opened, from connectivity, select UART6 [&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-3291","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\/3291"}],"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=3291"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3291\/revisions"}],"predecessor-version":[{"id":3299,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3291\/revisions\/3299"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}