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
@@ -22,6 +22,7 @@
#include "hal/gpio.h"
#include "hal/audio_driver.h"
#include "os/sleep.h"
#include "stm32_gpio.h"
#include "stm32_timer.h"
#include "stm32_dma.h"
@@ -162,7 +163,7 @@ void audioUnmute()
if (get_mute_pin()) {
// ..un-mute
set_mute_pin(false);
RTOS_WAIT_MS(AUDIO_UNMUTE_DELAY);
sleep_ms(AUDIO_UNMUTE_DELAY);
}
// reset the mute delay
audioQueue.lastAudioPlayTime = 0;
@@ -6,6 +6,8 @@ AddHWGenTarget(${HW_DESC_JSON} hal_keys hal_keys.inc)
set(BOOTLOADER_SRC ${BOOTLOADER_SRC}
${CMAKE_CURRENT_BINARY_DIR}/hal_keys.inc
${RADIO_SRC_DIR}/os/time_nortos.cpp
${RADIO_SRC_DIR}/os/sleep_nortos.cpp
${RADIO_SRC_DIR}/keys.cpp
${RADIO_SRC_DIR}/strhelpers.cpp
${RADIO_SRC_DIR}/stamp.cpp
@@ -135,6 +135,11 @@ void bootloaderInitApp()
boardBLInit();
}
#if defined(FIRMWARE_FORMAT_UF2)
// make linker happy
void per5ms() {}
#endif
int main()
#else // SIMU
void bootloaderInitApp() {}
@@ -2,9 +2,11 @@
#include "boot.h"
#include "hal/usb_driver.h"
#include "os/time.h"
#include "lcd.h"
extern volatile uint8_t tenms;
#define FRAME_INTERVAL_MS 20
void bootloaderDFU()
{
@@ -13,8 +15,11 @@ void bootloaderDFU()
bootloaderDrawDFUScreen();
uint32_t next_frame = time_get_ms();
for (;;) {
if (tenms) {
if (time_get_ms() - next_frame >= FRAME_INTERVAL_MS) {
next_frame += FRAME_INTERVAL_MS;
if (!usbPlugged()) break;
bootloaderDrawDFUScreen();
lcdRefresh();
@@ -3,9 +3,12 @@
#include "hal/usb_driver.h"
#include "hal/rotary_encoder.h"
#include "os/time.h"
#include "lcd.h"
#define FRAME_INTERVAL_MS 20
#if defined(SPI_FLASH)
#define MAIN_MENU_LEN 3
#else
@@ -17,16 +20,8 @@
#define SEL_CLEAR_FLASH_STORAGE_MENU_LEN 2
#endif
volatile tmr10ms_t g_tmr10ms;
volatile uint8_t tenms = 1;
void per5ms() {} // make linker happy
void per10ms()
void pollInputs()
{
tenms |= 1u; // 10 mS has passed
g_tmr10ms++;
keysPollingCycle();
// TODO: use rotaryEncoderPollingCycle() instead
@@ -42,6 +37,13 @@ void per10ms()
#endif
}
// poll inputs every 10ms
void per5ms()
{
static uint32_t cnt = 0;
if (++cnt & 1) { pollInputs(); }
}
FlashCheckRes valid;
int menuFlashFile(uint32_t index, event_t event)
@@ -74,10 +76,12 @@ void bootloaderMenu()
sdInit();
uint32_t next_frame = time_get_ms();
for (;;) {
if (tenms) {
tenms = 0;
if (time_get_ms() - next_frame >= FRAME_INTERVAL_MS) {
next_frame += FRAME_INTERVAL_MS;
if (state != ST_USB && state != ST_FLASHING
&& state != ST_FLASH_DONE && state != ST_RADIO_MENU) {
@@ -3,6 +3,7 @@
#include "hal/usb_driver.h"
#include "hal/storage.h"
#include "hal/fatfs_diskio.h"
#include "os/time.h"
#include "thirdparty/FatFs/diskio.h"
#include "drivers/uf2_ghostfat.h"
@@ -10,19 +11,10 @@
#include "board.h"
#include "lcd.h"
#define FRAME_INTERVAL_MS 20
extern uf2_fat_write_state_t _uf2_write_state;
volatile tmr10ms_t g_tmr10ms;
volatile uint8_t tenms = 1;
void per5ms() {} // make linker happy
void per10ms()
{
tenms |= 1u; // 10 mS has passed
g_tmr10ms++;
}
void bootloaderUF2()
{
BootloaderState state = ST_START;
@@ -31,10 +23,12 @@ void bootloaderUF2()
storageInit();
disk_initialize(0);
uint32_t next_frame = time_get_ms();
for (;;) {
if (tenms) {
tenms = 0;
if (time_get_ms() - next_frame >= FRAME_INTERVAL_MS) {
next_frame += FRAME_INTERVAL_MS;
if (state != ST_USB && state != ST_FLASHING && state != ST_FLASH_DONE) {
if (usbPlugged()) {
@@ -137,7 +137,7 @@ void rotaryEncoderCheck()
rotenc_t diff = (value - last_value);
if (diff != 0) {
uint32_t now = RTOS_GET_MS();
uint32_t now = timersGetMsTick();
uint32_t dt = now - last_tick;
// pre-compute accumulated dt (dx/dt is done later in LVGL driver)
rotencDt += dt;
@@ -100,8 +100,6 @@ static inline void _interrupt_1ms()
watchdogTimeout -= 1;
WDG_RESET(); // Retrigger hardware watchdog
}
per10ms();
}
}
@@ -21,6 +21,7 @@
#include "vs1053b.h"
#include "os/sleep.h"
#include "stm32_hal_ll.h"
#include "stm32_gpio_driver.h"
#include "stm32_gpio.h"
@@ -327,7 +328,7 @@ static void vs1053b_unmute()
if (_is_muted) {
// ..un-mute
_set_mute_pin(false);
RTOS_WAIT_MS(_instance->unmute_delay_ms);
sleep_ms(_instance->unmute_delay_ms);
}
// reset the mute delay
_last_play_ts = 0;
+3 -3
View File
@@ -42,8 +42,8 @@ volatile static bool touchEventOccured;
static tc_handle_TypeDef tc_handle = {0, 0};
tmr10ms_t downTime = 0;
tmr10ms_t tapTime = 0;
uint32_t downTime = 0;
uint32_t tapTime = 0;
short tapCount = 0;
#define TAP_TIME 250 // ms
@@ -251,7 +251,7 @@ TouchState touchPanelRead()
touchEventOccured = false;
tmr10ms_t now = RTOS_GET_MS();
uint32_t now = timersGetMsTick();
internalTouchState.tapCount = 0;
if (cst836u_TS_DetectTouch()) {
+6 -5
View File
@@ -29,10 +29,11 @@
#include "stm32_exti_driver.h"
#include "hal.h"
#include "timers_driver.h"
#include "tp_gt911.h"
#include "delays_driver.h"
#include "rtos.h"
#include "os/sleep.h"
#include "edgetx_types.h"
#include "debug.h"
@@ -651,7 +652,7 @@ struct TouchState touchPanelRead()
touchEventOccured = false;
uint32_t startReadStatus = RTOS_GET_MS();
uint32_t startReadStatus = timersGetMsTick();
do {
if (!I2C_GT911_ReadRegister(GT911_READ_XY_REG, &state, 1)) {
// ledRed();
@@ -665,15 +666,15 @@ struct TouchState touchPanelRead()
// ready
break;
}
RTOS_WAIT_MS(1);
} while (RTOS_GET_MS() - startReadStatus < GT911_TIMEOUT);
sleep_ms(1);
} while (timersGetMsTick() - startReadStatus < GT911_TIMEOUT);
internalTouchState.deltaX = 0;
internalTouchState.deltaY = 0;
TRACE("touch state = 0x%x", state);
if (state & 0x80u) {
uint8_t pointsCount = (state & 0x0Fu);
uint32_t now = RTOS_GET_MS();
uint32_t now = timersGetMsTick();
internalTouchState.tapCount = 0;
if (pointsCount > 0 && pointsCount <= GT911_MAX_TP) {
+1 -2
View File
@@ -7,7 +7,6 @@ endif()
set(SIMU_DRIVERS
simpgmspace.cpp
simueeprom.cpp
simufatfs.cpp
simudisk.cpp
simulcd.cpp
@@ -186,7 +185,7 @@ if(FOX_FOUND)
target_include_directories(simu PUBLIC ${FOX_INCLUDE_DIR} )
target_link_libraries(simu ${FOX_LIBRARY} pthread ${SDL2_LIBRARIES})
target_compile_options(simu PRIVATE -DSIMU)
target_compile_options(simu PRIVATE ${SIMU_SRC_OPTIONS})
endif()
if(APPLE)
+15 -18
View File
@@ -28,6 +28,7 @@
#include "hal/adc_driver.h"
#include "hal/rotary_encoder.h"
#include "os/time.h"
#include <QDebug>
#include <QElapsedTimer>
@@ -282,9 +283,9 @@ void OpenTxSimulator::start(const char * filename, bool tests)
QMutexLocker lckr(&m_mtxSimuMain);
QMutexLocker slckr(&m_mtxSettings);
startEepromThread(filename);
startAudioThread(volumeGain);
simuStart(tests, simuSdDirectory.toLatin1().constData(), simuSettingsDirectory.toLatin1().constData());
startAudio(volumeGain);
simuStart(tests, simuSdDirectory.toLatin1().constData(),
simuSettingsDirectory.toLatin1().constData());
emit started();
QTimer::singleShot(0, this, SLOT(run())); // old style for Qt < 5.4
@@ -300,8 +301,6 @@ void OpenTxSimulator::stop()
QMutexLocker lckr(&m_mtxSimuMain);
simuStop();
stopAudioThread();
stopEepromThread();
emit stopped();
}
@@ -321,20 +320,20 @@ void OpenTxSimulator::setVolumeGain(const int value)
void OpenTxSimulator::setRadioData(const QByteArray & data)
{
#if defined(EEPROM_SIZE)
QMutexLocker lckr(&m_mtxRadioData);
eeprom = (uint8_t *)malloc(qMin<int>(EEPROM_SIZE, data.size()));
memcpy(eeprom, data.data(), qMin<int>(EEPROM_SIZE, data.size()));
#endif
// #if defined(EEPROM_SIZE)
// QMutexLocker lckr(&m_mtxRadioData);
// eeprom = (uint8_t *)malloc(qMin<int>(EEPROM_SIZE, data.size()));
// memcpy(eeprom, data.data(), qMin<int>(EEPROM_SIZE, data.size()));
// #endif
}
void OpenTxSimulator::readRadioData(QByteArray & dest)
{
#if defined(EEPROM_SIZE)
QMutexLocker lckr(&m_mtxRadioData);
if (eeprom)
memcpy(dest.data(), eeprom, qMin<int>(EEPROM_SIZE, dest.size()));
#endif
// #if defined(EEPROM_SIZE)
// QMutexLocker lckr(&m_mtxRadioData);
// if (eeprom)
// memcpy(dest.data(), eeprom, qMin<int>(EEPROM_SIZE, dest.size()));
// #endif
}
uint8_t * OpenTxSimulator::getLcd()
@@ -428,7 +427,7 @@ void OpenTxSimulator::rotaryEncoderEvent(int steps)
steps *= -1;
rotencValue += steps * ROTARY_ENCODER_GRANULARITY;
// TODO: set rotencDt
uint32_t now = RTOS_GET_MS();
uint32_t now = time_get_ms();
uint32_t dt = now - last_tick;
rotencDt += dt;
last_tick = now;
@@ -771,8 +770,6 @@ void OpenTxSimulator::run()
++loops;
per10ms();
checkLcdChanged();
if (!(loops % 5)) {
+41 -72
View File
@@ -34,6 +34,9 @@
#include "hal/usb_driver.h"
#include "hal/audio_driver.h"
#include "os/task.h"
#include "os/timer_pthread_impl.h"
#include <errno.h>
#include <stdarg.h>
#include <string>
@@ -211,6 +214,7 @@ void simuStart(bool tests, const char * sdPath, const char * settingsPath)
lcdInit();
#if !defined(SIMU_BOOTLOADER)
timer_queue::instance().start();
simuMain();
#else
pthread_attr_t attr;
@@ -232,8 +236,8 @@ void simuStart(bool tests, const char * sdPath, const char * settingsPath)
#endif
}
extern RTOS_TASK_HANDLE mixerTaskId;
extern RTOS_TASK_HANDLE menusTaskId;
extern task_handle_t mixerTaskId;
extern task_handle_t menusTaskId;
void simuStop()
{
@@ -242,8 +246,12 @@ void simuStop()
simu_shutdown = true;
pthread_join(mixerTaskId, nullptr);
pthread_join(menusTaskId, nullptr);
pthread_join(mixerTaskId._thread_handle, nullptr);
pthread_join(menusTaskId._thread_handle, nullptr);
#if defined(SIMU_AUDIO)
stopAudio();
#endif
simu_running = false;
}
@@ -253,8 +261,6 @@ struct SimulatorAudio {
int currentVolume;
int16_t leftoverData[AUDIO_BUFFER_SIZE];
int leftoverLen;
bool threadRunning;
pthread_t threadPid;
} simuAudio;
bool simuIsRunning()
@@ -264,10 +270,9 @@ bool simuIsRunning()
uint8_t simuSleep(uint32_t ms)
{
for (uint32_t i = 0; i < ms; ++i){
if (simu_shutdown || !simu_running)
return 1;
sleep(1);
for (uint32_t i = 0; i < ms; i++) {
if (simu_shutdown || !simu_running) return 1;
usleep(1);
}
return 0;
}
@@ -336,77 +341,44 @@ void fillAudioBuffer(void *udata, Uint8 *stream, int len)
}
}
//fill the rest of buffer with silence
// fill the rest of buffer with silence
if (len > 0) {
SDL_memset(stream, 0x8000, len); // make sure this is silence.
// putchar('.');
}
}
void * audioThread(void *)
int startAudio(int volumeGain)
{
/*
Checking here if SDL audio was initialized is wrong, because
the SDL_CloseAudio() de-initializes it.
simuAudio = {
.volumeGain = volumeGain,
.leftoverLen = 0,
};
if ( !SDL_WasInit(SDL_INIT_AUDIO) ) {
fprintf(stderr, "ERROR: couldn't initialize SDL audio support\n");
return 0;
}
*/
SDL_AudioSpec wanted, have;
/* Set the audio format */
wanted.freq = AUDIO_SAMPLE_RATE;
wanted.format = AUDIO_S16SYS;
wanted.channels = 1; /* 1 = mono, 2 = stereo */
wanted.samples =
AUDIO_BUFFER_SIZE * 2; /* Good low-latency value for callback */
wanted.callback = fillAudioBuffer;
wanted.userdata = nullptr;
/*
SDL_OpenAudio() internally calls SDL_InitSubSystem(SDL_INIT_AUDIO),
which initializes SDL Audio subsystem if necessary
*/
if ( SDL_OpenAudio(&wanted, &have) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return nullptr;
}
SDL_PauseAudio(0);
while (simuAudio.threadRunning) {
audioQueue.wakeup();
sleep(1);
}
SDL_CloseAudio();
return nullptr;
}
void startAudioThread(int volumeGain)
{
simuAudio.leftoverLen = 0;
simuAudio.threadRunning = true;
simuAudio.volumeGain = volumeGain;
TRACE_SIMPGMSPACE("startAudioThread(%d)", volumeGain);
TRACE("startAudioThread(%d)", volumeGain);
audioSetVolume(VOLUME_LEVEL_DEF);
pthread_attr_t attr;
pthread_attr_init(&attr);
struct sched_param sp;
sp.sched_priority = SCHED_RR;
pthread_attr_setschedparam(&attr, &sp);
pthread_create(&simuAudio.threadPid, &attr, &audioThread, nullptr);
#ifdef __linux__
pthread_setname_np(simuAudio.threadPid, "audio");
#endif
/* Set the audio format */
SDL_AudioSpec desired = {
.freq = AUDIO_SAMPLE_RATE,
.format = AUDIO_S16SYS,
.channels = 1,
.samples = AUDIO_BUFFER_SIZE * 2,
.callback = fillAudioBuffer,
.userdata = nullptr,
};
SDL_AudioSpec obtained;
if ( SDL_OpenAudio(&desired, &obtained) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return -1;
}
SDL_PauseAudio(0);
return 0;
}
void stopAudioThread()
void stopAudio()
{
simuAudio.threadRunning = false;
pthread_join(simuAudio.threadPid, nullptr);
SDL_CloseAudio();
}
#endif // #if defined(SIMU_AUDIO)
@@ -675,6 +647,3 @@ struct TouchState getInternalTouchState()
return simTouchState;
}
#endif
void telemetryStart() {}
void telemetryStop() {}
+4 -15
View File
@@ -30,17 +30,11 @@
#include <stddef.h>
#include <errno.h>
// #undef min
// #undef max
extern uint8_t * eeprom;
#define __disable_irq()
#define __enable_irq()
extern char * main_thread_error;
uint64_t simuTimerMicros(void);
uint8_t simuSleep(uint32_t ms); // returns true if thread shutdown requested
@@ -57,21 +51,16 @@ void startEepromThread(const char * filename = "eeprom.bin");
void stopEepromThread();
#if defined(SIMU_AUDIO)
void startAudioThread(int volumeGain = 10);
void stopAudioThread(void);
int startAudio(int volumeGain = 10);
void stopAudio();
#else
#define startAudioThread(dummy)
#define stopAudioThread()
#define startAudio(dummy) (-1)
#define stopAudio()
#endif
#endif
void simuMain();
// inline void delay_01us(uint32_t dummy) { }
#define configure_pins(...)
#if !defined(SKIP_FATFS_DECLARATION) && !defined(SIMU_DISKIO)
#define SIMU_USE_SDCARD
#endif
+6 -4
View File
@@ -19,6 +19,7 @@
* GNU General Public License for more details.
*/
#include "os/sleep.h"
#include "stm32_hal_ll.h"
#include "stm32_hal.h"
#include "stm32_i2c_driver.h"
@@ -32,6 +33,7 @@
#include "hal.h"
#include "stm32h7rsxx_hal_gpio.h"
#include "timers_driver.h"
#include "tp_gt911.h"
#include "delays_driver.h"
@@ -215,7 +217,7 @@ struct TouchState touchPanelRead()
touchEventOccured = false;
uint32_t startReadStatus = RTOS_GET_MS();
uint32_t startReadStatus = timersGetMsTick();
do {
if (!I2C_GT911_ReadRegister(GT911_READ_XY_REG, &state, 1)) {
// ledRed();
@@ -229,15 +231,15 @@ struct TouchState touchPanelRead()
// ready
break;
}
RTOS_WAIT_MS(1);
} while (RTOS_GET_MS() - startReadStatus < GT911_TIMEOUT);
sleep_ms(1);
} while (timersGetMsTick() - startReadStatus < GT911_TIMEOUT);
internalTouchState.deltaX = 0;
internalTouchState.deltaY = 0;
TRACE("touch state = 0x%x", state);
if (state & 0x80u) {
uint8_t pointsCount = (state & 0x0Fu);
uint32_t now = RTOS_GET_MS();
uint32_t now = timersGetMsTick();
internalTouchState.tapCount = 0;
if (pointsCount > 0 && pointsCount <= GT911_MAX_TP) {
+12 -24
View File
@@ -20,17 +20,17 @@
*/
#include "bsp_io.h"
#include "hal/switch_driver.h"
#include "drivers/pca95xx.h"
#include "os/async.h"
#include "stm32_i2c_driver.h"
#include "timers_driver.h"
#include "stm32_ws2812.h"
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
#include "bitfield.h"
#include "drivers/pca95xx.h"
#include "hal/switch_driver.h"
#include "myeeprom.h"
#include "stm32_i2c_driver.h"
#include "stm32_ws2812.h"
#include "timers_driver.h"
struct bsp_io_expander {
pca95xx_t exp;
@@ -58,30 +58,18 @@ static uint32_t _read_io_expander(bsp_io_expander* io)
return io->state;
}
static void _poll_switches(void *pvParameter1, uint32_t ulParameter2)
static void _poll_switches(void *param1, uint32_t param2)
{
(void)ulParameter2;
(void)param1;
(void)param2;
_poll_switches_in_queue = false;
bsp_io_read_switches();
bsp_io_read_fs_switches();
_read_io_expander(&_io_switches);
_read_io_expander(&_io_fs_switches);
}
static void _io_int_handler()
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
BaseType_t xReturn = pdFALSE;
if (!_poll_switches_in_queue && xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
xReturn = xTimerPendFunctionCallFromISR(_poll_switches, nullptr, 0,
&xHigherPriorityTaskWoken);
if (xReturn == pdPASS) {
_poll_switches_in_queue = true;
} else {
TRACE("xTimerPendFunctionCallFromISR() queue full");
}
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
async_call_isr(_poll_switches, &_poll_switches_in_queue, nullptr, 0);
}
int bsp_io_init()