Jean-Pierre Aulas
/
CanS-Homes
start adapt S-Homes structure
main.cpp@11:e837b1a581cb, 2020-09-06 (annotated)
- Committer:
- jeanpierreaulas
- Date:
- Sun Sep 06 19:36:44 2020 +0000
- Revision:
- 11:e837b1a581cb
- Parent:
- 10:eadd73cf3981
add CANJpa.h
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 | 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 | |
hudakz | 8:c65afde7f7f5 | 19 | //#define TARGET_STM32F103C8T6 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 | |
hudakz | 0:1b9561cd1c36 | 23 | #if defined(TARGET_STM32F103C8T6) |
hudakz | 7:883da97339ab | 24 | #define LED_PIN PC_13 |
hudakz | 7:883da97339ab | 25 | const int OFF = 1; |
hudakz | 7:883da97339ab | 26 | const int ON = 0; |
hudakz | 0:1b9561cd1c36 | 27 | #else |
hudakz | 7:883da97339ab | 28 | #define LED_PIN LED1 |
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 | const unsigned int RX_ID = 0x101; |
hudakz | 7:883da97339ab | 33 | const unsigned int TX_ID = 0x100; |
hudakz | 7:883da97339ab | 34 | |
hudakz | 0:1b9561cd1c36 | 35 | #include "mbed.h" |
hudakz | 0:1b9561cd1c36 | 36 | #include "CANMsg.h" |
jeanpierreaulas | 11:e837b1a581cb | 37 | #include "CANJpa.h" |
hudakz | 0:1b9561cd1c36 | 38 | |
jeanpierreaulas | 11:e837b1a581cb | 39 | //Serial pc(PA_2, PA_3); |
hudakz | 0:1b9561cd1c36 | 40 | |
jeanpierreaulas | 11:e837b1a581cb | 41 | void init_can(); |
jeanpierreaulas | 11:e837b1a581cb | 42 | void can_write(); |
hudakz | 5:37ab4112d547 | 43 | |
hudakz | 5:37ab4112d547 | 44 | |
hudakz | 5:37ab4112d547 | 45 | /** |
hudakz | 0:1b9561cd1c36 | 46 | * @brief Main |
hudakz | 0:1b9561cd1c36 | 47 | * @note |
hudakz | 0:1b9561cd1c36 | 48 | * @param |
hudakz | 0:1b9561cd1c36 | 49 | * @retval |
hudakz | 0:1b9561cd1c36 | 50 | */ |
hudakz | 0:1b9561cd1c36 | 51 | int main(void) |
hudakz | 0:1b9561cd1c36 | 52 | { |
hudakz | 8:c65afde7f7f5 | 53 | pc.baud(9600); // set serial speed |
jeanpierreaulas | 11:e837b1a581cb | 54 | init_can(); |
hudakz | 1:6f8ffb2c2dd7 | 55 | led = ON; // turn the LED on |
hudakz | 1:6f8ffb2c2dd7 | 56 | timer.start(); // start timer |
hudakz | 0:1b9561cd1c36 | 57 | while(1) { |
hudakz | 9:3211e88e30a5 | 58 | if(timer.read_ms() >= 2000) { // check for timeout |
hudakz | 3:87a128bca8f5 | 59 | timer.stop(); // stop timer |
hudakz | 3:87a128bca8f5 | 60 | timer.reset(); // reset timer |
jeanpierreaulas | 11:e837b1a581cb | 61 | can_write(); |
jeanpierreaulas | 11:e837b1a581cb | 62 | |
hudakz | 0:1b9561cd1c36 | 63 | } |
hudakz | 0:1b9561cd1c36 | 64 | } |
hudakz | 0:1b9561cd1c36 | 65 | } |