feat(radio): OS compatibility layer (#5840)

This commit is contained in:
Raphael Coeffic
2025-05-03 19:07:49 +10:00
committed by GitHub
parent c996612b0a
commit ab2a19e153
92 changed files with 1740 additions and 896 deletions
+2 -3
View File
@@ -27,6 +27,7 @@
#include "flash_driver.h"
#include "hal/watchdog_driver.h"
#include "os/sleep.h"
#if defined(LIBOPENUI)
#include "libopenui.h"
@@ -104,9 +105,7 @@ void BootloaderFirmwareUpdate::flashFirmware(const char * filename, ProgressHand
if (f_eof(&file)) break;
#if defined(SIMU)
// add an artificial delay and check for simu quit
if (SIMU_SLEEP_OR_EXIT_MS(30))
break;
sleep_ms(30);
#endif
}
+13 -14
View File
@@ -23,6 +23,7 @@
#include "edgetx.h"
#include "frsky_firmware_update.h"
#include "debug.h"
#include "os/sleep.h"
#include "timers_driver.h"
#include "tasks/mixer_task.h"
@@ -118,11 +119,9 @@ bool FrskyDeviceFirmwareUpdate::readBuffer(uint8_t * buffer, uint8_t count, uint
while (index < count && elapsed < timeout) {
if (uart_drv->getByte(uart_ctx, &(buffer[index]))) {
++index;
}
else {
RTOS_WAIT_MS(1);
if (++elapsed == timeout)
return false;
} else {
sleep_ms(1);
if (++elapsed == timeout) return false;
}
}
@@ -131,7 +130,7 @@ bool FrskyDeviceFirmwareUpdate::readBuffer(uint8_t * buffer, uint8_t count, uint
const uint8_t * FrskyDeviceFirmwareUpdate::readFrame(uint32_t timeout)
{
RTOS_WAIT_MS(1);
sleep_ms(1);
uint8_t len = 0;
bool bytestuff = false;
@@ -142,7 +141,7 @@ const uint8_t * FrskyDeviceFirmwareUpdate::readFrame(uint32_t timeout)
uint8_t byte = 0;
while (uart_drv->getByte(uart_ctx, &byte) == 0) {
RTOS_WAIT_MS(1);
sleep_ms(1);
if (elapsed++ >= timeout) {
TRACE("timeout in frame (len=%d)",len);
return nullptr;
@@ -178,7 +177,7 @@ bool FrskyDeviceFirmwareUpdate::waitState(State newState, uint32_t timeout)
static uint8_t pass = 0;
if (++pass == 10) {
pass = 0;
RTOS_WAIT_MS(1);
sleep_ms(1);
}
return true;
#else
@@ -226,7 +225,7 @@ const char * FrskyDeviceFirmwareUpdate::sendPowerOn()
{
state = SPORT_POWERUP_REQ;
RTOS_WAIT_MS(50);
sleep_ms(50);
uart_drv->clearRxBuffer(uart_ctx);
for (int i=0; i<10; i++) {
@@ -242,7 +241,7 @@ const char * FrskyDeviceFirmwareUpdate::sendPowerOn()
const char * FrskyDeviceFirmwareUpdate::sendReqVersion()
{
RTOS_WAIT_MS(20);
sleep_ms(20);
uart_drv->clearRxBuffer(uart_ctx);
state = SPORT_VERSION_REQ;
@@ -354,7 +353,7 @@ const char *FrskyDeviceFirmwareUpdate::doFlashFirmware(
if (set_pwr) set_pwr(true);
// wait a bit for PWR to settle
RTOS_WAIT_MS(1);
sleep_ms(1);
// Special update method for X12S / X10 iXJT
if (module == INTERNAL_MODULE && port == ETX_MOD_PORT_UART && set_bootcmd != nullptr) {
@@ -408,7 +407,7 @@ const char *FrskyDeviceFirmwareUpdate::uploadFileToHorusXJT(
if (count == 0) {
uart_drv->sendByte(uart_ctx, 0xA1);
RTOS_WAIT_MS(50);
sleep_ms(50);
return nullptr;
}
@@ -453,7 +452,7 @@ const char *FrskyDeviceFirmwareUpdate::uploadFileNormal(
if (result)
return result;
RTOS_WAIT_MS(200);
sleep_ms(200);
uart_drv->clearRxBuffer(uart_ctx);
state = SPORT_DATA_TRANSFER;
@@ -519,7 +518,7 @@ const char *FrskyDeviceFirmwareUpdate::flashFirmware(
/* wait 2s off */
watchdogSuspend(1000 /*10s*/);
RTOS_WAIT_MS(2000);
sleep_ms(2000);
const char * result = doFlashFirmware(filename, progressHandler);
+9 -8
View File
@@ -19,6 +19,7 @@
* GNU General Public License for more details.
*/
#include "os/sleep.h"
#if !defined(DISABLE_MULTI_UPDATE)
#include "hal.h"
@@ -32,6 +33,7 @@
#include "timers_driver.h"
#include "hal/watchdog_driver.h"
#include "os/time.h"
#include <memory>
@@ -157,9 +159,9 @@ void MultiFirmwareUpdateDriver::deinit()
bool MultiFirmwareUpdateDriver::getRxByte(uint8_t & byte) const
{
uint32_t time = RTOS_GET_MS();
uint32_t time = time_get_ms();
while ((RTOS_GET_MS() - time) < 100) { // 100ms
while ((time_get_ms() - time) < 100) { // 100ms
if (getByte(byte)) {
#if defined(DEBUG_EXT_MODULE_FLASH)
TRACE("[RX] 0x%X", byte);
@@ -220,7 +222,7 @@ const char * MultiFirmwareUpdateDriver::waitForInitialSync()
// avoids sending STK_READ_SIGN with STK_OK
// in case the receiver is too slow changing
// to RX mode (half-duplex).
RTOS_WAIT_TICKS(1);
sleep_ms(1);
return nullptr;
}
@@ -258,7 +260,7 @@ const char * MultiFirmwareUpdateDriver::loadAddress(uint32_t offset) const
// avoids sending next page back-to-back with STK_OK
// in case the receiver is to slow changing to RX mode (half-duplex).
RTOS_WAIT_TICKS(1);
sleep_ms(1);
return nullptr;
}
@@ -310,8 +312,7 @@ const char* MultiFirmwareUpdateDriver::flashFirmware(
#if defined(SIMU)
for (uint16_t i = 0; i < 100; i++) {
progressHandler(label, STR_WRITING, i, 100);
if (SIMU_SLEEP_OR_EXIT_MS(30))
break;
sleep_ms(30);
}
return nullptr;
#endif
@@ -321,7 +322,7 @@ const char* MultiFirmwareUpdateDriver::flashFirmware(
/* wait 500ms for power on */
watchdogSuspend(500 /*5s*/);
RTOS_WAIT_MS(500);
sleep_ms(500);
result = waitForInitialSync();
if (result) {
@@ -553,7 +554,7 @@ bool MultiDeviceFirmwareUpdate::flashFirmware(const char * filename, ProgressHan
/* wait 2s off */
watchdogSuspend(500 /*5s*/);
RTOS_WAIT_MS(3000);
sleep_ms(3000);
MultiFirmwareUpdateDriver driver(module, type);
const char * result = driver.flashFirmware(&file, getBasename(filename), progressHandler);
+16 -52
View File
@@ -29,30 +29,20 @@
#include "gui_common.h"
#include "edgetx.h" // reusableBuffer
#include "os/time.h"
#include <algorithm>
#if !defined(SIMU)
static timer_handle_t _protoScanTimers[NUM_MODULES] = {TIMER_INITIALIZER};
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
struct ProtoScanTimer {
TimerHandle_t timer = nullptr;
StaticTimer_t timerBuffer;
};
static ProtoScanTimer _protoScanTimers[NUM_MODULES];
void MultiRfProtocols::timerCb(TimerHandle_t xTimer)
void MultiRfProtocols::timerCb(timer_handle_t* h)
{
uint8_t moduleIdx = (uint8_t)(uintptr_t)pvTimerGetTimerID(xTimer);
uint8_t moduleIdx = (uint8_t)(h - _protoScanTimers);
auto instance = MultiRfProtocols::instance(moduleIdx);
if (instance->scanState == ScanBegin || instance->scanState == Scanning) {
instance->fillBuiltinProtos();
}
}
#endif
MultiRfProtocols* MultiRfProtocols::_instance[MAX_MODULES] = {};
@@ -201,7 +191,7 @@ float MultiRfProtocols::getProgress() const
if (scanState == ScanStop) return 0.0;
if (scanState == ScanBegin) {
float t = (float)(RTOS_GET_MS() - scanStart) / (float)MULTI_PROTOLIST_START_TIMEOUT;
float t = (float)(time_get_ms() - scanStart) / (float)MULTI_PROTOLIST_START_TIMEOUT;
return t * WAIT_TIME_RATIO;
}
@@ -225,29 +215,17 @@ bool MultiRfProtocols::triggerScan()
scanState = ScanBegin;
currentProto = MULTI_INVALID_PROTO;
moduleState[moduleIdx].mode = MODULE_MODE_GET_HARDWARE_INFO;
scanStart = lastScan = RTOS_GET_MS();
scanStart = lastScan = time_get_ms();
#if !defined(SIMU)
auto scanTimer = &_protoScanTimers[moduleIdx];
if (!scanTimer->timer) {
scanTimer->timer = xTimerCreateStatic(
"MPM", MULTI_PROTOLIST_START_TIMEOUT / RTOS_MS_PER_TICK, pdTRUE,
(void*)moduleIdx, MultiRfProtocols::timerCb, &scanTimer->timerBuffer);
if (!timer_is_created(scanTimer)) {
timer_create(scanTimer, MultiRfProtocols::timerCb, "mpm",
MULTI_PROTOLIST_START_TIMEOUT, false);
timer_start(scanTimer);
} else {
if (xTimerChangePeriod(scanTimer->timer,
MULTI_PROTOLIST_START_TIMEOUT / RTOS_MS_PER_TICK,
0) != pdPASS) {
/* The timer period could not be reset. */
}
timer_set_period(scanTimer, MULTI_PROTOLIST_START_TIMEOUT);
}
if (scanTimer->timer) {
if( xTimerStart( scanTimer->timer, 0 ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
#endif
return true;
}
@@ -291,32 +269,18 @@ bool MultiRfProtocols::scanReply(const uint8_t* packet, uint8_t len)
}
currentProto++;
lastScan = RTOS_GET_MS();
lastScan = time_get_ms();
#if !defined(SIMU)
auto scanTimer = &_protoScanTimers[moduleIdx];
if (scanTimer->timer) {
if (xTimerChangePeriod(scanTimer->timer,
MULTI_PROTOLIST_TIMEOUT / RTOS_MS_PER_TICK,
0) != pdPASS) {
/* The timer period could not be reset. */
}
}
#endif
timer_set_period(scanTimer, MULTI_PROTOLIST_TIMEOUT);
return true;
} else {
scanState = ScanEnd;
setModuleMode(moduleIdx, MODULE_MODE_NORMAL);
#if !defined(SIMU)
auto scanTimer = &_protoScanTimers[moduleIdx];
if (scanTimer->timer) {
if (xTimerStop(scanTimer->timer, 0) != pdPASS) {
/* The timer period could not be stopped. */
}
}
#endif
timer_stop(scanTimer);
break;
}
} else {
@@ -328,7 +292,7 @@ bool MultiRfProtocols::scanReply(const uint8_t* packet, uint8_t len)
timeout = MULTI_PROTOLIST_START_TIMEOUT;
}
if (RTOS_GET_MS() - lastScan >= timeout) {
if (time_get_ms() - lastScan >= timeout) {
TRACE("proto scan timeout!");
scanState = ScanInvalid;
}
+3 -9
View File
@@ -22,17 +22,13 @@
#include "dataconstants.h"
#include "edgetx_types.h"
#include "os/timer.h"
#include <functional>
#include <string>
#include <vector>
#include <map>
#if !defined(SIMU)
// Forward declare FreeRTOS timer
struct tmrTimerControl;
typedef struct tmrTimerControl * TimerHandle_t;
#endif
class MultiRfProtocols
{
static MultiRfProtocols* _instance[NUM_MODULES];
@@ -51,9 +47,7 @@ class MultiRfProtocols
MultiRfProtocols(unsigned int moduleIdx);
void fillBuiltinProtos();
#if !defined(SIMU)
static void timerCb(TimerHandle_t xTimer);
#endif
static void timerCb(timer_handle_t* h);
public: