Jean-Pierre Aulas
/
CanS-Homes
start adapt S-Homes structure
Revision 11:e837b1a581cb, committed 2020-09-06
- Comitter:
- jeanpierreaulas
- Date:
- Sun Sep 06 19:36:44 2020 +0000
- Parent:
- 10:eadd73cf3981
- Commit message:
- add CANJpa.h
Changed in this revision
CANJpa/CANJpa.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CANJpa/CANJpa.h Sun Sep 06 19:36:44 2020 +0000 @@ -0,0 +1,90 @@ +#ifndef CANJpa_H +#define CANJpa_H + +extern Serial pc(PA_2, PA_3); +extern CAN can(PA_2, PA_3); // CAN Rx pin name, CAN Tx pin name +//CAN can(p30, p29); // CAN Rx pin name, CAN Tx pin name +CANMsg rxMsg; +CANMsg txMsg; + +uint8_t counter = 0; +AnalogIn analogIn(A0); +float voltage; +Timer timer; +DigitalOut led(LED_PIN); + +/** + * @brief Prints CAN message to PC's serial terminal + * @note + * @param CANMessage to print + * @retval + */ +void printMsgCan(CANMessage& msg) +{ + pc.printf(" ID = 0x%.3x\r\n", msg.id); + pc.printf(" Type = %d\r\n", msg.type); + pc.printf(" Format = %d\r\n", msg.format); + pc.printf(" Length = %d\r\n", msg.len); + pc.printf(" Data ="); + for(int i = 0; i < msg.len; i++) + pc.printf(" 0x%.2X", msg.data[i]); + pc.printf("\r\n"); +} + + +/** + * @brief Handles received CAN messages + * @note Called on 'CAN message received' interrupt. + * @param + * @retval + */ +void onCanReceived(void) +{ + can.read(rxMsg); + pc.printf("-------------------------------------\r\n"); + pc.printf("CAN message received\r\n"); + printMsgCan(rxMsg); + + if (rxMsg.id == RX_ID) { + // extract data from the received CAN message + // in the same order as it was added on the transmitter side + rxMsg >> counter; + rxMsg >> voltage; + pc.printf(" counter = %d\r\n", counter); + pc.printf(" voltage = %e V\r\n", voltage); + } + timer.start(); // to transmit next message in main +} + +void can_write() + { + counter++; // increment counter + voltage = analogIn * 3.3f; // read the small drift voltage from analog input + txMsg.clear(); // clear Tx message storage + txMsg.id = TX_ID; // set ID + // append data (total data length must not exceed 8 bytes!) + txMsg << counter; // one byte + txMsg << voltage; // four bytes + + if(can.write(txMsg)) { // transmit message + led = OFF; // turn the LED off + pc.printf("-------------------------------------\r\n"); + pc.printf("-------------------------------------\r\n"); + pc.printf("CAN message sent\r\n"); + printMsgCan(txMsg); + pc.printf(" counter = %d\r\n", counter); + pc.printf(" voltage = %e V\r\n", voltage); + } + else + pc.printf("Transmission error\r\n"); + } + +void init_can() + { + can.frequency(1000000); // set CAN bit rate to 1Mbps + can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID + can.attach(onCanReceived); // attach ISR to handle received messages + pc.printf("CAN_Hello board start\r\n"); + } + +#endif // CANJpa_H \ No newline at end of file
--- a/main.cpp Sun Sep 06 17:42:37 2020 +0000 +++ b/main.cpp Sun Sep 06 19:36:44 2020 +0000 @@ -29,70 +29,17 @@ const int OFF = 0; const int ON = 1; #endif - -#if defined(BOARD1) - const unsigned int RX_ID = 0x100; - const unsigned int TX_ID = 0x101; -#else const unsigned int RX_ID = 0x101; const unsigned int TX_ID = 0x100; -#endif #include "mbed.h" #include "CANMsg.h" - -Serial pc(PA_2, PA_3); -CAN can(PA_2, PA_3); // CAN Rx pin name, CAN Tx pin name -//CAN can(p30, p29); // CAN Rx pin name, CAN Tx pin name -CANMsg rxMsg; -CANMsg txMsg; -DigitalOut led(LED_PIN); -Timer timer; -uint8_t counter = 0; -AnalogIn analogIn(A0); -float voltage; +#include "CANJpa.h" -/** - * @brief Prints CAN message to PC's serial terminal - * @note - * @param CANMessage to print - * @retval - */ -void printMsg(CANMessage& msg) -{ - pc.printf(" ID = 0x%.3x\r\n", msg.id); - pc.printf(" Type = %d\r\n", msg.type); - pc.printf(" Format = %d\r\n", msg.format); - pc.printf(" Length = %d\r\n", msg.len); - pc.printf(" Data ="); - for(int i = 0; i < msg.len; i++) - pc.printf(" 0x%.2X", msg.data[i]); - pc.printf("\r\n"); -} +//Serial pc(PA_2, PA_3); -/** - * @brief Handles received CAN messages - * @note Called on 'CAN message received' interrupt. - * @param - * @retval - */ -void onCanReceived(void) -{ - can.read(rxMsg); - pc.printf("-------------------------------------\r\n"); - pc.printf("CAN message received\r\n"); - printMsg(rxMsg); - - if (rxMsg.id == RX_ID) { - // extract data from the received CAN message - // in the same order as it was added on the transmitter side - rxMsg >> counter; - rxMsg >> voltage; - pc.printf(" counter = %d\r\n", counter); - pc.printf(" voltage = %e V\r\n", voltage); - } - timer.start(); // to transmit next message in main -} +void init_can(); +void can_write(); /** @@ -104,41 +51,15 @@ int main(void) { pc.baud(9600); // set serial speed - can.frequency(1000000); // set CAN bit rate to 1Mbps - can.filter(RX_ID, 0xFFF, CANStandard, 0); // set filter #0 to accept only standard messages with ID == RX_ID - can.attach(onCanReceived); // attach ISR to handle received messages - -#if defined(BOARD1) + init_can(); led = ON; // turn the LED on timer.start(); // start timer - pc.printf("CAN_Hello board #1\r\n"); -#else - led = OFF; // turn LED off - pc.printf("CAN_Hello board #2\r\n"); -#endif while(1) { if(timer.read_ms() >= 2000) { // check for timeout timer.stop(); // stop timer timer.reset(); // reset timer - counter++; // increment counter - voltage = analogIn * 3.3f; // read the small drift voltage from analog input - txMsg.clear(); // clear Tx message storage - txMsg.id = TX_ID; // set ID - // append data (total data length must not exceed 8 bytes!) - txMsg << counter; // one byte - txMsg << voltage; // four bytes - - if(can.write(txMsg)) { // transmit message - led = OFF; // turn the LED off - pc.printf("-------------------------------------\r\n"); - pc.printf("-------------------------------------\r\n"); - pc.printf("CAN message sent\r\n"); - printMsg(txMsg); - pc.printf(" counter = %d\r\n", counter); - pc.printf(" voltage = %e V\r\n", voltage); - } - else - pc.printf("Transmission error\r\n"); + can_write(); + } } }