Explorar el Código

Update GB Emulator 2.0 New Serial API

Esteban Fuentealba hace 1 año
padre
commit
8f0541f0a1
Se han modificado 3 ficheros con 58 adiciones y 20 borrados
  1. 1 0
      application.fam
  2. 54 18
      malveke_gb_emulator.c
  3. 3 2
      malveke_gb_emulator.h

+ 1 - 0
application.fam

@@ -11,6 +11,7 @@ App(
 	fap_icon="icons/icon.png",
 	fap_icon="icons/icon.png",
     fap_icon_assets="icons",
     fap_icon_assets="icons",
     fap_category="GPIO",
     fap_category="GPIO",
+    fap_version=[2,0],
 	fap_description="GAME BOY Emulator (POC).",
 	fap_description="GAME BOY Emulator (POC).",
 	fap_author="Esteban Fuentealba",
 	fap_author="Esteban Fuentealba",
 	fap_weburl="https://github.com/EstebanFuentealba/MALVEKE-Flipper-Zero/"
 	fap_weburl="https://github.com/EstebanFuentealba/MALVEKE-Flipper-Zero/"

+ 54 - 18
malveke_gb_emulator.c

@@ -51,27 +51,39 @@ static bool malveke_gb_emulator_view_input_callback(InputEvent* event, void* con
     if (event->type == InputTypePress){
     if (event->type == InputTypePress){
         if (event->key == InputKeyUp){
         if (event->key == InputKeyUp){
             const char gbemulator_command_up[] = "U\n";
             const char gbemulator_command_up[] = "U\n";
-            furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_up, strlen(gbemulator_command_up));
+            furi_hal_serial_tx(instance->serial_handle_uart, 
+                            (uint8_t*)gbemulator_command_up, 
+                            strlen(gbemulator_command_up));
+
         }
         }
         else if (event->key == InputKeyDown){
         else if (event->key == InputKeyDown){
             const char gbemulator_command_down[] = "D\n";
             const char gbemulator_command_down[] = "D\n";
-            furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_down, strlen(gbemulator_command_down));
+            furi_hal_serial_tx(instance->serial_handle_uart, 
+                            (uint8_t*)gbemulator_command_down, 
+                            strlen(gbemulator_command_down));
         }
         }
         else if (event->key == InputKeyRight){
         else if (event->key == InputKeyRight){
             const char gbemulator_command_right[] = ">\n";
             const char gbemulator_command_right[] = ">\n";
-            furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_right, strlen(gbemulator_command_right));
+            furi_hal_serial_tx(instance->serial_handle_uart, 
+                            (uint8_t*)gbemulator_command_right, 
+                            strlen(gbemulator_command_right));
         }
         }
         else if (event->key == InputKeyLeft){
         else if (event->key == InputKeyLeft){
             const char gbemulator_command_left[] = "<\n";
             const char gbemulator_command_left[] = "<\n";
-            furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_left, strlen(gbemulator_command_left));
+            furi_hal_serial_tx(instance->serial_handle_uart, 
+                            (uint8_t*)gbemulator_command_left, 
+                            strlen(gbemulator_command_left));
         }
         }
         else if (event->key == InputKeyOk){
         else if (event->key == InputKeyOk){
             with_view_model(
             with_view_model(
                 instance->view,
                 instance->view,
                 UartDumpModel * model,
                 UartDumpModel * model,
                 {
                 {
+                    UNUSED(model);
                     const char gbemulator_command_OK[] = "S\n";
                     const char gbemulator_command_OK[] = "S\n";
-                    furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)gbemulator_command_OK, strlen(gbemulator_command_OK));  
+                    furi_hal_serial_tx(instance->serial_handle_uart, 
+                            (uint8_t*)gbemulator_command_OK, 
+                            strlen(gbemulator_command_OK)); 
                 },
                 },
                 false);
                 false);
 
 
@@ -82,17 +94,20 @@ static bool malveke_gb_emulator_view_input_callback(InputEvent* event, void* con
 }
 }
 
 
 static uint32_t malveke_gb_emulator_exit(void* context) {
 static uint32_t malveke_gb_emulator_exit(void* context) {
-    UNUSED(context);
+    UartEchoApp* instance = context;
     const char stop_command[] = "stopgblivecamera\n";
     const char stop_command[] = "stopgblivecamera\n";
-    furi_hal_uart_tx(FuriHalUartIdUSART1, (uint8_t*)stop_command, strlen(stop_command));
+    furi_hal_serial_tx(instance->serial_handle_uart, 
+                            (uint8_t*)stop_command, 
+                            strlen(stop_command));
     return VIEW_NONE;
     return VIEW_NONE;
 }
 }
 
 
-static void malveke_gb_emulator_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
+static void malveke_gb_emulator_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
     furi_assert(context);
     furi_assert(context);
     UartEchoApp* app = context;
     UartEchoApp* app = context;
 
 
-    if(ev == UartIrqEventRXNE) {
+    if(event == FuriHalSerialRxEventData) {
+        uint8_t data = furi_hal_serial_async_rx(handle);
         furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
         furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
         furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
         furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
     }
     }
