1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-20 06:45:16 +03:00

dshot bidir support for f7

This commit is contained in:
Thorsten Laux 2019-02-20 11:52:40 +01:00
parent 394807f558
commit d2147d4ece
8 changed files with 475 additions and 298 deletions

View file

@ -146,9 +146,16 @@ typedef struct {
volatile bool isInput;
volatile bool hasTelemetry;
uint16_t dshotTelemetryValue;
#ifdef USE_HAL_DRIVER
LL_TIM_OC_InitTypeDef ocInitStruct;
LL_TIM_IC_InitTypeDef icInitStruct;
LL_DMA_InitTypeDef dmaInitStruct;
uint32_t llChannel;
#else
TIM_OCInitTypeDef ocInitStruct;
TIM_ICInitTypeDef icInitStruct;
DMA_InitTypeDef dmaInitStruct;
#endif
uint8_t dmaInputLen;
#endif
#ifdef STM32F3

View file

@ -48,75 +48,7 @@
#include <string.h>
#endif
#if defined(STM32F4) || defined(STM32F7)
typedef DMA_Stream_TypeDef dmaStream_t;
#else
typedef DMA_Channel_TypeDef dmaStream_t;
#endif
static uint8_t dmaMotorTimerCount = 0;
static motorDmaTimer_t dmaMotorTimers[MAX_DMA_TIMERS];
static motorDmaOutput_t dmaMotors[MAX_SUPPORTED_MOTORS];
#ifdef USE_DSHOT_TELEMETRY
uint32_t readDoneCount;
// TODO remove once debugging no longer needed
uint32_t dshotInvalidPacketCount;
uint32_t inputBuffer[DSHOT_TELEMETRY_INPUT_LEN];
uint32_t setDirectionMicros;
#endif
motorDmaOutput_t *getMotorDmaOutput(uint8_t index)
{
return &dmaMotors[index];
}
uint8_t getTimerIndex(TIM_TypeDef *timer)
{
for (int i = 0; i < dmaMotorTimerCount; i++) {
if (dmaMotorTimers[i].timer == timer) {
return i;
}
}
dmaMotorTimers[dmaMotorTimerCount++].timer = timer;
return dmaMotorTimerCount - 1;
}
void pwmWriteDshotInt(uint8_t index, uint16_t value)
{
motorDmaOutput_t *const motor = &dmaMotors[index];
if (!motor->configured) {
return;
}
/*If there is a command ready to go overwrite the value and send that instead*/
if (pwmDshotCommandIsProcessing()) {
value = pwmGetDshotCommand(index);
if (value) {
motor->requestTelemetry = true;
}
}
motor->value = value;
uint16_t packet = prepareDshotPacket(motor);
uint8_t bufferSize;
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
bufferSize = loadDmaBuffer(&motor->timer->dmaBurstBuffer[timerLookupChannelIndex(motor->timerHardware->channel)], 4, packet);
motor->timer->dmaBurstLength = bufferSize * 4;
} else
#endif
{
bufferSize = loadDmaBuffer(motor->dmaBuffer, 1, packet);
motor->timer->timerDmaSources |= motor->timerDmaSource;
DMA_SetCurrDataCounter(motor->dmaRef, bufferSize);
DMA_Cmd(motor->dmaRef, ENABLE);
}
}
#include "pwm_output_dshot_shared.h"
#ifdef USE_DSHOT_TELEMETRY
@ -128,53 +60,15 @@ static void processInputIrq(motorDmaOutput_t * const motor)
readDoneCount++;
}
static uint16_t decodeDshotPacket(uint32_t buffer[])
static void enableChannels(uint8_t motorCount)
{
uint32_t value = 0;
for (int i = 1; i < DSHOT_TELEMETRY_INPUT_LEN; i += 2) {
int diff = buffer[i] - buffer[i-1];
value <<= 1;
if (diff > 0) {
if (diff >= 11) value |= 1;
for (int i = 0; i < motorCount; i++) {
if (dmaMotors[i].output & TIMER_OUTPUT_N_CHANNEL) {
TIM_CCxNCmd(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerHardware->channel, TIM_CCxN_Enable);
} else {
if (diff >= -9) value |= 1;
TIM_CCxCmd(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerHardware->channel, TIM_CCx_Enable);
}
}
uint32_t csum = value;
csum = csum ^ (csum >> 8); // xor bytes
csum = csum ^ (csum >> 4); // xor nibbles
if (csum & 0xf) {
return 0xffff;
}
return value >> 4;
}
static uint16_t decodeProshotPacket(uint32_t buffer[])
{
uint32_t value = 0;
for (int i = 1; i < PROSHOT_TELEMETRY_INPUT_LEN; i += 2) {
const int proshotModulo = MOTOR_NIBBLE_LENGTH_PROSHOT;
int diff = ((buffer[i] + proshotModulo - buffer[i-1]) % proshotModulo) - PROSHOT_BASE_SYMBOL;
int nibble;
if (diff < 0) {
nibble = 0;
} else {
nibble = (diff + PROSHOT_BIT_WIDTH / 2) / PROSHOT_BIT_WIDTH;
}
value <<= 4;
value |= (nibble & 0xf);
}
uint32_t csum = value;
csum = csum ^ (csum >> 8); // xor bytes
csum = csum ^ (csum >> 4); // xor nibbles
if (csum & 0xf) {
return 0xffff;
}
return value >> 4;
}
#endif
@ -249,49 +143,6 @@ inline FAST_CODE static void pwmDshotSetDirectionOutput(
DMA_ITConfig(dmaRef, DMA_IT_TC, ENABLE);
}
#ifdef USE_DSHOT_TELEMETRY
void pwmStartDshotMotorUpdate(uint8_t motorCount)
{
if (useDshotTelemetry) {
for (int i = 0; i < motorCount; i++) {
if (dmaMotors[i].hasTelemetry) {
uint16_t value = dmaMotors[i].useProshot ?
decodeProshotPacket(dmaMotors[i].dmaBuffer) :
decodeDshotPacket(dmaMotors[i].dmaBuffer);
if (value != 0xffff) {
dmaMotors[i].dshotTelemetryValue = value;
if (i < 4) {
DEBUG_SET(DEBUG_DSHOT_RPM_TELEMETRY, i, value);
}
} else {
dshotInvalidPacketCount++;
if (i == 0) {
memcpy(inputBuffer,dmaMotors[i].dmaBuffer,sizeof(inputBuffer));
}
}
dmaMotors[i].hasTelemetry = false;
} else {
TIM_DMACmd(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerDmaSource, DISABLE);
}
pwmDshotSetDirectionOutput(&dmaMotors[i], true);
}
for (int i = 0; i < motorCount; i++) {
if (dmaMotors[i].output & TIMER_OUTPUT_N_CHANNEL) {
TIM_CCxNCmd(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerHardware->channel, TIM_CCxN_Enable);
} else {
TIM_CCxCmd(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerHardware->channel, TIM_CCx_Enable);
}
}
}
}
uint16_t getDshotTelemetry(uint8_t index)
{
return dmaMotors[index].dshotTelemetryValue;
}
#endif
void pwmCompleteDshotMotorUpdate(uint8_t motorCount)
{

View file

@ -26,6 +26,8 @@
#ifdef USE_DSHOT
#include "build/debug.h"
#include "drivers/dma.h"
#include "drivers/dma_reqmap.h"
#include "drivers/io.h"
@ -36,63 +38,86 @@
#include "pwm_output.h"
typedef DMA_Stream_TypeDef dmaStream_t;
// TODO remove once debugging no longer needed
#ifdef USE_DSHOT_TELEMETRY
#include <string.h>
#endif
static FAST_RAM_ZERO_INIT uint8_t dmaMotorTimerCount = 0;
static FAST_RAM_ZERO_INIT motorDmaTimer_t dmaMotorTimers[MAX_DMA_TIMERS];
static FAST_RAM_ZERO_INIT motorDmaOutput_t dmaMotors[MAX_SUPPORTED_MOTORS];
#include "pwm_output_dshot_shared.h"
motorDmaOutput_t *getMotorDmaOutput(uint8_t index)
#ifdef USE_DSHOT_TELEMETRY
static void processInputIrq(motorDmaOutput_t * const motor)
{
return &dmaMotors[index];
}
uint8_t getTimerIndex(TIM_TypeDef *timer)
{
for (int i = 0; i < dmaMotorTimerCount; i++) {
if (dmaMotorTimers[i].timer == timer) {
return i;
}
}
dmaMotorTimers[dmaMotorTimerCount++].timer = timer;
return dmaMotorTimerCount - 1;
}
FAST_CODE void pwmWriteDshotInt(uint8_t index, uint16_t value)
{
motorDmaOutput_t *const motor = &dmaMotors[index];
if (!motor->configured) {
return;
}
/*If there is a command ready to go overwrite the value and send that instead*/
if (pwmDshotCommandIsProcessing()) {
value = pwmGetDshotCommand(index);
if (value) {
motor->requestTelemetry = true;
}
}
motor->value = value;
uint16_t packet = prepareDshotPacket(motor);
uint8_t bufferSize;
motor->hasTelemetry = true;
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
bufferSize = loadDmaBuffer(&motor->timer->dmaBurstBuffer[timerLookupChannelIndex(motor->timerHardware->channel)], 4, packet);
motor->timer->dmaBurstLength = bufferSize * 4;
LL_EX_DMA_DisableStream(motor->timerHardware->dmaTimUPRef);
LL_TIM_DisableDMAReq_UPDATE(motor->timerHardware->tim);
} else
#endif
{
bufferSize = loadDmaBuffer(motor->dmaBuffer, 1, packet);
motor->timer->timerDmaSources |= motor->timerDmaSource;
LL_EX_DMA_SetDataLength(motor->dmaRef, bufferSize);
LL_EX_DMA_EnableStream(motor->dmaRef);
LL_EX_DMA_DisableStream(motor->dmaRef);
LL_EX_TIM_DisableIT(motor->timerHardware->tim, motor->timerDmaSource);
}
readDoneCount++;
}
static void enableChannels(uint8_t motorCount)
{
for (int i = 0; i < motorCount; i++) {
if (dmaMotors[i].output & TIMER_OUTPUT_N_CHANNEL) {
LL_EX_TIM_CC_EnableNChannel(dmaMotors[i].timerHardware->tim, dmaMotors[i].llChannel);
} else {
LL_TIM_CC_EnableChannel(dmaMotors[i].timerHardware->tim, dmaMotors[i].llChannel);
}
}
}
#endif
static void motor_DMA_IRQHandler(dmaChannelDescriptor_t *descriptor);
inline static void pwmDshotSetDirectionOutput(
motorDmaOutput_t * const motor, bool output
#ifndef USE_DSHOT_TELEMETRY
, LL_TIM_OC_InitTypeDef* pOcInit, LL_DMA_InitTypeDef* pDmaInit)
#endif
)
{
#ifdef USE_DSHOT_TELEMETRY
LL_TIM_OC_InitTypeDef* pOcInit = &motor->ocInitStruct;
LL_DMA_InitTypeDef* pDmaInit = &motor->dmaInitStruct;
#endif
const timerHardware_t * const timerHardware = motor->timerHardware;
TIM_TypeDef *timer = timerHardware->tim;
LL_EX_DMA_DeInit(motor->dmaRef);
#ifdef USE_DSHOT_TELEMETRY
motor->isInput = !output;
if (!output) {
LL_TIM_IC_Init(timer, motor->llChannel, &motor->icInitStruct);
motor->dmaInitStruct.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
} else
#else
UNUSED(output);
#endif
{
LL_TIM_OC_DisablePreload(timer, motor->llChannel);
LL_TIM_OC_Init(timer, motor->llChannel, pOcInit);
LL_TIM_OC_EnablePreload(timer, motor->llChannel);
motor->dmaInitStruct.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
}
LL_EX_DMA_Init(motor->dmaRef, pDmaInit);
LL_EX_DMA_EnableIT_TC(motor->dmaRef);
}
FAST_CODE void pwmCompleteDshotMotorUpdate(uint8_t motorCount)
{
UNUSED(motorCount);
@ -131,24 +156,50 @@ static void motor_DMA_IRQHandler(dmaChannelDescriptor_t* descriptor)
{
if (DMA_GET_FLAG_STATUS(descriptor, DMA_IT_TCIF)) {
motorDmaOutput_t * const motor = &dmaMotors[descriptor->userParam];
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
LL_EX_DMA_DisableStream(motor->timerHardware->dmaTimUPRef);
LL_TIM_DisableDMAReq_UPDATE(motor->timerHardware->tim);
#ifdef USE_DSHOT_TELEMETRY
uint32_t irqStart = micros();
if (motor->isInput) {
processInputIrq(motor);
} else
#endif
{
LL_EX_DMA_DisableStream(motor->dmaRef);
LL_EX_TIM_DisableIT(motor->timerHardware->tim, motor->timerDmaSource);
}
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
LL_EX_DMA_DisableStream(motor->timerHardware->dmaTimUPRef);
LL_TIM_DisableDMAReq_UPDATE(motor->timerHardware->tim);
} else
#endif
{
LL_EX_DMA_DisableStream(motor->dmaRef);
LL_EX_TIM_DisableIT(motor->timerHardware->tim, motor->timerDmaSource);
}
#ifdef USE_DSHOT_TELEMETRY
if (useDshotTelemetry) {
pwmDshotSetDirectionOutput(motor, false);
LL_EX_DMA_SetDataLength(motor->dmaRef, motor->dmaInputLen);
LL_EX_DMA_EnableStream(motor->dmaRef);
LL_EX_TIM_EnableIT(motor->timerHardware->tim, motor->timerDmaSource);
setDirectionMicros = micros() - irqStart;
}
#endif
}
DMA_CLEAR_FLAG(descriptor, DMA_IT_TCIF);
}
}
void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t motorIndex, motorPwmProtocolTypes_e pwmProtocolType, uint8_t output)
{
#ifdef USE_DSHOT_TELEMETRY
#define OCINIT motor->ocInitStruct
#define DMAINIT motor->dmaInitStruct
#else
LL_TIM_OC_InitTypeDef ocInitStruct;
LL_DMA_InitTypeDef dmaInitStruct;
#define OCINIT ocInitStruct
#define DMAINIT dmaInitStruct
#endif
#if defined(USE_DMA_SPEC)
const dmaChannelSpec_t *dmaSpec = dmaGetChannelSpecByTimer(timerHardware);
@ -173,10 +224,10 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
return;
}
LL_TIM_OC_InitTypeDef oc_init;
LL_DMA_InitTypeDef dma_init;
motorDmaOutput_t * const motor = &dmaMotors[motorIndex];
#ifdef USE_DSHOT_TELEMETRY
motor->useProshot = (pwmProtocolType == PWM_TYPE_PROSHOT1000);
#endif
motor->timerHardware = timerHardware;
TIM_TypeDef *timer = timerHardware->tim;
@ -185,7 +236,18 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
const uint8_t timerIndex = getTimerIndex(timer);
const bool configureTimer = (timerIndex == dmaMotorTimerCount - 1);
IOConfigGPIOAF(motorIO, IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLDOWN), timerHardware->alternateFunction);
uint8_t pupMode = 0;
#ifdef USE_DSHOT_TELEMETRY
if (!useDshotTelemetry) {
pupMode = (output & TIMER_OUTPUT_INVERTED) ? GPIO_PULLDOWN : GPIO_PULLUP;
} else
#endif
{
pupMode = (output & TIMER_OUTPUT_INVERTED) ? GPIO_PULLUP : GPIO_PULLDOWN;
}
IOConfigGPIOAF(motorIO, IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, pupMode), timerHardware->alternateFunction);
if (configureTimer) {
LL_TIM_InitTypeDef init;
@ -202,18 +264,25 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
LL_TIM_Init(timer, &init);
}
LL_TIM_OC_StructInit(&oc_init);
oc_init.OCMode = LL_TIM_OCMODE_PWM1;
LL_TIM_OC_StructInit(&OCINIT);
OCINIT.OCMode = LL_TIM_OCMODE_PWM1;
if (output & TIMER_OUTPUT_N_CHANNEL) {
oc_init.OCNState = LL_TIM_OCSTATE_ENABLE;
oc_init.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
oc_init.OCNPolarity = (output & TIMER_OUTPUT_INVERTED) ? LL_TIM_OCPOLARITY_LOW : LL_TIM_OCPOLARITY_HIGH;
OCINIT.OCNState = LL_TIM_OCSTATE_ENABLE;
OCINIT.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
OCINIT.OCNPolarity = (output & TIMER_OUTPUT_INVERTED) ? LL_TIM_OCPOLARITY_LOW : LL_TIM_OCPOLARITY_HIGH;
} else {
oc_init.OCState = LL_TIM_OCSTATE_ENABLE;
oc_init.OCIdleState = LL_TIM_OCIDLESTATE_HIGH;
oc_init.OCPolarity = (output & TIMER_OUTPUT_INVERTED) ? LL_TIM_OCPOLARITY_LOW : LL_TIM_OCPOLARITY_HIGH;
OCINIT.OCState = LL_TIM_OCSTATE_ENABLE;
OCINIT.OCIdleState = LL_TIM_OCIDLESTATE_HIGH;
OCINIT.OCPolarity = (output & TIMER_OUTPUT_INVERTED) ? LL_TIM_OCPOLARITY_LOW : LL_TIM_OCPOLARITY_HIGH;
}
oc_init.CompareValue = 0;
OCINIT.CompareValue = 0;
#ifdef USE_DSHOT_TELEMETRY
LL_TIM_IC_StructInit(&motor->icInitStruct);
motor->icInitStruct.ICPolarity = LL_TIM_IC_POLARITY_BOTHEDGE;
motor->icInitStruct.ICPrescaler = LL_TIM_ICPSC_DIV1;
motor->icInitStruct.ICFilter = 0; //2;
#endif
uint32_t channel;
switch (timerHardware->channel) {
@ -222,10 +291,82 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
case TIM_CHANNEL_3: channel = LL_TIM_CHANNEL_CH3; break;
case TIM_CHANNEL_4: channel = LL_TIM_CHANNEL_CH4; break;
}
LL_TIM_OC_Init(timer, channel, &oc_init);
motor->llChannel = channel;
motor->timer = &dmaMotorTimers[timerIndex];
motor->index = motorIndex;
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
motor->timer->dmaBurstRef = dmaRef;
#ifdef USE_DSHOT_TELEMETRY
motor->dmaRef = dmaRef;
#endif
} else
#endif
{
motor->timerDmaSource = timerDmaSource(timerHardware->channel);
motor->timer->timerDmaSources &= ~motor->timerDmaSource;
}
LL_EX_DMA_DisableStream(dmaRef);
LL_EX_DMA_DeInit(dmaRef);
LL_DMA_StructInit(&DMAINIT);
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
dmaInit(timerHardware->dmaTimUPIrqHandler, OWNER_TIMUP, timerGetTIMNumber(timerHardware->tim));
DMAINIT.Channel = dmaChannel;
DMAINIT.MemoryOrM2MDstAddress = (uint32_t)motor->timer->dmaBurstBuffer;
DMAINIT.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_FULL;
DMAINIT.PeriphOrM2MSrcAddress = (uint32_t)&timerHardware->tim->DMAR;
} else
#endif
{
dmaInit(dmaGetIdentifier(dmaRef), OWNER_MOTOR, RESOURCE_INDEX(motorIndex));
DMAINIT.Channel = dmaChannel;
DMAINIT.MemoryOrM2MDstAddress = (uint32_t)motor->dmaBuffer;
DMAINIT.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_4;
DMAINIT.PeriphOrM2MSrcAddress = (uint32_t)timerChCCR(timerHardware);
}
DMAINIT.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
DMAINIT.FIFOMode = LL_DMA_FIFOMODE_ENABLE;
DMAINIT.MemBurst = LL_DMA_MBURST_SINGLE;
DMAINIT.PeriphBurst = LL_DMA_PBURST_SINGLE;
DMAINIT.NbData = pwmProtocolType == PWM_TYPE_PROSHOT1000 ? PROSHOT_DMA_BUFFER_SIZE : DSHOT_DMA_BUFFER_SIZE;
DMAINIT.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
DMAINIT.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
DMAINIT.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
DMAINIT.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD;
DMAINIT.Mode = LL_DMA_MODE_NORMAL;
DMAINIT.Priority = LL_DMA_PRIORITY_HIGH;
LL_EX_DMA_Init(dmaRef, &DMAINIT);
LL_EX_DMA_EnableIT_TC(dmaRef);
motor->dmaRef = dmaRef;
#ifdef USE_DSHOT_TELEMETRY
motor->dmaInputLen = motor->useProshot ? PROSHOT_TELEMETRY_INPUT_LEN : DSHOT_TELEMETRY_INPUT_LEN;
pwmDshotSetDirectionOutput(motor, true);
#else
pwmDshotSetDirectionOutput(motor, true, &OCINIT, &DMAINIT);
#endif
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
dmaSetHandler(timerHardware->dmaTimUPIrqHandler, motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(2, 1), motor->index);
} else
#endif
{
dmaSetHandler(dmaGetIdentifier(dmaRef), motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(2, 1), motor->index);
}
LL_TIM_OC_Init(timer, channel, &OCINIT);
LL_TIM_OC_EnablePreload(timer, channel);
LL_TIM_OC_DisableFast(timer, channel);
LL_TIM_EnableCounter(timer);
if (output & TIMER_OUTPUT_N_CHANNEL) {
LL_EX_TIM_CC_EnableNChannel(timer, channel);
} else {
@ -237,63 +378,6 @@ void pwmDshotMotorHardwareConfig(const timerHardware_t *timerHardware, uint8_t m
LL_TIM_EnableARRPreload(timer);
LL_TIM_EnableCounter(timer);
}
motor->timer = &dmaMotorTimers[timerIndex];
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
motor->timer->dmaBurstRef = dmaRef;
if (!configureTimer) {
motor->configured = true;
return;
}
} else
#endif
{
motor->timerDmaSource = timerDmaSource(timerHardware->channel);
motor->timer->timerDmaSources &= ~motor->timerDmaSource;
}
LL_EX_DMA_DeInit(dmaRef);
LL_DMA_StructInit(&dma_init);
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
dmaInit(timerHardware->dmaTimUPIrqHandler, OWNER_TIMUP, timerGetTIMNumber(timerHardware->tim));
dmaSetHandler(timerHardware->dmaTimUPIrqHandler, motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(1, 2), motorIndex);
dma_init.Channel = timerHardware->dmaTimUPChannel;
dma_init.MemoryOrM2MDstAddress = (uint32_t)motor->timer->dmaBurstBuffer;
dma_init.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_FULL;
dma_init.PeriphOrM2MSrcAddress = (uint32_t)&timerHardware->tim->DMAR;
} else
#endif
{
dmaInit(dmaGetIdentifier(dmaRef), OWNER_MOTOR, RESOURCE_INDEX(motorIndex));
dmaSetHandler(dmaGetIdentifier(dmaRef), motor_DMA_IRQHandler, NVIC_BUILD_PRIORITY(1, 2), motorIndex);
dma_init.Channel = dmaChannel;
dma_init.MemoryOrM2MDstAddress = (uint32_t)motor->dmaBuffer;
dma_init.FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_4;
dma_init.PeriphOrM2MSrcAddress = (uint32_t)timerChCCR(timerHardware);
}
dma_init.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
dma_init.FIFOMode = LL_DMA_FIFOMODE_ENABLE;
dma_init.MemBurst = LL_DMA_MBURST_SINGLE;
dma_init.PeriphBurst = LL_DMA_PBURST_SINGLE;
dma_init.NbData = pwmProtocolType == PWM_TYPE_PROSHOT1000 ? PROSHOT_DMA_BUFFER_SIZE : DSHOT_DMA_BUFFER_SIZE;
dma_init.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
dma_init.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
dma_init.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD;
dma_init.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD;
dma_init.Mode = LL_DMA_MODE_NORMAL;
dma_init.Priority = LL_DMA_PRIORITY_HIGH;
LL_EX_DMA_Init(dmaRef, &dma_init);
LL_EX_DMA_EnableIT_TC(dmaRef);
motor->configured = true;
}
#endif

