1+ // Glue layer for platforms that implement the Arduino's official HardwareCAN API.
2+
13#pragma once
24
35#include " ODriveCAN.h"
68// Must be defined by the application
79void onCanMessage (const CanMsg& msg);
810
11+ /* *
12+ * @brief Sends a CAN message over the specified platform-specific interface.
13+ *
14+ * @param can_intf A platform-specific reference to the CAN interface to use.
15+ * @param id: The CAN message ID to send.
16+ * Bit 31 indicates if the ID is extended (29-bit) or standard (11-bit).
17+ * Bits 30 and 29 are reserved.
18+ * @param length: The length of the data in bytes (0-8). For RTR messages, this
19+ * should be 0.
20+ * @param data: A pointer to the data to send. If null, a remote transmission
21+ * request (RTR=1) is sent, if supported by the interface.
22+ * @return: True if the message was sent successfully, false otherwise.
23+ */
924static bool sendMsg (HardwareCAN& can_intf, uint32_t id, uint8_t length, const uint8_t * data) {
1025 // Note: Arduino_CAN does not support the RTR bit. The ODrive interprets
1126 // zero-length packets the same as RTR=1, but it creates the possibility of
@@ -18,10 +33,28 @@ static bool sendMsg(HardwareCAN& can_intf, uint32_t id, uint8_t length, const ui
1833 return can_intf.write (msg) >= 0 ;
1934}
2035
36+ /* *
37+ * @brief Receives a CAN message from the platform-specific interface and passes
38+ * it to the ODriveCAN instance.
39+ *
40+ * @param msg: The received CAN message in a platform-specific format.
41+ * @param odrive: The ODriveCAN instance to pass the message to.
42+ */
2143static void onReceive (const CanMsg& msg, ODriveCAN& odrive) {
2244 odrive.onReceive (msg.id , msg.data_length , msg.data );
2345}
2446
47+ /* *
48+ * @brief Processes the CAN interface's RX buffer and calls onCanMessage for
49+ * each pending message.
50+ *
51+ * On hardware interfaces where onCanMessage() is already called from the
52+ * interrupt handler, this function is a no-op.
53+ *
54+ * @param intf: The platform-specific CAN interface to process.
55+ * @param max_events: The maximum number of events to process. This prevents
56+ * an infinite loop if messages come at a high rate.
57+ */
2558static void pumpEvents (HardwareCAN& intf, int max_events = 100 ) {
2659 // max_events prevents an infinite loop if messages come at a high rate
2760 while (intf.available () && max_events--) {
0 commit comments