diff --git a/firmware/application/m4_startup.cpp b/firmware/application/m4_startup.cpp index 6dc74b4b..6b9df47b 100644 --- a/firmware/application/m4_startup.cpp +++ b/firmware/application/m4_startup.cpp @@ -23,6 +23,9 @@ #include "hal.h" +#include "message.hpp" +#include "portapack_shared_memory.hpp" + #include /* TODO: OK, this is cool, but how do I put the M4 to sleep so I can switch to @@ -44,3 +47,8 @@ void m4_init(const portapack::spi_flash::region_t from, const portapack::memory: /* Reset M4 core */ LPC_RGU->RESET_CTRL[0] = (1 << 13); } + +void m4_request_shutdown() { + ShutdownMessage shutdown_message; + shared_memory.baseband_queue.push(shutdown_message); +} diff --git a/firmware/application/m4_startup.hpp b/firmware/application/m4_startup.hpp index 16758d90..ce22770e 100644 --- a/firmware/application/m4_startup.hpp +++ b/firmware/application/m4_startup.hpp @@ -28,5 +28,6 @@ #include "spi_image.hpp" void m4_init(const portapack::spi_flash::region_t from, const portapack::memory::region_t to); +void m4_request_shutdown(); #endif/*__M4_STARTUP_H__*/ diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index 3a6e4d11..9669b715 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -252,6 +252,13 @@ int main(void) { ui::Painter painter; EventDispatcher event_dispatcher { &system_view, painter, context }; + auto& message_handlers = context.message_map; + message_handlers.register_handler(Message::ID::Shutdown, + [&event_dispatcher](const Message* const) { + event_dispatcher.request_stop(); + } + ); + m4_init(portapack::spi_flash::baseband, portapack::memory::map::m4_code); controls_init(); @@ -261,5 +268,7 @@ int main(void) { event_dispatcher.run(); + portapack::shutdown(); + m4_init(portapack::spi_flash::hackrf, portapack::memory::map::m4_code_hackrf); return 0; } diff --git a/firmware/application/portapack.cpp b/firmware/application/portapack.cpp index 9f4c361c..8f60e064 100644 --- a/firmware/application/portapack.cpp +++ b/firmware/application/portapack.cpp @@ -153,6 +153,9 @@ void shutdown() { // TODO: Wait a bit for supplies to discharge? chSysDisable(); + + systick_stop(); + hackrf::one::reset(); } diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 2b2f9830..3096cfc2 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -27,10 +27,7 @@ #include "ui_debug.hpp" #include "ui_receiver.hpp" -#include "portapack.hpp" #include "m4_startup.hpp" -#include "spi_image.hpp" -using namespace portapack; namespace ui { @@ -149,13 +146,7 @@ Context& SystemView::context() const { HackRFFirmwareView::HackRFFirmwareView(NavigationView& nav) { button_yes.on_select = [&nav](Button&){ - shutdown(); - - m4_init(spi_flash::hackrf, memory::map::m4_code_hackrf); - - while(true) { - __WFE(); - } + m4_request_shutdown(); }; button_no.on_select = [&nav](Button&){ diff --git a/firmware/baseband/main.cpp b/firmware/baseband/main.cpp index f92d5f9f..72a13e0b 100755 --- a/firmware/baseband/main.cpp +++ b/firmware/baseband/main.cpp @@ -776,6 +776,10 @@ static void m0apptxevent_interrupt_enable() { nvicEnableVector(M0CORE_IRQn, CORTEX_PRIORITY_MASK(LPC43XX_M0APPTXEVENT_IRQ_PRIORITY)); } +static void m0apptxevent_interrupt_disable() { + nvicDisableVector(M0CORE_IRQn); +} + extern "C" { CH_IRQ_HANDLER(MAPP_IRQHandler) { @@ -792,6 +796,18 @@ CH_IRQ_HANDLER(MAPP_IRQHandler) { } +static void shutdown() { + // TODO: Is this complete? + + nvicDisableVector(DMA_IRQn); + + m0apptxevent_interrupt_disable(); + + chSysDisable(); + + systick_stop(); +} + static constexpr auto direction = baseband::Direction::Receive; int main(void) { @@ -849,6 +865,12 @@ int main(void) { } ); + message_handlers.register_handler(Message::ID::Shutdown, + [&event_dispatcher](const Message* const) { + event_dispatcher.request_stop(); + } + ); + /* TODO: Ensure DMAs are configured to point at first LLI in chain. */ if( direction == baseband::Direction::Receive ) { @@ -868,5 +890,10 @@ int main(void) { event_dispatcher.run(); + shutdown(); + + ShutdownMessage shutdown_message; + shared_memory.application_queue.push(shutdown_message); + return 0; } diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index 72f73369..fa06c703 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -43,7 +43,7 @@ public: BasebandConfiguration = 5, FSKConfiguration = 6, FSKPacket = 7, - TestResults = 8, + Shutdown = 8, MAX }; @@ -233,6 +233,14 @@ public: FSKPacket packet; }; +class ShutdownMessage : public Message { +public: + constexpr ShutdownMessage( + ) : Message { ID::Shutdown } + { + } +}; + class MessageHandlerMap { public: using MessageHandler = std::function;