diff --git a/radio/src/targets/common/arm/stm32/extmodule_driver.cpp b/radio/src/targets/common/arm/stm32/extmodule_driver.cpp index daf9e5238..3119b7f3c 100644 --- a/radio/src/targets/common/arm/stm32/extmodule_driver.cpp +++ b/radio/src/targets/common/arm/stm32/extmodule_driver.cpp @@ -73,20 +73,8 @@ static void config_ppm_output(uint16_t ppm_delay, bool polarity) // AAR register defines duration of each pulse, it is // updated after every pulse in Update interrupt handler. - LL_TIM_OC_InitTypeDef ocInit; - LL_TIM_OC_StructInit(&ocInit); - - ocInit.OCMode = LL_TIM_OCMODE_PWM1; - ocInit.OCState = LL_TIM_OCSTATE_ENABLE; - - if (polarity) { - ocInit.OCPolarity = LL_TIM_OCPOLARITY_LOW; - } else { - ocInit.OCPolarity = LL_TIM_OCPOLARITY_HIGH; - } - - ocInit.CompareValue = ppm_delay * 2; - stm32_pulse_config_output(&extmoduleTimer, &ocInit); + stm32_pulse_config_output(&extmoduleTimer, !polarity, LL_TIM_OCMODE_PWM1, + ppm_delay * 2); } void extmodulePpmStart(uint16_t ppm_delay, bool polarity) @@ -114,28 +102,17 @@ void extmoduleSendNextFramePpm(void* pulses, uint16_t length, void extmodulePxx1PulsesStart() { EXTERNAL_MODULE_ON(); - - LL_TIM_OC_InitTypeDef ocInit; - LL_TIM_OC_StructInit(&ocInit); - - ocInit.OCMode = LL_TIM_OCMODE_PWM1; - ocInit.OCState = LL_TIM_OCSTATE_ENABLE; - ocInit.OCPolarity = LL_TIM_OCPOLARITY_LOW; - ocInit.CompareValue = 9 * 2; // 9 uS - - stm32_pulse_config_output(&extmoduleTimer, &ocInit); - + stm32_pulse_config_output(&extmoduleTimer, false, LL_TIM_OCMODE_PWM1, 9 * 2); stm32_pulse_init(&extmoduleTimer); } void extmoduleSendNextFramePxx1(const void* pulses, uint16_t length) { - if (!stm32_pulse_if_not_running_disable(&extmoduleTimer)) - return; + if (!stm32_pulse_if_not_running_disable(&extmoduleTimer)) return; // Start DMA request and re-enable timer - stm32_pulse_start_dma_req(&extmoduleTimer, pulses, length, - LL_TIM_OCMODE_PWM1, 9 * 2); + stm32_pulse_start_dma_req(&extmoduleTimer, pulses, length, LL_TIM_OCMODE_PWM1, + 9 * 2); } #endif @@ -144,16 +121,7 @@ void extmoduleSerialStart() { EXTERNAL_MODULE_ON(); stm32_pulse_init(&extmoduleTimer); - - LL_TIM_OC_InitTypeDef ocInit; - LL_TIM_OC_StructInit(&ocInit); - - ocInit.OCMode = LL_TIM_OCMODE_TOGGLE; - ocInit.OCState = LL_TIM_OCSTATE_ENABLE; - ocInit.OCPolarity = LL_TIM_OCPOLARITY_HIGH; - ocInit.CompareValue = 0; - - stm32_pulse_config_output(&extmoduleTimer, &ocInit); + stm32_pulse_config_output(&extmoduleTimer, true, LL_TIM_OCMODE_TOGGLE, 0); } void extmoduleSendNextFrameSoftSerial(const void* pulses, uint16_t length, bool polarity) diff --git a/radio/src/targets/common/arm/stm32/stm32_pulse_driver.cpp b/radio/src/targets/common/arm/stm32/stm32_pulse_driver.cpp index 6dbb5bde0..290f87352 100644 --- a/radio/src/targets/common/arm/stm32/stm32_pulse_driver.cpp +++ b/radio/src/targets/common/arm/stm32/stm32_pulse_driver.cpp @@ -67,9 +67,36 @@ void stm32_pulse_deinit(const stm32_pulse_timer_t* tim) LL_GPIO_Init(tim->GPIOx, &pinInit); } -void stm32_pulse_config_output(const stm32_pulse_timer_t* tim, LL_TIM_OC_InitTypeDef* ocInit) +void stm32_pulse_config_output(const stm32_pulse_timer_t* tim, bool polarity, + uint32_t ocmode, uint32_t cmp_val) { - LL_TIM_OC_Init(tim->TIMx, tim->TIM_Channel, ocInit); + LL_TIM_OC_InitTypeDef ocInit; + LL_TIM_OC_StructInit(&ocInit); + + ocInit.OCMode = ocmode; + ocInit.CompareValue = cmp_val; + + if (tim->TIM_Channel != LL_TIM_CHANNEL_CH1N) { + ocInit.OCState = LL_TIM_OCSTATE_ENABLE; + } else { + ocInit.OCNState = LL_TIM_OCSTATE_ENABLE; + polarity = !polarity; + } + + uint32_t ll_polarity; + if (polarity) { + ll_polarity = LL_TIM_OCPOLARITY_HIGH; + } else { + ll_polarity = LL_TIM_OCPOLARITY_LOW; + } + + if (tim->TIM_Channel != LL_TIM_CHANNEL_CH1N) { + ocInit.OCPolarity = ll_polarity; + } else { + ocInit.OCNPolarity = ll_polarity; + } + + LL_TIM_OC_Init(tim->TIMx, tim->TIM_Channel, &ocInit); LL_TIM_OC_EnablePreload(tim->TIMx, tim->TIM_Channel); if (IS_TIM_BREAK_INSTANCE(tim->TIMx)) { diff --git a/radio/src/targets/common/arm/stm32/stm32_pulse_driver.h b/radio/src/targets/common/arm/stm32/stm32_pulse_driver.h index cce089da8..299ee6fec 100644 --- a/radio/src/targets/common/arm/stm32/stm32_pulse_driver.h +++ b/radio/src/targets/common/arm/stm32/stm32_pulse_driver.h @@ -43,7 +43,10 @@ struct stm32_pulse_timer_t { void stm32_pulse_init(const stm32_pulse_timer_t* tim); void stm32_pulse_deinit(const stm32_pulse_timer_t* tim); -void stm32_pulse_config_output(const stm32_pulse_timer_t* tim, LL_TIM_OC_InitTypeDef* ocInit); + +void stm32_pulse_config_output(const stm32_pulse_timer_t* tim, bool polarity, + uint32_t ocmode, uint32_t cmp_val); + void stm32_pulse_set_polarity(const stm32_pulse_timer_t* tim, bool polarity); // return true if it could be disabled without interrupting a pulse train, false otherwise