
Can Stable Einstein Version
Dependencies: mbed-STM32F103C8T6 CANMsg
main.cpp@5:3a5281126310, 2018-12-15 (annotated)
- Committer:
- sid26
- Date:
- Sat Dec 15 19:06:43 2018 +0000
- Revision:
- 5:3a5281126310
- Parent:
- 4:09d564da0e24
123
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:6f8ffb2c2dd7 | 10 | * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> |
hudakz | 0:1b9561cd1c36 | 11 | * |
hudakz | 4:09d564da0e24 | 12 | * NOTE: When using an STM32F103C8T6 board uncomment line 22 and import the mbed-STM32F103C8T6 library |
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 | 3:87a128bca8f5 | 16 | * For board #2 comment out line 21 before compiling |
hudakz | 0:1b9561cd1c36 | 17 | * |
hudakz | 3:87a128bca8f5 | 18 | * Once the binaries have been downloaded to the boards reset both boards at the same time. |
hudakz | 0:1b9561cd1c36 | 19 | * |
hudakz | 0:1b9561cd1c36 | 20 | */ |
hudakz | 1:6f8ffb2c2dd7 | 21 | |
sid26 | 5:3a5281126310 | 22 | #define TARGET_STM32F103C8T6 1 // uncomment this line and import the mbed-STM32F103C8T6 library when using STM32F103C8T6 boards! |
hudakz | 4:09d564da0e24 | 23 | |
sid26 | 5:3a5281126310 | 24 | //#define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 0:1b9561cd1c36 | 25 | |
hudakz | 0:1b9561cd1c36 | 26 | #if defined(TARGET_STM32F103C8T6) |
sid26 | 5:3a5281126310 | 27 | //#include "stm32f103c8t6.h" |
hudakz | 0:1b9561cd1c36 | 28 | #define LED_PIN PC_13 |
hudakz | 0:1b9561cd1c36 | 29 | const int OFF = 1; |
hudakz | 0:1b9561cd1c36 | 30 | const int ON = 0; |
hudakz | 0:1b9561cd1c36 | 31 | #else |
hudakz | 0:1b9561cd1c36 | 32 | #define LED_PIN LED1 |
hudakz | 0:1b9561cd1c36 | 33 | const int OFF = 0; |
hudakz | 0:1b9561cd1c36 | 34 | const int ON = 1; |
hudakz | 0:1b9561cd1c36 | 35 | #endif |
hudakz | 0:1b9561cd1c36 | 36 | #if defined(BOARD1) |
hudakz | 0:1b9561cd1c36 | 37 | const unsigned int RX_ID = 0x100; |
hudakz | 0:1b9561cd1c36 | 38 | const unsigned int TX_ID = 0x101; |
hudakz | 0:1b9561cd1c36 | 39 | #else |
hudakz | 0:1b9561cd1c36 | 40 | const unsigned int RX_ID = 0x101; |
hudakz | 0:1b9561cd1c36 | 41 | const unsigned int TX_ID = 0x100; |
hudakz | 0:1b9561cd1c36 | 42 | #endif |
hudakz | 0:1b9561cd1c36 | 43 | #include "mbed.h" |
hudakz | 0:1b9561cd1c36 | 44 | #include "CANMsg.h" |
hudakz | 0:1b9561cd1c36 | 45 | |
sid26 | 5:3a5281126310 | 46 | #define CAN_RF0R (*((volatile unsigned long *)0x4000640C)) |
sid26 | 5:3a5281126310 | 47 | #define CAN_RF1R (*((volatile unsigned long *)0x40006410)) |
sid26 | 5:3a5281126310 | 48 | #define CAN_IER (*((volatile unsigned long *)0x40006414)) |
sid26 | 5:3a5281126310 | 49 | |
sid26 | 5:3a5281126310 | 50 | |
sid26 | 5:3a5281126310 | 51 | Serial pc(PA_9, PA_10); |
sid26 | 5:3a5281126310 | 52 | CAN can2(PA_11, PA_12); |
sid26 | 5:3a5281126310 | 53 | CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name |
hudakz | 0:1b9561cd1c36 | 54 | CANMsg rxMsg; |
hudakz | 0:1b9561cd1c36 | 55 | CANMsg txMsg; |
hudakz | 0:1b9561cd1c36 | 56 | DigitalOut led(LED_PIN); |
hudakz | 0:1b9561cd1c36 | 57 | Timer timer; |
hudakz | 0:1b9561cd1c36 | 58 | uint8_t counter = 0; |
hudakz | 0:1b9561cd1c36 | 59 | AnalogIn analogIn(A0); |
hudakz | 0:1b9561cd1c36 | 60 | float voltage; |
sid26 | 5:3a5281126310 | 61 | volatile bool got_message = false; |
hudakz | 0:1b9561cd1c36 | 62 | /** |
hudakz | 0:1b9561cd1c36 | 63 | * @brief Prints CAN msg to PC's serial terminal |
hudakz | 2:6546e4a2d593 | 64 | * @note |
hudakz | 1:6f8ffb2c2dd7 | 65 | * @param CANMessage to print |
hudakz | 3:87a128bca8f5 | 66 | * @retval |
hudakz | 0:1b9561cd1c36 | 67 | */ |
hudakz | 1:6f8ffb2c2dd7 | 68 | void printMsg(CANMessage& msg) { |
hudakz | 0:1b9561cd1c36 | 69 | pc.printf(" ID = 0x%.3x\r\n", msg.id); |
hudakz | 0:1b9561cd1c36 | 70 | pc.printf(" Type = %d\r\n", msg.type); |
hudakz | 0:1b9561cd1c36 | 71 | pc.printf(" Format = %d\r\n", msg.format); |
hudakz | 0:1b9561cd1c36 | 72 | pc.printf(" Length = %d\r\n", msg.len); |
hudakz | 0:1b9561cd1c36 | 73 | pc.printf(" Data ="); |
hudakz | 0:1b9561cd1c36 | 74 | for(int i = 0; i < msg.len; i++) |
hudakz | 0:1b9561cd1c36 | 75 | pc.printf(" 0x%.2X", msg.data[i]); |
hudakz | 0:1b9561cd1c36 | 76 | pc.printf("\r\n"); |
hudakz | 0:1b9561cd1c36 | 77 | } |
hudakz | 0:1b9561cd1c36 | 78 | |
sid26 | 5:3a5281126310 | 79 | void RxIsr(){ |
sid26 | 5:3a5281126310 | 80 | CANMsg temp; |
sid26 | 5:3a5281126310 | 81 | led = ON; |
sid26 | 5:3a5281126310 | 82 | can.read(temp); |
sid26 | 5:3a5281126310 | 83 | got_message = true; |
sid26 | 5:3a5281126310 | 84 | return; |
sid26 | 5:3a5281126310 | 85 | } |
sid26 | 5:3a5281126310 | 86 | |
hudakz | 0:1b9561cd1c36 | 87 | /** |
hudakz | 0:1b9561cd1c36 | 88 | * @brief Main |
hudakz | 0:1b9561cd1c36 | 89 | * @note |
hudakz | 0:1b9561cd1c36 | 90 | * @param |
hudakz | 0:1b9561cd1c36 | 91 | * @retval |
hudakz | 0:1b9561cd1c36 | 92 | */ |
hudakz | 0:1b9561cd1c36 | 93 | int main(void) |
hudakz | 0:1b9561cd1c36 | 94 | { |
hudakz | 0:1b9561cd1c36 | 95 | #if defined(TARGET_STM32F103C8T6) |
sid26 | 5:3a5281126310 | 96 | // confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
hudakz | 0:1b9561cd1c36 | 97 | #endif |
hudakz | 1:6f8ffb2c2dd7 | 98 | pc.baud(9600); // set Serial speed |
sid26 | 5:3a5281126310 | 99 | can.frequency(5000000); // set bit rate to 1Mbps |
sid26 | 5:3a5281126310 | 100 | can.mode(CAN::Normal); |
sid26 | 5:3a5281126310 | 101 | can.attach(&RxIsr, CAN::RxIrq); |
sid26 | 5:3a5281126310 | 102 | #if defined(BOARD1) |
hudakz | 1:6f8ffb2c2dd7 | 103 | led = ON; // turn the LED on |
sid26 | 5:3a5281126310 | 104 | timer.start(); // start timert |
hudakz | 1:6f8ffb2c2dd7 | 105 | pc.printf("CAN_Hello board #1\r\n"); |
hudakz | 0:1b9561cd1c36 | 106 | #else |
hudakz | 0:1b9561cd1c36 | 107 | led = OFF; // turn LED off |
hudakz | 1:6f8ffb2c2dd7 | 108 | pc.printf("CAN_Hello board #2\r\n"); |
hudakz | 0:1b9561cd1c36 | 109 | #endif |
sid26 | 5:3a5281126310 | 110 | |
sid26 | 5:3a5281126310 | 111 | while(1) { |
sid26 | 5:3a5281126310 | 112 | pc.printf("%d\n",got_message); |
hudakz | 3:87a128bca8f5 | 113 | if(timer.read_ms() >= 1000) { // check for timeout |
hudakz | 3:87a128bca8f5 | 114 | timer.stop(); // stop timer |
hudakz | 3:87a128bca8f5 | 115 | timer.reset(); // reset timer |
hudakz | 3:87a128bca8f5 | 116 | counter++; // increment counter |
hudakz | 3:87a128bca8f5 | 117 | voltage = analogIn * 3.3f; // read the small drifting voltage from analog input |
hudakz | 3:87a128bca8f5 | 118 | txMsg.clear(); // clear Tx message storage |
hudakz | 3:87a128bca8f5 | 119 | txMsg.id = TX_ID; // set ID |
hudakz | 3:87a128bca8f5 | 120 | txMsg << counter << voltage; // append data (total data length must not exceed 8 bytes!) |
hudakz | 3:87a128bca8f5 | 121 | if(can.write(txMsg)) { // transmit message |
hudakz | 3:87a128bca8f5 | 122 | led = OFF; // turn the LED off |
hudakz | 0:1b9561cd1c36 | 123 | pc.printf("-------------------------------------\r\n"); |
hudakz | 0:1b9561cd1c36 | 124 | pc.printf("CAN message sent\r\n"); |
hudakz | 0:1b9561cd1c36 | 125 | printMsg(txMsg); |
hudakz | 0:1b9561cd1c36 | 126 | pc.printf(" counter = %d\r\n", counter); |
hudakz | 0:1b9561cd1c36 | 127 | pc.printf(" voltage = %e V\r\n", voltage); |
hudakz | 0:1b9561cd1c36 | 128 | } |
hudakz | 0:1b9561cd1c36 | 129 | else |
hudakz | 0:1b9561cd1c36 | 130 | pc.printf("Transmission error\r\n"); |
hudakz | 0:1b9561cd1c36 | 131 | } |
sid26 | 5:3a5281126310 | 132 | |
sid26 | 5:3a5281126310 | 133 | if(got_message){ |
sid26 | 5:3a5281126310 | 134 | got_message = false; |
sid26 | 5:3a5281126310 | 135 | can.read(rxMsg); |
hudakz | 0:1b9561cd1c36 | 136 | pc.printf("-------------------------------------\r\n"); |
hudakz | 0:1b9561cd1c36 | 137 | pc.printf("CAN message received\r\n"); |
hudakz | 0:1b9561cd1c36 | 138 | printMsg(rxMsg); |
hudakz | 0:1b9561cd1c36 | 139 | |
hudakz | 0:1b9561cd1c36 | 140 | // Filtering performed by software: |
sid26 | 5:3a5281126310 | 141 | //if(rxMsg.id == RX_ID) { |
hudakz | 0:1b9561cd1c36 | 142 | rxMsg >> counter >> voltage; // extract data from the received CAN message |
hudakz | 0:1b9561cd1c36 | 143 | pc.printf(" counter = %d\r\n", counter); |
hudakz | 0:1b9561cd1c36 | 144 | pc.printf(" voltage = %e V\r\n", voltage); |
hudakz | 1:6f8ffb2c2dd7 | 145 | timer.start(); // transmission lag |
sid26 | 5:3a5281126310 | 146 | // } |
hudakz | 0:1b9561cd1c36 | 147 | } |
sid26 | 5:3a5281126310 | 148 | |
hudakz | 0:1b9561cd1c36 | 149 | } |
hudakz | 0:1b9561cd1c36 | 150 | } |