Skip to content

Commit

Permalink
Implement auto-reset for supported boards and reset/boot menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
0xchocolate committed Jul 22, 2023
1 parent b28f122 commit 8068506
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
3 changes: 3 additions & 0 deletions esp_flasher_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ EspFlasherApp* esp_flasher_app_alloc() {

app->flash_worker_busy = false;

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

scene_manager_next_scene(app->scene_manager, EspFlasherSceneStart);

return app;
Expand Down
2 changes: 1 addition & 1 deletion esp_flasher_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern "C" {
#endif

#define ESP_FLASHER_APP_VERSION "v1.0"
#define ESP_FLASHER_APP_VERSION "v1.1"

typedef struct EspFlasherApp EspFlasherApp;

Expand Down
3 changes: 3 additions & 0 deletions esp_flasher_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ struct EspFlasherApp {

EspFlasherUart* uart;

bool reset;
bool boot;

bool selected_flash_options[NUM_FLASH_OPTIONS];
int num_selected_flash_options;
char bin_file_path_boot[100];
Expand Down
62 changes: 60 additions & 2 deletions esp_flasher_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ static int32_t esp_flasher_flash_bin(void* context) {
furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
#endif
loader_port_debug_print("Done flashing. Please reset the board manually.\n");
loader_port_reset_target();
}

// done
Expand All @@ -182,14 +183,60 @@ static int32_t esp_flasher_flash_bin(void* context) {
return 0;
}

static void _initDTR(void) {
furi_hal_gpio_init(&gpio_ext_pc3, GpioModeOutputPushPull, GpioPullDown, GpioSpeedVeryHigh);
}

static void _initRTS(void) {
furi_hal_gpio_init(&gpio_ext_pb2, GpioModeOutputPushPull, GpioPullDown, GpioSpeedVeryHigh);
}

static void _setDTR(bool state) {
furi_hal_gpio_write(&gpio_ext_pc3, state);
}

static void _setRTS(bool state) {
furi_hal_gpio_write(&gpio_ext_pb2, state);
}

static int32_t esp_flasher_reset(void* context) {
EspFlasherApp* app = (void*)context;

app->flash_worker_busy = true;

_setDTR(false);
_initDTR();
_setRTS(false);
_initRTS();

if (app->reset) {
loader_port_debug_print("Resetting board\n");
loader_port_reset_target();
} else if (app->boot) {
loader_port_debug_print("Entering bootloader\n");
loader_port_enter_bootloader();
}

// done
app->flash_worker_busy = false;
app->reset = false;
app->boot = false;

return 0;
}

void esp_flasher_worker_start_thread(EspFlasherApp* app) {
global_app = app;

app->flash_worker = furi_thread_alloc();
furi_thread_set_name(app->flash_worker, "EspFlasherFlashWorker");
furi_thread_set_stack_size(app->flash_worker, 2048);
furi_thread_set_context(app->flash_worker, app);
furi_thread_set_callback(app->flash_worker, esp_flasher_flash_bin);
if (app->reset || app->boot) {
furi_thread_set_callback(app->flash_worker, esp_flasher_reset);
} else {
furi_thread_set_callback(app->flash_worker, esp_flasher_flash_bin);
}
furi_thread_start(app->flash_worker);
}

Expand All @@ -213,8 +260,19 @@ esp_loader_error_t loader_port_write(const uint8_t* data, uint16_t size, uint32_
return ESP_LOADER_SUCCESS;
}

void loader_port_reset_target(void) {
_setDTR(true);
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
_setDTR(false);
}

void loader_port_enter_bootloader(void) {
// unimplemented
_setDTR(true);
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
_setRTS(true);
_setDTR(false);
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
_setRTS(false);
}

void loader_port_delay_ms(uint32_t ms) {
Expand Down
2 changes: 2 additions & 0 deletions esp_flasher_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef SERIAL_FLASHER_INTERFACE_UART
#define SERIAL_FLASHER_INTERFACE_UART /* TODO why is application.fam not passing this via cdefines */
#endif
#define SERIAL_FLASHER_RESET_HOLD_TIME_MS 100
#define SERIAL_FLASHER_BOOT_HOLD_TIME_MS 50
#include "esp_loader_io.h"

#define ESP_ADDR_BOOT_S3 0x0
Expand Down
22 changes: 22 additions & 0 deletions scenes/esp_flasher_scene_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
enum SubmenuIndex {
SubmenuIndexEspFlasherFlash,
SubmenuIndexEspFlasherAbout,
SubmenuIndexEspFlasherReset,
SubmenuIndexEspFlasherBootloader,
};

void esp_flasher_scene_start_submenu_callback(void* context, uint32_t index) {
Expand All @@ -23,6 +25,18 @@ void esp_flasher_scene_start_on_enter(void* context) {
SubmenuIndexEspFlasherFlash,
esp_flasher_scene_start_submenu_callback,
app);
submenu_add_item(
submenu,
"Reset Board",
SubmenuIndexEspFlasherReset,
esp_flasher_scene_start_submenu_callback,
app);
submenu_add_item(
submenu,
"Enter Bootloader",
SubmenuIndexEspFlasherBootloader,
esp_flasher_scene_start_submenu_callback,
app);
submenu_add_item(
submenu,
"About",
Expand All @@ -48,6 +62,14 @@ bool esp_flasher_scene_start_on_event(void* context, SceneManagerEvent event) {
} else if(event.event == SubmenuIndexEspFlasherFlash) {
scene_manager_next_scene(app->scene_manager, EspFlasherSceneBrowse);
consumed = true;
} else if(event.event == SubmenuIndexEspFlasherReset) {
app->reset = true;
scene_manager_next_scene(app->scene_manager, EspFlasherSceneConsoleOutput);
consumed = true;
} else if(event.event == SubmenuIndexEspFlasherBootloader) {
app->boot = true;
scene_manager_next_scene(app->scene_manager, EspFlasherSceneConsoleOutput);
consumed = true;
}
scene_manager_set_scene_state(app->scene_manager, EspFlasherSceneStart, event.event);
}
Expand Down

0 comments on commit 8068506

Please sign in to comment.