Skip to content

Commit a7be9a1

Browse files
committed
Add wrapper for STM32
1 parent 81307bd commit a7be9a1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/ODriveSTM32CAN.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "ODriveCAN.h"
4+
#include <STM32_CAN.h>
5+
6+
using CanMsg = CAN_message_t;
7+
8+
// Must be defined by the application
9+
void onCanMessage(const CanMsg& msg);
10+
11+
static bool sendMsg(STM32_CAN& can_intf, uint32_t id, uint8_t length, const uint8_t* data) {
12+
CanMsg msg;
13+
msg.id = id;
14+
msg.len = length;
15+
if (data) {
16+
for (int i = 0; i < length; ++i) {
17+
msg.buf[i] = data[i];
18+
}
19+
}
20+
return can_intf.write(msg) >= 0;
21+
}
22+
23+
static void onReceive(const CanMsg& msg, ODriveCAN& odrive) {
24+
odrive.onReceive(msg.id, msg.len, msg.buf);
25+
}
26+
27+
static void pumpEvents(STM32_CAN& intf, int max_events = 100) {
28+
// max_events prevents an infinite loop if messages come at a high rate
29+
CanMsg msg;
30+
while (intf.read(msg) && max_events--) {
31+
onCanMessage(msg);
32+
}
33+
}
34+
35+
CREATE_CAN_INTF_WRAPPER(STM32_CAN)

0 commit comments

Comments
 (0)