Skip to content

feat(radio): OS compatibility layer #5840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,16 @@ if(NATIVE_BUILD)
${RADIOLIB_NATIVE_SRC}
)

# Add -DSIMU here PUBLIC, so it can be imported by other targets
# Add SIMU and POSIX_THREAD here PUBLIC, so it can be imported by other targets
# via INTERFACE_COMPILE_OPTIONS
target_compile_options(radiolib_native PUBLIC -DSIMU)
target_compile_options(radiolib_native PUBLIC -DSIMU -DPOSIX_THREADS)
target_sources(radiolib_native PUBLIC
os/async_native.cpp
os/sleep_native.cpp
os/task_pthread.cpp
os/time_native.cpp
os/timer_pthread.cpp
)
add_dependencies(radiolib_native ${RADIO_DEPENDENCIES})
set_property(TARGET radiolib_native PROPERTY POSITION_INDEPENDENT_CODE ON)

Expand Down Expand Up @@ -554,13 +561,23 @@ if(BOOTLOADER)
set(RADIO_DEPENDENCIES ${RADIO_DEPENDENCIES} bootloader)
endif()

add_definitions(-DFREE_RTOS)

add_executable(firmware ${SRC})
link_libraries(firmware -lstdc++)
add_dependencies(firmware ${RADIO_DEPENDENCIES})
set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL TRUE)

target_compile_definitions(board PUBLIC FREE_RTOS)
target_compile_definitions(board_lib PUBLIC FREE_RTOS)
target_compile_definitions(firmware PUBLIC FREE_RTOS)

target_sources(firmware PUBLIC
os/async_freertos.cpp
os/sleep_freertos.cpp
os/task_freertos.cpp
os/time_freertos.cpp
os/timer_freertos.cpp
)

if(DEBUG)
target_compile_definitions(board PRIVATE -DDEBUG)
target_compile_definitions(firmware PRIVATE -DDEBUG)
Expand Down
51 changes: 16 additions & 35 deletions radio/src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

#include <math.h>

#include "os/sleep.h"
#include "os/task.h"

#include "edgetx.h"
#include "strhelpers.h"
#include "switches.h"
Expand All @@ -32,7 +35,7 @@
#include "model_audio.h"
#include "hal/audio_driver.h"

extern RTOS_MUTEX_HANDLE audioMutex;
extern mutex_handle_t audioMutex;

// Only first quadrant values - other quadrants calulated taking advantage of symmetry in sine wave.
const int16_t sineValues[] =
Expand Down Expand Up @@ -373,28 +376,6 @@ AudioQueue::AudioQueue()

#define CODEC_ID_PCM_S16LE 1

#if !defined(SIMU)
void audioTask(void * pdata)
{
while (!audioQueue.started()) {
RTOS_WAIT_TICKS(1);
}

#if defined(PCBX12S) || defined(RADIO_TX16S) || defined(RADIO_F16) || defined(RADIO_V16)
// The audio amp needs ~2s to start
RTOS_WAIT_MS(1000); // 1s
#endif

while (true) {
DEBUG_TIMER_SAMPLE(debugTimerAudioIterval);
DEBUG_TIMER_START(debugTimerAudioDuration);
audioQueue.wakeup();
DEBUG_TIMER_STOP(debugTimerAudioDuration);
RTOS_WAIT_MS(4); // ???
}
}
#endif

#if !defined(__SSAT)
#define _sat_s16(x) ((int16_t)limit<int32_t>(INT16_MIN, (x), INT16_MAX))
#else
Expand Down Expand Up @@ -638,9 +619,9 @@ void AudioQueue::wakeup()

// mix the normal context (tones and wavs)
if (normalContext.isEmpty() && !fragmentsFifo.empty()) {
RTOS_LOCK_MUTEX(audioMutex);
mutex_lock(&audioMutex);
normalContext.setFragment(fragmentsFifo.get());
RTOS_UNLOCK_MUTEX(audioMutex);
mutex_unlock(&audioMutex);
}
result = normalContext.mixBuffer(buffer, g_eeGeneral.beepVolume, g_eeGeneral.wavVolume, fade);
if (result > 0) {
Expand Down Expand Up @@ -724,7 +705,7 @@ void AudioQueue::playTone(uint16_t freq, uint16_t len, uint16_t pause, uint8_t f
return;
#endif

RTOS_LOCK_MUTEX(audioMutex);
mutex_lock(&audioMutex);

freq = limit<uint16_t>(BEEP_MIN_FREQ, freq, BEEP_MAX_FREQ);

Expand All @@ -747,7 +728,7 @@ void AudioQueue::playTone(uint16_t freq, uint16_t len, uint16_t pause, uint8_t f
}
}

RTOS_UNLOCK_MUTEX(audioMutex);
mutex_unlock(&audioMutex);
}

void AudioQueue::playFile(const char * filename, uint8_t flags, uint8_t id, int8_t fragmentVolume)
Expand All @@ -774,7 +755,7 @@ void AudioQueue::playFile(const char * filename, uint8_t flags, uint8_t id, int8
return;
}

RTOS_LOCK_MUTEX(audioMutex);
mutex_lock(&audioMutex);

if (flags & PLAY_BACKGROUND) {
backgroundContext.clear();
Expand All @@ -784,7 +765,7 @@ void AudioQueue::playFile(const char * filename, uint8_t flags, uint8_t id, int8
fragmentsFifo.push(AudioFragment(filename, flags & 0x0f, fragmentVolume, id));
}

RTOS_UNLOCK_MUTEX(audioMutex);
mutex_unlock(&audioMutex);
}

