Using CAN bus with NUCLEO boards (Demo for the CANnucleo library).

Dependencies:   CANnucleo mbed-dev

Dependents:   BMS_2 Can_sniffer_BMS_GER Can_sniffer_bms ECU_1

Using CAN bus with NUCLEO boards

Demo for the CANnucleo library


Information

Because CAN support has been finally implemented into the mbed library also for the STM boards there is no need to use the CANnucleo library anymore (however you may if you want). See the CAN_Hello example which is trying to demonstrate the mbed built-in CAN API using NUCLEO 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 you. 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/can_nucleo_hello.png

Hookup

/media/uploads/hudakz/20150724_080148.jpg Zoom in

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 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;

Important

Before compiling the project, in the mbed-dev library open the device.h file associated with the selected target board and add #undef DEVICE_CAN as follows:

device.h

#ifndef MBED_DEVICE_H
#define MBED_DEVICE_H

//=======================================
#define DEVICE_ID_LENGTH       24

#undef DEVICE_CAN

#include "objects.h"

#endif

NOTE: Failing to do so will result in compilation errors.

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 board #1.

NOTE:

The code published here was written for the official NUCLEO boards. When using STM32F103C8T6 boards, shown in the picture above (LED1 is connected to pin PC_13 and, via a resistor, to +3.3V),

  • Import the mbed-STM32F103C8T6 library into your project.
  • Include (uncomment) the line #define TARGET_STM32F103C8T6 1
  • Select NUCLEO-F103RB as target platform for the online compiler.

CAN bus related information

