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:
Sat Jul 25 19:43:00 2015 +0000
Revision:
4:ccf4ac2deac8
Parent:
3:464b06c16d24
Child:
5:c6503b7ae971
updated

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 2:49c9430860d1 4 * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MCP2551 or TJA1040, or etc.).
hudakz 0:c5e5d0df6f2a 5 * Transceivers are not part of the NUCLEO boards, therefore must be added by you.
hudakz 0:c5e5d0df6f2a 6 * Remember also that CAN bus must be terminated with 120 Ohm resitors on both ends.
hudakz 0:c5e5d0df6f2a 7 * See <https://developer.mbed.org/users/WiredHome/notebook/can---getting-started/>
hudakz 0:c5e5d0df6f2a 8 * The same source code is used for both NUCLEO boards, but:
hudakz 0:c5e5d0df6f2a 9 * For board #1 compile the example without any change.
hudakz 4:ccf4ac2deac8 10 * For board #2 comment out the line #define BOARD1 1 before compiling
hudakz 4:ccf4ac2deac8 11 *
hudakz 4:ccf4ac2deac8 12 * Once compiled, download the binaries to the boards.
hudakz 4:ccf4ac2deac8 13 * To start ping/ponging messages reset both boards at the same time.
hudakz 0:c5e5d0df6f2a 14 *
hudakz 0:c5e5d0df6f2a 15 * Note:
hudakz 0:c5e5d0df6f2a 16 * To simplify adding/getting data to/from a CAN message
hudakz 0:c5e5d0df6f2a 17 * inserter "<<" and extractor ">>" operators have been defined.
hudakz 0:c5e5d0df6f2a 18 * Please be aware that CAN message maximum data length is limited to eight bytes.
hudakz 0:c5e5d0df6f2a 19 * To make sure this limitation is not violated I recommend to first compile
hudakz 0:c5e5d0df6f2a 20 * your application with DEBUG enabled in "CAN.h" file.
hudakz 0:c5e5d0df6f2a 21 * Then run it and check for error messages.
hudakz 0:c5e5d0df6f2a 22 */
hudakz 0:c5e5d0df6f2a 23
hudakz 0:c5e5d0df6f2a 24 #include "mbed.h"
hudakz 0:c5e5d0df6f2a 25 #include "CAN.h"
hudakz 0:c5e5d0df6f2a 26
hudakz 3:464b06c16d24 27 #define BOARD1 1 // please comment out this line when compiling for board #2
hudakz 0:c5e5d0df6f2a 28
hudakz 0:c5e5d0df6f2a 29 #if defined(BOARD1)
hudakz 0:c5e5d0df6f2a 30 #define RX_ID 0x100
hudakz 0:c5e5d0df6f2a 31 #define TX_ID 0x101
hudakz 0:c5e5d0df6f2a 32 #else
hudakz 0:c5e5d0df6f2a 33 #define RX_ID 0x101
hudakz 0:c5e5d0df6f2a 34 #define TX_ID 0x100
hudakz 0:c5e5d0df6f2a 35 #endif
hudakz 0:c5e5d0df6f2a 36
hudakz 0:c5e5d0df6f2a 37 DigitalOut led(LED1);
hudakz 3:464b06c16d24 38 int ledTarget;
hudakz 0:c5e5d0df6f2a 39 Timer timer;
hudakz 0:c5e5d0df6f2a 40 CAN can(PA_11, PA_12); // rx, tx
hudakz 0:c5e5d0df6f2a 41 CANMessage rxMsg;
hudakz 0:c5e5d0df6f2a 42 CANMessage txMsg;
hudakz 2:49c9430860d1 43 long int counter = 0;
hudakz 0:c5e5d0df6f2a 44 volatile bool msgAvailable = false;
hudakz 0:c5e5d0df6f2a 45
hudakz 0:c5e5d0df6f2a 46 /**
hudakz 0:c5e5d0df6f2a 47 * @brief 'CAN receive-complete' interrup handler.
hudakz 0:c5e5d0df6f2a 48 * @note Called on arrival of new CAN message.
hudakz 0:c5e5d0df6f2a 49 * Keep it as short as possible.
hudakz 0:c5e5d0df6f2a 50 * @param
hudakz 0:c5e5d0df6f2a 51 * @retval
hudakz 0:c5e5d0df6f2a 52 */
hudakz 0:c5e5d0df6f2a 53 void onMsgReceived() {
hudakz 0:c5e5d0df6f2a 54 msgAvailable = true;
hudakz 0:c5e5d0df6f2a 55 }
hudakz 0:c5e5d0df6f2a 56
hudakz 0:c5e5d0df6f2a 57 /**
hudakz 0:c5e5d0df6f2a 58 * @brief Main
hudakz 0:c5e5d0df6f2a 59 * @note
hudakz 0:c5e5d0df6f2a 60 * @param
hudakz 0:c5e5d0df6f2a 61 * @retval
hudakz 0:c5e5d0df6f2a 62 */
hudakz 0:c5e5d0df6f2a 63 int main() {
hudakz 4:ccf4ac2deac8 64 can.frequency(1000000); // Initialize CAN and set bit rate to 1Mbps
hudakz 0:c5e5d0df6f2a 65 can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive complete' interrupt handler
hudakz 0:c5e5d0df6f2a 66 timer.reset();
hudakz 0:c5e5d0df6f2a 67 #if defined(BOARD1)
hudakz 2:49c9430860d1 68 led = 1;
hudakz 2:49c9430860d1 69 timer.start();
hudakz 0:c5e5d0df6f2a 70 #else
hudakz 2:49c9430860d1 71 led = 0;
hudakz 0:c5e5d0df6f2a 72 #endif
hudakz 0:c5e5d0df6f2a 73
hudakz 0:c5e5d0df6f2a 74 while(1) {
hudakz 4:ccf4ac2deac8 75 if(timer.read() >= 1.0) { // check for timeout
hudakz 0:c5e5d0df6f2a 76 timer.stop(); // stop timer
hudakz 0:c5e5d0df6f2a 77 timer.reset(); // reset timer (to avaoid repeated send)
hudakz 0:c5e5d0df6f2a 78 counter++; // increment counter
hudakz 0:c5e5d0df6f2a 79 txMsg.clear(); // clear Tx message storage
hudakz 0:c5e5d0df6f2a 80 txMsg.id = TX_ID; // set ID
hudakz 0:c5e5d0df6f2a 81 txMsg << counter; // append first data item (always make sure that CAN message total data lenght <= 8 bytes!)
hudakz 0:c5e5d0df6f2a 82 txMsg << led.read(); // append second data item (always make sure that CAN message total data lenght <= 8 bytes!)
hudakz 0:c5e5d0df6f2a 83 can.write(txMsg); // transmit message
hudakz 0:c5e5d0df6f2a 84 printf("CAN message sent\r\n");
hudakz 2:49c9430860d1 85 led = 0; // turn off LED
hudakz 0:c5e5d0df6f2a 86 }
hudakz 0:c5e5d0df6f2a 87 if(msgAvailable) {
hudakz 0:c5e5d0df6f2a 88 msgAvailable = false; // reset flag for next use
hudakz 0:c5e5d0df6f2a 89 can.read(rxMsg); // read message into Rx message storage
hudakz 2:49c9430860d1 90 printf("CAN message received:\r\n");
hudakz 2:49c9430860d1 91 printf(" ID = %#x\r\n", rxMsg.id);
hudakz 2:49c9430860d1 92 printf(" Type = %d\r\n", rxMsg.type);
hudakz 2:49c9430860d1 93 printf(" Format = %d\r\n", rxMsg.format);
hudakz 2:49c9430860d1 94 printf(" Length = %d\r\n", rxMsg.len);
hudakz 2:49c9430860d1 95 printf(" Data =");
hudakz 2:49c9430860d1 96 for(int i = 0; i < rxMsg.len; i++)
hudakz 2:49c9430860d1 97 printf(" %x", rxMsg.data[i]);
hudakz 2:49c9430860d1 98 printf("\r\n");
hudakz 0:c5e5d0df6f2a 99 if(rxMsg.id == RX_ID) { // if ID matches
hudakz 0:c5e5d0df6f2a 100 rxMsg >> counter; // extract first data item
hudakz 3:464b06c16d24 101 rxMsg >> ledTarget; // extract second data item
hudakz 3:464b06c16d24 102 led = ledTarget; // set LED
hudakz 1:267d6288df33 103 printf("counter = %d\r\n", counter);
hudakz 0:c5e5d0df6f2a 104 timer.start();
hudakz 0:c5e5d0df6f2a 105 }
hudakz 0:c5e5d0df6f2a 106 }
hudakz 0:c5e5d0df6f2a 107 }
hudakz 0:c5e5d0df6f2a 108 }
hudakz 0:c5e5d0df6f2a 109
hudakz 0:c5e5d0df6f2a 110