void AudioQueue::stopPlay(uint8_t id)
Expand All @@ -797,12 +778,12 @@ void AudioQueue::stopPlay(uint8_t id)
return;
#endif

RTOS_LOCK_MUTEX(audioMutex);
mutex_lock(&audioMutex);

fragmentsFifo.removePromptById(id);
backgroundContext.stop(id);

RTOS_UNLOCK_MUTEX(audioMutex);
mutex_unlock(&audioMutex);
}

void AudioQueue::stopSD()
Expand All @@ -815,19 +796,19 @@ void AudioQueue::stopSD()
void AudioQueue::stopAll()
{
flush();
RTOS_LOCK_MUTEX(audioMutex);
mutex_lock(&audioMutex);
priorityContext.clear();
normalContext.clear();
RTOS_UNLOCK_MUTEX(audioMutex);
mutex_unlock(&audioMutex);
}

void AudioQueue::flush()
{
RTOS_LOCK_MUTEX(audioMutex);
mutex_lock(&audioMutex);
fragmentsFifo.clear();
varioContext.clear();
backgroundContext.clear();
RTOS_UNLOCK_MUTEX(audioMutex);
mutex_unlock(&audioMutex);
}

void audioPlay(unsigned int index, uint8_t id)
Expand Down
1 change: 0 additions & 1 deletion radio/src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ void codecsInit();
void audioEvent(unsigned int index);
void audioPlay(unsigned int index, uint8_t id=0);
void audioStart();
void audioTask(void * pdata);

#if defined(AUDIO) && defined(BUZZER)
#define AUDIO_BUZZER(a, b) do { a; b; } while(0)
Expand Down
9 changes: 5 additions & 4 deletions radio/src/bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "edgetx.h"
#include "io/frsky_firmware_update.h"
#include "bluetooth_driver.h"
#include "os/sleep.h"
#include "trainer.h"

#if defined(LIBOPENUI)
Expand Down Expand Up @@ -571,7 +572,7 @@ uint8_t Bluetooth::read(uint8_t * data, uint8_t size, uint32_t timeout)
if (elapsed++ >= timeout) {
return len;
}
RTOS_WAIT_MS(1);
sleep_ms(1);
}
data[len++] = byte;
}
Expand Down Expand Up @@ -829,11 +830,11 @@ const char * Bluetooth::flashFirmware(const char * filename, ProgressHandler pro

bluetoothInit(BLUETOOTH_BOOTLOADER_BAUDRATE, true); // normal mode
watchdogSuspend(500 /*5s*/);
RTOS_WAIT_MS(1000);
sleep_ms(1000);

bluetoothInit(BLUETOOTH_BOOTLOADER_BAUDRATE, false); // bootloader mode
watchdogSuspend(500 /*5s*/);
RTOS_WAIT_MS(1000);
sleep_ms(1000);

const char * result = doFlashFirmware(filename, progressHandler);

Expand All @@ -851,7 +852,7 @@ const char * Bluetooth::flashFirmware(const char * filename, ProgressHandler pro

/* wait 1s off */
watchdogSuspend(500 /*5s*/);
RTOS_WAIT_MS(1000);
sleep_ms(1000);

state = BLUETOOTH_STATE_OFF;
pulsesStart();
Expand Down
33 changes: 9 additions & 24 deletions radio/src/boards/generic_stm32/rgb_leds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@
#include "stm32_dma.h"
#include "stm32_gpio.h"
#include "hal/gpio.h"

#if !defined(SIMU)
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
#endif
#include "os/timer.h"

static uint8_t _led_colors[WS2812_BYTES_PER_LED * LED_STRIP_LENGTH];

extern const stm32_pulse_timer_t _led_timer;

static TimerHandle_t rgbLedTimer = nullptr;
static StaticTimer_t rgbLedTimerBuffer;
static timer_handle_t _refresh_timer = TIMER_INITIALIZER;

void rgbSetLedColor(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
Expand Down Expand Up @@ -77,35 +72,25 @@ void rgbLedClearAll()
ws2812_update(&_led_timer);
}

static void rgbLedTimerCb(TimerHandle_t xTimer)
static void _refresh_cb(timer_handle_t* timer)
{
(void)xTimer;

(void)timer;
ws2812_update(&_led_timer);
}

void rgbLedStart()
{
if (!rgbLedTimer) {
rgbLedTimer =
xTimerCreateStatic("rgbLed", LED_STRIP_REFRESH_PERIOD / RTOS_MS_PER_TICK, pdTRUE, (void*)0,
rgbLedTimerCb, &rgbLedTimerBuffer);
if (!timer_is_created(&_refresh_timer)) {
timer_create(&_refresh_timer, _refresh_cb, "rgbled",
LED_STRIP_REFRESH_PERIOD, true);
}

if (rgbLedTimer) {
if( xTimerStart( rgbLedTimer, 0 ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
timer_start(&_refresh_timer);
}

void rgbLedStop()
{
if (rgbLedTimer) {
if( xTimerStop( rgbLedTimer, 5 / RTOS_MS_PER_TICK ) != pdPASS ) {
/* The timer could not be stopped. */
}
}
timer_stop(&_refresh_timer);
}

const stm32_pulse_timer_t _led_timer = {
Expand Down
Loading