{"id":2245,"date":"2024-01-04T05:06:37","date_gmt":"2024-01-04T05:06:37","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2245"},"modified":"2024-01-04T05:06:40","modified_gmt":"2024-01-04T05:06:40","slug":"stm32-advanced-peripherals-canbus-part-3-send-and-receive-data","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2245","title":{"rendered":"STM32 Advanced Peripherals : CANBus Part 3: Send and Receive Data"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"300\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/634d21bf24d32605e5020af5_CAN-bus-high-low-nodes.jpg\" alt=\"\" class=\"wp-image-2246\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/634d21bf24d32605e5020af5_CAN-bus-high-low-nodes.jpg 440w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/634d21bf24d32605e5020af5_CAN-bus-high-low-nodes-300x205.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/634d21bf24d32605e5020af5_CAN-bus-high-low-nodes-400x273.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/634d21bf24d32605e5020af5_CAN-bus-high-low-nodes-250x170.jpg 250w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=2221\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2221\" target=\"_blank\">here<\/a>), we initialized the peripheral and filter to accept data with identifier of 0x446.<\/p>\n\n\n\n<p>In this third part of CANBus guide, we shall start to transmit data from one STM32 and receive it from another STM32. (From STM32Ff407 to STM32F446).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Developing the data structure.<\/li><li>Developing the header file.<\/li><li>Transmit function.<\/li><li>Receive Function.<\/li><li>Main transmit code.<\/li><li>Main receive code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">13. Developing the Data Structure:<\/h2>\n\n\n\n<p>We start with CANBus.h header file.<\/p>\n\n\n\n<p>Within the header file, declare the following two structures:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The transmit data structure:<\/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;}\">typedef struct CANBusTxFrame\n{\n\tuint32_t identifier;\n\tuint8_t length;\n\tuint8_t data[8];\n\n\n}CANBusTxFrameDef;<\/pre><\/div>\n\n\n\n<p>The structure has three elements as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Identifier which hold the identifier number of the CAN node.<\/li><li>Length which is the length of the data to transmitted (Maximum 8).<\/li><li>data array of 8 elements which is the data buffer to hold the data to send.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>The receiver data structure:<\/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;}\">typedef struct CANBusRxFrame\n{\n\tuint32_t identifier;\n\tuint8_t length;\n\tuint8_t data[8];\n\n\n}CANBusRxFrameDef;<\/pre><\/div>\n\n\n\n<p>It is similar to transmit data structure.<\/p>\n\n\n\n<p>Thats all for the data structure part.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">14. Developing the Header File:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We shall continue with the header.<\/p>\n\n\n\n<p>Declare the following 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;}\">\/*\n * @brief This function will initialize the CANBus pins (PB8 and PB9) .\n * @return nothing.\n * *\/\nvoid CANBus_Pins_Init(void);\n\n\n\/*\n * @brief This function will initialize the CANBus.\n * @return nothing.\n * @see source file for configuration parameters\n * *\/\nvoid CANBus_Init(void);\n\n\/*\n * @brief This function will start the CANBus Peripheral.\n * @return nothing.\n * *\/\nvoid CANBus_Start(void);\n\n\n\/*\n * @brief This function will configure the filter of CANBus.\n * @return nothing.\n * @see source file for filter parameters\n * *\/\nvoid Filter_Configuration(void);\n\n\n\/*\n * @brief This function will transmit data over CANBus.\n * @param pointer to CANBusTxFrame data structure.\n * @return nothing.\n * *\/\nvoid CANBus_SendMessage(CANBusTxFrameDef *TXFrame);\n\n\n\/*\n * @brief This function will receive data over CANBus.\n * @param pointer to CANBusRxFrame data structure.\n * @return nothing.\n * *\/\nvoid CANBus_ReceiveMessage(CANBusRxFrameDef *RXFrame);\n<\/pre><\/div>\n\n\n\n<p><\/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;}\">\/*\n * CANBus.h\n *\n *  Created on: Dec 29, 2023\n *      Author: hussamaldean\n *\/\n\n#ifndef CANBUS_H_\n#define CANBUS_H_\n\n#include &quot;stdint.h&quot;\n\n\n\/*\n * @brief This data structure holds the CANBus\n * message to be send.\n * *\/\n\ntypedef struct CANBusTxFrame\n{\n\tuint32_t identifier; \/*@brief The identifier of the CANBus Message*\/\n\tuint8_t length;      \/*@brief The length of the CANBus Message*\/\n\tuint8_t data[8];     \/*@brief The buffer which hold the data to be sent*\/\n\n\n}CANBusTxFrameDef;\n\n\n\/*\n * @brief This data structure holds the CANBus\n * message to be received.\n * *\/\n\ntypedef struct CANBusRxFrame\n{\n\tuint32_t identifier; \/*@brief The identifier of the CANBus Message*\/\n\tuint8_t length;      \/*@brief The length of the CANBus Message*\/\n\tuint8_t data[8];     \/*@brief The buffer which hold the data to be received*\/\n\n}CANBusRxFrameDef;\n\n\n\/*\n * @brief This function will initialize the CANBus pins (PB8 and PB9) .\n * @return nothing.\n * *\/\nvoid CANBus_Pins_Init(void);\n\n\n\/*\n * @brief This function will initialize the CANBus.\n * @return nothing.\n * @see source file for configuration parameters\n * *\/\nvoid CANBus_Init(void);\n\n\/*\n * @brief This function will start the CANBus Peripheral.\n * @return nothing.\n * *\/\nvoid CANBus_Start(void);\n\n\n\/*\n * @brief This function will configure the filter of CANBus.\n * @return nothing.\n * @see source file for filter parameters\n * *\/\nvoid Filter_Configuration(void);\n\n\n\/*\n * @brief This function will transmit data over CANBus.\n * @param pointer to CANBusTxFrame data structure.\n * @return nothing.\n * *\/\nvoid CANBus_SendMessage(CANBusTxFrameDef *TXFrame);\n\n\n\/*\n * @brief This function will receive data over CANBus.\n * @param pointer to CANBusRxFrame data structure.\n * @return nothing.\n * *\/\nvoid CANBus_ReceiveMessage(CANBusRxFrameDef *RXFrame);\n\n\n\n#endif \/* CANBUS_H_ *\/\n<\/pre><\/div>\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\">14. Transmit Function:<\/h2>\n\n\n\n<p>For transmit 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 CANBus_SendMessage(CANBusTxFrameDef *TXFrame )<\/pre><\/div>\n\n\n\n<p>The function takes pointer to CANBusRxFrameDef data structure which holds the identifier, length and data.<\/p>\n\n\n\n<p>Within this function:<\/p>\n\n\n\n<p>First wait until the Mailbox is free by checking TME0 (Transmit Mailbox 0 is empty) if it is set to 1 or not:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"892\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-1024x892.png\" alt=\"\" class=\"wp-image-2247\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-1024x892.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-300x261.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-768x669.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-1536x1337.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-2048x1783.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-1150x1001.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-750x653.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-400x348.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.29.21\u202fAM-250x218.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">    \/\/ Wait until the transmit mailbox is empty\n    while ((CAN1-&gt;TSR &amp; CAN_TSR_TME0) == 0);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Set identifier mode to be standard:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1024x608.png\" alt=\"\" class=\"wp-image-2248\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1024x608.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-300x178.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-768x456.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1536x911.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-2048x1215.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1150x682.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-750x445.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-400x237.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-250x148.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">    \/\/ Set the standard identifier and data length\n    CAN1-&gt;sTxMailBox[0].TIR &amp;= ~CAN_TI0R_STID;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Set the identifier number:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1024x608.png\" alt=\"\" class=\"wp-image-2248\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1024x608.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-300x178.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-768x456.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1536x911.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-2048x1215.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-1150x682.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-750x445.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-400x237.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.31.42\u202fAM-250x148.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">    \/\/ Configure the transmit mailbox identifier\n    CAN1-&gt;sTxMailBox[0].TIR |= (TXFrame-&gt;identifier &lt;&lt; 21);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Set length of the data:<\/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=\"411\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-1024x411.png\" alt=\"\" class=\"wp-image-2249\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-1024x411.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-300x120.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-768x308.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-1536x616.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-2048x821.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-1150x461.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-750x301.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-400x160.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.34.12\u202fAM-250x100.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">    \/\/ Configure data length\n    CAN1-&gt;sTxMailBox[0].TDTR |= (TXFrame-&gt;length &lt;&lt; 0);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Store the buffer into the CANBus FIFO:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-806x1024.png\" alt=\"\" class=\"wp-image-2250\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-806x1024.png 806w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-236x300.png 236w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-768x976.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-1209x1536.png 1209w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-1150x1461.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-750x953.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-400x508.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM-250x318.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.36.44\u202fAM.png 1576w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/figure>\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;}\">    CAN1-&gt;sTxMailBox[0].TDLR=\n    \t\t((uint32_t)TXFrame-&gt;data[3] &lt;&lt; CAN_TDL0R_DATA3_Pos) |\n            ((uint32_t)TXFrame-&gt;data[2] &lt;&lt; CAN_TDL0R_DATA2_Pos) |\n            ((uint32_t)TXFrame-&gt;data[1] &lt;&lt; CAN_TDL0R_DATA1_Pos) |\n            ((uint32_t)TXFrame-&gt;data[0] &lt;&lt; CAN_TDL0R_DATA0_Pos);\n\n    CAN1-&gt;sTxMailBox[0].TDHR=\n            ((uint32_t)TXFrame-&gt;data[7] &lt;&lt; CAN_TDH0R_DATA7_Pos) |\n            ((uint32_t)TXFrame-&gt;data[6] &lt;&lt; CAN_TDH0R_DATA6_Pos) |\n            ((uint32_t)TXFrame-&gt;data[5] &lt;&lt; CAN_TDH0R_DATA5_Pos) |\n            ((uint32_t)TXFrame-&gt;data[4] &lt;&lt; CAN_TDH0R_DATA4_Pos);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Start the transmission :  <\/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=\"455\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-1024x455.png\" alt=\"\" class=\"wp-image-2251\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-1024x455.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-300x133.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-768x341.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-1536x682.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-2048x909.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-1150x511.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-750x333.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-400x178.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.39.27\u202fAM-250x111.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">    \/\/ Set the TXRQ bit to request transmission\n    CAN1-&gt;sTxMailBox[0].TIR |= CAN_TI0R_TXRQ;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the transmission part.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">15.Receive Function:<\/h2>\n\n\n\n<p>We start with the send message 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 CANBus_ReceiveMessage(CANBusRxFrameDef *RXFrame)<\/pre><\/div>\n\n\n\n<p>The function will take pointer to CANBusRxFrame data struct.<\/p>\n\n\n\n<p>Within the function:<\/p>\n\n\n\n<p>Check if the FIFO 0 has pending message:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"928\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-1024x928.png\" alt=\"\" class=\"wp-image-2252\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-1024x928.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-300x272.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-768x696.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-1536x1392.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-2048x1855.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-1150x1042.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-750x680.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-400x362.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.51.36\u202fAM-250x227.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">if (CAN1-&gt;RF0R &amp; CAN_RF0R_FMP0) {<\/pre><\/div>\n\n\n\n<p>Within the if condition:<\/p>\n\n\n\n<p>Get the identifier number:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"427\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-1024x427.png\" alt=\"\" class=\"wp-image-2253\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-1024x427.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-300x125.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-768x320.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-1536x640.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-2048x854.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-1150x479.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-750x313.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-400x167.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.54.34\u202fAM-250x104.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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 the received identifier\n        RXFrame-&gt;identifier = (CAN1-&gt;sFIFOMailBox[0].RIR &gt;&gt; 3) &amp; 0x1FFFFFFF;<\/pre><\/div>\n\n\n\n<p>Get the length of the data:<\/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=\"427\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-1024x427.png\" alt=\"\" class=\"wp-image-2254\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-1024x427.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-300x125.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-768x320.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-1536x640.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-2048x854.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-1150x479.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-750x313.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-400x167.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-04-at-7.55.24\u202fAM-250x104.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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 the data length\n        RXFrame-&gt;length = CAN1-&gt;sFIFOMailBox[0].RDTR &amp; 0x0F;<\/pre><\/div>\n\n\n\n<p>Clear the old 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;}\">        \/*Clear old data*\/\n        for (int i=0;i&lt;8;i++)\n        {\n        \tRXFrame-&gt;data[i]=0;\n        }<\/pre><\/div>\n\n\n\n<p>Store the new values:<\/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;}\">        RXFrame-&gt;data[0]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA0_Pos;\n        RXFrame-&gt;data[1]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA1_Pos;\n        RXFrame-&gt;data[2]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA2_Pos;\n        RXFrame-&gt;data[3]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA3_Pos;\n\n        RXFrame-&gt;data[4]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA4_Pos;\n        RXFrame-&gt;data[5]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA5_Pos;\n        RXFrame-&gt;data[6]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA6_Pos;\n        RXFrame-&gt;data[7]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA7_Pos;<\/pre><\/div>\n\n\n\n<p>Release the FIFO0:<\/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;}\">        \/\/ Release the FIFO (not necessary for FIFO0)\n        CAN1-&gt;RF0R |= CAN_RF0R_RFOM0;<\/pre><\/div>\n\n\n\n<p>Finally toggle the LED connected to PA5:<\/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;}\">        GPIOA-&gt;ODR^=GPIO_ODR_OD5;<\/pre><\/div>\n\n\n\n<p>Hence, the entire source code 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;CANBus.h&quot;\n#include &quot;stm32f4xx.h&quot;\n\n#define CAN_AF 0x09\n\nvoid CANBus_Pins_Init(void)\n{\n\t\/*Enable clock access to GPIOB*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOBEN;\n\n\t\/*Set PB8 and PB9 to alternate function*\/\n\tGPIOB-&gt;MODER|=GPIO_MODER_MODE8_1|GPIO_MODER_MODE9_1;\n\tGPIOB-&gt;MODER&amp;=~(GPIO_MODER_MODE8_0|GPIO_MODER_MODE9_0);\n\n\t\/*Select which alternate function*\/\n\tGPIOB-&gt;AFR[1]|=(CAN_AF&lt;&lt;GPIO_AFRH_AFSEL8_Pos)|(CAN_AF&lt;&lt;GPIO_AFRH_AFSEL9_Pos);\n\n}\n\nvoid CANBus_Init(void)\n{\n\n\n\t\/*Enable Clock access to CAN1*\/\n\tRCC-&gt;APB1ENR|=RCC_APB1ENR_CAN1EN;\n\n\n    \/* Enter Initialization mode*\/\n    CAN1-&gt;MCR |= CAN_MCR_INRQ;\n    \/*Wait until CANBus peripheral is in initialization mdoe*\/\n    while ((CAN1-&gt;MSR &amp; CAN_MSR_INAK)==0U);\n\n    \/\/ Enable CAN1 peripheral\n    CAN1-&gt;MCR &amp;= ~CAN_MCR_SLEEP;\n    while ((CAN1-&gt;MSR &amp; CAN_MSR_SLAK)!=0U);\n\n\n    \/*Configure the timing with the following parameters\n     * Normal mode.\n     * Loop back mode is disabled.\n     * Resynchronization jump width to 1 (value - 1).\n     * Prescale of 10. (value - 1)\n     * Time quanta segment 1 is 2 (value - 1)\n     * Time quanta segment 2 is 1 (value - 1)\n     * Baud is 400Kbps\n     * *\/\n\n    \/*Reset the non-zero initial values*\/\n    CAN1-&gt;BTR&amp;=~(CAN_BTR_TS1_Msk|CAN_BTR_TS2_Msk|CAN_BTR_SJW_Msk);\n\n    CAN1-&gt;BTR=(1&lt;&lt;CAN_BTR_TS1_Pos)|(0&lt;&lt;CAN_BTR_TS2_Pos)\n    \t\t|(9U&lt;&lt;CAN_BTR_BRP_Pos);\n\n    \/*Other basic parameters can be configured according your application\n     * The following is the configuration:\n     * Time triggered communication is disabled.\n     * Automatic bus-off management is disabled.\n     * Automatic wake-up is disabled.\n     * Automatic retransmission is off.\n     * Receive FIFO lock mode is disabled.\n     * Transmit FIFO Priority is off\n     * *\/\n}\n\n\nvoid Filter_Configuration(void)\n{\n\n\t\/*Filter is filter 18 and it will accept data from identifier 0x446*\/\n\n\t\/* Set the filter initialization mode*\/\n\tCAN1-&gt;FMR |= CAN_FMR_FINIT;\n\n\t\/*Set the slave Filter to start from 20*\/\n\tCAN1-&gt;FMR &amp;=~(CAN_FMR_CAN2SB_Msk);\n\tCAN1-&gt;FMR |=(20&lt;&lt;CAN_FMR_CAN2SB_Pos);\n\n\t\/*Disable Filter 18*\/\n\tCAN1-&gt;FA1R&amp;=~(CAN_FA1R_FACT18);\n\n\t\/* Set to 1 for 32-bit scale configuration*\/\n    CAN1-&gt;FS1R |= CAN_FS1R_FSC18;\n\n\n    CAN1-&gt;FM1R &amp;= ~CAN_FM1R_FBM18; \/\/ Set to 0 for identifier mask mode\n\n    CAN1-&gt;sFilterRegister[18].FR1 = (0x446&lt;&lt;5) &lt;&lt; 16; \/\/ Identifier\n\n    CAN1-&gt;sFilterRegister[18].FR2 = (0x446&lt;&lt;5) &lt;&lt; 16; \/\/ Identifier mask\n\n    \/*Assign filter 18 to FIFO0*\/\n    CAN1-&gt;FFA1R&amp;=~CAN_FFA1R_FFA18;\n\n    \/\/ Activate filter 18\n    CAN1-&gt;FA1R |= CAN_FA1R_FACT18;\n\n\n    CAN1-&gt;FMR &amp;= ~CAN_FMR_FINIT; \/\/ Clear the filter initialization mode\n\n\n}\n\nvoid CANBus_Start(void)\n{\n    \/\/ Leave Initialization mode\n    CAN1-&gt;MCR &amp;= ~CAN_MCR_INRQ;\n    while (CAN1-&gt;MSR &amp; CAN_MSR_INAK);\n\n}\n\n\n\n\nvoid CANBus_SendMessage(CANBusTxFrameDef *TXFrame )\n{\n    \/\/ Wait until the transmit mailbox is empty\n    while ((CAN1-&gt;TSR &amp; CAN_TSR_TME0) == 0);\n\n    \/\/ Set the standard identifier and data length\n    CAN1-&gt;sTxMailBox[0].TIR &amp;= ~CAN_TI0R_STID;\n\n    \/\/ Configure the transmit mailbox identifier\n    CAN1-&gt;sTxMailBox[0].TIR |= (TXFrame-&gt;identifier &lt;&lt; 21);\n\n    \/\/ Configure data length\n    CAN1-&gt;sTxMailBox[0].TDTR |= (TXFrame-&gt;length &lt;&lt; 0);\n\n\n\n    CAN1-&gt;sTxMailBox[0].TDLR=\n    \t\t((uint32_t)TXFrame-&gt;data[3] &lt;&lt; CAN_TDL0R_DATA3_Pos) |\n            ((uint32_t)TXFrame-&gt;data[2] &lt;&lt; CAN_TDL0R_DATA2_Pos) |\n            ((uint32_t)TXFrame-&gt;data[1] &lt;&lt; CAN_TDL0R_DATA1_Pos) |\n            ((uint32_t)TXFrame-&gt;data[0] &lt;&lt; CAN_TDL0R_DATA0_Pos);\n\n    CAN1-&gt;sTxMailBox[0].TDHR=\n            ((uint32_t)TXFrame-&gt;data[7] &lt;&lt; CAN_TDH0R_DATA7_Pos) |\n            ((uint32_t)TXFrame-&gt;data[6] &lt;&lt; CAN_TDH0R_DATA6_Pos) |\n            ((uint32_t)TXFrame-&gt;data[5] &lt;&lt; CAN_TDH0R_DATA5_Pos) |\n            ((uint32_t)TXFrame-&gt;data[4] &lt;&lt; CAN_TDH0R_DATA4_Pos);\n\n    \/\/ Set the TXRQ bit to request transmission\n    CAN1-&gt;sTxMailBox[0].TIR |= CAN_TI0R_TXRQ;\n}\n\nvoid CANBus_ReceiveMessage(CANBusRxFrameDef *RXFrame)\n{\n    if (CAN1-&gt;RF0R &amp; CAN_RF0R_FMP0) { \/\/ Check if there's a pending message in FIFO0\n        \/\/ Read the received identifier\n        RXFrame-&gt;identifier = (CAN1-&gt;sFIFOMailBox[0].RIR &gt;&gt; 3) &amp; 0x1FFFFFFF;\n\n        \/\/ Read the data length\n        RXFrame-&gt;length = CAN1-&gt;sFIFOMailBox[0].RDTR &amp; 0x0F;\n\n        \/*Clear old data*\/\n        for (int i=0;i&lt;8;i++)\n        {\n        \tRXFrame-&gt;data[i]=0;\n        }\n\n\n        RXFrame-&gt;data[0]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA0_Pos;\n        RXFrame-&gt;data[1]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA1_Pos;\n        RXFrame-&gt;data[2]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA2_Pos;\n        RXFrame-&gt;data[3]=CAN1-&gt;sFIFOMailBox[0].RDLR &gt;&gt;CAN_RDL0R_DATA3_Pos;\n\n        RXFrame-&gt;data[4]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA4_Pos;\n        RXFrame-&gt;data[5]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA5_Pos;\n        RXFrame-&gt;data[6]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA6_Pos;\n        RXFrame-&gt;data[7]=CAN1-&gt;sFIFOMailBox[0].RDHR &gt;&gt;CAN_RDH0R_DATA7_Pos;\n\n\n        \/\/ Release the FIFO (not necessary for FIFO0)\n        CAN1-&gt;RF0R |= CAN_RF0R_RFOM0;\n\n        GPIOA-&gt;ODR^=GPIO_ODR_OD5;\n    }\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the CANBus.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">16. Main Transmit Code:<\/h2>\n\n\n\n<p>The code of the transmit device (STM32F407 in this case):<\/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;delay.h&quot;\n#include &quot;CANBus.h&quot;\n#include &quot;stdlib.h&quot;\n\nCANBusTxFrameDef TXFrame;\n\nint main(void)\n{\n\n\tdelay_init(16000000);\n\tCANBus_Pins_Init();\n\tCANBus_Init();\n\tFilter_Configuration();\n\tCANBus_Start();\n\tsrand(millis());\n\twhile(1)\n\t{\n\t\tfor (int i=0;i&lt;8;i++)\n\t\t{\n\t\t\tTXFrame.data[i]=random()%255;\n\t\t}\n\n\t\tTXFrame.identifier=0x446;\n\t\tTXFrame.length=8;\n\t\tCANBus_SendMessage( &amp;TXFrame );\n\t\tdelay(1000);\n\n\t}\n\n\n}\n<\/pre><\/div>\n\n\n\n<p>We shall randomize the transmitted data every second.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">17. Main Receiver Code:<\/h2>\n\n\n\n<p>The receiver which is STM32F446RE Nucleo-64:<\/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;delay.h&quot;\n#include &quot;CANBus.h&quot;\n#include &quot;stm32f4xx.h&quot;\nCANBusRxFrameDef RXFrame;\n\n\nint main(void)\n{\n\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE5_1;\n\tdelay_init(16000000);\n\tCANBus_Pins_Init();\n\tCANBus_Init();\n\tFilter_Configuration();\n\tCANBus_Start();\n\n\n\twhile(1)\n\t{\n\t\tCANBus_ReceiveMessage(&amp;RXFrame);\n\n\n\t}\n\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">18. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open new debug session and put RXFrame in Live Expression and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"700\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/ezgif-1-d7a53ad056.gif\" alt=\"\" class=\"wp-image-2255\" \/><\/figure>\n\n\n\n<p>You will also notice the LED will toggle at rate of 1 second.<\/p>\n\n\n\n<p>Note: If there is not data to be transmitted, make sure that you terminated the CANH and CANL properly and use twisted pair of wires.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we initialized the peripheral and filter to accept data with identifier of 0x446. In this third part of CANBus guide, we shall start to transmit data from one STM32 and receive it from another STM32. (From STM32Ff407 to STM32F446). Developing the data structure. Developing the header file. Transmit function. Receive [&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-2245","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\/2245"}],"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=2245"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2245\/revisions"}],"predecessor-version":[{"id":2256,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2245\/revisions\/2256"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}