diff --git a/src/main/drivers/pwm_mapping.c b/src/main/drivers/pwm_mapping.c index 5ed17d7bb9..d631243790 100644 --- a/src/main/drivers/pwm_mapping.c +++ b/src/main/drivers/pwm_mapping.c @@ -42,6 +42,7 @@ #include "sensors/rangefinder.h" #include "io/serial.h" +#include "io/servo_sbus.h" enum { MAP_TO_NONE, @@ -442,15 +443,21 @@ static void pwmInitServos(timMotorServoHardware_t * timOutputs) return; } + // If mixer requests more servos than we have timer outputs - throw an error - if (servoCount > timOutputs->maxTimServoCount) { + uint16_t maxServos = timOutputs->maxTimServoCount; + if(servoConfig()->servo_protocol == SERVO_TYPE_SBUS_PWM) { + maxServos = MAX(SERVO_SBUS_MAX_SERVOS, timOutputs->maxTimServoCount); + } + + if (servoCount > maxServos) { pwmInitError = PWM_INIT_ERROR_NOT_ENOUGH_SERVO_OUTPUTS; LOG_ERROR(PWM, "Too many servos. Mixer requested %d, timer outputs %d", servoCount, timOutputs->maxTimServoCount); return; } // Configure individual servo outputs - for (int idx = 0; idx < servoCount; idx++) { + for (int idx = 0; idx < MIN(servoCount, timOutputs->maxTimServoCount); idx++) { const timerHardware_t *timHw = timOutputs->timServos[idx]; if (!pwmServoConfig(timHw, idx, servoConfig()->servoPwmRate, servoConfig()->servoCenterPulse, feature(FEATURE_PWM_OUTPUT_ENABLE))) { diff --git a/src/main/drivers/pwm_output.c b/src/main/drivers/pwm_output.c index faa9cd373d..619f4b95db 100644 --- a/src/main/drivers/pwm_output.c +++ b/src/main/drivers/pwm_output.c @@ -638,7 +638,7 @@ ioTag_t pwmGetMotorPinTag(int motorIndex) static void pwmServoWriteStandard(uint8_t index, uint16_t value) { - if (servos[index]) { + if (index < MAX_SERVOS && servos[index]) { *servos[index]->ccr = value; } } diff --git a/src/main/io/servo_sbus.h b/src/main/io/servo_sbus.h index 0dcc14ac8d..2def87b7bb 100644 --- a/src/main/io/servo_sbus.h +++ b/src/main/io/servo_sbus.h @@ -24,6 +24,8 @@ #pragma once +#define SERVO_SBUS_MAX_SERVOS 18 + bool sbusServoInitialize(void); void sbusServoUpdate(uint8_t index, uint16_t value); void sbusServoSendUpdate(void);