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 Mar 07 21:28:19 2017 +0000
Revision:
24:e2907bcba75e
Parent:
23:069287e799cd
Child:
25:1d0488a03905
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:c5e5d0df6f2a 1 /*
hudakz 16:a86f339d1c25 2 * An example showing how to use the CANnucleo library:
hudakz 0:c5e5d0df6f2a 3 *
hudakz 20:eb1a8042605e 4 * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
hudakz 20:eb1a8042605e 5 * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
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 16:a86f339d1c25 10 * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/>
hudakz 6:7ff95ce72f6d 11 *
hudakz 21:7120a0dcc8ee 12 * NOTE: If you'd like to use an STM32F103C8T6 board uncomment line 23
hudakz 6:7ff95ce72f6d 13 *
hudakz 6:7ff95ce72f6d 14 * The same code is used for both NUCLEO boards, but:
hudakz 0:c5e5d0df6f2a 15 * For board #1 compile the example without any change.
hudakz 18:22977a898fe9 16 * For board #2 comment out line 23 before compiling
hudakz 4:ccf4ac2deac8 17 *
hudakz 6:7ff95ce72f6d 18 * Once the binaries have been downloaded to the boards reset board #1.
hudakz 0:c5e5d0df6f2a 19 *
hudakz 0:c5e5d0df6f2a 20 */
hudakz 0:c5e5d0df6f2a 21
hudakz 22:f4682a5ddda6 22 #define BOARD1 1 // comment out this line when compiling for board #2
hudakz 24:e2907bcba75e 23 #define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards!
hudakz 0:c5e5d0df6f2a 24
hudakz 16:a86f339d1c25 25 #if defined(TARGET_STM32F103C8T6)
hudakz 21:7120a0dcc8ee 26 #include "stm32f103c8t6.h"
hudakz 19:872e304d7e17 27 #define LED_PIN PC_13
hudakz 10:66da8731bdb6 28 const int OFF = 1;
hudakz 10:66da8731bdb6 29 const int ON = 0;
hudakz 0:c5e5d0df6f2a 30 #else
hudakz 19:872e304d7e17 31 #define LED_PIN LED1
hudakz 10:66da8731bdb6 32 const int OFF = 0;
hudakz 10:66da8731bdb6 33 const int ON = 1;
hudakz 0:c5e5d0df6f2a 34 #endif
hudakz 0:c5e5d0df6f2a 35
hudakz 10:66da8731bdb6 36 #if defined(BOARD1)
hudakz 10:66da8731bdb6 37 const unsigned int RX_ID = 0x100;
hudakz 10:66da8731bdb6 38 const unsigned int TX_ID = 0x101;
hudakz 6:7ff95ce72f6d 39 #else
hudakz 10:66da8731bdb6 40 const unsigned int RX_ID = 0x101;
hudakz 10:66da8731bdb6 41 const unsigned int TX_ID = 0x100;
hudakz 6:7ff95ce72f6d 42 #endif
hudakz 6:7ff95ce72f6d 43
hudakz 24:e2907bcba75e 44 #include "CANnucleo.h"
hudakz 16:a86f339d1c25 45 #include "mbed.h"
hudakz 16:a86f339d1c25 46
hudakz 17:18d4d0ff26a6 47 /*
hudakz 17:18d4d0ff26a6 48 * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
hudakz 17:18d4d0ff26a6 49 * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
hudakz 20:eb1a8042605e 50 * Remember to qualify them with the CANnucleo namespace.
hudakz 17:18d4d0ff26a6 51 */
hudakz 21:7120a0dcc8ee 52 CANnucleo::CAN* can;
hudakz 17:18d4d0ff26a6 53 CANnucleo::CANMessage rxMsg;
hudakz 17:18d4d0ff26a6 54 CANnucleo::CANMessage txMsg;
hudakz 19:872e304d7e17 55 DigitalOut led(LED_PIN);
hudakz 24:e2907bcba75e 56 int ledState;
hudakz 18:22977a898fe9 57 Timer timer;
hudakz 17:18d4d0ff26a6 58 int counter = 0;
hudakz 17:18d4d0ff26a6 59 volatile bool msgAvailable = false;
hudakz 22:f4682a5ddda6 60 Serial* pc;
hudakz 0:c5e5d0df6f2a 61
hudakz 16:a86f339d1c25 62 /**
hudakz 16:a86f339d1c25 63 * @brief 'CAN receive-complete' interrup handler.
hudakz 16:a86f339d1c25 64 * @note Called on arrival of new CAN message.
hudakz 16:a86f339d1c25 65 * Keep it as short as possible.
hudakz 16:a86f339d1c25 66 * @param
hudakz 16:a86f339d1c25 67 * @retval
hudakz 16:a86f339d1c25 68 */
hudakz 16:a86f339d1c25 69 void onMsgReceived() {
hudakz 16:a86f339d1c25 70 msgAvailable = true;
hudakz 16:a86f339d1c25 71 }
hudakz 16:a86f339d1c25 72
hudakz 16:a86f339d1c25 73 /**
hudakz 16:a86f339d1c25 74 * @brief Main
hudakz 16:a86f339d1c25 75 * @note
hudakz 16:a86f339d1c25 76 * @param
hudakz 16:a86f339d1c25 77 * @retval
hudakz 16:a86f339d1c25 78 */
hudakz 16:a86f339d1c25 79 int main() {
hudakz 21:7120a0dcc8ee 80 #if defined(TARGET_STM32F103C8T6)
hudakz 21:7120a0dcc8ee 81 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
hudakz 21:7120a0dcc8ee 82 #endif
hudakz 22:f4682a5ddda6 83 pc = new Serial(PA_2, PA_3);
hudakz 21:7120a0dcc8ee 84 can = new CANnucleo::CAN(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 21:7120a0dcc8ee 85 can->frequency(1000000); // set bit rate to 1Mbps
hudakz 21:7120a0dcc8ee 86 can->attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
hudakz 16:a86f339d1c25 87
hudakz 0:c5e5d0df6f2a 88 #if defined(BOARD1)
hudakz 10:66da8731bdb6 89 led = ON; // turn LED on
hudakz 10:66da8731bdb6 90 timer.start(); // start timer
hudakz 22:f4682a5ddda6 91 pc->printf("CANnucleo_Hello board #1\r\n");
hudakz 0:c5e5d0df6f2a 92 #else
hudakz 10:66da8731bdb6 93 led = OFF; // turn LED off
hudakz 22:f4682a5ddda6 94 pc->printf("CANnucleo_Hello board #2\r\n");
hudakz 0:c5e5d0df6f2a 95 #endif
hudakz 0:c5e5d0df6f2a 96
hudakz 0:c5e5d0df6f2a 97 while(1) {
hudakz 23:069287e799cd 98 if(timer.read_ms() >= 1000) { // check for timeout
hudakz 16:a86f339d1c25 99 timer.stop(); // stop timer
hudakz 16:a86f339d1c25 100 timer.reset(); // reset timer
hudakz 16:a86f339d1c25 101 counter++; // increment counter
hudakz 24:e2907bcba75e 102 ledState = led.read(); // get led state
hudakz 16:a86f339d1c25 103 txMsg.clear(); // clear Tx message storage
hudakz 16:a86f339d1c25 104 txMsg.id = TX_ID; // set ID
hudakz 24:e2907bcba75e 105 txMsg << counter << ledState; // append data (total data length must be <= 8 bytes!)
hudakz 16:a86f339d1c25 106 led = OFF; // turn LED off
hudakz 24:e2907bcba75e 107 if(can->write(txMsg)) { // transmit message
hudakz 24:e2907bcba75e 108 pc->printf("-----------------------------------\r\n");
hudakz 22:f4682a5ddda6 109 pc->printf("CAN message sent\r\n");
hudakz 24:e2907bcba75e 110 pc->printf(" ID = 0x%.3x\r\n", txMsg.id);
hudakz 24:e2907bcba75e 111 pc->printf(" Type = %d\r\n", txMsg.type);
hudakz 24:e2907bcba75e 112 pc->printf(" Format = %d\r\n", txMsg.format);
hudakz 24:e2907bcba75e 113 pc->printf(" Length = %d\r\n", txMsg.len);
hudakz 24:e2907bcba75e 114 pc->printf(" Data =");
hudakz 24:e2907bcba75e 115 for(int i = 0; i < txMsg.len; i++)
hudakz 24:e2907bcba75e 116 pc->printf(" %.2x", txMsg.data[i]);
hudakz 24:e2907bcba75e 117 pc->printf("\r\n");
hudakz 24:e2907bcba75e 118 pc->printf(" counter = %d\r\n", counter);
hudakz 24:e2907bcba75e 119 }
hudakz 10:66da8731bdb6 120 else
hudakz 22:f4682a5ddda6 121 pc->printf("Transmission error\r\n");
hudakz 0:c5e5d0df6f2a 122 }
hudakz 16:a86f339d1c25 123 if(msgAvailable) {
hudakz 16:a86f339d1c25 124 msgAvailable = false; // reset flag for next use
hudakz 24:e2907bcba75e 125 can->read(rxMsg); // read message into Rx message storage
hudakz 22:f4682a5ddda6 126 pc->printf("CAN message received\r\n");
hudakz 22:f4682a5ddda6 127 pc->printf(" ID = 0x%.3x\r\n", rxMsg.id);
hudakz 22:f4682a5ddda6 128 pc->printf(" Type = %d\r\n", rxMsg.type);
hudakz 22:f4682a5ddda6 129 pc->printf(" Format = %d\r\n", rxMsg.format);
hudakz 22:f4682a5ddda6 130 pc->printf(" Length = %d\r\n", rxMsg.len);
hudakz 22:f4682a5ddda6 131 pc->printf(" Data =");
hudakz 2:49c9430860d1 132 for(int i = 0; i < rxMsg.len; i++)
hudakz 22:f4682a5ddda6 133 pc->printf(" %.2x", rxMsg.data[i]);
hudakz 22:f4682a5ddda6 134 pc->printf("\r\n");
hudakz 16:a86f339d1c25 135 // Filtering performed by software:
hudakz 24:e2907bcba75e 136 if(rxMsg.id == RX_ID) { // About filtering performed by hardware see comments in CANnucleo.cpp
hudakz 24:e2907bcba75e 137 rxMsg >> counter >> ledState; // extract data
hudakz 22:f4682a5ddda6 138 pc->printf(" counter = %d\r\n", counter);
hudakz 16:a86f339d1c25 139 led = ON; // turn LED on
hudakz 16:a86f339d1c25 140 timer.start(); // transmission lag
hudakz 0:c5e5d0df6f2a 141 }
hudakz 0:c5e5d0df6f2a 142 }
hudakz 0:c5e5d0df6f2a 143 }
hudakz 0:c5e5d0df6f2a 144 }
hudakz 24:e2907bcba75e 145
hudakz 24:e2907bcba75e 146
hudakz 24:e2907bcba75e 147