Skip to content

Commit ab2a19e

Browse files
feat(radio): OS compatibility layer (#5840)
1 parent c996612 commit ab2a19e

File tree

92 files changed

+1737
-893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1737
-893
lines changed

radio/src/CMakeLists.txt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,16 @@ if(NATIVE_BUILD)
484484
${RADIOLIB_NATIVE_SRC}
485485
)
486486

487-
# Add -DSIMU here PUBLIC, so it can be imported by other targets
487+
# Add SIMU and POSIX_THREAD here PUBLIC, so it can be imported by other targets
488488
# via INTERFACE_COMPILE_OPTIONS
489-
target_compile_options(radiolib_native PUBLIC -DSIMU)
489+
target_compile_options(radiolib_native PUBLIC -DSIMU -DPOSIX_THREADS)
490+
target_sources(radiolib_native PUBLIC
491+
os/async_native.cpp
492+
os/sleep_native.cpp
493+
os/task_pthread.cpp
494+
os/time_native.cpp
495+
os/timer_pthread.cpp
496+
)
490497
add_dependencies(radiolib_native ${RADIO_DEPENDENCIES})
491498
set_property(TARGET radiolib_native PROPERTY POSITION_INDEPENDENT_CODE ON)
492499

@@ -554,13 +561,23 @@ if(BOOTLOADER)
554561
set(RADIO_DEPENDENCIES ${RADIO_DEPENDENCIES} bootloader)
555562
endif()
556563

557-
add_definitions(-DFREE_RTOS)
558-
559564
add_executable(firmware ${SRC})
560565
link_libraries(firmware -lstdc++)
561566
add_dependencies(firmware ${RADIO_DEPENDENCIES})
562567
set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL TRUE)
563568

569+
target_compile_definitions(board PUBLIC FREE_RTOS)
570+
target_compile_definitions(board_lib PUBLIC FREE_RTOS)
571+
target_compile_definitions(firmware PUBLIC FREE_RTOS)
572+
573+
target_sources(firmware PUBLIC
574+
os/async_freertos.cpp
575+
os/sleep_freertos.cpp
576+
os/task_freertos.cpp
577+
os/time_freertos.cpp
578+
os/timer_freertos.cpp
579+
)
580+
564581
if(DEBUG)
565582
target_compile_definitions(board PRIVATE -DDEBUG)
566583
target_compile_definitions(firmware PRIVATE -DDEBUG)

radio/src/audio.cpp

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#include <math.h>
2323

24+
#include "os/sleep.h"
25+
#include "os/task.h"
26+
2427
#include "edgetx.h"
2528
#include "strhelpers.h"
2629
#include "switches.h"
@@ -32,7 +35,7 @@
3235
#include "model_audio.h"
3336
#include "hal/audio_driver.h"
3437

35-
extern RTOS_MUTEX_HANDLE audioMutex;
38+
extern mutex_handle_t audioMutex;
3639

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

374377
#define CODEC_ID_PCM_S16LE 1
375378

376-
#if !defined(SIMU)
377-
void audioTask(void * pdata)
378-
{
379-
while (!audioQueue.started()) {
380-
RTOS_WAIT_TICKS(1);
381-
}
382-
383-
#if defined(PCBX12S) || defined(RADIO_TX16S) || defined(RADIO_F16) || defined(RADIO_V16)
384-
// The audio amp needs ~2s to start
385-
RTOS_WAIT_MS(1000); // 1s
386-
#endif
387-
388-
while (true) {
389-
DEBUG_TIMER_SAMPLE(debugTimerAudioIterval);
390-
DEBUG_TIMER_START(debugTimerAudioDuration);
391-
audioQueue.wakeup();
392-
DEBUG_TIMER_STOP(debugTimerAudioDuration);
393-
RTOS_WAIT_MS(4); // ???
394-
}
395-
}
396-
#endif
397-
398379
#if !defined(__SSAT)
399380
#define _sat_s16(x) ((int16_t)limit<int32_t>(INT16_MIN, (x), INT16_MAX))
400381
#else
@@ -638,9 +619,9 @@ void AudioQueue::wakeup()
638619