Committer:
hudakz
Date:
Tue Dec 22 18:20:06 2015 +0000
Revision:
7:2dce8ed51091
Parent:
6:7ff95ce72f6d
Child:
10:66da8731bdb6
Support for NUCLEO-F303RE added.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:c5e5d0df6f2a 1 /*
hudakz 0:c5e5d0df6f2a 2 * An example showing how to use the CANnucleo library:
hudakz 0:c5e5d0df6f2a 3 *
hudakz 6:7ff95ce72f6d 4 * Two affordable (less than $4 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
hudakz 6:7ff95ce72f6d 5 * compatible with the NUCLEO-F103RB platform (20kB SRAM, 128kB Flash),
hudakz 6:7ff95ce72f6d 6 * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
hudakz 6:7ff95ce72f6d 7 * CAN transceivers are not part of NUCLEO boards, therefore must be added by you.
hudakz 6:7ff95ce72f6d 8 * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
hudakz 6:7ff95ce72f6d 9 *
hudakz 6:7ff95ce72f6d 10 * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Nucleo_Hello/>
hudakz 6:7ff95ce72f6d 11 *
hudakz 6:7ff95ce72f6d 12 * NOTE: If you'd like to use the official NUCLEO-F103RB boards
hudakz 6:7ff95ce72f6d 13 * comment out the line #define TARGET_STM32F103C8T6 1
hudakz 6:7ff95ce72f6d 14 *
hudakz 6:7ff95ce72f6d 15 * The same code is used for both NUCLEO boards, but:
hudakz 0:c5e5d0df6f2a 16 * For board #1 compile the example without any change.
hudakz 4:ccf4ac2deac8 17 * For board #2 comment out the line #define BOARD1 1 before compiling
hudakz 4:ccf4ac2deac8 18 *
hudakz 6:7ff95ce72f6d 19 * Once the binaries have been downloaded to the boards reset board #1.
hudakz 0:c5e5d0df6f2a 20 *
hudakz 0:c5e5d0df6f2a 21 */
hudakz 0:c5e5d0df6f2a 22
hudakz 0:c5e5d0df6f2a 23 #include "mbed.h"
hudakz 0:c5e5d0df6f2a 24 #include "CAN.h"
hudakz 0:c5e5d0df6f2a 25
hudakz 6:7ff95ce72f6d 26 #define BOARD1 1 // comment out this line when compiling for board #2
hudakz 0:c5e5d0df6f2a 27
hudakz 0:c5e5d0df6f2a 28 #if defined(BOARD1)
hudakz 0:c5e5d0df6f2a 29 #define RX_ID 0x100
hudakz 0:c5e5d0df6f2a 30 #define TX_ID 0x101
hudakz 0:c5e5d0df6f2a 31 #else
hudakz 0:c5e5d0df6f2a 32 #define RX_ID 0x101
hudakz 0:c5e5d0df6f2a 33 #define TX_ID 0x100
hudakz 0:c5e5d0df6f2a 34 #endif
hudakz 0:c5e5d0df6f2a 35
hudakz 6:7ff95ce72f6d 36 // See wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Nucleo_Hello/>
hudakz 7:2dce8ed51091 37 //#define TARGET_STM32F103C8T6 1 // comment out this line if you'd like to use the official NUCLEO-F103RB boards
hudakz 6:7ff95ce72f6d 38
hudakz 6:7ff95ce72f6d 39 #if defined(TARGET_STM32F103C8T6)
hudakz 6:7ff95ce72f6d 40 DigitalOut led(PC_13);
hudakz 6:7ff95ce72f6d 41 #else
hudakz 6:7ff95ce72f6d 42 DigitalOut led(LED1);
hudakz 6:7ff95ce72f6d 43 #endif
hudakz 6:7ff95ce72f6d 44
hudakz 5:c6503b7ae971 45 int ledReceived;
hudakz 0:c5e5d0df6f2a 46 Timer timer;
hudakz 5:c6503b7ae971 47 CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name, Automatic recovery from bus-off state enabled by default
hudakz 0:c5e5d0df6f2a 48 CANMessage rxMsg;
hudakz 0:c5e5d0df6f2a 49 CANMessage txMsg;
hudakz 5:c6503b7ae971 50 int counter = 0;
hudakz 0:c5e5d0df6f2a 51 volatile bool msgAvailable = false;
hudakz 0:c5e5d0df6f2a 52
hudakz 0:c5e5d0df6f2a 53 /**
hudakz 0:c5e5d0df6f2a 54 * @brief 'CAN receive-complete' interrup handler.
hudakz 0:c5e5d0df6f2a 55 * @note Called on arrival of new CAN message.
hudakz 0:c5e5d0df6f2a 56 * Keep it as short as possible.
hudakz 0:c5e5d0df6f2a 57 * @param
hudakz 0:c5e5d0df6f2a 58 * @retval
hudakz 0:c5e5d0df6f2a 59 */
hudakz 0:c5e5d0df6f2a 60 void onMsgReceived() {
hudakz 0:c5e5d0df6f2a 61 msgAvailable = true;
hudakz 0:c5e5d0df6f2a 62 }
hudakz 0:c5e5d0df6f2a 63
hudakz 0:c5e5d0df6f2a 64 /**
hudakz 0:c5e5d0df6f2a 65 * @brief Main
hudakz 0:c5e5d0df6f2a 66 * @note
hudakz 0:c5e5d0df6f2a 67 * @param
hudakz 0:c5e5d0df6f2a 68 * @retval
hudakz 0:c5e5d0df6f2a 69 */
hudakz 0:c5e5d0df6f2a 70 int main() {
hudakz 5:c6503b7ae971 71 can.frequency(1000000); // set bit rate to 1Mbps
hudakz 5:c6503b7ae971 72 can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive-complete' interrupt handler
hudakz 6:7ff95ce72f6d 73
hudakz 0:c5e5d0df6f2a 74 #if defined(BOARD1)
hudakz 6:7ff95ce72f6d 75 #if defined(TARGET_STM32F103C8T6)
hudakz 6:7ff95ce72f6d 76 led = 0; // turn LED on
hudakz 6:7ff95ce72f6d 77 #else
hudakz 6:7ff95ce72f6d 78 led = 1; // turn LED on
hudakz 6:7ff95ce72f6d 79 #endif
hudakz 2:49c9430860d1 80 timer.start();
hudakz 0:c5e5d0df6f2a 81 #else
hudakz 6:7ff95ce72f6d 82 #if defined(TARGET_STM32F103C8T6)
hudakz 6:7ff95ce72f6d 83 led = 1; // turn LED off
hudakz 6:7ff95ce72f6d 84 #else
hudakz 6:7ff95ce72f6d 85 led = 0; // turn LED off
hudakz 6:7ff95ce72f6d 86 #endif
hudakz 0:c5e5d0df6f2a 87 #endif
hudakz 0:c5e5d0df6f2a 88
hudakz 0:c5e5d0df6f2a 89 while(1) {
hudakz 4:ccf4ac2deac8 90 if(timer.read() >= 1.0) { // check for timeout
hudakz 0:c5e5d0df6f2a 91 timer.stop(); // stop timer
hudakz 0:c5e5d0df6f2a 92 timer.reset(); // reset timer (to avaoid repeated send)
hudakz 0:c5e5d0df6f2a 93 counter++; // increment counter
hudakz 0:c5e5d0df6f2a 94 txMsg.clear(); // clear Tx message storage
hudakz 0:c5e5d0df6f2a 95 txMsg.id = TX_ID; // set ID
hudakz 5:c6503b7ae971 96 txMsg << counter; // append first data item (make sure that CAN message total data lenght <= 8 bytes!)
hudakz 5:c6503b7ae971 97 txMsg << led.read(); // append second data item (make sure that CAN message total data lenght <= 8 bytes!)
hudakz 0:c5e5d0df6f2a 98 can.write(txMsg); // transmit message
hudakz 0:c5e5d0df6f2a 99 printf("CAN message sent\r\n");
hudakz 6:7ff95ce72f6d 100
hudakz 6:7ff95ce72f6d 101 #if defined(TARGET_STM32F103C8T6)
hudakz 6:7ff95ce72f6d 102 led = 1; // turn LED off
hudakz 6:7ff95ce72f6d 103 #else
hudakz 6:7ff95ce72f6d 104 led = 0; // turn LED off
hudakz 6:7ff95ce72f6d 105 #endif
hudakz 0:c5e5d0df6f2a 106 }
hudakz 0:c5e5d0df6f2a 107 if(msgAvailable) {
hudakz 0:c5e5d0df6f2a 108 msgAvailable = false; // reset flag for next use
hudakz 0:c5e5d0df6f2a 109 can.read(rxMsg); // read message into Rx message storage
hudakz 2:49c9430860d1 110 printf("CAN message received:\r\n");
hudakz 2:49c9430860d1 111 printf(" ID = %#x\r\n", rxMsg.id);
hudakz 2:49c9430860d1 112 printf(" Type = %d\r\n", rxMsg.type);
hudakz 2:49c9430860d1 113 printf(" Format = %d\r\n", rxMsg.format);
hudakz 2:49c9430860d1 114 printf(" Length = %d\r\n", rxMsg.len);
hudakz 2:49c9430860d1 115 printf(" Data =");
hudakz 2:49c9430860d1 116 for(int i = 0; i < rxMsg.len; i++)
hudakz 2:49c9430860d1 117 printf(" %x", rxMsg.data[i]);
hudakz 2:49c9430860d1 118 printf("\r\n");
hudakz 0:c5e5d0df6f2a 119 if(rxMsg.id == RX_ID) { // if ID matches
hudakz 0:c5e5d0df6f2a 120 rxMsg >> counter; // extract first data item
hudakz 5:c6503b7ae971 121 rxMsg >> ledReceived; // extract second data item
hudakz 5:c6503b7ae971 122 led = ledReceived; // set LED
hudakz 1:267d6288df33 123 printf("counter = %d\r\n", counter);
hudakz 0:c5e5d0df6f2a 124 timer.start();
hudakz 0:c5e5d0df6f2a 125 }
hudakz 0:c5e5d0df6f2a 126 }
hudakz 0:c5e5d0df6f2a 127 }
hudakz 0:c5e5d0df6f2a 128 }
hudakz 7:2dce8ed51091 129