pub
Dependencies: CANnucleo mbed-dev
Fork of CANnucleo_Hello by
main.cpp@31:65c8d1465863, 2017-05-28 (annotated)
- Committer:
- hudakz
- Date:
- Sun May 28 09:24:55 2017 +0000
- Revision:
- 31:65c8d1465863
- Parent:
- 30:9ae558ec888c
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:c5e5d0df6f2a | 1 | /* |
hudakz | 17:a86f339d1c25 | 2 | * An example showing how to use the CANnucleo library: |
hudakz | 0:c5e5d0df6f2a | 3 | * |
hudakz | 21:eb1a8042605e | 4 | * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), |
hudakz | 21: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 | 17:a86f339d1c25 | 10 | * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/> |
hudakz | 6:7ff95ce72f6d | 11 | * |
hudakz | 22: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 | 19: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 | 23:f4682a5ddda6 | 22 | #define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 30:9ae558ec888c | 23 | //#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! |
hudakz | 0:c5e5d0df6f2a | 24 | |
hudakz | 17:a86f339d1c25 | 25 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 22:7120a0dcc8ee | 26 | #include "stm32f103c8t6.h" |
hudakz | 20: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 | 20: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 | 25:e2907bcba75e | 44 | #include "CANnucleo.h" |
hudakz | 17:a86f339d1c25 | 45 | #include "mbed.h" |
hudakz | 17:a86f339d1c25 | 46 | |
hudakz | 29:dde6c4aef759 | 47 | Serial pc(PA_2, PA_3); |
hudakz | 29:dde6c4aef759 | 48 | CAN* can; |
hudakz | 29:dde6c4aef759 | 49 | CANMessage rxMsg; |
hudakz | 29:dde6c4aef759 | 50 | CANMessage txMsg; |
hudakz | 29:dde6c4aef759 | 51 | DigitalOut led(LED_PIN); |
hudakz | 29:dde6c4aef759 | 52 | Timer timer; |
hudakz | 30:9ae558ec888c | 53 | uint8_t counter = 0; // one byte |
hudakz | 29:dde6c4aef759 | 54 | AnalogIn analogIn(A0); |
hudakz | 30:9ae558ec888c | 55 | float voltage; // four bytes |
hudakz | 29:dde6c4aef759 | 56 | volatile bool msgAvailable = false; |
hudakz | 0:c5e5d0df6f2a | 57 | |
hudakz | 17:a86f339d1c25 | 58 | /** |
hudakz | 30:9ae558ec888c | 59 | * @brief 'CAN receive-complete' interrup service routine. |
hudakz | 17:a86f339d1c25 | 60 | * @note Called on arrival of new CAN message. |
hudakz | 17:a86f339d1c25 | 61 | * Keep it as short as possible. |
hudakz | 17:a86f339d1c25 | 62 | * @param |
hudakz | 17:a86f339d1c25 | 63 | * @retval |
hudakz | 17:a86f339d1c25 | 64 | */ |
hudakz | 17:a86f339d1c25 | 65 | void onMsgReceived() { |
hudakz | 17:a86f339d1c25 | 66 | msgAvailable = true; |
hudakz | 17:a86f339d1c25 | 67 | } |
hudakz | 17:a86f339d1c25 | 68 | |
hudakz | 17:a86f339d1c25 | 69 | /** |
hudakz | 30:9ae558ec888c | 70 | * @brief Prints CAN msg to PC's serial terminal. |
hudakz | 26:1d0488a03905 | 71 | * @note} |
hudakz | 26:1d0488a03905 | 72 | * @param CANMessage to print |
hudakz | 26:1d0488a03905 | 73 | * @retval none |
hudakz | 26:1d0488a03905 | 74 | */ |
hudakz | 29:dde6c4aef759 | 75 | void printMsg(CANMessage& msg) { |
hudakz | 29:dde6c4aef759 | 76 | pc.printf(" ID = 0x%.3x\r\n", msg.id); |
hudakz | 29:dde6c4aef759 | 77 | pc.printf(" Type = %d\r\n", msg.type); |
hudakz | 29:dde6c4aef759 | 78 | pc.printf(" Format = %d\r\n", msg.format); |
hudakz | 29:dde6c4aef759 | 79 | pc.printf(" Length = %d\r\n", msg.len); |
hudakz | 29:dde6c4aef759 | 80 | pc.printf(" Data ="); |
hudakz | 26:1d0488a03905 | 81 | for(int i = 0; i < msg.len; i++) |
hudakz | 29:dde6c4aef759 | 82 | pc.printf(" 0x%.2X", msg.data[i]); |
hudakz | 29:dde6c4aef759 | 83 | pc.printf("\r\n"); |
hudakz | 26:1d0488a03905 | 84 | } |
hudakz | 26:1d0488a03905 | 85 | |
hudakz | 26:1d0488a03905 | 86 | /** |
hudakz | 17:a86f339d1c25 | 87 | * @brief Main |
hudakz | 17:a86f339d1c25 | 88 | * @note |
hudakz | 17:a86f339d1c25 | 89 | * @param |
hudakz | 17:a86f339d1c25 | 90 | * @retval |
hudakz | 17:a86f339d1c25 | 91 | */ |
hudakz | 17:a86f339d1c25 | 92 | int main() { |
hudakz | 22:7120a0dcc8ee | 93 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 30:9ae558ec888c | 94 | confSysClock(); //Configure the system clock (72MHz HSE clock, 48MHz USB clock) |
hudakz | 22:7120a0dcc8ee | 95 | #endif |
hudakz | 30:9ae558ec888c | 96 | pc.baud(9600); // set the Serial speed |
hudakz | 29:dde6c4aef759 | 97 | |
hudakz | 29:dde6c4aef759 | 98 | can = new CAN(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 30:9ae558ec888c | 99 | can->frequency(1000000); // set the bit rate to 1Mbps |
hudakz | 30:9ae558ec888c | 100 | can->attach(&onMsgReceived); // attach the 'CAN receive-complete' interrupt service routine (ISR) |
hudakz | 17:a86f339d1c25 | 101 | |
hudakz | 0:c5e5d0df6f2a | 102 | #if defined(BOARD1) |
hudakz | 30:9ae558ec888c | 103 | led = ON; // turn the LED on |
hudakz | 30:9ae558ec888c | 104 | timer.start(); // start the timer |
hudakz | 29:dde6c4aef759 | 105 | pc.printf("CANnucleo_Hello board #1\r\n"); |
hudakz | 0:c5e5d0df6f2a | 106 | #else |
hudakz | 10:66da8731bdb6 | 107 | led = OFF; // turn LED off |
hudakz | 29:dde6c4aef759 | 108 | pc.printf("CANnucleo_Hello board #2\r\n"); |
hudakz | 0:c5e5d0df6f2a | 109 | #endif |
hudakz | 0:c5e5d0df6f2a | 110 | |
hudakz | 0:c5e5d0df6f2a | 111 | while(1) { |
hudakz | 24:069287e799cd | 112 | if(timer.read_ms() >= 1000) { // check for timeout |
hudakz | 30:9ae558ec888c | 113 | timer.stop(); // stop the timer |
hudakz | 30:9ae558ec888c | 114 | timer.reset(); // reset the timer |
hudakz | 30:9ae558ec888c | 115 | counter++; // increment the counter |
hudakz | 29:dde6c4aef759 | 116 | voltage = (analogIn * 3.3f)/4096.0f;// read the small drifting voltage from analog input |
hudakz | 30:9ae558ec888c | 117 | txMsg.clear(); // clear the Tx message storage |
hudakz | 30:9ae558ec888c | 118 | txMsg.id = TX_ID; // set the message ID |
hudakz | 30:9ae558ec888c | 119 | // We are about to transmit two data items to the CAN bus. |
hudakz | 30:9ae558ec888c | 120 | // counter: uint_8 (unsigned eight bits int) value (one byte). |
hudakz | 30:9ae558ec888c | 121 | // voltage: floating point value (four bytes). |
hudakz | 30:9ae558ec888c | 122 | // So the total length of payload data is five bytes. |
hudakz | 30:9ae558ec888c | 123 | // We'll use the "<<" (append) operator to add data to the CAN message. |
hudakz | 30:9ae558ec888c | 124 | // The usage is same as of the similar C++ io-stream operators. |
hudakz | 30:9ae558ec888c | 125 | // NOTE: The data length of CAN message is automatically updated when using "<<" operators. |
hudakz | 26:1d0488a03905 | 126 | txMsg << counter << voltage; // append data (total data length must be <= 8 bytes!) |
hudakz | 30:9ae558ec888c | 127 | if(can->write(txMsg)) { // transmit the CAN message |
hudakz | 30:9ae558ec888c | 128 | led = OFF; // turn the LED off |
hudakz | 29:dde6c4aef759 | 129 | pc.printf("-------------------------------------\r\n"); |
hudakz | 29:dde6c4aef759 | 130 | pc.printf("CAN message sent\r\n"); |
hudakz | 26:1d0488a03905 | 131 | printMsg(txMsg); |
hudakz | 29:dde6c4aef759 | 132 | pc.printf(" counter = %d\r\n", counter); |
hudakz | 29:dde6c4aef759 | 133 | pc.printf(" voltage = %e V\r\n", voltage); |
hudakz | 26:1d0488a03905 | 134 | } |
hudakz | 10:66da8731bdb6 | 135 | else |
hudakz | 29:dde6c4aef759 | 136 | pc.printf("Transmission error\r\n"); |
hudakz | 0:c5e5d0df6f2a | 137 | } |
hudakz | 17:a86f339d1c25 | 138 | if(msgAvailable) { |
hudakz | 30:9ae558ec888c | 139 | msgAvailable = false; // reset the flag for next use in the interrupt service routine |
hudakz | 30:9ae558ec888c | 140 | can->read(rxMsg); // read the message into the Rx message storage |
hudakz | 30:9ae558ec888c | 141 | led = ON; // turn the LED on |
hudakz | 29:dde6c4aef759 | 142 | pc.printf("-------------------------------------\r\n"); |
hudakz | 29:dde6c4aef759 | 143 | pc.printf("CAN message received\r\n"); |
hudakz | 26:1d0488a03905 | 144 | printMsg(rxMsg); |
hudakz | 17:a86f339d1c25 | 145 | // Filtering performed by software: |
hudakz | 30:9ae558ec888c | 146 | if(rxMsg.id == RX_ID) { // about filtering performed by hardware see the comments in CANnucleo.cpp |
hudakz | 30:9ae558ec888c | 147 | rxMsg >> counter >> voltage; // extract data from the received CAN message (in same sequence as they were added) |
hudakz | 29:dde6c4aef759 | 148 | pc.printf(" counter = %d\r\n", counter); |
hudakz | 29:dde6c4aef759 | 149 | pc.printf(" voltage = %e V\r\n", voltage); |
hudakz | 30:9ae558ec888c | 150 | timer.start(); // insert transmission lag |
hudakz | 0:c5e5d0df6f2a | 151 | } |
hudakz | 0:c5e5d0df6f2a | 152 | } |
hudakz | 0:c5e5d0df6f2a | 153 | } |
hudakz | 0:c5e5d0df6f2a | 154 | } |
hudakz | 25:e2907bcba75e | 155 | |
hudakz | 25:e2907bcba75e | 156 | |
hudakz | 25:e2907bcba75e | 157 |