639620
// mix the normal context (tones and wavs)
640621
if (normalContext.isEmpty() && !fragmentsFifo.empty()) {
641-
RTOS_LOCK_MUTEX(audioMutex);
622+
mutex_lock(&audioMutex);
642623
normalContext.setFragment(fragmentsFifo.get());
643-
RTOS_UNLOCK_MUTEX(audioMutex);
624+
mutex_unlock(&audioMutex);
644625
}
645626
result = normalContext.mixBuffer(buffer, g_eeGeneral.beepVolume, g_eeGeneral.wavVolume, fade);
646627
if (result > 0) {
@@ -724,7 +705,7 @@ void AudioQueue::playTone(uint16_t freq, uint16_t len, uint16_t pause, uint8_t f
724705
return;
725706
#endif
726707

727-
RTOS_LOCK_MUTEX(audioMutex);
708+
mutex_lock(&audioMutex);
728709

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

@@ -747,7 +728,7 @@ void AudioQueue::playTone(uint16_t freq, uint16_t len, uint16_t pause, uint8_t f
747728
}
748729
}
749730

750-
RTOS_UNLOCK_MUTEX(audioMutex);
731+
mutex_unlock(&audioMutex);
751732
}
752733

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

777-
RTOS_LOCK_MUTEX(audioMutex);
758+
mutex_lock(&audioMutex);
778759

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

787-
RTOS_UNLOCK_MUTEX(audioMutex);
768+
mutex_unlock(&audioMutex);
788769
}
789770

790771
void AudioQueue::stopPlay(uint8_t id)
@@ -797,12 +778,12 @@ void AudioQueue::stopPlay(uint8_t id)
797778
return;
798779
#endif
799780

800-
RTOS_LOCK_MUTEX(audioMutex);
781+
mutex_lock(&audioMutex);
801782

802783
fragmentsFifo.removePromptById(id);
803784
backgroundContext.stop(id);
804785

805-
RTOS_UNLOCK_MUTEX(audioMutex);
786+
mutex_unlock(&audioMutex);
806787
}
807788

808789
void AudioQueue::stopSD()
@@ -815,19 +796,19 @@ void AudioQueue::stopSD()
815796
void AudioQueue::stopAll()
816797
{
817798
flush();
818-
RTOS_LOCK_MUTEX(audioMutex);
799+
mutex_lock(&audioMutex);
819800
priorityContext.clear();
820801
normalContext.clear();
821-
RTOS_UNLOCK_MUTEX(audioMutex);
802+
mutex_unlock(&audioMutex);
822803
}
823804

824805
void AudioQueue::flush()
825806
{
826-
RTOS_LOCK_MUTEX(audioMutex);
807+
mutex_lock(&audioMutex);
827808
fragmentsFifo.clear();
828809
varioContext.clear();
829810
backgroundContext.clear();
830-
RTOS_UNLOCK_MUTEX(audioMutex);
811+
mutex_unlock(&audioMutex);
831812
}
832813

833814
void audioPlay(unsigned int index, uint8_t id)

radio/src/audio.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ void codecsInit();
470470
void audioEvent(unsigned int index);
471471
void audioPlay(unsigned int index, uint8_t id=0);
472472
void audioStart();
473-
void audioTask(void * pdata);
474473

475474
#if defined(AUDIO) && defined(BUZZER)
476475
#define AUDIO_BUZZER(a, b) do { a; b; } while(0)

radio/src/bluetooth.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "edgetx.h"
2424
#include "io/frsky_firmware_update.h"
2525
#include "bluetooth_driver.h"
26+
#include "os/sleep.h"
2627
#include "trainer.h"
2728

