![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
all bms comunicating with nucleo board and balancing
Fork of Can_sniffer_BMS_GER by
main.cpp@18:22977a898fe9, 2016-08-11 (annotated)
- Committer:
- hudakz
- Date:
- Thu Aug 11 12:07:34 2016 +0000
- Revision:
- 18:22977a898fe9
- Parent:
- 17:18d4d0ff26a6
- Child:
- 19:872e304d7e17
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:c5e5d0df6f2a | 1 | /* |
hudakz | 16:a86f339d1c25 | 2 | * An example showing how to use the CANnucleo library: |
hudakz | 0:c5e5d0df6f2a | 3 | * |
hudakz | 16:a86f339d1c25 | 4 | * Two affordable (less than $4 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), |
hudakz | 16:a86f339d1c25 | 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 | 16:a86f339d1c25 | 10 | * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/> |
hudakz | 6:7ff95ce72f6d | 11 | * |
hudakz | 17:18d4d0ff26a6 | 12 | * NOTE: If you'd like to use the official NUCLEO boards comment out line 22 |
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 | 18: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 | 17:18d4d0ff26a6 | 22 | #define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards! |
hudakz | 10:66da8731bdb6 | 23 | #define BOARD1 1 // comment out this line when compiling for board #2 |
hudakz | 0:c5e5d0df6f2a | 24 | |
hudakz | 16:a86f339d1c25 | 25 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 16:a86f339d1c25 | 26 | #include "stm32f103c8t6.h" |
hudakz | 10:66da8731bdb6 | 27 | const int OFF = 1; |
hudakz | 10:66da8731bdb6 | 28 | const int ON = 0; |
hudakz | 0:c5e5d0df6f2a | 29 | #else |
hudakz | 10:66da8731bdb6 | 30 | const int OFF = 0; |
hudakz | 10:66da8731bdb6 | 31 | const int ON = 1; |
hudakz | 0:c5e5d0df6f2a | 32 | #endif |
hudakz | 0:c5e5d0df6f2a | 33 | |
hudakz | 10:66da8731bdb6 | 34 | #if defined(BOARD1) |
hudakz | 10:66da8731bdb6 | 35 | const unsigned int RX_ID = 0x100; |
hudakz | 10:66da8731bdb6 | 36 | const unsigned int TX_ID = 0x101; |
hudakz | 6:7ff95ce72f6d | 37 | #else |
hudakz | 10:66da8731bdb6 | 38 | const unsigned int RX_ID = 0x101; |
hudakz | 10:66da8731bdb6 | 39 | const unsigned int TX_ID = 0x100; |
hudakz | 6:7ff95ce72f6d | 40 | #endif |
hudakz | 6:7ff95ce72f6d | 41 | |
hudakz | 16:a86f339d1c25 | 42 | #include "mbed.h" |
hudakz | 16:a86f339d1c25 | 43 | #include "CANnucleo.h" |
hudakz | 16:a86f339d1c25 | 44 | |
hudakz | 17:18d4d0ff26a6 | 45 | /* |
hudakz | 17:18d4d0ff26a6 | 46 | * To avaoid name collision with the CAN and CANMessage classes built into the mbed library |
hudakz | 17:18d4d0ff26a6 | 47 | * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace. |
hudakz | 17:18d4d0ff26a6 | 48 | * So remember to qualify them with the CANnucleo namespace. |
hudakz | 17:18d4d0ff26a6 | 49 | */ |
hudakz | 17:18d4d0ff26a6 | 50 | CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 17:18d4d0ff26a6 | 51 | CANnucleo::CANMessage rxMsg; |
hudakz | 17:18d4d0ff26a6 | 52 | CANnucleo::CANMessage txMsg; |
hudakz | 18:22977a898fe9 | 53 | DigitalOut led(LED1); |
hudakz | 18:22977a898fe9 | 54 | int ledState; |
hudakz | 18:22977a898fe9 | 55 | Timer timer; |
hudakz | 17:18d4d0ff26a6 | 56 | int counter = 0; |
hudakz | 17:18d4d0ff26a6 | 57 | volatile bool msgAvailable = false; |
hudakz | 0:c5e5d0df6f2a | 58 | |
hudakz | 16:a86f339d1c25 | 59 | /** |
hudakz | 16:a86f339d1c25 | 60 | * @brief 'CAN receive-complete' interrup handler. |
hudakz | 16:a86f339d1c25 | 61 | * @note Called on arrival of new CAN message. |
hudakz | 16:a86f339d1c25 | 62 | * Keep it as short as possible. |
hudakz | 16:a86f339d1c25 | 63 | * @param |
hudakz | 16:a86f339d1c25 | 64 | * @retval |
hudakz | 16:a86f339d1c25 | 65 | */ |
hudakz | 16:a86f339d1c25 | 66 | void onMsgReceived() { |
hudakz | 16:a86f339d1c25 | 67 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 68 | } |
hudakz | 16:a86f339d1c25 | 69 | |
hudakz | 16:a86f339d1c25 | 70 | /** |
hudakz | 16:a86f339d1c25 | 71 | * @brief Main |
hudakz | 16:a86f339d1c25 | 72 | * @note |
hudakz | 16:a86f339d1c25 | 73 | * @param |
hudakz | 16:a86f339d1c25 | 74 | * @retval |
hudakz | 16:a86f339d1c25 | 75 | */ |
hudakz | 16:a86f339d1c25 | 76 | int main() { |
hudakz | 16:a86f339d1c25 | 77 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 16:a86f339d1c25 | 78 | confSysClock(); |
hudakz | 16:a86f339d1c25 | 79 | #endif |
hudakz | 16:a86f339d1c25 | 80 | can.frequency(1000000); // set bit rate to 1Mbps |
hudakz | 16:a86f339d1c25 | 81 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
hudakz | 16:a86f339d1c25 | 82 | |
hudakz | 0:c5e5d0df6f2a | 83 | #if defined(BOARD1) |
hudakz | 10:66da8731bdb6 | 84 | led = ON; // turn LED on |
hudakz | 10:66da8731bdb6 | 85 | timer.start(); // start timer |
hudakz | 16:a86f339d1c25 | 86 | printf("CANnucleo_Hello board #1\r\n"); |
hudakz | 0:c5e5d0df6f2a | 87 | #else |
hudakz | 10:66da8731bdb6 | 88 | led = OFF; // turn LED off |
hudakz | 16:a86f339d1c25 | 89 | printf("CANnucleo_Hello board #2\r\n"); |
hudakz | 0:c5e5d0df6f2a | 90 | #endif |
hudakz | 0:c5e5d0df6f2a | 91 | |
hudakz | 0:c5e5d0df6f2a | 92 | while(1) { |
hudakz | 16:a86f339d1c25 | 93 | if(timer.read() >= 1.0) { // check for timeout |
hudakz | 16:a86f339d1c25 | 94 | timer.stop(); // stop timer |
hudakz | 16:a86f339d1c25 | 95 | timer.reset(); // reset timer |
hudakz | 16:a86f339d1c25 | 96 | counter++; // increment counter |
hudakz | 16:a86f339d1c25 | 97 | ledState = led.read(); // get led state |
hudakz | 16:a86f339d1c25 | 98 | txMsg.clear(); // clear Tx message storage |
hudakz | 16:a86f339d1c25 | 99 | txMsg.id = TX_ID; // set ID |
hudakz | 16:a86f339d1c25 | 100 | txMsg << counter; // append first data item |
hudakz | 16:a86f339d1c25 | 101 | txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) |
hudakz | 16:a86f339d1c25 | 102 | led = OFF; // turn LED off |
hudakz | 16:a86f339d1c25 | 103 | if(can.write(txMsg)) // transmit message |
hudakz | 16:a86f339d1c25 | 104 | printf("CAN message sent\r\n"); |
hudakz | 10:66da8731bdb6 | 105 | else |
hudakz | 16:a86f339d1c25 | 106 | printf("Transmission error\r\n"); |
hudakz | 0:c5e5d0df6f2a | 107 | } |
hudakz | 16:a86f339d1c25 | 108 | if(msgAvailable) { |
hudakz | 16:a86f339d1c25 | 109 | msgAvailable = false; // reset flag for next use |
hudakz | 16:a86f339d1c25 | 110 | can.read(rxMsg); // read message into Rx message storage |
hudakz | 16:a86f339d1c25 | 111 | printf("CAN message received:\r\n"); |
hudakz | 15:6449443e2207 | 112 | printf(" ID = 0x%.3x\r\n", rxMsg.id); |
hudakz | 2:49c9430860d1 | 113 | printf(" Type = %d\r\n", rxMsg.type); |
hudakz | 2:49c9430860d1 | 114 | printf(" Format = %d\r\n", rxMsg.format); |
hudakz | 2:49c9430860d1 | 115 | printf(" Length = %d\r\n", rxMsg.len); |
hudakz | 2:49c9430860d1 | 116 | printf(" Data ="); |
hudakz | 2:49c9430860d1 | 117 | for(int i = 0; i < rxMsg.len; i++) |
hudakz | 14:e12ebd1260b1 | 118 | printf(" 0x%.2x", rxMsg.data[i]); |
hudakz | 10:66da8731bdb6 | 119 | printf("\r\n"); |
hudakz | 16:a86f339d1c25 | 120 | // Filtering performed by software: |
hudakz | 16:a86f339d1c25 | 121 | if(rxMsg.id == RX_ID) { // See comments in CAN.cpp for filtering performed by hardware |
hudakz | 16:a86f339d1c25 | 122 | rxMsg >> counter; // extract first data item |
hudakz | 16:a86f339d1c25 | 123 | rxMsg >> ledState; // extract second data item |
hudakz | 16:a86f339d1c25 | 124 | printf(" counter = %d\r\n", counter); |
hudakz | 16:a86f339d1c25 | 125 | led = ON; // turn LED on |
hudakz | 16:a86f339d1c25 | 126 | timer.start(); // transmission lag |
hudakz | 0:c5e5d0df6f2a | 127 | } |
hudakz | 0:c5e5d0df6f2a | 128 | } |
hudakz | 0:c5e5d0df6f2a | 129 | } |
hudakz | 0:c5e5d0df6f2a | 130 | } |
hudakz | 7:2dce8ed51091 | 131 | |
hudakz | 12:e91e44924194 | 132 | |
hudakz | 17:18d4d0ff26a6 | 133 |