Developing the EFS CAN-DShot ESC Adapter Board Firmware
This document contains more details about the firmware for the CAN-DShot ESC Adapter board.
Table of Contents
Important Links for the CAN-DShot ESC Adapter Board
Board Usage: EFS CAN-DShot ESC Adapter Board Firmware
Firmware Repo: https://github.com/UWARG/efs-can-esc
EE Documentation: ESC CAN Adapter
Board Schematic (pinout, PCB layout): https://warg.365.altium.com/designs/9504A71D-9B2B-4328-B339-C3EBBC2F7DF1
MCU Datasheet: STM32L431KCU6
Overview - Current State of Firmware (at time of writing)
What Has Been Done
DShot code converted from STM32L5 development board to STM32L4 CAN ESC adapter board STM32 project, meaning that the CAN ESC adapter board can control an ESC with DShot.
efs-canard integrated with DShot code, so CAN ESC adapter board can receive throttle inputs from flight controller and mission planner through CAN (see video below).
Protocols
The flight controller communicates with the ESC CAN adapter through CAN (STM32L4 does not support FDCAN).
this is implemented with efs-canard. The board accepts throttle values from CAN messages and calls DShot functions to control the ESC.
The CAN ESC adapter controls the ESC over DShot.
implemented with memory to peripheral DMA in
dshot.c
a complementary timer pin (e.g. TIM1 CH1N) was used for DShot output generation due to the wiring of the board. This is the same as a normal timer output except
HAL_TIME_*()
was replaced withHAL_TIMEx_*()
function calls.DShot is currently normal DShot 150 (not bidirectional).
Setting up Development Environment
Below are instructions to setup everything you need to start developing the CAN ESC adapter firmware (at the time of writing).
Install STM32CubeIDE. It is used for developing, configuring, and flashing firmware to the STM32 MCU on this board.
Clone, import, and open the
can-esc
firmware repository.Wire up the CAN ESC adapter board:
ST-link between your laptop/development machine and the CAN ESC adapter board (see https://uwarg-docs.atlassian.net/wiki/x/1wD9VQ,https://uwarg-docs.atlassian.net/wiki/x/XICRlw)
logic analyzer is helpful for probing outputs like DShot.
CAN connection to the flight controller and DShot connection to the ESC:
- Example Wiring Diagram for Testing
here is an example of what the setup could look like:
Connect flight controller to your computer if you want to test CAN functionality. You’ll need to install ArduPilot Mission Planner to do so.
You are now ready to start writing firmware! Please see these troubleshooting and common pitfalls in case you come across similar issues while working on the board.
Troubleshooting and Pitfalls
Below are some pitfalls encountered when developing firmware for this board and tips to address them.
Debugging
printf()
does not work if you are using a ST-link v2 (blue usb adapter). This is because there is no SWO output pin, and thus we cannot create a SWV.As an alternative, we can use SWD by clicking the “Debug“ button in STM32CUBEIDE, which allows us to step through the code and view memory and variable values (see https://uwarg-docs.atlassian.net/wiki/x/EYADdw).
Error
No ST-LINK with the specified serial number detected
: sometimes a project is linked to a specific debugger serial number, and you can unlink it from the debug configuration menu.
MCU Configuration
Timer
No timer/DShot output
See latest working commit in firmware repository for valid configurations (specifically the .ioc file). The important parameters include: ARR/CCR, counter period, DMA (direction, mode, increment address, data width, ids), timer mode, timer tree (HSE output)
Inverted timer output: see PWM mode 1 vs PWM mode 2
CAN
CAN interrupt not triggering
Make sure CAN is initialized properly (
HAL_CAN_*
)See latest working commit in firmware repository for valid configurations (specifically the .ioc file). The important parameters include:
NVIC CAN interrupt, GPIO pullup, prescaler, time quanta
Timing Issues
DShot output timing is off
The function for setting DShot throttle values needs to be called continuous in an infinite while loop, and any delay in between could affect time timing. At the time of writing, the current implementation exposes a global throttle value that gets changed whenever a CAN message is handled by the CAN interrupt. This way, the DShot function call in the continuous while loop continues to run indefinitely.
There also needs to be a slight delay after calling the DShot write function each time it is called, currently the delay is set to 1000ms. Removing this delay causes unknown behavior in the DShot output.
ArduPilot not sending CAN messages
See how to configure Mission Planner to control ESCs over CAN: EFS CAN-DShot ESC Adapter Board Firmware | Flight Controller Configuration in Mission Planner
Motors not spinning with correct Dshot output
Most DShot ESCs require a throttle value of 0 to be set for some time (e.g. 300ms) before it starts accepting non-zero throttle values. See DSHOT - the missing Handbook
Similar Boards
This board was developed in parallel with a few other boards that share the same dronecan/libcanard code. It may be helpful to see the implementation of these boards for more examples.
Other Helpful Resources
DShot guide: DSHOT - the missing Handbook
DroneCAN Specs: 7. List of standard data types - DroneCAN