Skip to content

Commit 8479e07

Browse files
authored
Merge pull request #12 from BrechtSerckx/stm32
Add STM32 support
2 parents 81307bd + 0a0fe30 commit 8479e07

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

examples/SineWaveCAN/SineWaveCAN.ino

+23-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
// #define IS_TEENSY_BUILTIN // Teensy boards with built-in CAN interface (e.g. Teensy 4.1). See below to select which interface to use.
2121
// #define IS_ARDUINO_BUILTIN // Arduino boards with built-in CAN interface (e.g. Arduino Uno R4 Minima)
2222
// #define IS_MCP2515 // Any board with external MCP2515 based extension module. See below to configure the module.
23+
// #define IS_STM32_BUILTIN // STM32 boards with built-in CAN interface (e.g. STM32F4 Discovery).
2324

2425

2526
/* Board-specific includes ---------------------------------------------------*/
2627

27-
#if defined(IS_TEENSY_BUILTIN) + defined(IS_ARDUINO_BUILTIN) + defined(IS_MCP2515) != 1
28+
#if defined(IS_TEENSY_BUILTIN) + defined(IS_ARDUINO_BUILTIN) + defined(IS_MCP2515) + defined(IS_STM32_BUILTIN) != 1
2829
#warning "Select exactly one hardware option at the top of this file."
2930

3031
#if CAN_HOWMANY > 0 || CANFD_HOWMANY > 0
@@ -58,6 +59,11 @@
5859
struct ODriveStatus; // hack to prevent teensy compile error
5960
#endif // IS_TEENSY_BUILTIN
6061

62+
#ifdef IS_STM32_BUILTIN
63+
// See https://github.com/pazi88/STM32_CAN
64+
#include <STM32_CAN.h>
65+
#include "ODriveSTM32CAN.hpp"
66+
#endif // IS_STM32_BUILTIN
6167

6268

6369

@@ -138,6 +144,22 @@ bool setupCan() {
138144
#endif
139145

140146

147+
/* STM32 boards with built-in CAN */
148+
149+
#ifdef IS_STM32_BUILTIN
150+
151+
STM32_CAN Can1( CAN1 );
152+
STM32_CAN& can_intf = Can1;
153+
154+
bool setupCan() {
155+
can_intf.begin();
156+
can_intf.setBaudRate(CAN_BAUDRATE);
157+
return true;
158+
}
159+
160+
#endif // IS_STM32_BUILTIN
161+
162+
141163
/* Example sketch ------------------------------------------------------------*/
142164

143165
// Instantiate ODrive objects

platformio.ini

+16
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ framework = arduino
3131
build_flags = -DIS_MCP2515
3232
lib_deps =
3333
https://github.com/sandeepmistry/arduino-CAN.git
34+
35+
[env:stm32f405]
36+
platform = ststm32
37+
board = adafruit_feather_f405
38+
framework = arduino
39+
build_flags =
40+
-DIS_STM32_BUILTIN
41+
; Enable serial.
42+
; See https://github.com/platformio/platform-ststm32/issues/420#issuecomment-672277396
43+
-USBCON
44+
-DPIO_FRAMEWORK_ARDUINO_ENABLE_CDC
45+
; Enable CAN module in HAL drivers.
46+
; See https://github.com/pazi88/STM32_CAN
47+
-DHAL_CAN_MODULE_ENABLED
48+
lib_deps =
49+
https://github.com/pazi88/STM32_CAN.git#1.2.0

src/ODriveSTM32CAN.hpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 & 0x1ffffff;
14+
msg.flags.extended = id & 0x80000000;
15+
msg.flags.remote = (data == nullptr);
16+
msg.len = length;
17+
if (data) {
18+
for (int i = 0; i < length; ++i) {
19+
msg.buf[i] = data[i];
20+
}
21+
}
22+
return can_intf.write(msg) >= 0;
23+
}
24+
25+
static void onReceive(const CanMsg& msg, ODriveCAN& odrive) {
26+
odrive.onReceive(msg.id, msg.len, msg.buf);
27+
}
28+
29+
static void pumpEvents(STM32_CAN& intf, int max_events = 100) {
30+
// max_events prevents an infinite loop if messages come at a high rate
31+
CanMsg msg;
32+
while (intf.read(msg) && max_events--) {
33+
onCanMessage(msg);
34+
}
35+
}
36+
37+
CREATE_CAN_INTF_WRAPPER(STM32_CAN)

0 commit comments

Comments
 (0)