Skip to content

Commit

Permalink
Bidirectional DShot
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Oes <[email protected]>
  • Loading branch information
dakejahl and julianoes committed Oct 29, 2024
1 parent a3a83c7 commit 16dc6d4
Show file tree
Hide file tree
Showing 12 changed files with 1,055 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ __EXPORT int io_timer_free_channel(unsigned channel);
__EXPORT int io_timer_get_channel_mode(unsigned channel);
__EXPORT int io_timer_get_mode_channels(io_timer_channel_mode_t mode);
__EXPORT extern void io_timer_trigger(void);
__EXPORT void io_timer_update_dma_req(uint8_t timer, bool enable);

__EXPORT extern int io_timer_set_dshot_mode(uint8_t timer, unsigned dshot_pwm_rate, uint8_t dma_burst_length);

/**
* Returns the pin configuration for a specific channel, to be used as GPIO output.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
px4_add_library(arch_dshot
dshot.c
)
# target_compile_options(arch_dshot PRIVATE ${MAX_CUSTOM_OPT_LEVEL} -DDEBUG_BUILD)
target_compile_options(arch_dshot PRIVATE ${MAX_CUSTOM_OPT_LEVEL})
780 changes: 668 additions & 112 deletions platforms/nuttx/src/px4/stm/stm32_common/dshot/dshot.c

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
* Copyright (C) 2024 PX4 Development Team. All rights reserved.
* Author: Igor Misic <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -58,5 +58,6 @@
*/
typedef struct dshot_conf_t {
uint32_t dma_base;
uint32_t dmamap;
uint32_t dma_map_up;
uint32_t dma_map_ch[4];
} dshot_conf_t;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (C) 2012, 2017 PX4 Development Team. All rights reserved.
* Copyright (C) 2024 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -80,6 +80,8 @@ typedef enum io_timer_channel_mode_t {
IOTimerChanMode_LED = 7,
IOTimerChanMode_PPS = 8,
IOTimerChanMode_Other = 9,
IOTimerChanMode_DshotInverted = 10,
IOTimerChanMode_CaptureDMA = 11,
IOTimerChanModeSize
} io_timer_channel_mode_t;

Expand Down Expand Up @@ -158,7 +160,12 @@ __EXPORT int io_timer_unallocate_channel(unsigned channel);
__EXPORT int io_timer_get_channel_mode(unsigned channel);
__EXPORT int io_timer_get_mode_channels(io_timer_channel_mode_t mode);
__EXPORT extern void io_timer_trigger(unsigned channels_mask);

__EXPORT void io_timer_update_dma_req(uint8_t timer, bool enable);
__EXPORT int io_timer_set_dshot_burst_mode(uint8_t timer, unsigned dshot_pwm_rate, uint8_t dma_burst_length);

__EXPORT void io_timer_capture_dma_req(uint8_t timer, uint8_t timer_channel_index, bool enable);
__EXPORT int io_timer_set_dshot_capture_mode(uint8_t timer, uint8_t timer_channel_index, unsigned dshot_pwm_freq);

/**
* Reserve a timer
Expand All @@ -168,7 +175,6 @@ __EXPORT int io_timer_allocate_timer(unsigned timer, io_timer_channel_mode_t mod

__EXPORT int io_timer_unallocate_timer(unsigned timer);

__EXPORT extern int io_timer_set_dshot_mode(uint8_t timer, unsigned dshot_pwm_rate, uint8_t dma_burst_length);

/**
* Returns the pin configuration for a specific channel, to be used as GPIO output.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
* Copyright (C) 2024 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -103,7 +103,7 @@ static inline constexpr timer_io_channels_t initIOTimerChannelOutputClear(const
}


static inline constexpr io_timers_t initIOTimer(Timer::Timer timer, DMA dshot_dma = {})
static inline constexpr io_timers_t initIOTimer(Timer::Timer timer, DMA dma = {})
{
bool nuttx_config_timer_enabled = false;
io_timers_t ret{};
Expand Down Expand Up @@ -268,9 +268,10 @@ static inline constexpr io_timers_t initIOTimer(Timer::Timer timer, DMA dshot_dm
constexpr_assert(!nuttx_config_timer_enabled, "IO Timer requires NuttX timer config to be disabled (STM32_TIMx)");

// DShot
if (dshot_dma.index != DMA::Invalid) {
ret.dshot.dma_base = getDMABaseRegister(dshot_dma);
ret.dshot.dmamap = getTimerUpdateDMAMap(timer, dshot_dma);
if (dma.index != DMA::Invalid) {
ret.dshot.dma_base = getDMABaseRegister(dma);
ret.dshot.dma_map_up = getTimerUpdateDMAMap(timer, dma);
getTimerChannelDMAMap(timer, dma, ret.dshot.dma_map_ch);
}

return ret;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (C) 2012-2016 PX4 Development Team. All rights reserved.
* Copyright (C) 2024 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -365,8 +365,10 @@ int up_input_capture_set_trigger(unsigned channel, input_capture_edge edge)
rv = -ENXIO;

/* Any pins in capture mode */
int mode = io_timer_get_channel_mode(channel);

if (io_timer_get_channel_mode(channel) == IOTimerChanMode_Capture) {
if (mode == IOTimerChanMode_Capture ||
mode == IOTimerChanMode_CaptureDMA) {

uint16_t edge_bits = 0xffff;

Expand Down
Loading

0 comments on commit 16dc6d4

Please sign in to comment.