Skip to content

Commit

Permalink
espflasher version by Willy-JL
Browse files Browse the repository at this point in the history
with small changes
- Extra checks added for otg power
- Included firmware binaries are packed into fap, will be unpacked on first launch, second launch will be fast
  • Loading branch information
xMasterX committed Jan 19, 2024
1 parent 822dfe6 commit 509478f
Show file tree
Hide file tree
Showing 165 changed files with 31,858 additions and 60 deletions.
37 changes: 21 additions & 16 deletions non_catalog_apps/flipperzero-esp-flasher/application.fam
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
App(
appid="esp_flasher",
name="ESP Flasher",
fap_version=(1,4),
fap_version=(1, 4),
apptype=FlipperAppType.EXTERNAL,
entry_point="esp_flasher_app",
requires=["gui"],
stack_size=4 * 1024,
order=90,
fap_file_assets="packedfws",
fap_icon="wifi_10px.png",
fap_category="GPIO",
fap_private_libs=[
Lib(
name="esp-serial-flasher",
fap_include_paths=["include"],
sources=[
"src/esp_loader.c",
"src/esp_targets.c",
"src/md5_hash.c",
"src/protocol_common.c",
"src/protocol_uart.c",
"src/slip.c"
],
cincludes=["lib/esp-serial-flasher/private_include"],
cdefines=["SERIAL_FLASHER_INTERFACE_UART=1", "MD5_ENABLED=1", "SERIAL_FLASHER_WRITE_BLOCK_RETRIES=10"],
),
],
Lib(
name="esp-serial-flasher",
fap_include_paths=["include"],
sources=[
"src/esp_loader.c",
"src/esp_targets.c",
"src/md5_hash.c",
"src/protocol_common.c",
"src/protocol_uart.c",
"src/slip.c",
],
cincludes=["lib/esp-serial-flasher/private_include"],
cdefines=[
"SERIAL_FLASHER_INTERFACE_UART=1",
"MD5_ENABLED=1",
"SERIAL_FLASHER_WRITE_BLOCK_RETRIES=10",
],
),
],
cdefines=["SERIAL_FLASHER_INTERFACE_UART=1"],
fap_icon_assets="assets",
)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion non_catalog_apps/flipperzero-esp-flasher/esp_flasher_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ EspFlasherApp* esp_flasher_app_alloc() {

app->reset = false;
app->boot = false;
app->quickflash = false;

app->turbospeed = false;

Expand Down Expand Up @@ -118,6 +119,7 @@ int32_t esp_flasher_app(void* p) {
UNUSED(p);

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,7 +136,7 @@ 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();
}

Expand Down
3 changes: 3 additions & 0 deletions non_catalog_apps/flipperzero-esp-flasher/esp_flasher_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <dialogs/dialogs.h>
#include <notification/notification_messages.h>

#include <assets_icons.h>

#define ESP_FLASHER_TEXT_BOX_STORE_SIZE (4096)

#define ESP_APP_FOLDER_USER "apps_data/esp_flasher"
Expand Down Expand Up @@ -66,6 +68,7 @@ struct EspFlasherApp {

bool reset;
bool boot;
bool quickflash;

SwitchFirmware switch_fw;

Expand Down
42 changes: 20 additions & 22 deletions non_catalog_apps/flipperzero-esp-flasher/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 non_catalog_apps/flipperzero-esp-flasher/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);
46 changes: 35 additions & 11 deletions non_catalog_apps/flipperzero-esp-flasher/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 All @@ -272,6 +276,7 @@ static int32_t esp_flasher_flash_bin(void* context) {

// done
app->flash_worker_busy = false;
app->quickflash = false;

// cleanup
furi_stream_buffer_free(flash_rx_stream);
Expand Down Expand Up @@ -306,6 +311,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 All @@ -319,6 +327,10 @@ static int32_t esp_flasher_reset(void* context) {
app->reset = false;
app->boot = false;

if(app->quickflash) {
esp_flasher_flash_bin(app);
}

return 0;
}

Expand Down Expand Up @@ -353,7 +365,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 +376,18 @@ 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);
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_disable_otg();
}
loader_port_delay_ms(100);
if(!furi_hal_power_is_otg_enabled()) {
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 Expand Up @@ -409,4 +433,4 @@ void esp_flasher_worker_handle_rx_data_cb(uint8_t* buf, size_t len, void* contex
// done flashing
if(global_app) esp_flasher_console_output_handle_rx_data_cb(buf, len, global_app);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Bug report
description: Report a problem with esp-serial-flasher operation
labels: ['Type: Bug']
body:
- type: markdown
attributes:
value: |
* Please ensure you are using the latest version of `esp-serial-flasher`.
* Try using one of the examples from the `examples` directory and following the example documentation.
* If your board is a custom design, consider using our [free-of-charge schematic and PCB review service](https://www.espressif.com/en/contact-us/circuit-schematic-pcb-design-review).
* If still experiencing issues, please provide as many details as possible below about your hardware and software setup.
- type: input
id: port
attributes:
label: Port
description: Which port are you experiencing the issue with?
placeholder: ex. ESP, STM32
validations:
required: true
- type: input
id: target
attributes:
label: Target chip
description: Which chip are you trying to flash?
placeholder: ex. ESP8266, ESP32, ESP32-C3
validations:
required: true
- type: textarea
id: other-hw
attributes:
label: Hardware Configuration
description: What dev boards/custom PCB are you using, how are the chips connected, which baudrate are you trying to flash with?
validations:
required: true
id: output
- type: textarea
attributes:
label: Log output
description: Enable tracing with SERIAL_FLASHER_DEBUG_TRACE and provide the full log.
render: plain
validations:
required: true
- type: textarea
id: more-info
attributes:
label: More Information
description: Provide any additional information relevant to the issue.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Feature request
description: Suggest an idea for this project
labels: ['Type: Feature Request']
body:
- type: markdown
attributes:
value: |
* We welcome any ideas or feature requests! It's helpful if you can explain exactly why the feature would be useful.
* Please check if the feature has already been suggested in [existing issues](https://github.com/espressif/esp-serial-flasher/issues) or [pull requests](https://github.com/espressif/esp-serial-flasher/pulls).
* Please provide enough context so that the reasoning behind the suggestion can be understood.
- type: textarea
id: problem-related
attributes:
label: Is your feature request related to a problem?
description: Please provide a clear and concise description of what the problem is.
validations:
required: true
- type: textarea
id: solution
attributes:
label: Describe the solution you'd like
description: Please provide a clear and concise description of a solution of the described problem or usecase.
validations:
required: true
- type: textarea
id: alternatives
attributes:
label: Describe alternatives you've considered
description: Please provide a clear and concise description of any alternative solutions or features you've considered.
- type: textarea
id: context
attributes:
label: Additional context
description: Please add any other context here.
Loading

0 comments on commit 509478f

Please sign in to comment.