chore: New module/serial API (#3055)

* WiP: new module interface

* WiP: softserial timer DMA

* WiP: setup() and send() in one step

and remove methods specific to internal or external module

* fixes and extra port config

* horus targets

* module ports have moved

* T8 support (only internal module)

* fix nv14

* stupid GCC....

* disable BT

* remove nv14 telem driver

* remove old aux serial driver

* Move module buffers out of pulses.h

* Soft serial fixes and PXX1 support

* Fixed PPM module output

* Fixed CLI

* compilation fixes

* Ported bluetooth and flysky gimbal to new serial driver

* Re-enable soft-serial RX

* fixes and cleanup

* nv14 fixes

* Fix flysky gimbals

* Ghost module

* 16 bytes of RAM

* Remove getByte from module API

* Ghost cleanup

* fix: _half_duplex_input() must be called after usart init

* WiP

* more cleanup

* Remove board specific settings from PXX1 / PXX2 drivers

* A bit of PXX2 header cleanup

* Add support for OVER8 and ONEBIT

* Add some support for inverters (NV14 / EL18)

* Support NV14 power fixes in generic board

* Fix polarity handling

* Fix RX DMA

* Fix mixer not running

* Add soft-serial polarity control

* Quick fix for telemetry polling while module port is changed

* Fix bootloader DEBUG

* Fix MPM telemetry

* Fix pulse start

* DSM2 soft-serial fallback

* DSMP async module restart

* Fix CRSF LUA upstream

* Remove global variable telemetryProtocol

* Use HAL/LL constants

* Fix various issue with AFHDS3 and NV14/EL18 internal module

* Fixed S.PORT unit tests

* fix: update widx after writing at current index

* fix: init error condition

* Fix: possible infinite loop in _find_port()

* Add support for bidir S.PORT init

* Add support for "getLastByte" in HW serial driver

* PXX1: Add support for S.PORT based on IDLE callback

* Fix rebase error

* Fix softserial _rx_fifo_push()

* Add softserial support for waitForTxCompleted()

* Fix MPM flashing (internal / external)

* Fixed external MPM

* Activate RX DMA on internal module

Seems to work with serial passthrough without issues, at least on the L3R PRO.

* Fix softserial DMA on stdlcd F4 targets

* Add softserial support for 32bit timers (mostly TIM2)

* More precise soft-serial

* Fix MPM update using inverters & HW uart

* Raise S.PORT DMA/USART IRQ prio to achieve faster switching to input

* Remove useless delays

* Fix flashing external ELRS module

* Added a retry mechanism for FrSky S.PORT update

* Remove useless enableRx()

* External FrSky modules are all flashed via S.PORT

* Remove power management unit update

This was only used on the first batch of X-lite.
This commit is contained in:
Raphael Coeffic
2023-02-09 19:44:32 +10:00
committed by GitHub
parent d80adc6c4d
commit 72193eda82
189 changed files with 7960 additions and 8364 deletions
@@ -20,8 +20,20 @@
*/
#include "stm32_pulse_driver.h"
#include "stm32_dma.h"
#include "definitions.h"
#include <string.h>
// 0.5uS (2Mhz)
#define STM32_DEFAULT_TIMER_FREQ 2000000
#define STM32_DEFAULT_TIMER_AUTORELOAD 65535
// TODO:
// - DMA IRQ prio configurable (now 7)
// - Timer IRQ prio configurable (now 7)
static void enable_tim_clock(TIM_TypeDef* TIMx)
{
if (TIMx == TIM1) {
@@ -37,8 +49,12 @@ static void enable_tim_clock(TIM_TypeDef* TIMx)
}
}
void stm32_pulse_init(const stm32_pulse_timer_t* tim)
void stm32_pulse_init(const stm32_pulse_timer_t* tim, uint32_t freq)
{
if (tim->DMA_TC_CallbackPtr) {
memset(tim->DMA_TC_CallbackPtr, 0, sizeof(stm32_pulse_dma_tc_cb_t));
}
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = tim->GPIO_Pin;
@@ -49,9 +65,12 @@ void stm32_pulse_init(const stm32_pulse_timer_t* tim)
LL_TIM_InitTypeDef timInit;
LL_TIM_StructInit(&timInit);
// 0.5uS (2Mhz)
timInit.Prescaler = tim->TIM_Prescaler;
timInit.Autoreload = 65535;
if (!freq) {
freq = STM32_DEFAULT_TIMER_FREQ;
}
timInit.Prescaler = __LL_TIM_CALC_PSC(tim->TIM_Freq, freq);
timInit.Autoreload = STM32_DEFAULT_TIMER_AUTORELOAD;
enable_tim_clock(tim->TIMx);
LL_TIM_Init(tim->TIMx, &timInit);
@@ -93,6 +112,11 @@ void stm32_pulse_deinit(const stm32_pulse_timer_t* tim)
if (tim->DMAx) {
LL_DMA_DeInit(tim->DMAx, tim->DMA_Stream);
}
if (tim->DMA_TC_CallbackPtr) {
memset(tim->DMA_TC_CallbackPtr, 0, sizeof(stm32_pulse_dma_tc_cb_t));
}
LL_TIM_DeInit(tim->TIMx);
disable_tim_clock(tim->TIMx);
@@ -144,7 +168,6 @@ void stm32_pulse_config_output(const stm32_pulse_timer_t* tim, bool polarity,
LL_TIM_EnableAllOutputs(tim->TIMx);
}
LL_TIM_EnableDMAReq_UPDATE(tim->TIMx);
}
void stm32_pulse_set_polarity(const stm32_pulse_timer_t* tim, bool polarity)
@@ -158,6 +181,12 @@ void stm32_pulse_set_polarity(const stm32_pulse_timer_t* tim, bool polarity)
LL_TIM_OC_SetPolarity(tim->TIMx, tim->TIM_Channel, ll_polarity);
}
bool stm32_pulse_get_polarity(const stm32_pulse_timer_t* tim)
{
return LL_TIM_OC_GetPolarity(tim->TIMx, tim->TIM_Channel) ==
LL_TIM_OCPOLARITY_HIGH;
}
// return true if stopped, false otherwise
bool stm32_pulse_if_not_running_disable(const stm32_pulse_timer_t* tim)
{
@@ -204,6 +233,16 @@ static void set_oc_mode(const stm32_pulse_timer_t* tim, uint32_t ocmode)
LL_TIM_OC_SetMode(tim->TIMx, channel, ocmode);
}
void stm32_pulse_wait_for_completed(const stm32_pulse_timer_t* tim)
{
uint32_t channel = tim->TIM_Channel;
if (channel == LL_TIM_CHANNEL_CH1N)
channel = LL_TIM_CHANNEL_CH1;
while(LL_TIM_IsEnabledCounter(tim->TIMx) &&
LL_TIM_OC_GetMode(tim->TIMx, channel) != LL_TIM_OCMODE_FORCED_INACTIVE);
}
static void force_start_level(const stm32_pulse_timer_t* tim)
{
uint32_t channel = tim->TIM_Channel;
@@ -259,48 +298,43 @@ void stm32_pulse_start_dma_req(const stm32_pulse_timer_t* tim,
// Enable TC IRQ
LL_DMA_EnableIT_TC(tim->DMAx, tim->DMA_Stream);
// Enable DMA
LL_TIM_ClearFlag_UPDATE(tim->TIMx);
LL_TIM_DisableDMAReq_UPDATE(tim->TIMx);
LL_TIM_SetCounter(tim->TIMx, 0);
// Reset counter close to overflow
if (IS_TIM_32B_COUNTER_INSTANCE(tim->TIMx)) {
LL_TIM_SetCounter(tim->TIMx, 0xFFFFFFFF);
} else {
LL_TIM_SetCounter(tim->TIMx, 0xFFFF);
}
// only on PWM (preloads the first period)
if (ocmode == LL_TIM_OCMODE_PWM1)
LL_TIM_GenerateEvent_UPDATE(tim->TIMx);
LL_TIM_EnableDMAReq_UPDATE(tim->TIMx);
LL_DMA_EnableStream(tim->DMAx, tim->DMA_Stream);
// Trigger update to effect the first DMA transation
// Trigger update to effect the first DMA transaction
// and thus load ARR with the first duration
LL_TIM_EnableDMAReq_UPDATE(tim->TIMx);
LL_TIM_GenerateEvent_UPDATE(tim->TIMx);
// start timer
force_start_level(tim);
LL_TIM_EnableCounter(tim->TIMx);
}
static bool check_and_clean_dma_tc_flag(const stm32_pulse_timer_t* tim)
{
switch(tim->DMA_Stream) {
case LL_DMA_STREAM_1:
if (!LL_DMA_IsActiveFlag_TC1(tim->DMAx)) return false;
LL_DMA_ClearFlag_TC1(tim->DMAx);
break;
case LL_DMA_STREAM_5:
if (!LL_DMA_IsActiveFlag_TC5(tim->DMAx)) return false;
LL_DMA_ClearFlag_TC5(tim->DMAx);
break;
case LL_DMA_STREAM_7:
if (!LL_DMA_IsActiveFlag_TC7(tim->DMAx)) return false;
LL_DMA_ClearFlag_TC7(tim->DMAx);
break;
}
return true;
}
void stm32_pulse_dma_tc_isr(const stm32_pulse_timer_t* tim)
{
if (!check_and_clean_dma_tc_flag(tim)) return;
if (!stm32_dma_check_tc_flag(tim->DMAx, tim->DMA_Stream))
return;
if (tim->DMA_TC_CallbackPtr) {
auto closure = tim->DMA_TC_CallbackPtr;
if (closure->cb && closure->cb(closure->ctx)) {
return;
}
}
LL_TIM_ClearFlag_UPDATE(tim->TIMx);
LL_TIM_EnableIT_UPDATE(tim->TIMx);
// PWM mode with compare value = 0 then OCxRef is held at ‘0’
set_compare_reg(tim, 0);
set_oc_mode(tim, LL_TIM_OCMODE_PWM1);
}
@@ -315,6 +349,7 @@ void stm32_pulse_tim_update_isr(const stm32_pulse_timer_t* tim)
// Halt pulses by forcing to inactive level
set_oc_mode(tim, LL_TIM_OCMODE_FORCED_INACTIVE);
LL_TIM_DisableCounter(tim->TIMx);
}
// input mode