1
+ // Glue layer for platforms that implement the Arduino's official HardwareCAN API.
2
+
1
3
#pragma once
2
4
3
5
#include " ODriveCAN.h"
6
8
// Must be defined by the application
7
9
void onCanMessage (const CanMsg& msg);
8
10
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
+ */
9
24
static bool sendMsg (HardwareCAN& can_intf, uint32_t id, uint8_t length, const uint8_t * data) {
10
25
// Note: Arduino_CAN does not support the RTR bit. The ODrive interprets
11
26
// 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
18
33
return can_intf.write (msg) >= 0 ;
19
34
}
20
35
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
+ */
21
43
static void onReceive (const CanMsg& msg, ODriveCAN& odrive) {
22
44
odrive.onReceive (msg.id , msg.data_length , msg.data );
23
45
}
24
46
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
+ */
25
58
static void pumpEvents (HardwareCAN& intf, int max_events = 100 ) {
26
59
// max_events prevents an infinite loop if messages come at a high rate
27
60
while (intf.available () && max_events--) {
0 commit comments