@@ -208,12 +223,24 @@ static UartEchoApp* malveke_gb_emulator_app_alloc() {
     furi_thread_start(app->worker_thread);
     furi_thread_start(app->worker_thread);
 
 
     // Enable uart listener (UART & UART1)
     // Enable uart listener (UART & UART1)
-    // furi_hal_console_disable();
-    furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
-    furi_hal_uart_init(FuriHalUartIdLPUART1, 250000);
-    furi_hal_uart_set_br(FuriHalUartIdLPUART1, 250000);
-    furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, malveke_gb_emulator_on_irq_cb, app);
-    // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, malveke_gb_emulator_on_irq_cb, app);
+    app->serial_handle_uart = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
+    if(!app->serial_handle_uart) {
+        furi_delay_ms(5000);
+    }
+    furi_check(app->serial_handle_uart);
+    furi_hal_serial_init(app->serial_handle_uart,  115200);
+
+
+    app->serial_handle_lp_uart = furi_hal_serial_control_acquire(FuriHalSerialIdLpuart);
+    if(!app->serial_handle_lp_uart) {
+        furi_delay_ms(5000);
+    }
+    furi_check(app->serial_handle_lp_uart);
+    furi_hal_serial_init(app->serial_handle_lp_uart,  250000);
+    furi_hal_serial_async_rx_start(app->serial_handle_lp_uart, malveke_gb_emulator_on_irq_cb, app, false);
+
+
+
     furi_hal_power_enable_otg();
     furi_hal_power_enable_otg();
     furi_delay_ms(1); 
     furi_delay_ms(1); 
     return app;
     return app;
@@ -226,9 +253,11 @@ static void malveke_gb_emulator_app_free(UartEchoApp* app) {
     furi_thread_join(app->worker_thread);
     furi_thread_join(app->worker_thread);
     furi_thread_free(app->worker_thread);
     furi_thread_free(app->worker_thread);
 
 
-    // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
-    furi_hal_uart_set_irq_cb(FuriHalUartIdLPUART1, NULL, NULL);
-    furi_hal_uart_deinit(FuriHalUartIdLPUART1);
+    furi_hal_serial_deinit(app->serial_handle_uart);
+    furi_hal_serial_control_release(app->serial_handle_uart);
+
+    furi_hal_serial_deinit(app->serial_handle_lp_uart);
+    furi_hal_serial_control_release(app->serial_handle_lp_uart);
 
 
     // Free views
     // Free views
     view_dispatcher_remove_view(app->view_dispatcher, 0);
     view_dispatcher_remove_view(app->view_dispatcher, 0);
@@ -249,6 +278,9 @@ static void malveke_gb_emulator_app_free(UartEchoApp* app) {
 
 
 int32_t malveke_gb_emulator_app(void* p) {
 int32_t malveke_gb_emulator_app(void* p) {
     UNUSED(p);
     UNUSED(p);
+    // Disable expansion protocol to avoid interference with UART Handle
+    Expansion* expansion = furi_record_open(RECORD_EXPANSION);
+    expansion_disable(expansion);
 
 
     UartEchoApp* app = malveke_gb_emulator_app_alloc();
     UartEchoApp* app = malveke_gb_emulator_app_alloc();
     view_dispatcher_run(app->view_dispatcher);
     view_dispatcher_run(app->view_dispatcher);
@@ -256,5 +288,9 @@ int32_t malveke_gb_emulator_app(void* p) {
     
     
     furi_hal_power_disable_otg();
     furi_hal_power_disable_otg();
 
 
+    // Return previous state of expansion
+    expansion_enable(expansion);
+    furi_record_close(RECORD_EXPANSION);
+    
     return 0;
     return 0;
 }
 }

+ 3 - 2
malveke_gb_emulator.h

@@ -14,12 +14,11 @@
 #include <notification/notification.h>
 #include <notification/notification.h>
 #include <notification/notification_messages.h>
 #include <notification/notification_messages.h>
 #include <gui/elements.h>
 #include <gui/elements.h>
-#include <furi_hal_uart.h>
-#include <furi_hal_console.h>
 #include <gui/view_dispatcher.h>
 #include <gui/view_dispatcher.h>
 #include <gui/modules/dialog_ex.h>
 #include <gui/modules/dialog_ex.h>
 #include <storage/filesystem_api_defines.h>
 #include <storage/filesystem_api_defines.h>
 #include <storage/storage.h>
 #include <storage/storage.h>
+#include <expansion/expansion.h>
 
 
 #define THREAD_ALLOC 2048
 #define THREAD_ALLOC 2048
 
 
@@ -53,6 +52,8 @@ typedef struct {
     Storage* storage;
     Storage* storage;
     NotificationApp* notification;
     NotificationApp* notification;
     ViewDispatcher* view_dispatcher;
     ViewDispatcher* view_dispatcher;
+    FuriHalSerialHandle* serial_handle_uart;
+    FuriHalSerialHandle* serial_handle_lp_uart;
     View* view;
     View* view;
     FuriThread* worker_thread;
     FuriThread* worker_thread;
     FuriStreamBuffer* rx_stream;
     FuriStreamBuffer* rx_stream;