Merge pull request #5860 from mikeller/eliminate_inefficient_serial_runtime_calls

Eliminated inefficient serial function calls at runtime.
This commit is contained in:
jflyper 2018-05-12 09:56:24 +09:00 committed by GitHub
commit 58ea3a8180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 29 deletions

View File

@ -537,6 +537,7 @@ bool processRx(timeUs_t currentTimeUs)
{ {
static bool armedBeeperOn = false; static bool armedBeeperOn = false;
static bool airmodeIsActivated; static bool airmodeIsActivated;
static bool sharedPortTelemetryEnabled = false;
if (!calculateRxChannelsAndUpdateFailsafe(currentTimeUs)) { if (!calculateRxChannelsAndUpdateFailsafe(currentTimeUs)) {
return false; return false;
@ -788,14 +789,18 @@ bool processRx(timeUs_t currentTimeUs)
#ifdef USE_TELEMETRY #ifdef USE_TELEMETRY
if (feature(FEATURE_TELEMETRY)) { if (feature(FEATURE_TELEMETRY)) {
if ((!isModeActivationConditionPresent(BOXTELEMETRY) && ARMING_FLAG(ARMED)) || bool enableSharedPortTelemetry = (!isModeActivationConditionPresent(BOXTELEMETRY) && ARMING_FLAG(ARMED)) || (isModeActivationConditionPresent(BOXTELEMETRY) && IS_RC_MODE_ACTIVE(BOXTELEMETRY));
(isModeActivationConditionPresent(BOXTELEMETRY) && IS_RC_MODE_ACTIVE(BOXTELEMETRY))) { if (enableSharedPortTelemetry && !sharedPortTelemetryEnabled) {
mspSerialReleaseSharedTelemetryPorts(); mspSerialReleaseSharedTelemetryPorts();
} else { telemetryCheckState();
sharedPortTelemetryEnabled = true;
} else if (!enableSharedPortTelemetry && sharedPortTelemetryEnabled) {
// the telemetry state must be checked immediately so that shared serial ports are released. // the telemetry state must be checked immediately so that shared serial ports are released.
telemetryCheckState(); telemetryCheckState();
mspSerialAllocatePorts(); mspSerialAllocatePorts();
sharedPortTelemetryEnabled = false;
} }
} }
#endif #endif

View File

@ -202,8 +202,6 @@ static void taskCalculateAltitude(timeUs_t currentTimeUs)
#ifdef USE_TELEMETRY #ifdef USE_TELEMETRY
static void taskTelemetry(timeUs_t currentTimeUs) static void taskTelemetry(timeUs_t currentTimeUs)
{ {
telemetryCheckState();
if (!cliMode && feature(FEATURE_TELEMETRY)) { if (!cliMode && feature(FEATURE_TELEMETRY)) {
telemetryProcess(currentTimeUs); telemetryProcess(currentTimeUs);
} }

View File

@ -72,7 +72,7 @@ static bool rcdeviceIsCameraControlEnabled(void)
bool rcdeviceIsEnabled(void) bool rcdeviceIsEnabled(void)
{ {
return findSerialPortConfig(FUNCTION_RCDEVICE) != NULL; return camDevice->serialPort != NULL;
} }
static bool rcdeviceIs5KeyEnabled(void) static bool rcdeviceIs5KeyEnabled(void)

View File

@ -221,10 +221,6 @@ serialPortConfig_t *findNextSerialPortConfig(serialPortFunction_e function)
return NULL; return NULL;
} }
typedef struct findSharedSerialPortState_s {
uint8_t lastIndex;
} findSharedSerialPortState_t;
portSharing_e determinePortSharing(const serialPortConfig_t *portConfig, serialPortFunction_e function) portSharing_e determinePortSharing(const serialPortConfig_t *portConfig, serialPortFunction_e function)
{ {
if (!portConfig || (portConfig->functionMask & function) == 0) { if (!portConfig || (portConfig->functionMask & function) == 0) {
@ -238,19 +234,10 @@ bool isSerialPortShared(const serialPortConfig_t *portConfig, uint16_t functionM
return (portConfig) && (portConfig->functionMask & sharedWithFunction) && (portConfig->functionMask & functionMask); return (portConfig) && (portConfig->functionMask & sharedWithFunction) && (portConfig->functionMask & functionMask);
} }
static findSharedSerialPortState_t findSharedSerialPortState;
serialPort_t *findSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction) serialPort_t *findSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction)
{ {
memset(&findSharedSerialPortState, 0, sizeof(findSharedSerialPortState)); for (unsigned i = 0; i < SERIAL_PORT_COUNT; i++) {
const serialPortConfig_t *candidate = &serialConfig()->portConfigs[i];
return findNextSharedSerialPort(functionMask, sharedWithFunction);
}
serialPort_t *findNextSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction)
{
while (findSharedSerialPortState.lastIndex < SERIAL_PORT_COUNT) {
const serialPortConfig_t *candidate = &serialConfig()->portConfigs[findSharedSerialPortState.lastIndex++];
if (isSerialPortShared(candidate, functionMask, sharedWithFunction)) { if (isSerialPortShared(candidate, functionMask, sharedWithFunction)) {
const serialPortUsage_t *serialPortUsage = findSerialPortUsageByIdentifier(candidate->identifier); const serialPortUsage_t *serialPortUsage = findSerialPortUsageByIdentifier(candidate->identifier);

View File

@ -107,7 +107,6 @@ typedef struct serialPortUsage_s {
} serialPortUsage_t; } serialPortUsage_t;
serialPort_t *findSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction); serialPort_t *findSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction);
serialPort_t *findNextSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction);
// //
// configuration // configuration

View File

@ -41,11 +41,12 @@
static mspPort_t mspPorts[MAX_MSP_PORT_COUNT]; static mspPort_t mspPorts[MAX_MSP_PORT_COUNT];
static void resetMspPort(mspPort_t *mspPortToReset, serialPort_t *serialPort) static void resetMspPort(mspPort_t *mspPortToReset, serialPort_t *serialPort, bool sharedWithTelemetry)
{ {
memset(mspPortToReset, 0, sizeof(mspPort_t)); memset(mspPortToReset, 0, sizeof(mspPort_t));
mspPortToReset->port = serialPort; mspPortToReset->port = serialPort;
mspPortToReset->sharedWithTelemetry = sharedWithTelemetry;
} }
void mspSerialAllocatePorts(void) void mspSerialAllocatePorts(void)
@ -61,7 +62,8 @@ void mspSerialAllocatePorts(void)
serialPort_t *serialPort = openSerialPort(portConfig->identifier, FUNCTION_MSP, NULL, NULL, baudRates[portConfig->msp_baudrateIndex], MODE_RXTX, SERIAL_NOT_INVERTED); serialPort_t *serialPort = openSerialPort(portConfig->identifier, FUNCTION_MSP, NULL, NULL, baudRates[portConfig->msp_baudrateIndex], MODE_RXTX, SERIAL_NOT_INVERTED);
if (serialPort) { if (serialPort) {
resetMspPort(mspPort, serialPort); bool sharedWithTelemetry = isSerialPortShared(portConfig, FUNCTION_MSP, TELEMETRY_PORT_FUNCTIONS_MASK);
resetMspPort(mspPort, serialPort, sharedWithTelemetry);
portIndex++; portIndex++;
} }
@ -82,10 +84,12 @@ void mspSerialReleasePortIfAllocated(serialPort_t *serialPort)
#if defined(USE_TELEMETRY) #if defined(USE_TELEMETRY)
void mspSerialReleaseSharedTelemetryPorts(void) { void mspSerialReleaseSharedTelemetryPorts(void) {
serialPort_t *sharedPort = findSharedSerialPort(TELEMETRY_PORT_FUNCTIONS_MASK, FUNCTION_MSP); for (uint8_t portIndex = 0; portIndex < MAX_MSP_PORT_COUNT; portIndex++) {
while (sharedPort) { mspPort_t *candidateMspPort = &mspPorts[portIndex];
mspSerialReleasePortIfAllocated(sharedPort); if (candidateMspPort->sharedWithTelemetry) {
sharedPort = findNextSharedSerialPort(TELEMETRY_PORT_FUNCTIONS_MASK, FUNCTION_MSP); closeSerialPort(candidateMspPort->port);
memset(candidateMspPort, 0, sizeof(mspPort_t));
}
} }
} }
#endif #endif

View File

@ -108,6 +108,7 @@ typedef struct mspPort_s {
uint_fast16_t dataSize; uint_fast16_t dataSize;
uint8_t checksum1; uint8_t checksum1;
uint8_t checksum2; uint8_t checksum2;
bool sharedWithTelemetry;
} mspPort_t; } mspPort_t;
void mspSerialInit(void); void mspSerialInit(void);