Jean-Pierre Aulas
/
CanS-Homes
start adapt S-Homes structure
CANJpa/CANJpa.h@11:e837b1a581cb, 2020-09-06 (annotated)
- Committer:
- jeanpierreaulas
- Date:
- Sun Sep 06 19:36:44 2020 +0000
- Revision:
- 11:e837b1a581cb
add CANJpa.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jeanpierreaulas | 11:e837b1a581cb | 1 | #ifndef CANJpa_H |
jeanpierreaulas | 11:e837b1a581cb | 2 | #define CANJpa_H |
jeanpierreaulas | 11:e837b1a581cb | 3 | |
jeanpierreaulas | 11:e837b1a581cb | 4 | extern Serial pc(PA_2, PA_3); |
jeanpierreaulas | 11:e837b1a581cb | 5 | extern CAN can(PA_2, PA_3); // CAN Rx pin name, CAN Tx pin name |
jeanpierreaulas | 11:e837b1a581cb | 6 | //CAN can(p30, p29); // CAN Rx pin name, CAN Tx pin name |
jeanpierreaulas | 11:e837b1a581cb | 7 | CANMsg rxMsg; |
jeanpierreaulas | 11:e837b1a581cb | 8 | CANMsg txMsg; |
jeanpierreaulas | 11:e837b1a581cb | 9 | |
jeanpierreaulas | 11:e837b1a581cb | 10 | uint8_t counter = 0; |
jeanpierreaulas | 11:e837b1a581cb | 11 | AnalogIn analogIn(A0); |
jeanpierreaulas | 11:e837b1a581cb | 12 | float voltage; |
jeanpierreaulas | 11:e837b1a581cb | 13 | Timer timer; |
jeanpierreaulas | 11:e837b1a581cb | 14 | DigitalOut led(LED_PIN); |
jeanpierreaulas | 11:e837b1a581cb | 15 | |
jeanpierreaulas | 11:e837b1a581cb | 16 | /** |
jeanpierreaulas | 11:e837b1a581cb | 17 | * @brief Prints CAN message to PC's serial terminal |
jeanpierreaulas | 11:e837b1a581cb | 18 | * @note |
jeanpierreaulas | 11:e837b1a581cb | 19 | * @param CANMessage to print |
jeanpierreaulas | 11:e837b1a581cb | 20 | * @retval |
jeanpierreaulas | 11:e837b1a581cb | 21 | */ |
jeanpierreaulas | 11:e837b1a581cb | 22 | void printMsgCan(CANMessage& msg) |
jeanpierreaulas | 11:e837b1a581cb | 23 | { |
jeanpierreaulas | 11:e837b1a581cb | 24 | pc.printf(" ID = 0x%.3x\r\n", msg.id); |
jeanpierreaulas | 11:e837b1a581cb | 25 | pc.printf(" Type = %d\r\n", msg.type); |
jeanpierreaulas | 11:e837b1a581cb | 26 | pc.printf(" Format = %d\r\n", msg.format); |
jeanpierreaulas | 11:e837b1a581cb | 27 | pc.printf(" Length = %d\r\n", msg.len); |
jeanpierreaulas | 11:e837b1a581cb | 28 | pc.printf(" Data ="); |
jeanpierreaulas | 11:e837b1a581cb | 29 | for(int i = 0; i < msg.len; i++) |
jeanpierreaulas | 11:e837b1a581cb | 30 | pc.printf(" 0x%.2X", msg.data[i]); |
jeanpierreaulas | 11:e837b1a581cb | 31 | pc.printf("\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 32 | } |
jeanpierreaulas | 11:e837b1a581cb | 33 | |
jeanpierreaulas | 11:e837b1a581cb | 34 | |
jeanpierreaulas | 11:e837b1a581cb | 35 | /** |
jeanpierreaulas | 11:e837b1a581cb | 36 | * @brief Handles received CAN messages |
jeanpierreaulas | 11:e837b1a581cb | 37 | * @note Called on 'CAN message received' interrupt. |
jeanpierreaulas | 11:e837b1a581cb | 38 | * @param |
jeanpierreaulas | 11:e837b1a581cb | 39 | * @retval |
jeanpierreaulas | 11:e837b1a581cb | 40 | */ |
jeanpierreaulas | 11:e837b1a581cb | 41 | void onCanReceived(void) |
jeanpierreaulas | 11:e837b1a581cb | 42 | { |
jeanpierreaulas | 11:e837b1a581cb | 43 | can.read(rxMsg); |
jeanpierreaulas | 11:e837b1a581cb | 44 | pc.printf("-------------------------------------\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 45 | pc.printf("CAN message received\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 46 | printMsgCan(rxMsg); |
jeanpierreaulas | 11:e837b1a581cb | 47 | |
jeanpierreaulas | 11:e837b1a581cb | 48 | if (rxMsg.id == RX_ID) { |
jeanpierreaulas | 11:e837b1a581cb | 49 | // extract data from the received CAN message |
jeanpierreaulas | 11:e837b1a581cb | 50 | // in the same order as it was added on the transmitter side |
jeanpierreaulas | 11:e837b1a581cb | 51 | rxMsg >> counter; |
jeanpierreaulas | 11:e837b1a581cb | 52 | rxMsg >> voltage; |
jeanpierreaulas | 11:e837b1a581cb | 53 | pc.printf(" counter = %d\r\n", counter); |
jeanpierreaulas | 11:e837b1a581cb | 54 | pc.printf(" voltage = %e V\r\n", voltage); |
jeanpierreaulas | 11:e837b1a581cb | 55 | } |
jeanpierreaulas | 11:e837b1a581cb | 56 | timer.start(); // to transmit next message in main |
jeanpierreaulas | 11:e837b1a581cb | 57 | } |
jeanpierreaulas | 11:e837b1a581cb | 58 | |
jeanpierreaulas | 11:e837b1a581cb | 59 | void can_write() |
jeanpierreaulas | 11:e837b1a581cb | 60 | { |
jeanpierreaulas | 11:e837b1a581cb | 61 | counter++; // increment counter |
jeanpierreaulas | 11:e837b1a581cb | 62 | voltage = analogIn * 3.3f; // read the small drift voltage from analog input |
jeanpierreaulas | 11:e837b1a581cb | 63 | txMsg.clear(); // clear Tx message storage |
jeanpierreaulas | 11:e837b1a581cb | 64 | txMsg.id = TX_ID; // set ID |
jeanpierreaulas | 11:e837b1a581cb | 65 | // append data (total data length must not exceed 8 bytes!) |
jeanpierreaulas | 11:e837b1a581cb | 66 | txMsg << counter; // one byte |
jeanpierreaulas | 11:e837b1a581cb | 67 | txMsg << voltage; // four bytes |
jeanpierreaulas | 11:e837b1a581cb | 68 | |
jeanpierreaulas | 11:e837b1a581cb | 69 | if(can.write(txMsg)) { // transmit message |
jeanpierreaulas | 11:e837b1a581cb | 70 | led = OFF; // turn the LED off |
jeanpierreaulas | 11:e837b1a581cb | 71 | pc.printf("-------------------------------------\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 72 | pc.printf("-------------------------------------\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 73 | pc.printf("CAN message sent\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 74 | printMsgCan(txMsg); |
jeanpierreaulas | 11:e837b1a581cb | 75 | pc.printf(" counter = %d\r\n", counter); |
jeanpierreaulas | 11:e837b1a581cb | 76 | pc.printf(" voltage = %e V\r\n", voltage); |
jeanpierreaulas | 11:e837b1a581cb | 77 | } |
jeanpierreaulas | 11:e837b1a581cb | 78 | else |
jeanpierreaulas | 11:e837b1a581cb | 79 | pc.printf("Transmission error\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 80 | } |
jeanpierreaulas | 11:e837b1a581cb | 81 | |
jeanpierreaulas | 11:e837b1a581cb | 82 | void init_can() |
jeanpierreaulas | 11:e837b1a581cb | 83 | { |
jeanpierreaulas | 11:e837b1a581cb | 84 | can.frequency(1000000); // set CAN bit rate to 1Mbps |
jeanpierreaulas | 11:e837b1a581cb | 85 | can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID |
jeanpierreaulas | 11:e837b1a581cb | 86 | can.attach(onCanReceived); // attach ISR to handle received messages |
jeanpierreaulas | 11:e837b1a581cb | 87 | pc.printf("CAN_Hello board start\r\n"); |
jeanpierreaulas | 11:e837b1a581cb | 88 | } |
jeanpierreaulas | 11:e837b1a581cb | 89 | |
jeanpierreaulas | 11:e837b1a581cb | 90 | #endif // CANJpa_H |