View file

@ -0,0 +1,227 @@
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_DSHOT
#if defined(STM32F4) || defined(STM32F7)
typedef DMA_Stream_TypeDef dmaStream_t;
#else
typedef DMA_Channel_TypeDef dmaStream_t;
#endif
FAST_RAM_ZERO_INIT static uint8_t dmaMotorTimerCount = 0;
#ifdef STM32F7
FAST_RAM_ZERO_INIT static motorDmaTimer_t dmaMotorTimers[MAX_DMA_TIMERS];
FAST_RAM_ZERO_INIT static motorDmaOutput_t dmaMotors[MAX_SUPPORTED_MOTORS];
#else
static motorDmaTimer_t dmaMotorTimers[MAX_DMA_TIMERS];
static motorDmaOutput_t dmaMotors[MAX_SUPPORTED_MOTORS];
#endif
#ifdef USE_DSHOT_TELEMETRY
uint32_t readDoneCount;
// TODO remove once debugging no longer needed
FAST_RAM_ZERO_INIT uint32_t dshotInvalidPacketCount;
FAST_RAM_ZERO_INIT uint32_t inputBuffer[DSHOT_TELEMETRY_INPUT_LEN];
FAST_RAM_ZERO_INIT uint32_t setDirectionMicros;
#endif
motorDmaOutput_t *getMotorDmaOutput(uint8_t index)
{
return &dmaMotors[index];
}
uint8_t getTimerIndex(TIM_TypeDef *timer)
{
for (int i = 0; i < dmaMotorTimerCount; i++) {
if (dmaMotorTimers[i].timer == timer) {
return i;
}
}
dmaMotorTimers[dmaMotorTimerCount++].timer = timer;
return dmaMotorTimerCount - 1;
}
FAST_CODE void pwmWriteDshotInt(uint8_t index, uint16_t value)
{
motorDmaOutput_t *const motor = &dmaMotors[index];
if (!motor->configured) {
return;
}
/*If there is a command ready to go overwrite the value and send that instead*/
if (pwmDshotCommandIsProcessing()) {
value = pwmGetDshotCommand(index);
if (value == DSHOT_CMD_SIGNAL_LINE_CONTINUOUS_ERPM_TELEMETRY) {
dshotInvalidPacketCount = 0;
readDoneCount = 0;
}
if (value) {
motor->requestTelemetry = true;
}
}
motor->value = value;
uint16_t packet = prepareDshotPacket(motor);
uint8_t bufferSize;
#ifdef USE_DSHOT_DMAR
if (useBurstDshot) {
bufferSize = loadDmaBuffer(&motor->timer->dmaBurstBuffer[timerLookupChannelIndex(motor->timerHardware->channel)], 4, packet);
motor->timer->dmaBurstLength = bufferSize * 4;
} else
#endif
{
bufferSize = loadDmaBuffer(motor->dmaBuffer, 1, packet);
motor->timer->timerDmaSources |= motor->timerDmaSource;
#ifdef STM32F7
LL_EX_DMA_SetDataLength(motor->dmaRef, bufferSize);
LL_EX_DMA_EnableStream(motor->dmaRef);
#else
DMA_SetCurrDataCounter(motor->dmaRef, bufferSize);
DMA_Cmd(motor->dmaRef, ENABLE);
#endif
}
}
#ifdef USE_DSHOT_TELEMETRY
static void enableChannels(uint8_t motorCount);
static uint16_t decodeDshotPacket(uint32_t buffer[])
{
uint32_t value = 0;
for (int i = 1; i < DSHOT_TELEMETRY_INPUT_LEN; i += 2) {
int diff = buffer[i] - buffer[i-1];
value <<= 1;
if (diff > 0) {
if (diff >= 11) value |= 1;
} else {
if (diff >= -9) value |= 1;
}
}
uint32_t csum = value;
csum = csum ^ (csum >> 8); // xor bytes
csum = csum ^ (csum >> 4); // xor nibbles
if (csum & 0xf) {
return 0xffff;
}
return value >> 4;
}
static uint16_t decodeProshotPacket(uint32_t buffer[])
{
uint32_t value = 0;
for (int i = 1; i < PROSHOT_TELEMETRY_INPUT_LEN; i += 2) {
const int proshotModulo = MOTOR_NIBBLE_LENGTH_PROSHOT;
int diff = ((buffer[i] + proshotModulo - buffer[i-1]) % proshotModulo) - PROSHOT_BASE_SYMBOL;
int nibble;
if (diff < 0) {
nibble = 0;
} else {
nibble = (diff + PROSHOT_BIT_WIDTH / 2) / PROSHOT_BIT_WIDTH;
}
value <<= 4;
value |= (nibble & 0xf);
}
uint32_t csum = value;
csum = csum ^ (csum >> 8); // xor bytes
csum = csum ^ (csum >> 4); // xor nibbles
if (csum & 0xf) {
return 0xffff;
}
return value >> 4;
}
#endif
#ifdef USE_DSHOT_TELEMETRY
uint16_t getDshotTelemetry(uint8_t index)
{
return dmaMotors[index].dshotTelemetryValue;
}
inline FAST_CODE static void pwmDshotSetDirectionOutput(
motorDmaOutput_t * const motor, bool output
#ifndef USE_DSHOT_TELEMETRY
#ifdef STM32F7
, LL_TIM_OC_InitTypeDef* pOcInit, LL_DMA_InitTypeDef* pDmaInit)
#else
, TIM_OCInitTypeDef *pOcInit, DMA_InitTypeDef* pDmaInit
#endif
#endif
);
void pwmStartDshotMotorUpdate(uint8_t motorCount)
{
if (useDshotTelemetry) {
for (int i = 0; i < motorCount; i++) {
if (dmaMotors[i].hasTelemetry) {
#ifdef STM32F7
uint32_t edges = LL_EX_DMA_GetDataLength(dmaMotors[i].dmaRef);
#else
uint32_t edges = DMA_GetCurrDataCounter(dmaMotors[i].dmaRef);
#endif
uint16_t value = 0xffff;
if (edges == 0) {
if (dmaMotors[i].useProshot) {
value = decodeProshotPacket(dmaMotors[i].dmaBuffer);
} else {
value = decodeDshotPacket(dmaMotors[i].dmaBuffer);
}
}
if (value != 0xffff) {
dmaMotors[i].dshotTelemetryValue = value;
if (i < 4) {
DEBUG_SET(DEBUG_DSHOT_RPM_TELEMETRY, i, value);
}
} else {
dshotInvalidPacketCount++;
if (i == 0) {
memcpy(inputBuffer,dmaMotors[i].dmaBuffer,sizeof(inputBuffer));
}
}
dmaMotors[i].hasTelemetry = false;
} else {
#ifdef STM32F7
LL_EX_TIM_DisableIT(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerDmaSource);
#else
TIM_DMACmd(dmaMotors[i].timerHardware->tim, dmaMotors[i].timerDmaSource, DISABLE);
#endif
}
pwmDshotSetDirectionOutput(&dmaMotors[i], true);
}
enableChannels(motorCount);
}
}
#endif
#endif

