1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-17 05:15:25 +03:00

Use striding loadDmaBuffer to avoid buffer copy

This commit is contained in:
jflyper 2017-12-23 02:52:01 +09:00
parent 809a7d6b41
commit 9f04705130
5 changed files with 17 additions and 21 deletions

View file

@ -137,20 +137,20 @@ static void pwmWriteDshot(uint8_t index, float value)
pwmWriteDshotInt(index, lrintf(value));
}
static uint8_t loadDmaBufferDshot(motorDmaOutput_t *const motor, uint16_t packet)
static uint8_t loadDmaBufferDshot(uint32_t *dmaBuffer, int stride, uint16_t packet)
{
for (int i = 0; i < 16; i++) {
motor->dmaBuffer[i] = (packet & 0x8000) ? MOTOR_BIT_1 : MOTOR_BIT_0; // MSB first
dmaBuffer[i * stride] = (packet & 0x8000) ? MOTOR_BIT_1 : MOTOR_BIT_0; // MSB first
packet <<= 1;
}
return DSHOT_DMA_BUFFER_SIZE;
}
static uint8_t loadDmaBufferProshot(motorDmaOutput_t *const motor, uint16_t packet)
static uint8_t loadDmaBufferProshot(uint32_t *dmaBuffer, int stride, uint16_t packet)
{
for (int i = 0; i < 4; i++) {
motor->dmaBuffer[i] = PROSHOT_BASE_SYMBOL + ((packet & 0xF000) >> 12) * PROSHOT_BIT_WIDTH; // Most significant nibble first
dmaBuffer[i * stride] = PROSHOT_BASE_SYMBOL + ((packet & 0xF000) >> 12) * PROSHOT_BIT_WIDTH; // Most significant nibble first
packet <<= 4; // Shift 4 bits
}

View file

@ -185,7 +185,7 @@ void pwmServoConfig(const struct timerHardware_s *timerHardware, uint8_t servoIn
bool isMotorProtocolDshot(void);
#ifdef USE_DSHOT
typedef uint8_t loadDmaBufferFn(motorDmaOutput_t *const motor, uint16_t packet); // function pointer used to encode a digital motor value into the DMA buffer representation
typedef uint8_t loadDmaBufferFn(uint32_t *dmaBuffer, int stride, uint16_t packet); // function pointer used to encode a digital motor value into the DMA buffer representation
uint16_t prepareDshotPacket(motorDmaOutput_t *const motor, uint16_t value);

View file

@ -71,19 +71,13 @@ void pwmWriteDshotInt(uint8_t index, uint16_t value)
#endif
uint16_t packet = prepareDshotPacket(motor, value);
// XXX Replace this with striding loader in the next refactor
uint8_t bufferSize = loadDmaBuffer(motor, packet);
uint8_t bufferSize;
#ifdef USE_DSHOT_DMAR
// Load channel data into burst buffer
uint8_t channelIndex = timerLookupChannelIndex(motor->timerHardware->channel);
// XXX This copy will be deleted once the striding loader is available
for (int i = 0; i < bufferSize; i++) {
motor->timer->dmaBurstBuffer[channelIndex + i * 4] = motor->dmaBuffer[i];
}
bufferSize = loadDmaBuffer(&motor->timer->dmaBurstBuffer[timerLookupChannelIndex(motor->timerHardware->channel)], 4, packet);
motor->timer->dmaBurstLength = bufferSize * 4;
#else
bufferSize = loadDmaBuffer(motor->dmaBuffer, 1, packet);
motor->timer->timerDmaSources |= motor->timerDmaSource;
DMA_SetCurrDataCounter(motor->timerHardware->dmaRef, bufferSize);
DMA_Cmd(motor->timerHardware->dmaRef, ENABLE);

View file

@ -64,19 +64,16 @@ void pwmWriteDshotInt(uint8_t index, uint16_t value)
uint16_t packet = prepareDshotPacket(motor, value);
uint8_t bufferSize = loadDmaBuffer(motor, packet);
uint8_t bufferSize;
#ifdef USE_DSHOT_DMAR
STATIC_ASSERT(TIM_CHANNEL_1==0, tim_channel_0_indexing);
uint8_t channel_index = motor->timerHardware->channel / TIM_CHANNEL_2;
// load channel data into burst buffer
for(int i = 0; i < bufferSize; i++) {
motor->timer->dmaBurstBuffer[channel_index + i * 4] = motor->dmaBuffer[i];
}
bufferSize = loadDmaBuffer(&motor->timer->dmaBurstBuffer[timerLookupChannelIndex(motor->timerHardware->channel)], 4, packet);
if(HAL_DMA_STATE_READY == motor->TimHandle.hdma[motor->timerDmaIndex]->State) {
HAL_DMA_Start_IT(motor->TimHandle.hdma[motor->timerDmaIndex], (uint32_t)motor->timer->dmaBurstBuffer, (uint32_t)&motor->TimHandle.Instance->DMAR, bufferSize * 4);
}
#else
bufferSize = loadDmaBuffer(motor->dmaBuffer, 1, packet);
if (DMA_SetCurrDataCounter(&motor->TimHandle, motor->timerHardware->channel, motor->dmaBuffer, bufferSize) != HAL_OK) {
/* DMA set error */
return;

View file

@ -272,6 +272,11 @@ static inline uint8_t lookupChannelIndex(const uint16_t channel)
return channel >> 2;
}
uint8_t timerLookupChannelIndex(const uint16_t channel)
{
return lookupChannelIndex(channel);
}
rccPeriphTag_t timerRCC(TIM_TypeDef *tim)
{
for (int i = 0; i < HARDWARE_TIMER_DEFINITION_COUNT; i++) {