From 1f6ca725d0ba2897a03b4c3cba8ec552bfbb00b6 Mon Sep 17 00:00:00 2001 From: Electromuis Date: Fri, 22 Mar 2024 12:15:43 +0100 Subject: [PATCH 1/3] Working on desktop support --- .gitmodules | 3 + examples/common/example_common.c | 17 +- examples/desktop_esp32_example/CMakeLists.txt | 80 +++++++++ examples/desktop_esp32_example/main/main.cpp | 62 +++++++ port/serial_port.cpp | 169 ++++++++++++++++++ port/serial_port.h | 35 ++++ private_include/protocol.h | 41 +++-- src/protocol_common.c | 8 +- submodules/serial | 1 + 9 files changed, 392 insertions(+), 24 deletions(-) create mode 100644 examples/desktop_esp32_example/CMakeLists.txt create mode 100644 examples/desktop_esp32_example/main/main.cpp create mode 100644 port/serial_port.cpp create mode 100644 port/serial_port.h create mode 160000 submodules/serial diff --git a/.gitmodules b/.gitmodules index 0bb1151..0dcecd1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "submodules/stm32-cmake"] path = submodules/stm32-cmake url = https://github.com/ObKo/stm32-cmake.git +[submodule "submodules/serial"] + path = submodules/serial + url = https://github.com/wjwwood/serial diff --git a/examples/common/example_common.c b/examples/common/example_common.c index c6c8aff..e74428f 100644 --- a/examples/common/example_common.c +++ b/examples/common/example_common.c @@ -17,11 +17,21 @@ #include #include #include +#if !defined(WIN32) #include +#endif #include "esp_loader_io.h" #include "esp_loader.h" #include "example_common.h" +#ifndef MAX +#define MAX(a, b) ((a) > (b)) ? (a) : (b) +#endif + +#ifndef MIN +#define MIN(a, b) ((a) < (b)) ? (a) : (b) +#endif + #ifndef SINGLE_TARGET_SUPPORT @@ -336,7 +346,8 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) printf("Start loading\n"); esp_loader_error_t err; const esp_loader_bin_header_t *header = (const esp_loader_bin_header_t *)bin; - esp_loader_bin_segment_t segments[header->segments]; + //esp_loader_bin_segment_t segments[header->segments]; + esp_loader_bin_segment_t* segments = malloc(header->segments * sizeof(esp_loader_bin_segment_t)); // Parse segments uint32_t seg; @@ -357,6 +368,7 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) err = esp_loader_mem_start(segments[seg].addr, segments[seg].size, ESP_RAM_BLOCK); if (err != ESP_LOADER_SUCCESS) { printf("Loading ram start with error %d.\n", err); + free(segments); return err; } @@ -367,6 +379,7 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) err = esp_loader_mem_write(data_pos, data_size); if (err != ESP_LOADER_SUCCESS) { printf("\nPacket could not be written! Error %d.\n", err); + free(segments); return err; } data_pos += data_size; @@ -377,9 +390,11 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) err = esp_loader_mem_finish(header->entrypoint); if (err != ESP_LOADER_SUCCESS) { printf("\nLoad ram finish with Error %d.\n", err); + free(segments); return err; } printf("\nFinished loading\n"); + free(segments); return ESP_LOADER_SUCCESS; } diff --git a/examples/desktop_esp32_example/CMakeLists.txt b/examples/desktop_esp32_example/CMakeLists.txt new file mode 100644 index 0000000..970b778 --- /dev/null +++ b/examples/desktop_esp32_example/CMakeLists.txt @@ -0,0 +1,80 @@ +set(ESPSERIAL_PATH ${CMAKE_CURRENT_LIST_DIR}/../../) + +# LIB serial + +SET(serial_path ${ESPSERIAL_PATH}submodules/serial) +SET(serial_SRCS ${serial_path}/src/serial.cc) + +if(APPLE) + # If OSX + list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_osx.cc) +elseif(UNIX) + # If unix + list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_linux.cc) +else() + # If windows + list(APPEND serial_SRCS ${serial_path}/src/impl/win.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_win.cc) +endif() + +add_library(serial + ${serial_SRCS} +) + +target_include_directories(serial PUBLIC + ${serial_path}/include +) + +# LIB esp_serial + +include(${ESPSERIAL_PATH}examples/common/bin2array.cmake) +create_resources(${ESPSERIAL_PATH}examples/binaries/Hello-world ${CMAKE_BINARY_DIR}/binaries_1.c) +set_property(SOURCE ${CMAKE_BINARY_DIR}/binaries_1.c PROPERTY GENERATED 1) +create_resources(${ESPSERIAL_PATH}examples/binaries/RAM_APP ${CMAKE_BINARY_DIR}/binaries_2.c) +set_property(SOURCE ${CMAKE_BINARY_DIR}/binaries_2.c PROPERTY GENERATED 1) + + +add_library(esp_serial + ${ESPSERIAL_PATH}port/serial_port.cpp + ${ESPSERIAL_PATH}src/esp_loader.c + ${ESPSERIAL_PATH}src/esp_targets.c + ${ESPSERIAL_PATH}src/md5_hash.c + ${ESPSERIAL_PATH}src/slip.c + ${ESPSERIAL_PATH}src/protocol_uart.c + ${ESPSERIAL_PATH}src/protocol_common.c + ${CMAKE_BINARY_DIR}/binaries_1.c + ${CMAKE_BINARY_DIR}/binaries_2.c + ${ESPSERIAL_PATH}examples/common/example_common.c +) + +target_include_directories(esp_serial PUBLIC + ${ESPSERIAL_PATH}include + ${ESPSERIAL_PATH}port + ${ESPSERIAL_PATH}examples/common +) + +target_include_directories(esp_serial PRIVATE + ${ESPSERIAL_PATH}private_include +) + +target_compile_definitions(esp_serial PUBLIC + -DSERIAL_FLASHER_INTERFACE_USB + -DMD5_ENABLED + -DSERIAL_FLASHER_WRITE_BLOCK_RETRIES=4 +) + +target_link_libraries(esp_serial PUBLIC + serial +) + +# EXECUTABLE desktop_esp32_example + +add_executable(desktop_esp32_example + main/main.cpp +) + +target_link_libraries(desktop_esp32_example PRIVATE + esp_serial +) \ No newline at end of file diff --git a/examples/desktop_esp32_example/main/main.cpp b/examples/desktop_esp32_example/main/main.cpp new file mode 100644 index 0000000..216188d --- /dev/null +++ b/examples/desktop_esp32_example/main/main.cpp @@ -0,0 +1,62 @@ +/* Flash multiple partitions example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include "stdio.h" +#include + +#include "serial_port.h" +#include "esp_loader.h" +#include "esp_loader_io.h" +extern "C" { +#include "example_common.h" + +} + +#include "serial/serial.h" + +#define HIGHER_BAUDRATE 230400 + +int main(int argv, char **argc) +{ + if(argv < 2) { + printf("Usage: %s \n", argc[0]); + return 1; + } + + example_binaries_t bin; + + loader_serial_config_t config; + config.portName = argc[1]; + // config.portName = "COM3"; + config.baudrate = 115200; + config.timeout = 1000; + + if (loader_port_serial_init((const loader_serial_config_t*)&config) != ESP_LOADER_SUCCESS) { + printf("Serial initialization failed.\n"); + return -1; + } + + if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) { + + get_example_binaries(esp_loader_get_target(), &bin); + + printf("Loading bootloader...\n"); + flash_binary(bin.boot.data, bin.boot.size, bin.boot.addr); + printf("Loading partition table...\n"); + flash_binary(bin.part.data, bin.part.size, bin.part.addr); + printf("Loading app...\n"); + flash_binary(bin.app.data, bin.app.size, bin.app.addr); + printf("Done!\n"); + } else { + printf("Connect failed\n"); + return -1; + } + + return 0; +} diff --git a/port/serial_port.cpp b/port/serial_port.cpp new file mode 100644 index 0000000..55fe9e5 --- /dev/null +++ b/port/serial_port.cpp @@ -0,0 +1,169 @@ +/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "serial/serial.h" + +#include "serial_port.h" + +using std::chrono::high_resolution_clock; +using std::chrono::duration_cast; +using std::chrono::duration; +using std::chrono::milliseconds; +using namespace std::chrono_literals; + +loader_serial_config_t serial_config; +std::unique_ptr serial_port; +std::chrono::steady_clock::time_point serial_timer; +uint32_t serial_timer_offset = 0; + +#ifdef SERIAL_FLASHER_DEBUG_TRACE +static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write) +{ + static bool write_prev = false; + + if (write_prev != write) { + write_prev = write; + printf("\n--- %s ---\n", write ? "WRITE" : "READ"); + } + + for (uint32_t i = 0; i < size; i++) { + printf("%02x ", data[i]); + } +} +#endif + +static uint32_t s_time_end; + +esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout) +{ + try { + if(!serial_port || !serial_port->isOpen()) + return ESP_LOADER_ERROR_FAIL; + + serial_port->setTimeout(serial::Timeout::simpleTimeout(timeout)); + size_t result = serial_port->write(data, size); + if (result != size) { + return ESP_LOADER_ERROR_FAIL; + } + } catch (...) { + return ESP_LOADER_ERROR_FAIL; + } + + #ifdef SERIAL_FLASHER_DEBUG_TRACE + transfer_debug_print(data, size, true); + #endif + + return ESP_LOADER_SUCCESS; +} + + +esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout) +{ + try { + if(!serial_port || !serial_port->isOpen()) + return ESP_LOADER_ERROR_FAIL; + + size_t result = serial_port->read(data, size); + if (result != size) { + return ESP_LOADER_ERROR_FAIL; + } + } catch (...) { + return ESP_LOADER_ERROR_FAIL; + } + + #ifdef SERIAL_FLASHER_DEBUG_TRACE + transfer_debug_print(data, size, true); + #endif + + return ESP_LOADER_SUCCESS; +} + +esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config) +{ + serial_config = *config; + + try { + serial_port = std::make_unique(serial_config.portName, config->baudrate, serial::Timeout::simpleTimeout(config->timeout)); + if ( serial_port->isOpen() == false ) { + return ESP_LOADER_ERROR_FAIL; + } + } catch (...) { + return ESP_LOADER_ERROR_FAIL; + } + + return ESP_LOADER_SUCCESS; +} + +// Set GPIO0 LOW, then +// assert reset pin for 100 milliseconds. +void loader_port_enter_bootloader(void) +{ + // todo + loader_port_reset_target(); +} + + +void loader_port_reset_target(void) +{ + // todo +} + + +void loader_port_delay_ms(uint32_t ms) +{ + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); +} + + +void loader_port_start_timer(uint32_t ms) +{ + serial_timer = high_resolution_clock::now(); + serial_timer_offset = ms; +} + + +uint32_t loader_port_remaining_time(void) +{ + auto time_now = high_resolution_clock::now(); + int32_t remaining = (duration_cast(time_now - serial_timer - serial_timer_offset * 1ms)).count(); + return (remaining > 0) ? (uint32_t)remaining : 0; +} + + +void loader_port_debug_print(const char *str) +{ + printf("DEBUG: %s", str); +} + +esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate) +{ + try { + serial_port = std::make_unique(serial_config.portName, baudrate, serial::Timeout::simpleTimeout(serial_config.timeout)); + + if ( serial_port->isOpen() == false ) { + return ESP_LOADER_ERROR_FAIL; + } + } catch (...) { + return ESP_LOADER_ERROR_FAIL; + } + + return ESP_LOADER_SUCCESS; +} diff --git a/port/serial_port.h b/port/serial_port.h new file mode 100644 index 0000000..506861b --- /dev/null +++ b/port/serial_port.h @@ -0,0 +1,35 @@ +/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include "esp_loader_io.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char* portName; + int baudrate; + int timeout; +} loader_serial_config_t; + +esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config); + +#ifdef __cplusplus +} +#endif diff --git a/private_include/protocol.h b/private_include/protocol.h index 1d0701b..0bbe3df 100644 --- a/private_include/protocol.h +++ b/private_include/protocol.h @@ -31,7 +31,8 @@ extern "C" { #define MD5_SIZE 32 -typedef enum __attribute__((packed)) +__pragma( pack(push, 1) ) +typedef enum { FLASH_BEGIN = 0x02, FLASH_DATA = 0x03, @@ -52,7 +53,7 @@ typedef enum __attribute__((packed)) SPI_FLASH_MD5 = 0x13, } command_t; -typedef enum __attribute__((packed)) +typedef enum { RESPONSE_OK = 0x00, INVALID_COMMAND = 0x05, // parameters or length field is invalid @@ -64,7 +65,7 @@ typedef enum __attribute__((packed)) DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only } error_code_t; -typedef struct __attribute__((packed)) +typedef struct { uint8_t direction; uint8_t command; // One of command_t @@ -72,7 +73,7 @@ typedef struct __attribute__((packed)) uint32_t checksum; } command_common_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t erase_size; @@ -82,7 +83,7 @@ typedef struct __attribute__((packed)) uint32_t encrypted; } flash_begin_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t data_size; @@ -91,13 +92,13 @@ typedef struct __attribute__((packed)) uint32_t zero_1; } data_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t stay_in_loader; } flash_end_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t total_size; @@ -106,20 +107,20 @@ typedef struct __attribute__((packed)) uint32_t offset; } mem_begin_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t stay_in_loader; uint32_t entry_point_address; } mem_end_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint8_t sync_sequence[36]; } sync_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t address; @@ -128,27 +129,27 @@ typedef struct __attribute__((packed)) uint32_t delay_us; } write_reg_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t address; } read_reg_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t configuration; uint32_t zero; // ESP32 ROM only } spi_attach_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t new_baudrate; uint32_t old_baudrate; } change_baudrate_command_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t address; @@ -157,7 +158,7 @@ typedef struct __attribute__((packed)) uint32_t reserved_1; } spi_flash_md5_command_t; -typedef struct __attribute__((packed)) +typedef struct { uint8_t direction; uint8_t command; // One of command_t @@ -165,26 +166,26 @@ typedef struct __attribute__((packed)) uint32_t value; } common_response_t; -typedef struct __attribute__((packed)) +typedef struct { uint8_t failed; uint8_t error; } response_status_t; -typedef struct __attribute__((packed)) +typedef struct { common_response_t common; response_status_t status; } response_t; -typedef struct __attribute__((packed)) +typedef struct { common_response_t common; uint8_t md5[MD5_SIZE]; // ROM only response_status_t status; } rom_md5_response_t; -typedef struct __attribute__((packed)) +typedef struct { command_common_t common; uint32_t id; @@ -195,6 +196,8 @@ typedef struct __attribute__((packed)) uint32_t status_mask; } write_spi_command_t; +__pragma( pack(pop)) + esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args); #if (defined SERIAL_FLASHER_INTERFACE_UART) || (defined SERIAL_FLASHER_INTERFACE_USB) diff --git a/src/protocol_common.c b/src/protocol_common.c index c925330..1b516fc 100644 --- a/src/protocol_common.c +++ b/src/protocol_common.c @@ -295,7 +295,7 @@ esp_loader_error_t loader_spi_parameters(uint32_t total_size) return send_cmd(&spi_cmd, sizeof(spi_cmd), NULL); } -__attribute__ ((weak)) void loader_port_debug_print(const char *str) -{ - (void) str; -} +// void loader_port_debug_print(const char *str) +// { +// (void) str; +// } diff --git a/submodules/serial b/submodules/serial new file mode 160000 index 0000000..69e0372 --- /dev/null +++ b/submodules/serial @@ -0,0 +1 @@ +Subproject commit 69e0372cf0d3796e84ce9a09aff1d74496f68720 From 98d215bfb5d4c1374083d9e63ae74ab1ed23c96d Mon Sep 17 00:00:00 2001 From: Electromuis Date: Fri, 22 Mar 2024 15:28:45 +0100 Subject: [PATCH 2/3] Serial port config update. Firmware flash working --- examples/desktop_esp32_example/main/main.cpp | 3 +- port/serial_port.cpp | 42 +++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/examples/desktop_esp32_example/main/main.cpp b/examples/desktop_esp32_example/main/main.cpp index 216188d..52a5c22 100644 --- a/examples/desktop_esp32_example/main/main.cpp +++ b/examples/desktop_esp32_example/main/main.cpp @@ -33,7 +33,7 @@ int main(int argv, char **argc) loader_serial_config_t config; config.portName = argc[1]; - // config.portName = "COM3"; + // config.portName = "COM4"; config.baudrate = 115200; config.timeout = 1000; @@ -42,6 +42,7 @@ int main(int argv, char **argc) return -1; } + printf("Connecting...\n"); if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) { get_example_binaries(esp_loader_get_target(), &bin); diff --git a/port/serial_port.cpp b/port/serial_port.cpp index 55fe9e5..2ec214b 100644 --- a/port/serial_port.cpp +++ b/port/serial_port.cpp @@ -32,7 +32,6 @@ using namespace std::chrono_literals; loader_serial_config_t serial_config; std::unique_ptr serial_port; std::chrono::steady_clock::time_point serial_timer; -uint32_t serial_timer_offset = 0; #ifdef SERIAL_FLASHER_DEBUG_TRACE static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write) @@ -50,7 +49,17 @@ static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write) } #endif -static uint32_t s_time_end; +void setTimeout(uint32_t timeout) +{ + if(timeout == serial_config.timeout) + return; + + if(!serial_port || !serial_port->isOpen()) + return; + + serial_port->setTimeout(serial::Timeout::simpleTimeout(timeout)); + serial_config.timeout = timeout; +} esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout) { @@ -58,12 +67,13 @@ esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_ if(!serial_port || !serial_port->isOpen()) return ESP_LOADER_ERROR_FAIL; - serial_port->setTimeout(serial::Timeout::simpleTimeout(timeout)); + setTimeout(timeout); size_t result = serial_port->write(data, size); if (result != size) { return ESP_LOADER_ERROR_FAIL; } - } catch (...) { + } catch (std::exception &e){ + loader_port_debug_print(e.what()); return ESP_LOADER_ERROR_FAIL; } @@ -81,11 +91,13 @@ esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeo if(!serial_port || !serial_port->isOpen()) return ESP_LOADER_ERROR_FAIL; + setTimeout(timeout); size_t result = serial_port->read(data, size); if (result != size) { return ESP_LOADER_ERROR_FAIL; } - } catch (...) { + } catch (std::exception &e){ + loader_port_debug_print(e.what()); return ESP_LOADER_ERROR_FAIL; } @@ -105,7 +117,8 @@ esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config) if ( serial_port->isOpen() == false ) { return ESP_LOADER_ERROR_FAIL; } - } catch (...) { + } catch (std::exception &e){ + loader_port_debug_print(e.what()); return ESP_LOADER_ERROR_FAIL; } @@ -135,15 +148,14 @@ void loader_port_delay_ms(uint32_t ms) void loader_port_start_timer(uint32_t ms) { - serial_timer = high_resolution_clock::now(); - serial_timer_offset = ms; + serial_timer = high_resolution_clock::now() + (ms * 1ms); } uint32_t loader_port_remaining_time(void) { auto time_now = high_resolution_clock::now(); - int32_t remaining = (duration_cast(time_now - serial_timer - serial_timer_offset * 1ms)).count(); + int32_t remaining = (duration_cast(serial_timer - time_now)).count(); return (remaining > 0) ? (uint32_t)remaining : 0; } @@ -155,13 +167,13 @@ void loader_port_debug_print(const char *str) esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate) { + if(!serial_port || !serial_port->isOpen()) + return ESP_LOADER_ERROR_FAIL; + try { - serial_port = std::make_unique(serial_config.portName, baudrate, serial::Timeout::simpleTimeout(serial_config.timeout)); - - if ( serial_port->isOpen() == false ) { - return ESP_LOADER_ERROR_FAIL; - } - } catch (...) { + serial_port->setBaudrate(baudrate); + } catch (std::exception &e){ + loader_port_debug_print(e.what()); return ESP_LOADER_ERROR_FAIL; } From 4b650f88f585704d87c2af811428dc55fc4f75f5 Mon Sep 17 00:00:00 2001 From: Electromuis Date: Thu, 28 Mar 2024 14:53:20 +0100 Subject: [PATCH 3/3] Fixed timing bug. Added PR feedback --- .gitmodules | 3 - README.md | 4 +- examples/common/example_common.c | 13 +-- examples/desktop_esp32_example/CMakeLists.txt | 37 ++------ examples/desktop_esp32_example/main/main.cpp | 12 +-- ...erial_port.cpp => wjwwood_serial_port.cpp} | 89 ++++++++++++++----- port/{serial_port.h => wjwwood_serial_port.h} | 5 +- src/protocol_common.c | 10 ++- submodules/FindWjwwoodSerial.cmake | 35 ++++++++ 9 files changed, 136 insertions(+), 72 deletions(-) rename port/{serial_port.cpp => wjwwood_serial_port.cpp} (65%) rename port/{serial_port.h => wjwwood_serial_port.h} (81%) create mode 100644 submodules/FindWjwwoodSerial.cmake diff --git a/.gitmodules b/.gitmodules index 0dcecd1..0bb1151 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [submodule "submodules/stm32-cmake"] path = submodules/stm32-cmake url = https://github.com/ObKo/stm32-cmake.git -[submodule "submodules/serial"] - path = submodules/serial - url = https://github.com/wjwwood/serial diff --git a/README.md b/README.md index d181f3c..a4f4368 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # esp-serial-flasher -`esp-serial-flasher` is a portable C library for flashing or loading apps to RAM of Espressif SoCs from other host microcontrollers. +`esp-serial-flasher` is a portable C library for flashing or loading apps to RAM of Espressif SoCs from other host that are supported through their dedicated port drivers. +These can be found in the `port` folder. ## Using the library + `esp-serial-flasher` supports a variety of host/target/interface combinations: Supported **host** microcontrollers: diff --git a/examples/common/example_common.c b/examples/common/example_common.c index e74428f..2e57eb9 100644 --- a/examples/common/example_common.c +++ b/examples/common/example_common.c @@ -41,6 +41,7 @@ #define BOOTLOADER_ADDRESS_V1 0x0 #define PARTITION_ADDRESS 0x8000 #define APPLICATION_ADDRESS 0x10000 +#define MAX_BIN_HEADER_SEGMENTS 16 extern const uint8_t ESP32_bootloader_bin[]; extern const uint32_t ESP32_bootloader_bin_size; @@ -346,8 +347,12 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) printf("Start loading\n"); esp_loader_error_t err; const esp_loader_bin_header_t *header = (const esp_loader_bin_header_t *)bin; - //esp_loader_bin_segment_t segments[header->segments]; - esp_loader_bin_segment_t* segments = malloc(header->segments * sizeof(esp_loader_bin_segment_t)); + if(header->segments > MAX_BIN_HEADER_SEGMENTS) { + printf("Too many segments in binary header\n"); + return ESP_LOADER_ERROR_INVALID_PARAM; + } + + esp_loader_bin_segment_t segments[MAX_BIN_HEADER_SEGMENTS]; // Parse segments uint32_t seg; @@ -368,7 +373,6 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) err = esp_loader_mem_start(segments[seg].addr, segments[seg].size, ESP_RAM_BLOCK); if (err != ESP_LOADER_SUCCESS) { printf("Loading ram start with error %d.\n", err); - free(segments); return err; } @@ -379,7 +383,6 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) err = esp_loader_mem_write(data_pos, data_size); if (err != ESP_LOADER_SUCCESS) { printf("\nPacket could not be written! Error %d.\n", err); - free(segments); return err; } data_pos += data_size; @@ -390,11 +393,9 @@ esp_loader_error_t load_ram_binary(const uint8_t *bin) err = esp_loader_mem_finish(header->entrypoint); if (err != ESP_LOADER_SUCCESS) { printf("\nLoad ram finish with Error %d.\n", err); - free(segments); return err; } printf("\nFinished loading\n"); - free(segments); return ESP_LOADER_SUCCESS; } diff --git a/examples/desktop_esp32_example/CMakeLists.txt b/examples/desktop_esp32_example/CMakeLists.txt index 970b778..1e074ec 100644 --- a/examples/desktop_esp32_example/CMakeLists.txt +++ b/examples/desktop_esp32_example/CMakeLists.txt @@ -1,31 +1,11 @@ -set(ESPSERIAL_PATH ${CMAKE_CURRENT_LIST_DIR}/../../) - -# LIB serial -SET(serial_path ${ESPSERIAL_PATH}submodules/serial) -SET(serial_SRCS ${serial_path}/src/serial.cc) +cmake_minimum_required(VERSION 3.5) +project(esp-serial-flasher) -if(APPLE) - # If OSX - list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) - list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_osx.cc) -elseif(UNIX) - # If unix - list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) - list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_linux.cc) -else() - # If windows - list(APPEND serial_SRCS ${serial_path}/src/impl/win.cc) - list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_win.cc) -endif() - -add_library(serial - ${serial_SRCS} -) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../submodules) +find_package(WjwwoodSerial REQUIRED) -target_include_directories(serial PUBLIC - ${serial_path}/include -) +set(ESPSERIAL_PATH ${CMAKE_CURRENT_LIST_DIR}/../../) # LIB esp_serial @@ -37,7 +17,7 @@ set_property(SOURCE ${CMAKE_BINARY_DIR}/binaries_2.c PROPERTY GENERATED 1) add_library(esp_serial - ${ESPSERIAL_PATH}port/serial_port.cpp + ${ESPSERIAL_PATH}port/wjwwood_serial_port.cpp ${ESPSERIAL_PATH}src/esp_loader.c ${ESPSERIAL_PATH}src/esp_targets.c ${ESPSERIAL_PATH}src/md5_hash.c @@ -63,11 +43,10 @@ target_compile_definitions(esp_serial PUBLIC -DSERIAL_FLASHER_INTERFACE_USB -DMD5_ENABLED -DSERIAL_FLASHER_WRITE_BLOCK_RETRIES=4 + -DSERIAL_FLASHER_DEBUG_TRACE ) -target_link_libraries(esp_serial PUBLIC - serial -) +target_link_libraries(esp_serial PUBLIC wjwwood_serial) # EXECUTABLE desktop_esp32_example diff --git a/examples/desktop_esp32_example/main/main.cpp b/examples/desktop_esp32_example/main/main.cpp index 52a5c22..40d68af 100644 --- a/examples/desktop_esp32_example/main/main.cpp +++ b/examples/desktop_esp32_example/main/main.cpp @@ -10,7 +10,7 @@ #include "stdio.h" #include -#include "serial_port.h" +#include "wjwwood_serial_port.h" #include "esp_loader.h" #include "esp_loader_io.h" extern "C" { @@ -31,13 +31,13 @@ int main(int argv, char **argc) example_binaries_t bin; - loader_serial_config_t config; + loader_wjwwood_serial_config_t config; config.portName = argc[1]; - // config.portName = "COM4"; + //config.portName = "COM4"; config.baudrate = 115200; - config.timeout = 1000; + config.timeout = 600; - if (loader_port_serial_init((const loader_serial_config_t*)&config) != ESP_LOADER_SUCCESS) { + if (loader_port_wjwwood_serial_init((const loader_wjwwood_serial_config_t*)&config) != ESP_LOADER_SUCCESS) { printf("Serial initialization failed.\n"); return -1; } @@ -54,8 +54,10 @@ int main(int argv, char **argc) printf("Loading app...\n"); flash_binary(bin.app.data, bin.app.size, bin.app.addr); printf("Done!\n"); + loader_port_wjwwood_serial_deinit(); } else { printf("Connect failed\n"); + loader_port_wjwwood_serial_deinit(); return -1; } diff --git a/port/serial_port.cpp b/port/wjwwood_serial_port.cpp similarity index 65% rename from port/serial_port.cpp rename to port/wjwwood_serial_port.cpp index 2ec214b..e1c3cf2 100644 --- a/port/serial_port.cpp +++ b/port/wjwwood_serial_port.cpp @@ -21,7 +21,7 @@ #include #include "serial/serial.h" -#include "serial_port.h" +#include "wjwwood_serial_port.h" using std::chrono::high_resolution_clock; using std::chrono::duration_cast; @@ -29,10 +29,13 @@ using std::chrono::duration; using std::chrono::milliseconds; using namespace std::chrono_literals; -loader_serial_config_t serial_config; +loader_wjwwood_serial_config_t serial_config; std::unique_ptr serial_port; std::chrono::steady_clock::time_point serial_timer; +// During the connection process a small delay is needed between write operations. +static bool write_delay = true; + #ifdef SERIAL_FLASHER_DEBUG_TRACE static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write) { @@ -55,7 +58,10 @@ void setTimeout(uint32_t timeout) return; if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("Port not open\n"); return; + } serial_port->setTimeout(serial::Timeout::simpleTimeout(timeout)); serial_config.timeout = timeout; @@ -63,14 +69,18 @@ void setTimeout(uint32_t timeout) esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout) { - try { - if(!serial_port || !serial_port->isOpen()) - return ESP_LOADER_ERROR_FAIL; + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_write: Port not open\n"); + return ESP_LOADER_ERROR_FAIL; + } + try { setTimeout(timeout); size_t result = serial_port->write(data, size); if (result != size) { - return ESP_LOADER_ERROR_FAIL; + loader_port_debug_print("Could not write requested data\n"); + return ESP_LOADER_ERROR_TIMEOUT; } } catch (std::exception &e){ loader_port_debug_print(e.what()); @@ -78,23 +88,30 @@ esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_ } #ifdef SERIAL_FLASHER_DEBUG_TRACE - transfer_debug_print(data, size, true); + transfer_debug_print(data, size, true); #endif + if(write_delay) + std::this_thread::sleep_for(std::chrono::microseconds(200)); + return ESP_LOADER_SUCCESS; } esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout) { - try { - if(!serial_port || !serial_port->isOpen()) - return ESP_LOADER_ERROR_FAIL; + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_read: Port not open\n"); + return ESP_LOADER_ERROR_FAIL; + } + try { setTimeout(timeout); size_t result = serial_port->read(data, size); if (result != size) { - return ESP_LOADER_ERROR_FAIL; + loader_port_debug_print("Could not read requested data\n"); + return ESP_LOADER_ERROR_TIMEOUT; } } catch (std::exception &e){ loader_port_debug_print(e.what()); @@ -102,19 +119,23 @@ esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeo } #ifdef SERIAL_FLASHER_DEBUG_TRACE - transfer_debug_print(data, size, true); + transfer_debug_print(data, size, false); #endif return ESP_LOADER_SUCCESS; } -esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config) +esp_loader_error_t loader_port_wjwwood_serial_init(const loader_wjwwood_serial_config_t *config) { serial_config = *config; try { serial_port = std::make_unique(serial_config.portName, config->baudrate, serial::Timeout::simpleTimeout(config->timeout)); + serial_port->setRTS(false); + serial_port->setDTR(false); + serial_port->setFlowcontrol(serial::flowcontrol_hardware); if ( serial_port->isOpen() == false ) { + loader_port_debug_print("Port open failed\n"); return ESP_LOADER_ERROR_FAIL; } } catch (std::exception &e){ @@ -125,33 +146,54 @@ esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config) return ESP_LOADER_SUCCESS; } -// Set GPIO0 LOW, then -// assert reset pin for 100 milliseconds. +esp_loader_error_t loader_port_wjwwood_serial_deinit() +{ + if(!serial_port || !serial_port->isOpen()) + return ESP_LOADER_ERROR_FAIL; + + try { + serial_port->close(); + serial_port.reset(); + serial_config = {}; + } catch (std::exception &e){ + loader_port_debug_print(e.what()); + return ESP_LOADER_ERROR_FAIL; + } + + return ESP_LOADER_SUCCESS; +} + void loader_port_enter_bootloader(void) { - // todo loader_port_reset_target(); } - void loader_port_reset_target(void) { - // todo -} + if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_reset_target: Port not open\n"); + return; + } + // This might be the best we can do without GPIO + serial_port->setDTR(true); + serial_port->setRTS(true); + loader_port_delay_ms(50); + serial_port->setDTR(false); + serial_port->setRTS(false); +} void loader_port_delay_ms(uint32_t ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } - void loader_port_start_timer(uint32_t ms) { serial_timer = high_resolution_clock::now() + (ms * 1ms); } - uint32_t loader_port_remaining_time(void) { auto time_now = high_resolution_clock::now(); @@ -159,7 +201,6 @@ uint32_t loader_port_remaining_time(void) return (remaining > 0) ? (uint32_t)remaining : 0; } - void loader_port_debug_print(const char *str) { printf("DEBUG: %s", str); @@ -168,10 +209,14 @@ void loader_port_debug_print(const char *str) esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate) { if(!serial_port || !serial_port->isOpen()) + { + loader_port_debug_print("loader_port_change_transmission_rate: Port not open\n"); return ESP_LOADER_ERROR_FAIL; + } try { serial_port->setBaudrate(baudrate); + write_delay = false; } catch (std::exception &e){ loader_port_debug_print(e.what()); return ESP_LOADER_ERROR_FAIL; diff --git a/port/serial_port.h b/port/wjwwood_serial_port.h similarity index 81% rename from port/serial_port.h rename to port/wjwwood_serial_port.h index 506861b..cc2183e 100644 --- a/port/serial_port.h +++ b/port/wjwwood_serial_port.h @@ -26,9 +26,10 @@ typedef struct { char* portName; int baudrate; int timeout; -} loader_serial_config_t; +} loader_wjwwood_serial_config_t; -esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config); +esp_loader_error_t loader_port_wjwwood_serial_deinit(); +esp_loader_error_t loader_port_wjwwood_serial_init(const loader_wjwwood_serial_config_t *config); #ifdef __cplusplus } diff --git a/src/protocol_common.c b/src/protocol_common.c index 1b516fc..61dade0 100644 --- a/src/protocol_common.c +++ b/src/protocol_common.c @@ -295,7 +295,9 @@ esp_loader_error_t loader_spi_parameters(uint32_t total_size) return send_cmd(&spi_cmd, sizeof(spi_cmd), NULL); } -// void loader_port_debug_print(const char *str) -// { -// (void) str; -// } +#ifndef _MSC_VER +__attribute__ ((weak)) void loader_port_debug_print(const char *str) +{ + (void) str; +} +#endif diff --git a/submodules/FindWjwwoodSerial.cmake b/submodules/FindWjwwoodSerial.cmake new file mode 100644 index 0000000..eadb5fd --- /dev/null +++ b/submodules/FindWjwwoodSerial.cmake @@ -0,0 +1,35 @@ +SET(DEPENDENCY_NAME "WjwwoodSerial") +SET(GIT_URL "https://github.com/wjwwood/serial") + +if(NOT EXISTS "${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}") + # Clone the repository if it's not already present + message(STATUS "Cloning ${DEPENDENCY_NAME} from ${GIT_URL}") + execute_process(COMMAND git clone ${GIT_URL} ${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}) +else() + message(STATUS "${DEPENDENCY_NAME} already cloned") +endif() + +SET(serial_path ${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}) +SET(serial_SRCS ${serial_path}/src/serial.cc) + +if(APPLE) + # If OSX + list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_osx.cc) +elseif(UNIX) + # If unix + list(APPEND serial_SRCS ${serial_path}/src/impl/unix.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_linux.cc) +else() + # If windows + list(APPEND serial_SRCS ${serial_path}/src/impl/win.cc) + list(APPEND serial_SRCS ${serial_path}/src/impl/list_ports/list_ports_win.cc) +endif() + +add_library(wjwwood_serial + ${serial_SRCS} +) + +target_include_directories(wjwwood_serial PUBLIC + ${serial_path}/include +)