Skip to content

Commit

Permalink
Copy serial fixes from xtreme-apps
Browse files Browse the repository at this point in the history
  • Loading branch information
0xchocolate committed Feb 4, 2024
1 parent 98fed49 commit bd52f0c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
12 changes: 11 additions & 1 deletion esp_flasher_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <furi.h>
#include <furi_hal.h>
#include <expansion/expansion.h>

static bool esp_flasher_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Expand Down Expand Up @@ -117,7 +118,12 @@ void esp_flasher_app_free(EspFlasherApp* app) {
int32_t esp_flasher_app(void* 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;
bool otg_was_enabled = furi_hal_power_is_otg_enabled();
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
furi_hal_power_enable_otg();
furi_delay_ms(10);
Expand All @@ -134,9 +140,13 @@ int32_t esp_flasher_app(void* p) {

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();
}

// Return previous state of expansion
expansion_enable(expansion);
furi_record_close(RECORD_EXPANSION);

return 0;
}
42 changes: 20 additions & 22 deletions esp_flasher_uart.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#include "esp_flasher_app_i.h"
#include "esp_flasher_uart.h"

#define UART_CH (FuriHalUartIdUSART1)
#define BAUDRATE (115200)

struct EspFlasherUart {
EspFlasherApp* app;
FuriHalUartId channel;
FuriThread* rx_thread;
FuriStreamBuffer* rx_stream;
uint8_t rx_buf[RX_BUF_SIZE + 1];
void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
FuriHalSerialHandle* serial_handle;
};

typedef enum {
Expand All @@ -27,10 +24,14 @@ void esp_flasher_uart_set_handle_rx_data_cb(

#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;

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_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
}
Expand All @@ -57,30 +58,30 @@ static int32_t uart_worker(void* context) {
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*
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));

uart->app = app;
uart->channel = channel;
uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
uart->rx_thread = furi_thread_alloc();
furi_thread_set_name(uart->rx_thread, thread_name);
furi_thread_set_stack_size(uart->rx_thread, 1024);
furi_thread_set_context(uart->rx_thread, uart);
furi_thread_set_callback(uart->rx_thread, uart_worker);
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;
}
Expand All @@ -96,11 +97,8 @@ void esp_flasher_uart_free(EspFlasherUart* uart) {
furi_thread_join(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);
}
7 changes: 6 additions & 1 deletion esp_flasher_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

#include "furi_hal.h"

#define UART_CH (FuriHalSerialIdUsart)
#define BAUDRATE (115200)
#define FAST_BAUDRATE (921600)

#define RX_BUF_SIZE (2048)

typedef struct EspFlasherUart EspFlasherUart;

void esp_flasher_uart_set_handle_rx_data_cb(
EspFlasherUart* uart,
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);
void esp_flasher_uart_free(EspFlasherUart* uart);
35 changes: 25 additions & 10 deletions esp_flasher_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,29 +230,33 @@ static int32_t esp_flasher_flash_bin(void* context) {
// higher BR
if(!err && app->turbospeed) {
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];
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);
}
furi_hal_uart_set_br(FuriHalUartIdUSART1, 921600);
esp_flasher_uart_set_br(app->uart, FAST_BAUDRATE);
}

if(!err) {
loader_port_debug_print("Connected\n");
uint32_t start_time = furi_get_tick();

if(!_switch_fw(app)) {
_flash_all_files(app);
}
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");
furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
esp_flasher_uart_set_br(app->uart, BAUDRATE);
}

loader_port_debug_print(
Expand Down Expand Up @@ -306,6 +310,9 @@ static int32_t esp_flasher_reset(void* context) {
_setRTS(false);
_initRTS();

furi_hal_gpio_init_simple(&gpio_swclk, GpioModeOutputPushPull);
furi_hal_gpio_write(&gpio_swclk, true);

if(app->reset) {
loader_port_debug_print("Resetting board\n");
loader_port_reset_target();
Expand Down Expand Up @@ -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) {
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;
}

Expand All @@ -364,6 +371,14 @@ void loader_port_reset_target(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
// (works on official wifi dev board)
_setDTR(true);
Expand Down

0 comments on commit bd52f0c

Please sign in to comment.