diff --git a/openrtx/include/core/state.h b/openrtx/include/core/state.h index 2543d83e..d166f4e8 100644 --- a/openrtx/include/core/state.h +++ b/openrtx/include/core/state.h @@ -46,6 +46,7 @@ m17_t; */ typedef struct { + uint8_t devStatus; datetime_t time; uint16_t v_bat; uint8_t charge; @@ -61,12 +62,13 @@ typedef struct uint16_t bank; uint8_t rtxStatus; - bool shutdown; bool emergency; settings_t settings; gps_t gps_data; bool gps_set_time; bool gpsDetected; + bool backup_eflash; + bool restore_eflash; m17_t m17_data; } state_t; @@ -86,6 +88,15 @@ enum RtxStatus RTX_TX }; +enum DeviceStatus +{ + PWROFF = 0, + STARTUP, + RUNNING, + DATATRANSFER, + SHUTDOWN +}; + extern state_t state; extern pthread_mutex_t state_mutex; diff --git a/openrtx/src/core/openrtx.c b/openrtx/src/core/openrtx.c index 54b6eb06..be8f18cf 100644 --- a/openrtx/src/core/openrtx.c +++ b/openrtx/src/core/openrtx.c @@ -30,6 +30,8 @@ extern void *dev_task(void *arg); void openrtx_init() { + state.devStatus = STARTUP; + platform_init(); // Initialize low-level platform drivers state_init(); // Initialize radio state @@ -47,20 +49,29 @@ void openrtx_init() sleepFor(0u, 30u); platform_setBacklightLevel(state.settings.brightness); - // Detect and initialise GPS #if defined(GPS_PRESENT) + // Detect and initialise GPS state.gpsDetected = gps_detect(1000); if(state.gpsDetected) gps_init(9600); + #else + // Keep the splash screen for 1 second + sleepFor(1u, 0u); #endif } void *openrtx_run() { + state.devStatus = RUNNING; + // Start the OpenRTX threads create_threads(); // Jump to the device management task dev_task(NULL); + // Device task terminated, complete shutdown sequence + state_terminate(); + platform_terminate(); + return NULL; } diff --git a/openrtx/src/core/state.c b/openrtx/src/core/state.c index 5994b9b3..ac72ff7b 100644 --- a/openrtx/src/core/state.c +++ b/openrtx/src/core/state.c @@ -87,7 +87,6 @@ void state_init() state.channel_index = 1; // Set default channel index (it is 1-based) state.bank_enabled = false; state.rtxStatus = RTX_OFF; - state.shutdown = false; state.emergency = false; // Force brightness field to be in range 0 - 100 diff --git a/openrtx/src/core/threads.c b/openrtx/src/core/threads.c index 311fac25..2c111709 100644 --- a/openrtx/src/core/threads.c +++ b/openrtx/src/core/threads.c @@ -33,6 +33,7 @@ #include #include #include +#include #ifdef GPS_PRESENT #include #include @@ -48,31 +49,25 @@ void *ui_task(void *arg) { (void) arg; + event_t kbd_event = {{EVENT_KBD, 0}}; kbd_msg_t kbd_msg; rtxStatus_t rtx_cfg; bool sync_rtx = true; long long time = 0; - // Keep the splash screen for 1 second - sleepFor(1u, 0u); - // Load initial state and perform a GUI draw ui_saveState(); ui_updateGUI(); gfx_render(); - while(1) + while(state.devStatus != SHUTDOWN) { time = getTick(); - // Scan keyboard - bool kbd_event = input_scanKeyboard(&kbd_msg); - if(kbd_event) + if(input_scanKeyboard(&kbd_msg)) { - event_t event; - event.type = EVENT_KBD; - event.payload = kbd_msg.value; - ui_pushEvent(event); + kbd_event.payload = kbd_msg.value; + ui_pushEvent(kbd_event); } pthread_mutex_lock(&state_mutex); // Lock r/w access to radio state @@ -115,6 +110,11 @@ void *ui_task(void *arg) time += 25; sleepUntil(time); } + + ui_terminate(); + gfx_terminate(); + + return NULL; } /** @@ -128,17 +128,47 @@ void *dev_task(void *arg) long long time = 0; uint8_t tick_5ms = 0; - while(state.shutdown == false) + while(state.devStatus != SHUTDOWN) { time = getTick(); tick_5ms++; + // Check if power off is requested + pthread_mutex_lock(&state_mutex); + if(platform_pwrButtonStatus() == false) + state.devStatus = SHUTDOWN; + pthread_mutex_unlock(&state_mutex); + + // Handle external flash backup/restore + #if !defined(PLATFORM_LINUX) && !defined(PLATFORM_MOD17) + if(state.backup_eflash) + { + eflash_dump(); + + pthread_mutex_lock(&state_mutex); + state.backup_eflash = false; + state.devStatus = SHUTDOWN; + pthread_mutex_unlock(&state_mutex); + } + + if(state.restore_eflash) + { + eflash_restore(); + + pthread_mutex_lock(&state_mutex); + state.restore_eflash = false; + state.devStatus = SHUTDOWN; + pthread_mutex_unlock(&state_mutex); + } + #endif + + // Run GPS task #if defined(GPS_PRESENT) && !defined(MD3x0_ENABLE_DBG) if(state.gpsDetected) gps_taskFunc(); #endif - // Update radio state and push an event to the UI every 100ms + // Update radio state every 100ms if((tick_5ms % 20) == 0) { state_update(); @@ -170,7 +200,7 @@ void *rtx_task(void *arg) rtx_init(&rtx_mutex); - while(state.shutdown == false) + while(state.devStatus == RUNNING) { rtx_taskFunc(); } diff --git a/openrtx/src/ui/ui.c b/openrtx/src/ui/ui.c index b316cca0..45a60b31 100644 --- a/openrtx/src/ui/ui.c +++ b/openrtx/src/ui/ui.c @@ -936,15 +936,6 @@ void ui_saveState() void ui_updateFSM(bool *sync_rtx) { - // User wants to power off the radio, so shutdown. - if(!platform_pwrButtonStatus()) - { - state.shutdown = true; - state_terminate(); - platform_terminate(); - return; - } - // Check for events if(evQueue_wrPos == evQueue_rdPos) return; diff --git a/openrtx/src/ui/ui_menu.c b/openrtx/src/ui/ui_menu.c index be898cb4..e9487a03 100644 --- a/openrtx/src/ui/ui_menu.c +++ b/openrtx/src/ui/ui_menu.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -464,18 +463,8 @@ void _ui_drawMenuBackup(ui_state_t* ui_state) gfx_print(line, FONT_SIZE_8PT, TEXT_ALIGN_CENTER, color_white, "press PTT to start."); - // Shutdown all the other parts - state.shutdown = true; - - if(platform_getPttStatus() == 1) - { - platform_ledOn(GREEN); - #if !defined(PLATFORM_LINUX) && !defined(PLATFORM_MOD17) - eflash_dump(); - platform_terminate(); - NVIC_SystemReset(); - #endif - } + state.devStatus = DATATRANSFER; + state.backup_eflash = true; } void _ui_drawMenuRestore(ui_state_t* ui_state) @@ -497,18 +486,8 @@ void _ui_drawMenuRestore(ui_state_t* ui_state) gfx_print(line, FONT_SIZE_8PT, TEXT_ALIGN_CENTER, color_white, "press PTT to start."); - // Shutdown all the other parts - state.shutdown = true; - - if(platform_getPttStatus() == 1) - { - platform_ledOn(GREEN); - #if !defined(PLATFORM_LINUX) && !defined(PLATFORM_MOD17) - eflash_restore(); - platform_terminate(); - NVIC_SystemReset(); - #endif - } + state.devStatus = DATATRANSFER; + state.restore_eflash = true; } void _ui_drawMenuInfo(ui_state_t* ui_state)