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:
Wed Jul 27 09:16:40 2016 +0000
Revision:
13:e741def3e4ff
Parent:
12:e91e44924194
Child:
14:e12ebd1260b1
Modified to use CAN support built into the mbed library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:c5e5d0df6f2a 1 /*
hudakz 13:e741def3e4ff 2 * An example of using CAN bus with NUCLEO boards:
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 13:e741def3e4ff 10 * For more details see the wiki page [https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/]
hudakz 6:7ff95ce72f6d 11 *
hudakz 13:e741def3e4ff 12 * NOTE: If you'd like to use the official NUCLEO boards comment out line 24
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 13:e741def3e4ff 16 * For board #2 comment out line 25 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 0:c5e5d0df6f2a 22 #include "mbed.h"
hudakz 0:c5e5d0df6f2a 23
hudakz 13:e741def3e4ff 24 #define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards!
hudakz 10:66da8731bdb6 25 #define BOARD1 1 // comment out this line when compiling for board #2
hudakz 0:c5e5d0df6f2a 26
hudakz 10:66da8731bdb6 27 #if defined(TARGET_STM32F103C8T6)
hudakz 10:66da8731bdb6 28 #define LED_PIN PC_13
hudakz 10:66da8731bdb6 29 const int OFF = 1;
hudakz 10:66da8731bdb6 30 const int ON = 0;
hudakz 0:c5e5d0df6f2a 31 #else
hudakz 10:66da8731bdb6 32 #define LED_PIN LED1
hudakz 10:66da8731bdb6 33 const int OFF = 0;
hudakz 10:66da8731bdb6 34 const int ON = 1;
hudakz 0:c5e5d0df6f2a 35 #endif
hudakz 0:c5e5d0df6f2a 36
hudakz 10:66da8731bdb6 37 #if defined(BOARD1)
hudakz 10:66da8731bdb6 38 const unsigned int RX_ID = 0x100;
hudakz 10:66da8731bdb6 39 const unsigned int TX_ID = 0x101;
hudakz 6:7ff95ce72f6d 40 #else
hudakz 10:66da8731bdb6 41 const unsigned int RX_ID = 0x101;
hudakz 10:66da8731bdb6 42 const unsigned int TX_ID = 0x100;
hudakz 6:7ff95ce72f6d 43 #endif
hudakz 6:7ff95ce72f6d 44
hudakz 11:07d927da1a94 45 DigitalOut led(LED_PIN);
hudakz 11:07d927da1a94 46 Timer timer;
hudakz 11:07d927da1a94 47 CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 11:07d927da1a94 48 CANMessage rxMsg;
hudakz 11:07d927da1a94 49 CANMessage txMsg;
hudakz 13:e741def3e4ff 50 char counter = 0;
hudakz 0:c5e5d0df6f2a 51
hudakz 0:c5e5d0df6f2a 52 /**
hudakz 0:c5e5d0df6f2a 53 * @brief Main
hudakz 0:c5e5d0df6f2a 54 * @note
hudakz 0:c5e5d0df6f2a 55 * @param
hudakz 0:c5e5d0df6f2a 56 * @retval
hudakz 0:c5e5d0df6f2a 57 */
hudakz 0:c5e5d0df6f2a 58 int main() {
hudakz 6:7ff95ce72f6d 59
hudakz 0:c5e5d0df6f2a 60 #if defined(BOARD1)
hudakz 10:66da8731bdb6 61 led = ON; // turn LED on
hudakz 10:66da8731bdb6 62 timer.start(); // start timer
hudakz 0:c5e5d0df6f2a 63 #else
hudakz 10:66da8731bdb6 64 led = OFF; // turn LED off
hudakz 0:c5e5d0df6f2a 65 #endif
hudakz 0:c5e5d0df6f2a 66
hudakz 0:c5e5d0df6f2a 67 while(1) {
hudakz 13:e741def3e4ff 68 if(timer.read_ms() >= 200) { // check for timeout
hudakz 0:c5e5d0df6f2a 69 timer.stop(); // stop timer
hudakz 10:66da8731bdb6 70 timer.reset(); // reset timer
hudakz 0:c5e5d0df6f2a 71 counter++; // increment counter
hudakz 0:c5e5d0df6f2a 72 txMsg.id = TX_ID; // set ID
hudakz 13:e741def3e4ff 73 txMsg.data[0] = counter; // set data
hudakz 10:66da8731bdb6 74 led = OFF; // turn LED off
hudakz 10:66da8731bdb6 75 if(can.write(txMsg)) // transmit message
hudakz 10:66da8731bdb6 76 printf("CAN message sent\r\n");
hudakz 10:66da8731bdb6 77 else
hudakz 10:66da8731bdb6 78 printf("Transmission error\r\n");
hudakz 0:c5e5d0df6f2a 79 }
hudakz 13:e741def3e4ff 80 if(can.read(rxMsg)) {
hudakz 2:49c9430860d1 81 printf("CAN message received:\r\n");
hudakz 2:49c9430860d1 82 printf(" ID = %#x\r\n", rxMsg.id);
hudakz 2:49c9430860d1 83 printf(" Type = %d\r\n", rxMsg.type);
hudakz 2:49c9430860d1 84 printf(" Format = %d\r\n", rxMsg.format);
hudakz 2:49c9430860d1 85 printf(" Length = %d\r\n", rxMsg.len);
hudakz 2:49c9430860d1 86 printf(" Data =");
hudakz 2:49c9430860d1 87 for(int i = 0; i < rxMsg.len; i++)
hudakz 2:49c9430860d1 88 printf(" %x", rxMsg.data[i]);
hudakz 10:66da8731bdb6 89 printf("\r\n");
hudakz 13:e741def3e4ff 90
hudakz 13:e741def3e4ff 91 if(rxMsg.id == RX_ID) {
hudakz 13:e741def3e4ff 92 counter = rxMsg.data[0]; // set counter
hudakz 1:267d6288df33 93 printf("counter = %d\r\n", counter);
hudakz 13:e741def3e4ff 94 led = !led; // set LED
hudakz 10:66da8731bdb6 95 timer.start(); // transmission lag
hudakz 0:c5e5d0df6f2a 96 }
hudakz 0:c5e5d0df6f2a 97 }
hudakz 0:c5e5d0df6f2a 98 }
hudakz 0:c5e5d0df6f2a 99 }
hudakz 7:2dce8ed51091 100
hudakz 12:e91e44924194 101