Explorar el Código

Copy serial fixes from xtreme-apps

0xchocolate hace 1 año
padre
commit
bd52f0cffb
Se han modificado 4 ficheros con 62 adiciones y 34 borrados
  1. 11 1
      esp_flasher_app.c
  2. 20 22
      esp_flasher_uart.c
  3. 6 1
      esp_flasher_uart.h
  4. 25 10
      esp_flasher_worker.c

+ 11 - 1
esp_flasher_app.c

@@ -2,6 +2,7 @@
 
 
 #include <furi.h>
 #include <furi.h>
 #include <furi_hal.h>
 #include <furi_hal.h>
+#include <expansion/expansion.h>
 
 
 static bool esp_flasher_app_custom_event_callback(void* context, uint32_t event) {
 static bool esp_flasher_app_custom_event_callback(void* context, uint32_t event) {
     furi_assert(context);
     furi_assert(context);
@@ -117,7 +118,12 @@ void esp_flasher_app_free(EspFlasherApp* app) {
 int32_t esp_flasher_app(void* p) {
 int32_t esp_flasher_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);
+
     uint8_t attempts = 0;
     uint8_t attempts = 0;
+    bool otg_was_enabled = furi_hal_power_is_otg_enabled();
     while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
     while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
         furi_hal_power_enable_otg();
         furi_hal_power_enable_otg();
         furi_delay_ms(10);
         furi_delay_ms(10);
@@ -134,9 +140,13 @@ int32_t esp_flasher_app(void* p) {
 
 
     esp_flasher_app_free(esp_flasher_app);
     esp_flasher_app_free(esp_flasher_app);
 
 
-    if(furi_hal_power_is_otg_enabled()) {
+    if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
         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;
 }
 }

+ 20 - 22
esp_flasher_uart.c

@@ -1,16 +1,13 @@
 #include "esp_flasher_app_i.h"
 #include "esp_flasher_app_i.h"
 #include "esp_flasher_uart.h"
 #include "esp_flasher_uart.h"
 
 
-#define UART_CH (FuriHalUartIdUSART1)
-#define BAUDRATE (115200)
-
 struct EspFlasherUart {
 struct EspFlasherUart {
     EspFlasherApp* app;
     EspFlasherApp* app;
-    FuriHalUartId channel;
     FuriThread* rx_thread;
     FuriThread* rx_thread;
     FuriStreamBuffer* rx_stream;
     FuriStreamBuffer* rx_stream;
     uint8_t rx_buf[RX_BUF_SIZE + 1];
     uint8_t rx_buf[RX_BUF_SIZE + 1];
     void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
     void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
+    FuriHalSerialHandle* serial_handle;
 };
 };
 
 
 typedef enum {
 typedef enum {
@@ -27,10 +24,14 @@ void esp_flasher_uart_set_handle_rx_data_cb(
 
 
 #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
 #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
 
 
-void esp_flasher_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
+void esp_flasher_uart_on_irq_cb(
+    FuriHalSerialHandle* handle,
+    FuriHalSerialRxEvent event,
+    void* context) {
     EspFlasherUart* uart = (EspFlasherUart*)context;
     EspFlasherUart* uart = (EspFlasherUart*)context;
 
 
-    if(ev == UartIrqEventRXNE) {
+    if(event == FuriHalSerialRxEventData) {
+        uint8_t data = furi_hal_serial_async_rx(handle);
         furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
         furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
         furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
         furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
     }
     }
@@ -57,16 +58,19 @@ static int32_t uart_worker(void* context) {
     return 0;
     return 0;
 }
 }
 
 
-void esp_flasher_uart_tx(uint8_t* data, size_t len) {
-    furi_hal_uart_tx(UART_CH, data, len);
+void esp_flasher_uart_tx(EspFlasherUart* uart, uint8_t* data, size_t len) {
+    furi_hal_serial_tx(uart->serial_handle, data, len);
+}
+
+void esp_flasher_uart_set_br(EspFlasherUart* uart, uint32_t baud) {
+    furi_hal_serial_set_br(uart->serial_handle, baud);
 }
 }
 
 
 EspFlasherUart*
 EspFlasherUart*
-    esp_flasher_uart_init(EspFlasherApp* app, FuriHalUartId channel, const char* thread_name) {
+    esp_flasher_uart_init(EspFlasherApp* app, FuriHalSerialId channel, const char* thread_name) {
     EspFlasherUart* uart = malloc(sizeof(EspFlasherUart));
     EspFlasherUart* uart = malloc(sizeof(EspFlasherUart));
 
 
     uart->app = app;
     uart->app = app;
-    uart->channel = channel;
     uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
     uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
     uart->rx_thread = furi_thread_alloc();
     uart->rx_thread = furi_thread_alloc();
     furi_thread_set_name(uart->rx_thread, thread_name);
     furi_thread_set_name(uart->rx_thread, thread_name);
@@ -74,13 +78,10 @@ EspFlasherUart*
     furi_thread_set_context(uart->rx_thread, uart);
     furi_thread_set_context(uart->rx_thread, uart);
     furi_thread_set_callback(uart->rx_thread, uart_worker);
     furi_thread_set_callback(uart->rx_thread, uart_worker);
     furi_thread_start(uart->rx_thread);
     furi_thread_start(uart->rx_thread);
-    if(channel == FuriHalUartIdUSART1) {
-        furi_hal_console_disable();
-    } else if(channel == FuriHalUartIdLPUART1) {
-        furi_hal_uart_init(channel, BAUDRATE);
-    }
-    furi_hal_uart_set_br(channel, BAUDRATE);
-    furi_hal_uart_set_irq_cb(channel, esp_flasher_uart_on_irq_cb, uart);
+    uart->serial_handle = furi_hal_serial_control_acquire(channel);
+    furi_check(uart->serial_handle);
+    furi_hal_serial_init(uart->serial_handle, BAUDRATE);
+    furi_hal_serial_async_rx_start(uart->serial_handle, esp_flasher_uart_on_irq_cb, uart, false);
 
 
     return uart;
     return uart;
 }
 }
@@ -96,11 +97,8 @@ void esp_flasher_uart_free(EspFlasherUart* uart) {
     furi_thread_join(uart->rx_thread);
     furi_thread_join(uart->rx_thread);
     furi_thread_free(uart->rx_thread);
     furi_thread_free(uart->rx_thread);
 
 
-    furi_hal_uart_set_irq_cb(uart->channel, NULL, NULL);
-    if(uart->channel == FuriHalUartIdLPUART1) {
-        furi_hal_uart_deinit(uart->channel);
-    }
-    furi_hal_console_enable();
+    furi_hal_serial_deinit(uart->serial_handle);
+    furi_hal_serial_control_release(uart->serial_handle);
 
 
     free(uart);
     free(uart);
 }
 }

+ 6 - 1
esp_flasher_uart.h

@@ -2,6 +2,10 @@
 
 
 #include "furi_hal.h"
 #include "furi_hal.h"
 
 
+#define UART_CH (FuriHalSerialIdUsart)
+#define BAUDRATE (115200)
+#define FAST_BAUDRATE (921600)
+
 #define RX_BUF_SIZE (2048)
 #define RX_BUF_SIZE (2048)
 
 
 typedef struct EspFlasherUart EspFlasherUart;
 typedef struct EspFlasherUart EspFlasherUart;
@@ -9,6 +13,7 @@ typedef struct EspFlasherUart EspFlasherUart;
 void esp_flasher_uart_set_handle_rx_data_cb(
 void esp_flasher_uart_set_handle_rx_data_cb(
     EspFlasherUart* uart,
     EspFlasherUart* uart,
     void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context));
     void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context));
-void esp_flasher_uart_tx(uint8_t* data, size_t len);
+void esp_flasher_uart_tx(EspFlasherUart* uart, uint8_t* data, size_t len);
+void esp_flasher_uart_set_br(EspFlasherUart* uart, uint32_t baud);
 EspFlasherUart* esp_flasher_usart_init(EspFlasherApp* app);
 EspFlasherUart* esp_flasher_usart_init(EspFlasherApp* app);
 void esp_flasher_uart_free(EspFlasherUart* uart);
 void esp_flasher_uart_free(EspFlasherUart* uart);

+ 25 - 10
esp_flasher_worker.c

@@ -230,29 +230,33 @@ static int32_t esp_flasher_flash_bin(void* context) {
     // higher BR
     // higher BR
     if(!err && app->turbospeed) {
     if(!err && app->turbospeed) {
         loader_port_debug_print("Increasing speed for faster flash\n");
         loader_port_debug_print("Increasing speed for faster flash\n");
-        err = esp_loader_change_transmission_rate(921600);
-        if (err != ESP_LOADER_SUCCESS) {
+        err = esp_loader_change_transmission_rate(FAST_BAUDRATE);
+        if(err != ESP_LOADER_SUCCESS) {
             char err_msg[256];
             char err_msg[256];
             snprintf(
             snprintf(
-                err_msg,
-                sizeof(err_msg),
-                "Cannot change transmission rate. Error: %u\n",
-                err);
+                err_msg, sizeof(err_msg), "Cannot change transmission rate. Error: %u\n", err);
             loader_port_debug_print(err_msg);
             loader_port_debug_print(err_msg);
         }
         }
-        furi_hal_uart_set_br(FuriHalUartIdUSART1, 921600);
+        esp_flasher_uart_set_br(app->uart, FAST_BAUDRATE);
     }
     }
 
 
     if(!err) {
     if(!err) {
         loader_port_debug_print("Connected\n");
         loader_port_debug_print("Connected\n");
+        uint32_t start_time = furi_get_tick();
+
         if(!_switch_fw(app)) {
         if(!_switch_fw(app)) {
             _flash_all_files(app);
             _flash_all_files(app);
         }
         }
         app->switch_fw = SwitchNotSet;
         app->switch_fw = SwitchNotSet;
 
 
-        if (app->turbospeed) {
+        FuriString* flash_time =
+            furi_string_alloc_printf("Flash took: %lds\n", (furi_get_tick() - start_time) / 1000);
+        loader_port_debug_print(furi_string_get_cstr(flash_time));
+        furi_string_free(flash_time);
+
+        if(app->turbospeed) {
             loader_port_debug_print("Restoring transmission rate\n");
             loader_port_debug_print("Restoring transmission rate\n");
-            furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
+            esp_flasher_uart_set_br(app->uart, BAUDRATE);
         }
         }
 
 
         loader_port_debug_print(
         loader_port_debug_print(
@@ -306,6 +310,9 @@ static int32_t esp_flasher_reset(void* context) {
     _setRTS(false);
     _setRTS(false);
     _initRTS();
     _initRTS();
 
 
+    furi_hal_gpio_init_simple(&gpio_swclk, GpioModeOutputPushPull);
+    furi_hal_gpio_write(&gpio_swclk, true);
+
     if(app->reset) {
     if(app->reset) {
         loader_port_debug_print("Resetting board\n");
         loader_port_debug_print("Resetting board\n");
         loader_port_reset_target();
         loader_port_reset_target();
@@ -353,7 +360,7 @@ esp_loader_error_t loader_port_read(uint8_t* data, uint16_t size, uint32_t timeo
 
 
 esp_loader_error_t loader_port_write(const uint8_t* data, uint16_t size, uint32_t timeout) {
 esp_loader_error_t loader_port_write(const uint8_t* data, uint16_t size, uint32_t timeout) {
     UNUSED(timeout);
     UNUSED(timeout);
-    esp_flasher_uart_tx((uint8_t*)data, size);
+    if(global_app) esp_flasher_uart_tx(global_app->uart, (uint8_t*)data, size);
     return ESP_LOADER_SUCCESS;
     return ESP_LOADER_SUCCESS;
 }
 }
 
 
@@ -364,6 +371,14 @@ void loader_port_reset_target(void) {
 }
 }
 
 
 void loader_port_enter_bootloader(void) {
 void loader_port_enter_bootloader(void) {
+    // Also support for the Multi-fucc and Xeon boards
+    furi_hal_gpio_write(&gpio_swclk, false);
+    furi_hal_power_disable_otg();
+    loader_port_delay_ms(100);
+    furi_hal_power_enable_otg();
+    furi_hal_gpio_init_simple(&gpio_swclk, GpioModeAnalog);
+    loader_port_delay_ms(100);
+
     // adapted from custom usb-jtag-serial reset in esptool
     // adapted from custom usb-jtag-serial reset in esptool
     // (works on official wifi dev board)
     // (works on official wifi dev board)
     _setDTR(true);
     _setDTR(true);