Communication between two NUCLEO 144 using CAN bus.

Dependencies:   mbed CANMsg

Committer:
Pankaj2201
Date:
Fri Mar 22 14:53:12 2019 +0000
Revision:
10:c73d30edad03
Parent:
9:3211e88e30a5
Communication between two NUCLEO 144 using CAN bus;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:1b9561cd1c36 1 /*
hudakz 1:6f8ffb2c2dd7 2 * An example showing how to use the mbed CAN API:
hudakz 0:1b9561cd1c36 3 *
hudakz 3:87a128bca8f5 4 * Two affordable (about $2 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
hudakz 1:6f8ffb2c2dd7 5 * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
hudakz 0:1b9561cd1c36 6 * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
hudakz 0:1b9561cd1c36 7 * CAN transceivers are not part of NUCLEO boards, therefore must be added by you.
hudakz 0:1b9561cd1c36 8 * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
hudakz 1:6f8ffb2c2dd7 9 *
hudakz 0:1b9561cd1c36 10 *
hudakz 1:6f8ffb2c2dd7 11 * The same code is used for both mbed boards, but:
hudakz 0:1b9561cd1c36 12 * For board #1 compile the example without any change.
hudakz 3:87a128bca8f5 13 * For board #2 comment out line 21 before compiling
hudakz 0:1b9561cd1c36 14 *
hudakz 3:87a128bca8f5 15 * Once the binaries have been downloaded to the boards reset both boards at the same time.
hudakz 0:1b9561cd1c36 16 *
hudakz 0:1b9561cd1c36 17 */
hudakz 1:6f8ffb2c2dd7 18
Pankaj2201 10:c73d30edad03 19 #define TARGET_NUCLEOF767ZI 1 // uncomment this line to use STM32F103C8T6 boards
hudakz 4:09d564da0e24 20
hudakz 4:09d564da0e24 21 #define BOARD1 1 // comment out this line when compiling for board #2
hudakz 0:1b9561cd1c36 22
Pankaj2201 10:c73d30edad03 23 #if defined(TARGET_NUCLEOF767ZI)
Pankaj2201 10:c73d30edad03 24 #define LED_PIN PB_7
hudakz 7:883da97339ab 25 const int OFF = 1;
hudakz 7:883da97339ab 26 const int ON = 0;
hudakz 0:1b9561cd1c36 27 #else
Pankaj2201 10:c73d30edad03 28 #define LED_PIN PB_7
hudakz 7:883da97339ab 29 const int OFF = 0;
hudakz 7:883da97339ab 30 const int ON = 1;
hudakz 0:1b9561cd1c36 31 #endif
hudakz 7:883da97339ab 32
hudakz 0:1b9561cd1c36 33 #if defined(BOARD1)
hudakz 7:883da97339ab 34 const unsigned int RX_ID = 0x100;
hudakz 7:883da97339ab 35 const unsigned int TX_ID = 0x101;
hudakz 0:1b9561cd1c36 36 #else
hudakz 7:883da97339ab 37 const unsigned int RX_ID = 0x101;
hudakz 7:883da97339ab 38 const unsigned int TX_ID = 0x100;
hudakz 0:1b9561cd1c36 39 #endif
hudakz 7:883da97339ab 40
hudakz 0:1b9561cd1c36 41 #include "mbed.h"
hudakz 0:1b9561cd1c36 42 #include "CANMsg.h"
Pankaj2201 10:c73d30edad03 43 #include "mbed_error.h"
Pankaj2201 10:c73d30edad03 44 CircularBuffer<char, 1024> rxbuf; // PC receiving Buffer
hudakz 3:87a128bca8f5 45 Serial pc(USBTX, USBRX);
Pankaj2201 10:c73d30edad03 46 CAN can(PD_0, PD_1); // CAN Rx pin name, CAN Tx pin name
hudakz 9:3211e88e30a5 47 //CAN can(p30, p29); // CAN Rx pin name, CAN Tx pin name
hudakz 0:1b9561cd1c36 48 CANMsg rxMsg;
hudakz 0:1b9561cd1c36 49 CANMsg txMsg;
hudakz 0:1b9561cd1c36 50 DigitalOut led(LED_PIN);
hudakz 0:1b9561cd1c36 51 Timer timer;
hudakz 0:1b9561cd1c36 52 uint8_t counter = 0;
hudakz 0:1b9561cd1c36 53 AnalogIn analogIn(A0);
hudakz 0:1b9561cd1c36 54 float voltage;
hudakz 0:1b9561cd1c36 55
hudakz 0:1b9561cd1c36 56 /**
hudakz 8:c65afde7f7f5 57 * @brief Prints CAN message to PC's serial terminal
hudakz 2:6546e4a2d593 58 * @note
hudakz 1:6f8ffb2c2dd7 59 * @param CANMessage to print
hudakz 3:87a128bca8f5 60 * @retval
hudakz 0:1b9561cd1c36 61 */
hudakz 5:37ab4112d547 62 void printMsg(CANMessage& msg)
hudakz 5:37ab4112d547 63 {
hudakz 0:1b9561cd1c36 64 pc.printf(" ID = 0x%.3x\r\n", msg.id);
hudakz 0:1b9561cd1c36 65 pc.printf(" Type = %d\r\n", msg.type);
hudakz 0:1b9561cd1c36 66 pc.printf(" Format = %d\r\n", msg.format);
hudakz 0:1b9561cd1c36 67 pc.printf(" Length = %d\r\n", msg.len);
hudakz 0:1b9561cd1c36 68 pc.printf(" Data =");
hudakz 0:1b9561cd1c36 69 for(int i = 0; i < msg.len; i++)
hudakz 0:1b9561cd1c36 70 pc.printf(" 0x%.2X", msg.data[i]);
hudakz 0:1b9561cd1c36 71 pc.printf("\r\n");
hudakz 0:1b9561cd1c36 72 }
hudakz 0:1b9561cd1c36 73
hudakz 0:1b9561cd1c36 74 /**
hudakz 5:37ab4112d547 75 * @brief Handles received CAN messages
hudakz 8:c65afde7f7f5 76 * @note Called on 'CAN message received' interrupt.
hudakz 5:37ab4112d547 77 * @param
hudakz 5:37ab4112d547 78 * @retval
hudakz 5:37ab4112d547 79 */
Pankaj2201 10:c73d30edad03 80
Pankaj2201 10:c73d30edad03 81
Pankaj2201 10:c73d30edad03 82
Pankaj2201 10:c73d30edad03 83
Pankaj2201 10:c73d30edad03 84
hudakz 5:37ab4112d547 85 void onCanReceived(void)
hudakz 5:37ab4112d547 86 {
hudakz 5:37ab4112d547 87 can.read(rxMsg);
hudakz 9:3211e88e30a5 88 pc.printf("-------------------------------------\r\n");
hudakz 9:3211e88e30a5 89 pc.printf("CAN message received\r\n");
hudakz 5:37ab4112d547 90 printMsg(rxMsg);
Pankaj2201 10:c73d30edad03 91
Pankaj2201 10:c73d30edad03 92 rxbuf.reset();
hudakz 5:37ab4112d547 93
hudakz 5:37ab4112d547 94 if (rxMsg.id == RX_ID) {
hudakz 5:37ab4112d547 95 // extract data from the received CAN message
hudakz 8:c65afde7f7f5 96 // in the same order as it was added on the transmitter side
hudakz 5:37ab4112d547 97 rxMsg >> counter;
Pankaj2201 10:c73d30edad03 98 rxMsg >> voltage;
hudakz 5:37ab4112d547 99 pc.printf(" counter = %d\r\n", counter);
hudakz 5:37ab4112d547 100 pc.printf(" voltage = %e V\r\n", voltage);
hudakz 5:37ab4112d547 101 }
Pankaj2201 10:c73d30edad03 102
hudakz 8:c65afde7f7f5 103 timer.start(); // to transmit next message in main
hudakz 5:37ab4112d547 104 }
hudakz 5:37ab4112d547 105
hudakz 5:37ab4112d547 106
hudakz 5:37ab4112d547 107 /**
hudakz 0:1b9561cd1c36 108 * @brief Main
hudakz 0:1b9561cd1c36 109 * @note
hudakz 0:1b9561cd1c36 110 * @param
hudakz 0:1b9561cd1c36 111 * @retval
hudakz 0:1b9561cd1c36 112 */
Pankaj2201 10:c73d30edad03 113
Pankaj2201 10:c73d30edad03 114
Pankaj2201 10:c73d30edad03 115
hudakz 0:1b9561cd1c36 116 int main(void)
hudakz 0:1b9561cd1c36 117 {
hudakz 8:c65afde7f7f5 118 pc.baud(9600); // set serial speed
hudakz 8:c65afde7f7f5 119 can.frequency(1000000); // set CAN bit rate to 1Mbps
hudakz 5:37ab4112d547 120 can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID
hudakz 8:c65afde7f7f5 121 can.attach(onCanReceived); // attach ISR to handle received messages
hudakz 5:37ab4112d547 122
Pankaj2201 10:c73d30edad03 123
hudakz 0:1b9561cd1c36 124 #if defined(BOARD1)
hudakz 1:6f8ffb2c2dd7 125 led = ON; // turn the LED on
hudakz 1:6f8ffb2c2dd7 126 timer.start(); // start timer
Pankaj2201 10:c73d30edad03 127 pc.printf("reset pressed from board #1\r\n");
hudakz 0:1b9561cd1c36 128 #else
hudakz 0:1b9561cd1c36 129 led = OFF; // turn LED off
Pankaj2201 10:c73d30edad03 130 pc.printf("reset pressed from board #2\r\n");
hudakz 0:1b9561cd1c36 131 #endif
hudakz 0:1b9561cd1c36 132 while(1) {
Pankaj2201 10:c73d30edad03 133 if(timer.read_ms() >= 3000) { // check for timeout
hudakz 3:87a128bca8f5 134 timer.stop(); // stop timer
hudakz 3:87a128bca8f5 135 timer.reset(); // reset timer
hudakz 3:87a128bca8f5 136 counter++; // increment counter
hudakz 5:37ab4112d547 137 voltage = analogIn * 3.3f; // read the small drift voltage from analog input
hudakz 3:87a128bca8f5 138 txMsg.clear(); // clear Tx message storage
hudakz 3:87a128bca8f5 139 txMsg.id = TX_ID; // set ID
hudakz 5:37ab4112d547 140 // append data (total data length must not exceed 8 bytes!)
hudakz 5:37ab4112d547 141 txMsg << counter; // one byte
hudakz 5:37ab4112d547 142 txMsg << voltage; // four bytes
Pankaj2201 10:c73d30edad03 143 if (counter==10){
Pankaj2201 10:c73d30edad03 144 break;
Pankaj2201 10:c73d30edad03 145 }
hudakz 3:87a128bca8f5 146 if(can.write(txMsg)) { // transmit message
hudakz 3:87a128bca8f5 147 led = OFF; // turn the LED off
hudakz 0:1b9561cd1c36 148 pc.printf("-------------------------------------\r\n");
hudakz 9:3211e88e30a5 149 pc.printf("-------------------------------------\r\n");
hudakz 0:1b9561cd1c36 150 pc.printf("CAN message sent\r\n");
hudakz 0:1b9561cd1c36 151 printMsg(txMsg);
hudakz 0:1b9561cd1c36 152 pc.printf(" counter = %d\r\n", counter);
hudakz 0:1b9561cd1c36 153 pc.printf(" voltage = %e V\r\n", voltage);
hudakz 0:1b9561cd1c36 154 }
hudakz 0:1b9561cd1c36 155 else
hudakz 0:1b9561cd1c36 156 pc.printf("Transmission error\r\n");
Pankaj2201 10:c73d30edad03 157
Pankaj2201 10:c73d30edad03 158
hudakz 0:1b9561cd1c36 159 }
hudakz 0:1b9561cd1c36 160 }
hudakz 0:1b9561cd1c36 161 }