Using CAN bus with (not just NUCLEO) mbed boards support Cortex-M0

Dependencies:   CANMsg mbed

Fork of CAN_Hello by QT Chan

Committer:
szqt
Date:
Thu Mar 22 13:41:08 2018 +0000
Revision:
3:36b034f320b6
Parent:
2:6546e4a2d593
Support NUCLEO-F091RC (Cortex-M0)

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 0:1b9561cd1c36 4 * Two affordable (less than $3 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 1:6f8ffb2c2dd7 10 * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/>
hudakz 0:1b9561cd1c36 11 *
hudakz 1:6f8ffb2c2dd7 12 * NOTE: If you'd like to use an STM32F103C8T6 board uncomment line 23
hudakz 0:1b9561cd1c36 13 *
hudakz 1:6f8ffb2c2dd7 14 * The same code is used for both mbed boards, but:
hudakz 0:1b9561cd1c36 15 * For board #1 compile the example without any change.
hudakz 1:6f8ffb2c2dd7 16 * For board #2 comment out line 23 before compiling
hudakz 0:1b9561cd1c36 17 *
hudakz 0:1b9561cd1c36 18 * Once the binaries have been downloaded to the boards reset board #1.
hudakz 0:1b9561cd1c36 19 *
hudakz 0:1b9561cd1c36 20 */
hudakz 1:6f8ffb2c2dd7 21 #define BOARD1 1 // comment out this line when compiling for board #2
hudakz 1:6f8ffb2c2dd7 22
hudakz 1:6f8ffb2c2dd7 23 //#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards!
hudakz 0:1b9561cd1c36 24
hudakz 0:1b9561cd1c36 25 #if defined(TARGET_STM32F103C8T6)
hudakz 0:1b9561cd1c36 26 #include "stm32f103c8t6.h"
hudakz 0:1b9561cd1c36 27 #define LED_PIN PC_13
hudakz 0:1b9561cd1c36 28 const int OFF = 1;
hudakz 0:1b9561cd1c36 29 const int ON = 0;
hudakz 0:1b9561cd1c36 30 #else
hudakz 0:1b9561cd1c36 31 #define LED_PIN LED1
hudakz 0:1b9561cd1c36 32 const int OFF = 0;
hudakz 0:1b9561cd1c36 33 const int ON = 1;
hudakz 0:1b9561cd1c36 34 #endif
hudakz 0:1b9561cd1c36 35 #if defined(BOARD1)
hudakz 0:1b9561cd1c36 36 const unsigned int RX_ID = 0x100;
hudakz 0:1b9561cd1c36 37 const unsigned int TX_ID = 0x101;
hudakz 0:1b9561cd1c36 38 #else
hudakz 0:1b9561cd1c36 39 const unsigned int RX_ID = 0x101;
hudakz 0:1b9561cd1c36 40 const unsigned int TX_ID = 0x100;
hudakz 0:1b9561cd1c36 41 #endif
hudakz 0:1b9561cd1c36 42 #include "mbed.h"
hudakz 0:1b9561cd1c36 43 #include "CANMsg.h"
hudakz 0:1b9561cd1c36 44
hudakz 1:6f8ffb2c2dd7 45 Serial pc(PA_2, PA_3);
hudakz 1:6f8ffb2c2dd7 46 CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
hudakz 0:1b9561cd1c36 47 CANMsg rxMsg;
hudakz 0:1b9561cd1c36 48 CANMsg txMsg;
hudakz 0:1b9561cd1c36 49 DigitalOut led(LED_PIN);
hudakz 0:1b9561cd1c36 50 Timer timer;
hudakz 0:1b9561cd1c36 51 uint8_t counter = 0;
hudakz 0:1b9561cd1c36 52 AnalogIn analogIn(A0);
hudakz 0:1b9561cd1c36 53 float voltage;
hudakz 0:1b9561cd1c36 54
hudakz 0:1b9561cd1c36 55 /**
hudakz 0:1b9561cd1c36 56 * @brief Prints CAN msg to PC's serial terminal
hudakz 2:6546e4a2d593 57 * @note
hudakz 1:6f8ffb2c2dd7 58 * @param CANMessage to print
hudakz 0:1b9561cd1c36 59 * @retval none
hudakz 0:1b9561cd1c36 60 */
hudakz 1:6f8ffb2c2dd7 61 void printMsg(CANMessage& msg) {
hudakz 0:1b9561cd1c36 62 pc.printf(" ID = 0x%.3x\r\n", msg.id);
hudakz 0:1b9561cd1c36 63 pc.printf(" Type = %d\r\n", msg.type);
hudakz 0:1b9561cd1c36 64 pc.printf(" Format = %d\r\n", msg.format);
hudakz 0:1b9561cd1c36 65 pc.printf(" Length = %d\r\n", msg.len);
hudakz 0:1b9561cd1c36 66 pc.printf(" Data =");
hudakz 0:1b9561cd1c36 67 for(int i = 0; i < msg.len; i++)
hudakz 0:1b9561cd1c36 68 pc.printf(" 0x%.2X", msg.data[i]);
hudakz 0:1b9561cd1c36 69 pc.printf("\r\n");
hudakz 0:1b9561cd1c36 70 }
hudakz 0:1b9561cd1c36 71
hudakz 0:1b9561cd1c36 72 /**
hudakz 0:1b9561cd1c36 73 * @brief Main
hudakz 0:1b9561cd1c36 74 * @note
hudakz 0:1b9561cd1c36 75 * @param
hudakz 0:1b9561cd1c36 76 * @retval
hudakz 0:1b9561cd1c36 77 */
hudakz 0:1b9561cd1c36 78 int main(void)
hudakz 0:1b9561cd1c36 79 {
hudakz 0:1b9561cd1c36 80 #if defined(TARGET_STM32F103C8T6)
hudakz 1:6f8ffb2c2dd7 81 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
hudakz 0:1b9561cd1c36 82 #endif
hudakz 1:6f8ffb2c2dd7 83 pc.baud(9600); // set Serial speed
hudakz 1:6f8ffb2c2dd7 84 can.frequency(1000000); // set bit rate to 1Mbps
hudakz 0:1b9561cd1c36 85 #if defined(BOARD1)
hudakz 1:6f8ffb2c2dd7 86 led = ON; // turn the LED on
hudakz 1:6f8ffb2c2dd7 87 timer.start(); // start timer
hudakz 1:6f8ffb2c2dd7 88 pc.printf("CAN_Hello board #1\r\n");
hudakz 0:1b9561cd1c36 89 #else
hudakz 0:1b9561cd1c36 90 led = OFF; // turn LED off
hudakz 1:6f8ffb2c2dd7 91 pc.printf("CAN_Hello board #2\r\n");
hudakz 0:1b9561cd1c36 92 #endif
hudakz 0:1b9561cd1c36 93 while(1) {
hudakz 1:6f8ffb2c2dd7 94 if(timer.read_ms() >= 1000) { // check for timeout
hudakz 1:6f8ffb2c2dd7 95 timer.stop(); // stop timer
hudakz 1:6f8ffb2c2dd7 96 timer.reset(); // reset timer
hudakz 1:6f8ffb2c2dd7 97 counter++; // increment counter
hudakz 1:6f8ffb2c2dd7 98 voltage = (analogIn * 3.3f) / 4096.0f; // read the small drifting voltage from analog input
hudakz 1:6f8ffb2c2dd7 99 txMsg.clear(); // clear Tx message storage
hudakz 1:6f8ffb2c2dd7 100 txMsg.id = TX_ID; // set ID
szqt 3:36b034f320b6 101 txMsg << voltage << counter;// append data (total data length must be <= 8 bytes!)
hudakz 1:6f8ffb2c2dd7 102 if(can.write(txMsg)) { // transmit message
hudakz 1:6f8ffb2c2dd7 103 led = OFF; // turn the LED off
hudakz 0:1b9561cd1c36 104 pc.printf("-------------------------------------\r\n");
hudakz 0:1b9561cd1c36 105 pc.printf("CAN message sent\r\n");
hudakz 0:1b9561cd1c36 106 printMsg(txMsg);
hudakz 0:1b9561cd1c36 107 pc.printf(" counter = %d\r\n", counter);
hudakz 0:1b9561cd1c36 108 pc.printf(" voltage = %e V\r\n", voltage);
hudakz 0:1b9561cd1c36 109 }
hudakz 0:1b9561cd1c36 110 else
hudakz 0:1b9561cd1c36 111 pc.printf("Transmission error\r\n");
hudakz 0:1b9561cd1c36 112 }
hudakz 0:1b9561cd1c36 113
hudakz 0:1b9561cd1c36 114 if(can.read(rxMsg)) {
hudakz 1:6f8ffb2c2dd7 115 led = ON; // turn the LED on
hudakz 0:1b9561cd1c36 116 pc.printf("-------------------------------------\r\n");
hudakz 0:1b9561cd1c36 117 pc.printf("CAN message received\r\n");
hudakz 0:1b9561cd1c36 118 printMsg(rxMsg);
hudakz 0:1b9561cd1c36 119
hudakz 0:1b9561cd1c36 120 // Filtering performed by software:
hudakz 0:1b9561cd1c36 121 if(rxMsg.id == RX_ID) {
szqt 3:36b034f320b6 122 rxMsg >> voltage >> counter; // extract data from the received CAN message
hudakz 0:1b9561cd1c36 123 pc.printf(" counter = %d\r\n", counter);
hudakz 0:1b9561cd1c36 124 pc.printf(" voltage = %e V\r\n", voltage);
hudakz 1:6f8ffb2c2dd7 125 timer.start(); // transmission lag
hudakz 0:1b9561cd1c36 126 }
hudakz 0:1b9561cd1c36 127 }
hudakz 0:1b9561cd1c36 128 }
hudakz 0:1b9561cd1c36 129 }