Skip to content

Commit 8044fef

Browse files
committed
improve docs for CAN glue layers
1 parent fb18543 commit 8044fef

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/ODriveFlexCAN.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Glue layer for FlexCAN-based CAN stacks.
2+
// See ODriveHardwareCAN.hpp for documentation.
3+
14
#pragma once
25

36
#include "ODriveCAN.h"

src/ODriveHardwareCAN.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Glue layer for platforms that implement the Arduino's official HardwareCAN API.
2+
13
#pragma once
24

35
#include "ODriveCAN.h"
@@ -6,6 +8,19 @@
68
// Must be defined by the application
79
void 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+
*/
924
static 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+
*/
2143
static 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+
*/
2558
static 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--) {

src/ODriveMCPCAN.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Glue layer for MCP2515-based CAN interfaces.
2+
// See ODriveHardwareCAN.hpp for documentation.
3+
14
#pragma once
25

36
#include "MCP2515.h"
@@ -14,6 +17,8 @@ struct CanMsg {
1417
// Must be defined by the application if you want to use defaultCanReceiveCallback().
1518
void onCanMessage(const CanMsg& msg);
1619

20+
// MSB of id means "extended"
21+
// if data is null, it's a remote request frame
1722
static bool sendMsg(MCP2515Class& can_intf, uint32_t id, uint8_t length, const uint8_t* data) {
1823
if (id & 0x80000000) {
1924
can_intf.beginExtendedPacket(id & 0x1fffffff, length, !data);

src/ODriveSTM32CAN.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// CAN glue layer for STM32 platforms.
2+
// See ODriveHardwareCAN.hpp for documentation.
3+
14
#pragma once
25

36
#include "ODriveCAN.h"

0 commit comments

Comments
 (0)