2829
#if defined(LIBOPENUI)
@@ -571,7 +572,7 @@ uint8_t Bluetooth::read(uint8_t * data, uint8_t size, uint32_t timeout)
571572
if (elapsed++ >= timeout) {
572573
return len;
573574
}
574-
RTOS_WAIT_MS(1);
575+
sleep_ms(1);
575576
}
576577
data[len++] = byte;
577578
}
@@ -829,11 +830,11 @@ const char * Bluetooth::flashFirmware(const char * filename, ProgressHandler pro
829830

830831
bluetoothInit(BLUETOOTH_BOOTLOADER_BAUDRATE, true); // normal mode
831832
watchdogSuspend(500 /*5s*/);
832-
RTOS_WAIT_MS(1000);
833+
sleep_ms(1000);
833834

834835
bluetoothInit(BLUETOOTH_BOOTLOADER_BAUDRATE, false); // bootloader mode
835836
watchdogSuspend(500 /*5s*/);
836-
RTOS_WAIT_MS(1000);
837+
sleep_ms(1000);
837838

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

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

852853
/* wait 1s off */
853854
watchdogSuspend(500 /*5s*/);
854-
RTOS_WAIT_MS(1000);
855+
sleep_ms(1000);
855856

856857
state = BLUETOOTH_STATE_OFF;
857858
pulsesStart();

radio/src/boards/generic_stm32/rgb_leds.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,13 @@
3030
#include "stm32_dma.h"
3131
#include "stm32_gpio.h"
3232
#include "hal/gpio.h"
33-
34-
#if !defined(SIMU)
35-
#include <FreeRTOS/include/FreeRTOS.h>
36-
#include <FreeRTOS/include/timers.h>
37-
#endif
33+
#include "os/timer.h"
3834

3935
static uint8_t _led_colors[WS2812_BYTES_PER_LED * LED_STRIP_LENGTH];
4036

4137
extern const stm32_pulse_timer_t _led_timer;
4238

43-
static TimerHandle_t rgbLedTimer = nullptr;
44-
static StaticTimer_t rgbLedTimerBuffer;
39+
static timer_handle_t _refresh_timer = TIMER_INITIALIZER;
4540

4641
void rgbSetLedColor(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
4742
{
@@ -77,35 +72,25 @@ void rgbLedClearAll()
7772
ws2812_update(&_led_timer);
7873
}
7974

80-
static void rgbLedTimerCb(TimerHandle_t xTimer)
75+
static void _refresh_cb(timer_handle_t* timer)
8176
{
82-
(void)xTimer;
83-
77+
(void)timer;
8478
ws2812_update(&_led_timer);
8579
}
8680

8781
void rgbLedStart()
8882
{
89-
if (!rgbLedTimer) {
90-
rgbLedTimer =
91-
xTimerCreateStatic("rgbLed", LED_STRIP_REFRESH_PERIOD / RTOS_MS_PER_TICK, pdTRUE, (void*)0,
92-
rgbLedTimerCb, &rgbLedTimerBuffer);
83+
if (!timer_is_created(&_refresh_timer)) {
84+
timer_create(&_refresh_timer, _refresh_cb, "rgbled",
85+
LED_STRIP_REFRESH_PERIOD, true);
9386
}
9487

95-
if (rgbLedTimer) {
96-
if( xTimerStart( rgbLedTimer, 0 ) != pdPASS ) {
97-
/* The timer could not be set into the Active state. */
98-
}
99-
}
88+
timer_start(&_refresh_timer);
10089
}
10190

10291
void rgbLedStop()
10392
{
104-
if (rgbLedTimer) {
105-
if( xTimerStop( rgbLedTimer, 5 / RTOS_MS_PER_TICK ) != pdPASS ) {
106-
/* The timer could not be stopped. */
107-
}
108-
}
93+
timer_stop(&_refresh_timer);
10994
}
11095

11196
const stm32_pulse_timer_t _led_timer = {

0 commit comments

Comments
 (0)