chore: Introduce FreeRTOS Software timers

Introduce FreeRTOS Software timers
This commit is contained in:
Peter Feerick
2022-03-31 16:26:38 +10:00
committed by GitHub
31 changed files with 140 additions and 132 deletions
+3 -2
View File
@@ -42,7 +42,7 @@
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 0
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TIMERS 0
#define configUSE_TIMERS 1
#if !defined(DEBUG)
#define configMAX_TASK_NAME_LEN 4
@@ -65,7 +65,7 @@
/* Software timer definitions. */
#define configTIMER_TASK_PRIORITY ( 2 )
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 )
#define configTIMER_TASK_STACK_DEPTH ( 512 )
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
@@ -73,6 +73,7 @@ to exclude the API function. */
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_xTimerPendFunctionCall 1
#if defined(THREADSAFE_MALLOC)
#define INCLUDE_xTaskGetSchedulerState 1
-2
View File
@@ -1059,8 +1059,6 @@ static const etx_serial_init spIntmoduleSerialInitParams = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
static void spInternalModuleSetBaudRate(uint32_t baud)
-2
View File
@@ -47,8 +47,6 @@ typedef struct {
uint8_t word_length; // = ETX_WordLength_8;
uint8_t rx_enable; // = false;
void (*on_receive)(uint8_t data); // = nullptr;
void (*on_error)(); // = nullptr;
} etx_serial_init;
struct etx_serial_callbacks_t {
-2
View File
@@ -316,8 +316,6 @@ static const etx_serial_init serialInitParams = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
#endif
+5 -4
View File
@@ -22,10 +22,12 @@
#ifndef _IO_PXX2_H_
#define _IO_PXX2_H_
#include "../fifo.h"
#include "../pulses/pxx.h"
#include "fifo.h"
#include "pulses/pxx.h"
class ModuleFifo : public Fifo<uint8_t, PXX2_FRAME_MAXLENGTH> {
#include "board.h"
class ModuleFifo : public Fifo<uint8_t, INTMODULE_FIFO_SIZE> {
public:
bool getFrame(uint8_t * frame)
{
@@ -74,7 +76,6 @@ class ModuleFifo : public Fifo<uint8_t, PXX2_FRAME_MAXLENGTH> {
return ((crc >> 8) == crcLow) && ((crc & 0xFF) == crcHigh);
}
uint32_t errors;
};
#endif
+9 -17
View File
@@ -82,8 +82,6 @@ static const etx_serial_init serialInitParams = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
class MultiInternalUpdateDriver: public MultiFirmwareUpdateDriver
@@ -130,6 +128,7 @@ class MultiInternalUpdateDriver: public MultiFirmwareUpdateDriver
};
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
class MultiExternalUpdateDriver: public MultiFirmwareUpdateDriver
{
public:
@@ -143,15 +142,7 @@ class MultiExternalUpdateDriver: public MultiFirmwareUpdateDriver
void init(bool inverted) override
{
#if !defined(EXTMODULE_USART)
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = EXTMODULE_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(EXTMODULE_TX_GPIO, &GPIO_InitStructure);
#endif
extmoduleInitTxPin();
if (inverted)
telemetryPortInvertedInit(57600);
@@ -166,9 +157,7 @@ class MultiExternalUpdateDriver: public MultiFirmwareUpdateDriver
void sendByte(uint8_t byte) const override
{
#if defined(HARDWARE_EXTERNAL_MODULE)
extmoduleSendInvertedByte(byte);
#endif
}
void clear() const override
@@ -186,6 +175,7 @@ class MultiExternalUpdateDriver: public MultiFirmwareUpdateDriver
clear();
}
};
#endif
class MultiExtSportUpdateDriver: public MultiFirmwareUpdateDriver
{
@@ -618,14 +608,16 @@ bool MultiDeviceFirmwareUpdate::flashFirmware(const char * filename, ProgressHan
}
std::unique_ptr<MultiFirmwareUpdateDriver> driver;
if (module == EXTERNAL_MODULE)
driver.reset(new MultiExternalUpdateDriver());
#if defined(INTERNAL_MODULE_MULTI)
else if (module == INTERNAL_MODULE)
if (type == MULTI_TYPE_MULTIMODULE && module == INTERNAL_MODULE)
driver.reset(new MultiInternalUpdateDriver());
#endif
else if (type == MULTI_TYPE_ELRS)
#if defined(HARDWARE_EXTERNAL_MODULE)
if (type == MULTI_TYPE_MULTIMODULE && module == EXTERNAL_MODULE)
driver.reset(new MultiExternalUpdateDriver());
if (type == MULTI_TYPE_ELRS && module == EXTERNAL_MODULE)
driver.reset(new MultiExtSportUpdateDriver());
#endif
pausePulses();
-2
View File
@@ -45,8 +45,6 @@ const etx_serial_init afhds2SerialInitParams = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
static void* afhds2Init(uint8_t module)
-22
View File
@@ -111,31 +111,12 @@ static void crossfireSetupMixerScheduler(uint8_t module)
}
#if defined(INTERNAL_MODULE_CRSF)
static void intmoduleCRSF_rx(uint8_t data)
{
intmoduleFifo.push(data);
#if !defined(SIMU)
// wakeup mixer when rx buffer is quarter full (16 bytes)
if (intmoduleFifo.size() >= 16) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(mixerTaskId.rtos_handle, 0, eNoAction,
&xHigherPriorityTaskWoken);
// might effect a context switch on ISR exit
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
#endif
}
static const etx_serial_init intmoduleCrossfireInitParams = {
.baudrate = 0,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleCRSF_rx,
.on_error = nullptr,
};
static void* crossfireInitInternal(uint8_t module)
@@ -146,9 +127,6 @@ static void* crossfireInitInternal(uint8_t module)
etx_serial_init params(intmoduleCrossfireInitParams);
params.baudrate = INT_CROSSFIRE_BAUDRATE;
// wakeup mixer when rx buffer is quarter full (16 bytes)
params.on_receive = intmoduleCRSF_rx;
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&params);
INTERNAL_MODULE_ON();
-2
View File
@@ -60,8 +60,6 @@ etx_serial_init multiSerialInitParams = {
.stop_bits = ETX_StopBits_Two,
.word_length = ETX_WordLength_9,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
#endif
+30
View File
@@ -49,12 +49,42 @@
#include "pulses/afhds2.h"
#endif
#include <FreeRTOS.h>
#include <timers.h>
uint8_t s_pulses_paused = 0;
ModuleState moduleState[NUM_MODULES];
InternalModulePulsesData intmodulePulsesData __DMA;
ExternalModulePulsesData extmodulePulsesData __DMA;
TrainerPulsesData trainerPulsesData __DMA;
void startPulses()
{
telemetryStart();
s_pulses_paused = false;
#if defined(HARDWARE_INTERNAL_MODULE)
setupPulsesInternalModule();
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
setupPulsesExternalModule();
#endif
#if defined(HARDWARE_EXTRA_MODULE)
extramodulePpmStart();
#endif
}
void stopPulses()
{
telemetryStop();
s_pulses_paused = true;
for (uint8_t i = 0; i < NUM_MODULES; i++)
moduleState[i].protocol = PROTOCOL_CHANNELS_UNINITIALIZED;
}
void restartModule(uint8_t idx)
{
if (idx == INTERNAL_MODULE) {
+2 -23
View File
@@ -262,29 +262,8 @@ uint8_t actualAfhdsRunPower(int moduleIndex);
#endif
void extramodulePpmStart();
inline void startPulses()
{
s_pulses_paused = false;
#if defined(HARDWARE_INTERNAL_MODULE)
setupPulsesInternalModule();
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
setupPulsesExternalModule();
#endif
#if defined(HARDWARE_EXTRA_MODULE)
extramodulePpmStart();
#endif
}
inline void stopPulses()
{
s_pulses_paused = true;
for (uint8_t i = 0; i < NUM_MODULES; i++)
moduleState[i].protocol = PROTOCOL_CHANNELS_UNINITIALIZED;
}
void startPulses();
void stopPulses();
inline bool pulsesStarted()
{
-4
View File
@@ -231,8 +231,6 @@ static const etx_serial_init pxx1SerialInit = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = false,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
static void* pxx1InitInternal(uint8_t module)
@@ -293,8 +291,6 @@ static const etx_serial_init pxx1ExtSerialInit = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = false,
.on_receive = extmoduleFifoReceive,
.on_error = extmoduleFifoError,
};
static void* pxx1InitExternal(uint8_t module)
-4
View File
@@ -36,8 +36,6 @@ const etx_serial_init pxx2SerialInitParams = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
#endif
@@ -717,8 +715,6 @@ static const etx_serial_init pxx2ExtSerialInitParams = {
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = extmoduleFifoReceive,
.on_error = extmoduleFifoError,
};
static void* pxx2InitLowSpeed(uint8_t module)
-2
View File
@@ -290,8 +290,6 @@ void serialInit(uint8_t port_nr, int mode)
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = false,
.on_receive = nullptr,
.on_error = nullptr,
};
bool power_required = false;
@@ -52,7 +52,7 @@ static void* aux_serial_init(const SerialState* st, const etx_serial_init* param
{
stm32_usart_init(st->usart, params);
if (params->rx_enable && st->usart->rxDMA && !params->on_receive) {
if (params->rx_enable && st->usart->rxDMA) {
st->rxFifo->clear();
stm32_usart_init_rx_dma(st->usart, st->rxFifo->buffer(), st->rxFifo->size());
}
@@ -144,6 +144,17 @@ void extmoduleSendNextFrameSoftSerial(const void* pulses, uint16_t length, bool
stm32_pulse_start_dma_req(&extmoduleTimer, pulses, length, LL_TIM_OCMODE_TOGGLE, 0);
}
void extmoduleInitTxPin()
{
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = extmoduleTimer.GPIO_Pin;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
pinInit.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(extmoduleTimer.GPIOx, &pinInit);
}
// Delay based byte sending @ 57600 bps
void extmoduleSendInvertedByte(uint8_t byte)
{
@@ -41,4 +41,5 @@ void extmoduleSendNextFrameSoftSerial(const void* pulses, uint16_t length,
bool polarity = true);
// Bitbang serial
void extmoduleInitTxPin();
void extmoduleSendInvertedByte(uint8_t byte);
@@ -38,19 +38,12 @@ void extmoduleFifoReceive(uint8_t data)
extmoduleFifo.push(data);
}
void extmoduleFifoError()
{
extmoduleFifo.errors++;
}
static const etx_serial_init extmoduleSerialParams = {
.baudrate = 0,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = extmoduleFifoReceive,
.on_error = extmoduleFifoError,
};
static const LL_GPIO_InitTypeDef extmoduleUSART_PinDef = {
@@ -80,8 +73,8 @@ static void* extmoduleSerialStart(const etx_serial_init* params)
{
if (!params) return nullptr;
extmodule_driver.on_receive = params->on_receive;
extmodule_driver.on_error = params->on_error;
extmodule_driver.on_receive = extmoduleFifoReceive;
extmodule_driver.on_error = nullptr;
// UART config
stm32_usart_init(&extmoduleUSART, params);
@@ -23,9 +23,9 @@
#include "intmodule_serial_driver.h"
#include "stm32_usart_driver.h"
#include "board.h"
#include "fifo.h"
#include "io/frsky_pxx2.h"
ModuleFifo intmoduleFifo;
Fifo<uint8_t, INTMODULE_FIFO_SIZE> intmoduleFifo;
#if !defined(INTMODULE_DMA_STREAM)
static uint8_t * intmoduleTxBufferData;
@@ -54,16 +54,11 @@ static etx_serial_callbacks_t intmodule_driver = {
};
// TODO: move this somewhere else
void intmoduleFifoReceive(uint8_t data)
static void intmoduleFifoReceive(uint8_t data)
{
intmoduleFifo.push(data);
}
void intmoduleFifoError()
{
intmoduleFifo.errors++;
}
static const LL_GPIO_InitTypeDef intmoduleUSART_PinDef = {
.Pin = INTMODULE_TX_GPIO_PIN | INTMODULE_RX_GPIO_PIN,
.Mode = LL_GPIO_MODE_ALTERNATE,
@@ -110,8 +105,8 @@ void* intmoduleSerialStart(const etx_serial_init* params)
// - the UART seems to block when initialised with baudrate = 0
// init callbacks
intmodule_driver.on_receive = params->on_receive;
intmodule_driver.on_error = params->on_error;
intmodule_driver.on_receive = intmoduleFifoReceive;
intmodule_driver.on_error = nullptr;
stm32_usart_init(&intmoduleUSART, params);
return nullptr;
@@ -26,8 +26,4 @@
#define INTMODULE_USART_IRQ_PRIORITY 6
// Callbacks using intmoduleFifo
void intmoduleFifoReceive(uint8_t data);
void intmoduleFifoError();
extern const etx_serial_driver_t IntmoduleSerialDriver;
@@ -67,8 +67,7 @@ static void interrupt1ms()
#endif
// 5ms loop
if (pre_scale == 5 || pre_scale == 10)
{
if (pre_scale == 5 || pre_scale == 10) {
#if defined(HAPTIC)
DEBUG_TIMER_START(debugTimerHaptic);
HAPTIC_HEARTBEAT();
@@ -77,8 +76,7 @@ static void interrupt1ms()
}
// 10ms loop
if (pre_scale == 10)
{
if (pre_scale == 10) {
pre_scale = 0;
DEBUG_TIMER_START(debugTimerPer10ms);
DEBUG_TIMER_SAMPLE(debugTimerPer10msPeriod);
+1
View File
@@ -615,6 +615,7 @@ int32_t getVolume();
#define VOLUME_LEVEL_DEF 12
// Telemetry driver
#define INTMODULE_FIFO_SIZE 512
#define TELEMETRY_FIFO_SIZE 512
void telemetryPortInit(uint32_t baudrate, uint8_t mode);
void telemetryPortSetDirectionInput();
+1
View File
@@ -478,6 +478,7 @@ int32_t getVolume();
#define VOLUME_LEVEL_DEF 12
// Telemetry driver
#define INTMODULE_FIFO_SIZE 512
#define TELEMETRY_FIFO_SIZE 512
void telemetryPortInit(uint32_t baudrate, uint8_t mode);
void telemetryPortSetDirectionOutput();
+3 -1
View File
@@ -42,16 +42,18 @@ void extmodulePpmStart(unsigned short, bool) {}
void extmoduleSerialStart() {}
void extmodulePxx1SerialStart() {}
void extmodulePxx1PulsesStart() {}
void extmoduleInitTxPin() {}
void extmoduleSendInvertedByte(uint8_t) {}
void extmoduleSendNextFramePxx1(void const*, unsigned short) {}
void extmoduleSendNextFrameSoftSerial(void const*, unsigned short, bool) {}
void extmoduleSendNextFramePpm(void*, unsigned short, unsigned short, bool) {}
#if defined(TRAINER_GPIO)
void init_trainer_ppm() {}
void stop_trainer_ppm() {}
void init_trainer_capture() {}
void stop_trainer_capture() {}
#endif
void init_trainer_module_cppm() {}
void stop_trainer_module_cppm() {}
+4 -1
View File
@@ -52,7 +52,7 @@ GPIO_TypeDef gpioa, gpiob, gpioc, gpiod, gpioe, gpiof, gpiog, gpioh, gpioi, gpio
USART_TypeDef Usart0, Usart1, Usart2, Usart3, Usart4;
ADC_Common_TypeDef adc;
RTC_TypeDef rtc;
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) { }
void lcdCopy(void * dest, void * src);
FATFS g_FATFS_Obj;
@@ -802,3 +802,6 @@ struct TouchState getInternalTouchState()
return simTouchState;
}
#endif
void telemetryStart() {}
void telemetryStop() {}
+2
View File
@@ -976,6 +976,8 @@ void setTopBatteryValue(uint32_t volts);
extern Fifo<uint8_t, TELEMETRY_FIFO_SIZE> telemetryFifo;
#endif
#define INTMODULE_FIFO_SIZE 128
// Gyro driver
#define GYRO_VALUES_COUNT 6
#define GYRO_BUFFER_LENGTH (GYRO_VALUES_COUNT * sizeof(int16_t))
-6
View File
@@ -100,12 +100,6 @@ void execMixerFrequentActions()
#if defined(BLUETOOTH)
bluetooth.wakeup();
#endif
if (!s_pulses_paused) {
DEBUG_TIMER_START(debugTimerTelemetryWakeup);
telemetryWakeup();
DEBUG_TIMER_STOP(debugTimerTelemetryWakeup);
}
}
TASK_FUNCTION(mixerTask)
-8
View File
@@ -283,14 +283,6 @@ void sportProcessTelemetryPacket(uint16_t id, uint8_t subId, uint8_t instance,
uint32_t data, TelemetryUnit unit = UNIT_RAW);
void sportProcessTelemetryPacketWithoutCrc(uint8_t origin, const uint8_t *packet);
void telemetryWakeup();
void telemetryReset();
extern uint8_t telemetryProtocol;
void telemetryInit(uint8_t protocol);
void telemetryInterrupt10ms();
bool pushFrskyTelemetryData(uint8_t data); // returns true when end of frame detected
void processFrskyTelemetryData(uint8_t data);
+2
View File
@@ -29,6 +29,8 @@
#include "intmodule_serial_driver.h"
#endif
static_assert(PXX2_FRAME_MAXLENGTH <= INTMODULE_FIFO_SIZE);
void processGetHardwareInfoFrame(uint8_t module, const uint8_t * frame)
{
if (moduleState[module].mode != MODULE_MODE_GET_HARDWARE_INFO) {
+45
View File
@@ -31,6 +31,9 @@
#include "libopenui.h"
#endif
#include <FreeRTOS.h>
#include <timers.h>
uint8_t telemetryStreaming = 0;
uint8_t telemetryRxBuffer[TELEMETRY_RX_PACKET_SIZE]; // Receive buffer. 9 bytes (full packet), worst case 18 bytes with byte-stuffing (+1)
uint8_t telemetryRxBufferCount = 0;
@@ -106,6 +109,45 @@ void telemetryMirrorSend(uint8_t data)
}
}
#if !defined(SIMU)
static TimerHandle_t telemetryTimer = nullptr;
static StaticTimer_t telemetryTimerBuffer;
static void telemetryTimerCb(TimerHandle_t xTimer)
{
(void)xTimer;
if (!s_pulses_paused) {
DEBUG_TIMER_START(debugTimerTelemetryWakeup);
telemetryWakeup();
DEBUG_TIMER_STOP(debugTimerTelemetryWakeup);
}
}
void telemetryStart()
{
if (!telemetryTimer) {
telemetryTimer =
xTimerCreateStatic("Telem", 4 / RTOS_MS_PER_TICK, pdTRUE, (void*)0,
telemetryTimerCb, &telemetryTimerBuffer);
}
if (telemetryTimer) {
if( xTimerStart( telemetryTimer, 0 ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
}
void telemetryStop()
{
if (telemetryTimer) {
if( xTimerStop( telemetryTimer, 5 / RTOS_MS_PER_TICK ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
}
#endif
void processTelemetryData(uint8_t data)
{
#if defined(CROSSFIRE)
@@ -275,6 +317,9 @@ void telemetryWakeup()
{
uint8_t requiredTelemetryProtocol = modelTelemetryProtocol();
// TODO: needs to be moved to protocol/module init
// as-is, it implies only ONE telemetry protocol
// enabled at the same time
if (telemetryProtocol != requiredTelemetryProtocol) {
telemetryInit(requiredTelemetryProtocol);
}
+11
View File
@@ -87,6 +87,17 @@ void telemetrySetMirrorCb(void* ctx, void (*fct)(void*, uint8_t));
// Mirror telemetry byte
void telemetryMirrorSend(uint8_t data);
void telemetryWakeup();
void telemetryReset();
extern uint8_t telemetryProtocol;
void telemetryInit(uint8_t protocol);
void telemetryInterrupt10ms();
void telemetryStart();
void telemetryStop();
#define TELEMETRY_AVERAGE_COUNT 3
enum {