View file

@ -87,6 +87,13 @@ __STATIC_INLINE void LL_EX_DMA_SetDataLength(DMA_Stream_TypeDef* DMAx_Streamy, u
MODIFY_REG(DMAx_Streamy->NDTR, DMA_SxNDT, NbData);
}
__STATIC_INLINE uint32_t LL_EX_DMA_GetDataLength(DMA_Stream_TypeDef* DMAx_Streamy)
{
DMA_TypeDef *DMA = LL_EX_DMA_Stream_to_DMA(DMAx_Streamy);
const uint32_t Stream = LL_EX_DMA_Stream_to_Stream(DMAx_Streamy);
return LL_DMA_GetDataLength(DMA, Stream);
}
__STATIC_INLINE void LL_EX_TIM_EnableIT(TIM_TypeDef *TIMx, uint32_t Sources)
{
SET_BIT(TIMx->DIER, Sources);

View file

@ -53,15 +53,15 @@ typedef struct rpmNotchFilter_s
biquadFilter_t notch[XYZ_AXIS_COUNT][MAX_SUPPORTED_MOTORS][RPM_FILTER_MAXHARMONICS];
} rpmNotchFilter_t;
static float erpmToHz;
static float filteredMotorErpm[MAX_SUPPORTED_MOTORS];
static uint8_t numberFilters;
static uint8_t numberRpmNotchFilters;
static uint8_t filterUpdatesPerIteration;
static float pidLooptime;
static rpmNotchFilter_t filters[2];
static rpmNotchFilter_t* gyroFilter;
static rpmNotchFilter_t* dtermFilter;
FAST_RAM_ZERO_INIT static float erpmToHz;
FAST_RAM_ZERO_INIT static float filteredMotorErpm[MAX_SUPPORTED_MOTORS];
FAST_RAM_ZERO_INIT static uint8_t numberFilters;
FAST_RAM_ZERO_INIT static uint8_t numberRpmNotchFilters;
FAST_RAM_ZERO_INIT static uint8_t filterUpdatesPerIteration;
FAST_RAM_ZERO_INIT static float pidLooptime;
FAST_RAM_ZERO_INIT static rpmNotchFilter_t filters[2];
FAST_RAM_ZERO_INIT static rpmNotchFilter_t* gyroFilter;
FAST_RAM_ZERO_INIT static rpmNotchFilter_t* dtermFilter;
PG_REGISTER_WITH_RESET_FN(rpmFilterConfig_t, rpmFilterConfig, PG_RPM_FILTER_CONFIG, 3);
@ -154,14 +154,19 @@ float rpmFilterDterm(int axis, float value)
return applyFilter(dtermFilter, axis, value);
}
static float motorFrequency[MAX_SUPPORTED_MOTORS];
FAST_RAM_ZERO_INIT static float motorFrequency[MAX_SUPPORTED_MOTORS];
void rpmFilterUpdate()
FAST_CODE_NOINLINE void rpmFilterUpdate()
{
if (gyroFilter == NULL && dtermFilter == NULL) {
return;
}
FAST_RAM_ZERO_INIT static uint8_t motor;
FAST_RAM_ZERO_INIT static uint8_t harmonic;
FAST_RAM_ZERO_INIT static uint8_t filter;
FAST_RAM static rpmNotchFilter_t* currentFilter = &filters[0];
for (int motor = 0; motor < getMotorCount(); motor++) {
filteredMotorErpm[motor] = pt1FilterApply(&rpmFilters[motor], getDshotTelemetry(motor));
if (motor < 4) {
@ -169,13 +174,7 @@ void rpmFilterUpdate()
}
}
static uint8_t motor;
static uint8_t harmonic;
static uint8_t filter;
static rpmNotchFilter_t* currentFilter = &filters[0];
for (int i = 0; i < filterUpdatesPerIteration; i++) {
float frequency = constrainf(
(harmonic + 1) * motorFrequency[motor], currentFilter->minHz, currentFilter->maxHz);
biquadFilter_t* template = &currentFilter->notch[0][motor][harmonic];

View file

@ -30,7 +30,7 @@
const timerHardware_t timerHardware[USABLE_TIMER_CHANNEL_COUNT] = {
DEF_TIM(TIM9, CH2, PA3, TIM_USE_PPM, 0, 0), // PPM
DEF_TIM(TIM8, CH1, PC6, TIM_USE_MOTOR, 0, 0), // S1 DMA2_ST2 (XXX was DMA1_ST4)
DEF_TIM(TIM8, CH1, PC6, TIM_USE_MOTOR, 0, 1), // S1 DMA2_ST2 (XXX was DMA1_ST4)
DEF_TIM(TIM8, CH2, PC7, TIM_USE_MOTOR, 0, 1), // S2 DMA2_ST3
DEF_TIM(TIM8, CH3, PC8, TIM_USE_MOTOR, 0, 1), // S3 DMA2_ST4
DEF_TIM(TIM8, CH4, PC9, TIM_USE_MOTOR, 0, 0), // S4 DMA2_ST7

View file

@ -81,6 +81,8 @@
#define USE_ITCM_RAM
#define USE_FAST_RAM
#define USE_DSHOT
#define USE_DSHOT_TELEMETRY
#define USE_RPM_FILTER
#define I2C3_OVERCLOCK true
#define I2C4_OVERCLOCK true
#define USE_GYRO_DATA_ANALYSE