Using CAN bus with (not just NUCLEO) mbed boards

Dependencies:   mbed CANMsg

Using CAN bus with mbed boards

Two low cost STM32F103C8T6 boards are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.). CAN transceivers are not part of NUCLEO boards, therefore must be added by us. Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.

Schematic

Zoom in

/media/uploads/hudakz/stm32f103c8t6_can_hello.png

Hookup

/media/uploads/hudakz/20150724_080148.jpg

The mbed boards in this example are transmitting CAN messages carrying two data items:

uint8_t   counter;  // one byte
float     voltage;  // four bytes

So in this case the total length of payload data is five bytes (must not exceed eight bytes).
For our convenience, the "<<" (append) operator (defined in CANMsg library) is used to add data to the CAN message.
The usage of "<<" and ">>" operators is similar to the C++ io-streams operators. We can append data one at a time

txMsg << counter;
txMsg << voltage;

or combine all into one expression.

txMsg << counter << voltage;

The actual data length of a CAN message is automatically updated when using "<<" or ">>" operators.
After successful transmission the CAN message is printed to the serial terminal of the connected PC. So we can check the details (ID, type, format, length and raw data). If something goes wrong during transmission a "Transmission error" message is printed to the serial terminal.

On arrival of a CAN message it's also printed to the serial terminal of the connected PC. So we can see the details (ID, type, format, length and raw data). Then its ID is checked. If there is a match with the ID of awaited message then data is extracted from the CAN message (in the same sequence as it was appended before transmitting) using the ">>" (extract) operator one at a time

rxMsg >> counter;
rxMsg >> voltage;

or all in one shot

rxMsg >> counter >> voltage;


The same source code is used for both boards, but:

  • For board #1 compile the example without any change to main.cpp
  • For board #2 comment out the line #define BOARD1 1 before compiling

Once binaries have been downloaded to the boards reset both board at the same time.

NOTE:

The code published here was written for the official NUCLEO boards. When using STM32F103C8T6 boards, shown in the picture above (on-board LED is active on 0),

  • Include (uncomment) the line #define TARGET_STM32F103C8T6 1
  • Select NUCLEO-F103RB as target platform for the online compiler.

CAN bus related information

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Fri Feb 08 13:16:55 2019 +0000
Parent:
8:c65afde7f7f5
Commit message:
Updated.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Feb 05 19:03:26 2019 +0000
+++ b/main.cpp	Fri Feb 08 13:16:55 2019 +0000
@@ -22,8 +22,6 @@
 
 #if defined(TARGET_STM32F103C8T6)
     #define LED_PIN     PC_13
-    #define USBTX       PA_2
-    #define USBRX       PA_3
     const int           OFF = 1;
     const int           ON = 0;
 #else
@@ -45,6 +43,7 @@
 
 Serial              pc(USBTX, USBRX);
 CAN                 can(PB_8, PB_9);  // CAN Rx pin name, CAN Tx pin name
+//CAN                 can(p30, p29);  // CAN Rx pin name, CAN Tx pin name
 CANMsg              rxMsg;
 CANMsg              txMsg;
 DigitalOut          led(LED_PIN);
@@ -80,6 +79,8 @@
 void onCanReceived(void)
 {
     can.read(rxMsg);
+    pc.printf("-------------------------------------\r\n");
+    pc.printf("CAN message received\r\n");
     printMsg(rxMsg);
 
     if (rxMsg.id == RX_ID) {
@@ -116,7 +117,7 @@
     pc.printf("CAN_Hello board #2\r\n");
 #endif
     while(1) {
-        if(timer.read_ms() >= 1000) {    // check for timeout
+        if(timer.read_ms() >= 2000) {    // check for timeout
             timer.stop();                // stop timer
             timer.reset();               // reset timer
             counter++;                   // increment counter
@@ -130,6 +131,7 @@
             if(can.write(txMsg)) {       // transmit message
                 led = OFF;               // turn the LED off
                 pc.printf("-------------------------------------\r\n");
+                pc.printf("-------------------------------------\r\n");
                 pc.printf("CAN message sent\r\n");
                 printMsg(txMsg);
                 pc.printf("  counter = %d\r\n", counter);