|
| 1 | +/************************************************************************************** |
| 2 | + * INCLUDE |
| 3 | + **************************************************************************************/ |
| 4 | + |
| 5 | +#include <CAN.h> |
| 6 | + |
| 7 | +/************************************************************************************** |
| 8 | + * CONSTANTS |
| 9 | + **************************************************************************************/ |
| 10 | + |
| 11 | +static uint32_t const CAN_ID = 0x20; |
| 12 | + |
| 13 | +/************************************************************************************** |
| 14 | + * SETUP/LOOP |
| 15 | + **************************************************************************************/ |
| 16 | + |
| 17 | +void setup() |
| 18 | +{ |
| 19 | + //Serial.begin(115200); |
| 20 | + //while (!Serial) { } |
| 21 | + |
| 22 | + /* You need to enable the CAN transceiver |
| 23 | + * by commenting in below code when using |
| 24 | + * a Portenta H33 on a Portenta Max Carrier. |
| 25 | + * Note: Only CAN1 is available on the Portenta |
| 26 | + * Max Carrier's RJ10 CAN connector. |
| 27 | + */ |
| 28 | +#if (PIN_CAN1_STBY >= 0) |
| 29 | + pinMode(PIN_CAN1_STBY, OUTPUT); |
| 30 | + digitalWrite(PIN_CAN1_STBY, LOW); |
| 31 | +#endif |
| 32 | + |
| 33 | + if (!CAN1.begin(CanBitRate::BR_125k)) |
| 34 | + { |
| 35 | + //Serial.println("CAN.begin(...) failed."); |
| 36 | + for (;;) {} |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +static uint32_t msg_cnt = 0; |
| 41 | + |
| 42 | +void loop() |
| 43 | +{ |
| 44 | + /* Assemble a CAN message with the format of |
| 45 | + * 0xCA 0xFE 0x00 0x00 [4 byte message counter] |
| 46 | + */ |
| 47 | + uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0}; |
| 48 | + memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt)); |
| 49 | + CanMsg msg(CAN_ID, sizeof(msg_data), msg_data); |
| 50 | + |
| 51 | + /* Transmit the CAN message, capture and display an |
| 52 | + * error core in case of failure. |
| 53 | + */ |
| 54 | + if (int const rc = CAN1.write(msg); rc < 0) |
| 55 | + { |
| 56 | + //Serial.print ("CAN.write(...) failed with error code "); |
| 57 | + //Serial.println(rc); |
| 58 | + for (;;) { } |
| 59 | + } |
| 60 | + |
| 61 | + /* Increase the message counter. */ |
| 62 | + msg_cnt++; |
| 63 | + |
| 64 | + /* Only send one message per second. */ |
| 65 | + delay(1000); |
| 66 | +} |
0 commit comments