Skip to content

Commit

Permalink
stm32 timers: add dshot implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor-Misic authored and bkueng committed Oct 11, 2019
1 parent 061ee15 commit 5bd9659
Show file tree
Hide file tree
Showing 10 changed files with 790 additions and 41 deletions.
87 changes: 87 additions & 0 deletions platforms/nuttx/src/px4/stm/stm32_common/include/px4_arch/dshot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/****************************************************************************
*
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
* Author: Igor Misic <igy1000mb@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#pragma once

#include <drivers/drv_pwm_output.h>

#define DSHOT_MOTOR_PWM_BIT_WIDTH 20u

/* Configuration for each timer to setup DShot. Some timers have only one while others have two choices for the stream.
*
* TIM1UP - DMA2, Channel6, Stream5
* TIM2UP - DMA1, Channel3, Stream1 or Stream7
* TIM3UP - DMA1, Channel5, Stream2
* TIM4UP - DMA1, Channel2, Stream6
* TIM5UP - DMA1, Channel6, Stream0 or Stream6
* TIM6UP - DMA1, Channel7, Stream1
* TIM7UP - DMA1, Channel1, Stream2 or Stream4
* TIM8UP - DMA2, Channel7, Stream1
*/

#define DSHOT_DMA1_BASE STM32_DMA1_BASE
#define DSHOT_DMA2_BASE STM32_DMA2_BASE

typedef enum dshot_dma_channel_t {
DShot_Channel0 = 0u,
DShot_Channel1 = 1u,
DShot_Channel2 = 2u,
DShot_Channel3 = 3u,
DShot_Channel4 = 4u,
DShot_Channel5 = 5u,
DShot_Channel6 = 6u,
DShot_Channel7 = 7u
} dshot_dma_channel_t;

typedef enum dshot_dma_stream_t {
DShot_Stream0 = 0u,
DShot_Stream1 = 1u,
DShot_Stream2 = 2u,
DShot_Stream3 = 3u,
DShot_Stream4 = 4u,
DShot_Stream5 = 5u,
DShot_Stream6 = 6u,
DShot_Stream7 = 7u
} dshot_dma_stream_t;


/* The structure which contains configuration for DShot
*/
typedef struct dshot_conf_t {
uint32_t dma_base;
dshot_dma_channel_t channel;
dshot_dma_stream_t stream;
uint32_t start_ccr_register;
uint8_t channels_number;
} dshot_conf_t;
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -32,15 +32,14 @@
****************************************************************************/

/**
* @file drv_io_timer.h
*
* stm32-specific PWM output data.
* @file io_timer.h
*/
#include <px4_config.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>

#include <drivers/drv_hrt.h>
#include "dshot.h"

#pragma once
__BEGIN_DECLS
Expand All @@ -53,13 +52,20 @@ __BEGIN_DECLS

#define IO_TIMER_ALL_MODES_CHANNELS 0

/* TIM_DMA_Base_address TIM DMA Base Address */
#define TIM_DMABASE_CCR1 0x0000000DU
#define TIM_DMABASE_CCR2 0x0000000EU
#define TIM_DMABASE_CCR3 0x0000000FU
#define TIM_DMABASE_CCR4 0x00000010U

typedef enum io_timer_channel_mode_t {
IOTimerChanMode_NotUsed = 0,
IOTimerChanMode_PWMOut = 1,
IOTimerChanMode_PWMIn = 2,
IOTimerChanMode_Capture = 3,
IOTimerChanMode_OneShot = 4,
IOTimerChanMode_Trigger = 5,
IOTimerChanMode_Dshot = 6,
IOTimerChanModeSize
} io_timer_channel_mode_t;

Expand All @@ -74,14 +80,15 @@ typedef uint8_t io_timer_channel_allocation_t; /* big enough to hold MAX_TIMER_I
*** the resulting PSC will be one and the timer will count at it's clock frequency.
*/
typedef struct io_timers_t {
uint32_t base;
uint32_t clock_register;
uint32_t clock_bit;
uint32_t clock_freq;
uint32_t vectorno;
uint32_t first_channel_index;
uint32_t last_channel_index;
xcpt_t handler;
uint32_t base;
uint32_t clock_register;
uint32_t clock_bit;
uint32_t clock_freq;
uint32_t vectorno;
uint32_t first_channel_index;
uint32_t last_channel_index;
xcpt_t handler;
dshot_conf_t dshot;
} io_timers_t;

/* array of channels in logical order */
Expand Down Expand Up @@ -133,4 +140,6 @@ __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 extern int io_timer_set_dshot_mode(uint8_t timer, unsigned dshot_pwm_rate, uint8_t dma_burst_length);

__END_DECLS
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ px4_add_library(arch_io_pins
pwm_servo.c
pwm_trigger.c
input_capture.c
dshot.c
)
Loading

0 comments on commit 5bd9659

Please sign in to comment.