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

Revise ESCSERIAL to work with unified targets

Remove dependencies on timer definition order and base on the pins assigned as motor resources.
This commit is contained in:
Bruce Luckcuck 2019-03-29 08:32:47 -04:00
parent ef831e64b1
commit 4caa20bab2
6 changed files with 47 additions and 41 deletions

View file

@ -3581,7 +3581,9 @@ static void cliEscPassthrough(char *cmdline)
pch = strtok_r(NULL, " ", &saveptr); pch = strtok_r(NULL, " ", &saveptr);
} }
escEnablePassthrough(cliPort, escIndex, mode); if (!escEnablePassthrough(cliPort, &motorConfig()->dev, escIndex, mode)) {
cliPrintErrorLinef("Error starting ESC connection");
}
} }
#endif #endif

View file

@ -660,16 +660,27 @@ static void resetBuffers(escSerial_t *escSerial)
escSerial->port.txBufferHead = 0; escSerial->port.txBufferHead = 0;
} }
static serialPort_t *openEscSerial(escSerialPortIndex_e portIndex, serialReceiveCallbackPtr callback, uint16_t output, uint32_t baud, portOptions_e options, uint8_t mode) static serialPort_t *openEscSerial(const motorDevConfig_t *motorConfig, escSerialPortIndex_e portIndex, serialReceiveCallbackPtr callback, uint16_t output, uint32_t baud, portOptions_e options, uint8_t mode)
{ {
escSerial_t *escSerial = &(escSerialPorts[portIndex]); escSerial_t *escSerial = &(escSerialPorts[portIndex]);
if (escSerialConfig()->ioTag == IO_TAG_NONE) {
return NULL;
}
if (mode != PROTOCOL_KISSALL) { if (mode != PROTOCOL_KISSALL) {
escSerial->rxTimerHardware = &(TIMER_HARDWARE[output]); const ioTag_t tag = motorConfig->ioTags[output];
const timerHardware_t *timerHardware = timerGetByTag(tag);
if (timerHardware == NULL) {
return NULL;
}
escSerial->rxTimerHardware = timerHardware;
// N-Channels can't be used as RX. // N-Channels can't be used as RX.
if (escSerial->rxTimerHardware->output & TIMER_OUTPUT_N_CHANNEL) { if (escSerial->rxTimerHardware->output & TIMER_OUTPUT_N_CHANNEL) {
return NULL; return NULL;
} }
#ifdef USE_HAL_DRIVER #ifdef USE_HAL_DRIVER
escSerial->rxTimerHandle = timerFindTimerHandle(escSerial->rxTimerHardware->tim); escSerial->rxTimerHandle = timerFindTimerHandle(escSerial->rxTimerHardware->tim);
#endif #endif
@ -677,6 +688,9 @@ static serialPort_t *openEscSerial(escSerialPortIndex_e portIndex, serialReceive
escSerial->mode = mode; escSerial->mode = mode;
escSerial->txTimerHardware = timerGetByTag(escSerialConfig()->ioTag); escSerial->txTimerHardware = timerGetByTag(escSerialConfig()->ioTag);
if (escSerial->txTimerHardware == NULL) {
return NULL;
}
#ifdef USE_HAL_DRIVER #ifdef USE_HAL_DRIVER
escSerial->txTimerHandle = timerFindTimerHandle(escSerial->txTimerHardware->tim); escSerial->txTimerHandle = timerFindTimerHandle(escSerial->txTimerHardware->tim);
@ -729,23 +743,21 @@ static serialPort_t *openEscSerial(escSerialPortIndex_e portIndex, serialReceive
memset(&escOutputs, 0, sizeof(escOutputs)); memset(&escOutputs, 0, sizeof(escOutputs));
pwmOutputPort_t *pwmMotors = pwmGetMotors(); pwmOutputPort_t *pwmMotors = pwmGetMotors();
for (volatile uint8_t i = 0; i < MAX_SUPPORTED_MOTORS; i++) { for (volatile uint8_t i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
if (pwmMotors[i].enabled) { if (pwmMotors[i].enabled && pwmMotors[i].io != IO_NONE) {
if (pwmMotors[i].io != IO_NONE) { const ioTag_t tag = motorConfig->ioTags[i];
for (volatile uint8_t j = 0; j < TIMER_CHANNEL_COUNT; j++) { if (tag != IO_TAG_NONE) {
if (pwmMotors[i].io == IOGetByTag(TIMER_HARDWARE[j].tag)) const timerHardware_t *timerHardware = timerGetByTag(tag);
{ if (timerHardware) {
escSerialOutputPortConfig(&TIMER_HARDWARE[j]); escSerialOutputPortConfig(timerHardware);
if (TIMER_HARDWARE[j].output & TIMER_OUTPUT_INVERTED) { escOutputs[escSerial->outputCount].io = pwmMotors[i].io;
if (timerHardware->output & TIMER_OUTPUT_INVERTED) {
escOutputs[escSerial->outputCount].inverted = 1; escOutputs[escSerial->outputCount].inverted = 1;
} }
break;
}
}
escOutputs[escSerial->outputCount].io = pwmMotors[i].io;
escSerial->outputCount++; escSerial->outputCount++;
} }
} }
} }
}
setTxSignalEsc(escSerial, ENABLE); setTxSignalEsc(escSerial, ENABLE);
serialTimerTxConfigBL(escSerial->txTimerHardware, portIndex, baud); serialTimerTxConfigBL(escSerial->txTimerHardware, portIndex, baud);
} }
@ -929,10 +941,10 @@ static bool processExitCommand(uint8_t c)
} }
void escEnablePassthrough(serialPort_t *escPassthroughPort, uint16_t output, uint8_t mode) bool escEnablePassthrough(serialPort_t *escPassthroughPort, const motorDevConfig_t *motorConfig, uint16_t escIndex, uint8_t mode)
{ {
bool exitEsc = false; bool exitEsc = false;
uint8_t motor_output = 0; uint8_t motor_output = escIndex;
LED0_OFF; LED0_OFF;
LED1_OFF; LED1_OFF;
//StopPwmAllMotors(); //StopPwmAllMotors();
@ -952,30 +964,16 @@ void escEnablePassthrough(serialPort_t *escPassthroughPort, uint16_t output, uin
break; break;
} }
if ((mode == PROTOCOL_KISS) && (output == 255)) { if ((mode == PROTOCOL_KISS) && (motor_output == 255)) {
motor_output = 255;
mode = PROTOCOL_KISSALL; mode = PROTOCOL_KISSALL;
} } else if (motor_output >= MAX_SUPPORTED_MOTORS) {
else { return false;
uint8_t first_output = 0;
for (unsigned i = 0; i < TIMER_CHANNEL_COUNT; i++) {
if (TIMER_HARDWARE[i].usageFlags & TIM_USE_MOTOR) {
first_output = i;
break;
}
} }
//doesn't work with messy timertable escPort = openEscSerial(motorConfig, ESCSERIAL1, NULL, motor_output, escBaudrate, 0, mode);
motor_output = first_output + output;
if (motor_output >= TIMER_CHANNEL_COUNT) {
return;
}
}
escPort = openEscSerial(ESCSERIAL1, NULL, motor_output, escBaudrate, 0, mode);
if (!escPort) { if (!escPort) {
return; return false;
} }
uint8_t ch; uint8_t ch;
@ -1007,7 +1005,7 @@ void escEnablePassthrough(serialPort_t *escPassthroughPort, uint16_t output, uin
serialWrite(escPassthroughPort, 0xF4); serialWrite(escPassthroughPort, 0xF4);
serialWrite(escPassthroughPort, 0xF4); serialWrite(escPassthroughPort, 0xF4);
closeEscSerial(ESCSERIAL1, mode); closeEscSerial(ESCSERIAL1, mode);
return; return true;
} }
if (mode==PROTOCOL_BLHELI) { if (mode==PROTOCOL_BLHELI) {
serialWrite(escPassthroughPort, ch); // blheli loopback serialWrite(escPassthroughPort, ch); // blheli loopback

View file

@ -20,6 +20,8 @@
#pragma once #pragma once
#include "drivers/pwm_output.h"
#define ESCSERIAL_BUFFER_SIZE 1024 #define ESCSERIAL_BUFFER_SIZE 1024
typedef enum { typedef enum {
@ -37,7 +39,7 @@ typedef enum {
} escProtocol_e; } escProtocol_e;
// serialPort API // serialPort API
void escEnablePassthrough(serialPort_t *escPassthroughPort, uint16_t output, uint8_t mode); bool escEnablePassthrough(serialPort_t *escPassthroughPort, const motorDevConfig_t *motorConfig, uint16_t escIndex, uint8_t mode);
typedef struct escSerialConfig_s { typedef struct escSerialConfig_s {
ioTag_t ioTag; ioTag_t ioTag;

View file

@ -190,7 +190,7 @@ uint8_t escPortIndex;
#ifdef USE_ESCSERIAL #ifdef USE_ESCSERIAL
static void mspEscPassthroughFn(serialPort_t *serialPort) static void mspEscPassthroughFn(serialPort_t *serialPort)
{ {
escEnablePassthrough(serialPort, escPortIndex, escMode); escEnablePassthrough(serialPort, &motorConfig()->dev, escPortIndex, escMode);
} }
#endif #endif

View file

@ -303,3 +303,8 @@
#undef USE_RANGEFINDER_UIB #undef USE_RANGEFINDER_UIB
#undef USE_RANGEFINDER_TF #undef USE_RANGEFINDER_TF
#endif #endif
// TODO: Remove this once HAL support is fixed for ESCSERIAL
#ifdef STM32F7
#undef USE_ESCSERIAL
#endif

View file

@ -95,8 +95,7 @@
#define USE_USB_DETECT #define USE_USB_DETECT
//TODO: Re-enable this after it's been fixed to work with unified targets #define USE_ESCSERIAL
//#define USE_ESCSERIAL
#define USE_ADC #define USE_ADC