Skip to content

Add STM32 support #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion examples/SineWaveCAN/SineWaveCAN.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
// #define IS_TEENSY_BUILTIN // Teensy boards with built-in CAN interface (e.g. Teensy 4.1). See below to select which interface to use.
// #define IS_ARDUINO_BUILTIN // Arduino boards with built-in CAN interface (e.g. Arduino Uno R4 Minima)
// #define IS_MCP2515 // Any board with external MCP2515 based extension module. See below to configure the module.
// #define IS_STM32_BUILTIN // STM32 boards with built-in CAN interface (e.g. STM32F4 Discovery).


/* Board-specific includes ---------------------------------------------------*/

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

#if CAN_HOWMANY > 0 || CANFD_HOWMANY > 0
Expand Down Expand Up @@ -58,6 +59,11 @@
struct ODriveStatus; // hack to prevent teensy compile error
#endif // IS_TEENSY_BUILTIN

#ifdef IS_STM32_BUILTIN
// See https://github.com/pazi88/STM32_CAN
#include <STM32_CAN.h>
#include "ODriveSTM32CAN.hpp"
#endif // IS_STM32_BUILTIN



Expand Down Expand Up @@ -138,6 +144,22 @@ bool setupCan() {
#endif


/* STM32 boards with built-in CAN */

#ifdef IS_STM32_BUILTIN

STM32_CAN Can1( CAN1 );
STM32_CAN& can_intf = Can1;

bool setupCan() {
can_intf.begin();
can_intf.setBaudRate(CAN_BAUDRATE);
return true;
}

#endif // IS_STM32_BUILTIN


/* Example sketch ------------------------------------------------------------*/

// Instantiate ODrive objects
Expand Down
16 changes: 16 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@ framework = arduino
build_flags = -DIS_MCP2515
lib_deps =
https://github.com/sandeepmistry/arduino-CAN.git

[env:stm32f405]
platform = ststm32
board = adafruit_feather_f405
framework = arduino
build_flags =
-DIS_STM32_BUILTIN
; Enable serial.
; See https://github.com/platformio/platform-ststm32/issues/420#issuecomment-672277396
-USBCON
-DPIO_FRAMEWORK_ARDUINO_ENABLE_CDC
; Enable CAN module in HAL drivers.
; See https://github.com/pazi88/STM32_CAN
-DHAL_CAN_MODULE_ENABLED
lib_deps =
https://github.com/pazi88/STM32_CAN.git#1.2.0
35 changes: 35 additions & 0 deletions src/ODriveSTM32CAN.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "ODriveCAN.h"
#include <STM32_CAN.h>

using CanMsg = CAN_message_t;

// Must be defined by the application
void onCanMessage(const CanMsg& msg);

static bool sendMsg(STM32_CAN& can_intf, uint32_t id, uint8_t length, const uint8_t* data) {
CanMsg msg;
msg.id = id;
msg.len = length;
if (data) {
for (int i = 0; i < length; ++i) {
msg.buf[i] = data[i];
}
}
return can_intf.write(msg) >= 0;
}

static void onReceive(const CanMsg& msg, ODriveCAN& odrive) {
odrive.onReceive(msg.id, msg.len, msg.buf);
}

static void pumpEvents(STM32_CAN& intf, int max_events = 100) {
// max_events prevents an infinite loop if messages come at a high rate
CanMsg msg;
while (intf.read(msg) && max_events--) {
onCanMessage(msg);
}
}

CREATE_CAN_INTF_WRAPPER(STM32_CAN)