Quellcode durchsuchen

feature: Refactor to make adding ports for non-UART peripherals possible

This commit does the following:
1. Removes the serial from names of port functions
2. Removes serial from names of modules and headers containing peripheral-invariant code and gives more sensible names
3. Adjusts the comments and the documentation
4. Factors out SLIP code into a separate module
5. Merges serial_comm and serial_comm_prv (now protocol.h) into one header as they are never used without each other and are both private

This should enable adding non-UART ports more easily and provide better project structure in general. Wrappers are added for legacy function names to avoid breaking changes.
Djordje Nedic vor 2 Jahren
Ursprung
Commit
adc18ffdd4

+ 6 - 4
CMakeLists.txt

@@ -1,9 +1,11 @@
 cmake_minimum_required(VERSION 3.5)
 
-set(srcs src/esp_loader.c
-         src/esp_targets.c
-         src/serial_comm.c
-         src/md5_hash.c
+set(srcs
+    src/esp_loader.c
+    src/esp_targets.c
+    src/md5_hash.c
+    src/protocol.c
+    src/slip.c
 )
 
 

+ 6 - 6
README.md

@@ -25,21 +25,21 @@ Supported **target** microcontrollers:
 
 In order to support new target, following function has to be implemented by user:
 
-- loader_port_serial_read()
-- loader_port_serial_write()
+- loader_port_read()
+- loader_port_write()
 - loader_port_enter_bootloader()
 - loader_port_delay_ms()
 - loader_port_start_timer()
 - loader_port_remaining_time()
 
-Following functions are part of serial_io.h header for convenience, however, user does not have to strictly follow function signatures, as there are not called directly from library.
+Following functions are part of io.h header for convenience, however, user does not have to strictly follow function signatures, as there are not called directly from library.
 
-- loader_port_change_baudrate()
+- loader_port_change_transmission_rate()
 - loader_port_reset_target()
 - loader_port_debug_print()
 
-Prototypes of all function mentioned above can be found in [serial_io.h](include/serial_io.h).
-Please refer to ports in `port` directory. Currently, only ports for [ESP32 port](port/esp32_port.c), [STM32 port](port/stm32_port.c), and [Zephyr port](port/zephyr_port.c) are available.
+Prototypes of all function mentioned above can be found in [io.h](include/io.h).
+Please refer to ports in `port` directory. Currently, ports for [ESP32](port/esp32_port.c), [STM32](port/stm32_port.c), and [Zephyr](port/zephyr_port.c) are available.
 
 ## Configuration
 

+ 10 - 10
examples/common/example_common.c

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -18,7 +18,7 @@
 #include <string.h>
 #include <inttypes.h>
 #include <sys/param.h>
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include "esp_loader.h"
 #include "example_common.h"
 
@@ -123,7 +123,7 @@ void get_example_ram_app_binary(target_chip_t target, example_ram_app_binary_t *
 
 #endif
 
-esp_loader_error_t connect_to_target(uint32_t higher_baudrate)
+esp_loader_error_t connect_to_target(uint32_t higher_transmission_rate)
 {
     esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
 
@@ -134,21 +134,21 @@ esp_loader_error_t connect_to_target(uint32_t higher_baudrate)
     }
     printf("Connected to target\n");
 
-    if (higher_baudrate && esp_loader_get_target() != ESP8266_CHIP) {
-        err = esp_loader_change_baudrate(higher_baudrate);
+    if (higher_transmission_rate && esp_loader_get_target() != ESP8266_CHIP) {
+        err = esp_loader_change_transmission_rate(higher_transmission_rate);
         if (err == ESP_LOADER_ERROR_UNSUPPORTED_FUNC) {
-            printf("ESP8266 does not support change baudrate command.");
+            printf("ESP8266 does not support change transmission rate command.");
             return err;
         } else if (err != ESP_LOADER_SUCCESS) {
-            printf("Unable to change baud rate on target.");
+            printf("Unable to change transmission rate on target.");
             return err;
         } else {
-            err = loader_port_change_baudrate(higher_baudrate);
+            err = loader_port_change_transmission_rate(higher_transmission_rate);
             if (err != ESP_LOADER_SUCCESS) {
-                printf("Unable to change baud rate.");
+                printf("Unable to change transmission rate.");
                 return err;
             }
-            printf("Baudrate changed\n");
+            printf("Transmission rate changed changed\n");
         }
     }
 

+ 3 - 3
examples/common/example_common.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -58,6 +58,6 @@ typedef struct example_bin_segment {
 
 void get_example_binaries(target_chip_t target, example_binaries_t *binaries);
 void get_example_ram_app_binary(target_chip_t target, example_ram_app_binary_t *bin);
-esp_loader_error_t connect_to_target(uint32_t higrer_baudrate);
+esp_loader_error_t connect_to_target(uint32_t higher_transmission_rate);
 esp_loader_error_t flash_binary(const uint8_t *bin, size_t size, size_t address);
-esp_loader_error_t load_ram_binary(const uint8_t *bin);
+esp_loader_error_t load_ram_binary(const uint8_t *bin);

+ 1 - 1
examples/esp32_example/README.md

@@ -13,7 +13,7 @@ Following steps are performed in order to re-program target's memory:
 5. Then `esp_loader_flash_start()` is called to enter flashing mode and erase amount of memory to be flashed.
 6. `esp_loader_flash_write()` function is called repeatedly until the whole binary image is transfered.
 
-Note: In addition, to steps mentioned above, `esp_loader_change_baudrate`  is called after connection is established in order to increase flashing speed. This does not apply for ESP8266, as its bootloader does not support this command. However, ESP8266 is capable of detecting baud rate during connection phase, and can be changed before calling `esp_loader_connect`, if necessary.
+Note: In addition, to steps mentioned above, `esp_loader_change_transmission_rate`  is called after connection is established in order to increase flashing speed. This does not apply for ESP8266, as its bootloader does not support this command. However, ESP8266 is capable of detecting baud rate during connection phase, and can be changed before calling `esp_loader_connect`, if necessary.
 
 ## SPI pin initialization
 

+ 1 - 1
examples/raspberry_example/README.md

@@ -12,7 +12,7 @@ Following steps are performed in order to re-program target's memory:
 4. `esp_loader_flash_write()` function is called repeatedly until the whole binary image is transfered.
 5. At the end, `loader_port_reset_target()` is called to restart ESP32 and execute updated firmware.
 
-Note: In addition, to steps mentioned above, `esp_loader_change_baudrate`  is called after connection is established in order to increase flashing speed. Bootloader is also capable of detecting baud rate during connection phase, and can be changed before calling `esp_loader_connect`. However, it is recommended to start at lower speed and then use dedicated command to increase baud rate. This does not apply for ESP8266, as its bootloader does not support this command, therefore, baud rate can only be changed before connection phase in this case.
+Note: In addition, to steps mentioned above, `esp_loader_change_transmission_rate`  is called after connection is established in order to increase flashing speed. Bootloader is also capable of detecting baud rate during connection phase, and can be changed before calling `esp_loader_connect`. However, it is recommended to start at lower speed and then use dedicated command to increase baud rate. This does not apply for ESP8266, as its bootloader does not support this command, therefore, baud rate can only be changed before connection phase in this case.
 
 ## Hardware Required
 

+ 1 - 1
examples/stm32_example/README.md

@@ -11,7 +11,7 @@ Following steps are performed in order to re-program target's memory:
 3. Then `esp_loader_flash_start()` is called to enter flashing mode and erase amount of memory to be flashed.
 4. `esp_loader_flash_write()` function is called repeatedly until the whole binary image is transfered.
 
-Note: In addition, to steps mentioned above, `esp_loader_change_baudrate`  is called after connection is established in order to increase flashing speed. Bootloader is also capable of detecting baud rate during connection phase, and can be changed before calling `esp_loader_connect`. However, it is recommended to start at lower speed and then use dedicated command to increase baud rate. This does not apply for ESP8266, as its bootloader does not support this command, therefore, baud rate can only be changed before connection phase in this case.
+Note: In addition, to steps mentioned above, `esp_loader_change_transmission_rate`  is called after connection is established in order to increase flashing speed. Bootloader is also capable of detecting baud rate during connection phase, and can be changed before calling `esp_loader_connect`. However, it is recommended to start at lower speed and then use dedicated command to increase baud rate. This does not apply for ESP8266, as its bootloader does not support this command, therefore, baud rate can only be changed before connection phase in this case.
 
 ## Hardware Required
 

+ 1 - 1
examples/zephyr_example/README.md

@@ -16,7 +16,7 @@ Following steps are performed in order to re-program target's memory:
 5. Then `esp_loader_flash_start()` is called to enter flashing mode and erase amount of memory to be flashed.
 6. `esp_loader_flash_write()` function is called repeatedly until the whole binary image is transfered.
 
-Note: In addition, to steps mentioned above, `esp_loader_change_baudrate`  is called after connection
+Note: In addition, to steps mentioned above, `esp_loader_change_transmission_rate`  is called after connection
 is established in order to increase flashing speed. This does not apply for ESP8266, as its bootloader
 does not support this command. However, ESP8266 is capable of detecting baud rate during connection
 phase, and can be changed before calling `esp_loader_connect`, if necessary.

+ 1 - 1
examples/zephyr_example/src/main.c

@@ -75,7 +75,7 @@ void main(void)
 		flash_binary(bin.app.data,  bin.app.size,  bin.app.addr);
 	}
 
-	loader_port_change_baudrate(DEFAULT_BAUDRATE);
+	loader_port_change_transmission_rate(DEFAULT_BAUDRATE);
 
 	loader_port_reset_target();
 }

+ 7 - 4
include/esp_loader.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -22,6 +22,9 @@
 extern "C" {
 #endif
 
+/* Used for backwards compatibility with the previous API */
+#define esp_loader_change_baudrate esp_loader_change_transmission_rate
+
 /**
  * Macro which can be used to check the error code,
  * and return in case the code is not ESP_LOADER_SUCCESS.
@@ -108,7 +111,7 @@ esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args);
 /**
   * @brief   Returns attached target chip.
   *
-  * @warning This function can only be called after connection with target 
+  * @warning This function can only be called after connection with target
   *          has been successfully established by calling esp_loader_connect().
   *
   * @return  One of target_chip_t
@@ -245,7 +248,7 @@ esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_valu
   * @note  Baud rate has to be also adjusted accordingly on host MCU, as
   *        target's baud rate is changed upon return from this function.
   *
-  * @param baudrate[in]     new baud rate to be set.
+  * @param transmission_rate[in]     new baud rate to be set.
   *
   * @return
   *     - ESP_LOADER_SUCCESS Success
@@ -253,7 +256,7 @@ esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_valu
   *     - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
   *     - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
   */
-esp_loader_error_t esp_loader_change_baudrate(uint32_t baudrate);
+esp_loader_error_t esp_loader_change_transmission_rate(uint32_t transmission_rate);
 
 /**
   * @brief Verify target's flash integrity by checking MD5.

+ 105 - 0
include/esp_loader_io.h

@@ -0,0 +1,105 @@
+/* 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 <stdint.h>
+#include "esp_loader.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+  * @brief Changes the transmission rate of the used peripheral.
+  */
+esp_loader_error_t loader_port_change_transmission_rate(uint32_t transmission_rate);
+
+/**
+  * @brief Writes data over the io interface.
+  *
+  * @param data[in]     Buffer with data to be written.
+  * @param size[in]     Size of data in bytes.
+  * @param timeout[in]  Timeout in milliseconds.
+  *
+  * @return
+  *     - ESP_LOADER_SUCCESS Success
+  *     - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
+  */
+esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout);
+
+/**
+  * @brief Reads data from the io interface.
+  *
+  * @param data[out]    Buffer into which received data will be written.
+  * @param size[in]     Number of bytes to read.
+  * @param timeout[in]  Timeout in milliseconds.
+  *
+  * @return
+  *     - ESP_LOADER_SUCCESS Success
+  *     - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
+  */
+esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout);
+
+/**
+  * @brief Delay in milliseconds.
+  *
+  * @param ms[in]   Number of milliseconds.
+  *
+  */
+void loader_port_delay_ms(uint32_t ms);
+
+/**
+  * @brief Starts timeout timer.
+  *
+  * @param ms[in]   Number of milliseconds.
+  *
+  */
+void loader_port_start_timer(uint32_t ms);
+
+/**
+  * @brief Returns remaining time since timer was started by calling esp_loader_start_timer.
+  *        0 if timer has elapsed.
+  *
+  * @return   Number of milliseconds.
+  *
+  */
+uint32_t loader_port_remaining_time(void);
+
+/**
+  * @brief Asserts bootstrap pins to enter boot mode and toggles reset pin.
+  *
+  * @note  Reset pin should stay asserted for at least 20 milliseconds.
+  */
+void loader_port_enter_bootloader(void);
+
+/**
+  * @brief Toggles reset pin.
+  *
+  * @note  Reset pin should stay asserted for at least 20 milliseconds.
+  */
+void loader_port_reset_target(void);
+
+/**
+  * @brief Function can be defined by user to print debug message.
+  *
+  * @note  Empty weak function is used, otherwise.
+  *
+  */
+void loader_port_debug_print(const char *str);
+
+#ifdef __cplusplus
+}
+#endif

+ 8 - 89
include/serial_io.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -13,93 +13,12 @@
  * limitations under the License.
  */
 
- #pragma once
+#warning Please replace serial_io.h with esp_loader_io.h and change the function names \
+         to match the new API
 
-#include <stdint.h>
-#include "esp_loader.h"
+/* Defines used to avoid breaking existing ports */
+#define loader_port_change_baudrate loader_port_change_transmission_rate
+#define loader_port_serial_write loader_port_write
+#define loader_port_serial_read loader_port_read
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
-  * @brief Changes baud rate of serial peripheral.
-  */
-esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate);
-
-/**
-  * @brief Writes data to serial interface.
-  *
-  * @param data[in]     Buffer with data to be written.
-  * @param size[in]     Size of data in bytes.
-  * @param timeout[in]  Timeout in milliseconds.
-  *
-  * @return
-  *     - ESP_LOADER_SUCCESS Success
-  *     - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
-  */
-esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size, uint32_t timeout);
-
-/**
-  * @brief Reads data from serial interface.
-  *
-  * @param data[out]    Buffer into which received data will be written.
-  * @param size[in]     Number of bytes to read.
-  * @param timeout[in]  Timeout in milliseconds.
-  *
-  * @return
-  *     - ESP_LOADER_SUCCESS Success
-  *     - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
-  */
-esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout);
-
-/**
-  * @brief Delay in milliseconds.
-  *
-  * @param ms[in]   Number of milliseconds.
-  *
-  */
-void loader_port_delay_ms(uint32_t ms);
-
-/**
-  * @brief Starts timeout timer.
-  *
-  * @param ms[in]   Number of milliseconds.
-  *
-  */
-void loader_port_start_timer(uint32_t ms);
-
-/**
-  * @brief Returns remaining time since timer was started by calling esp_loader_start_timer.
-  *        0 if timer has elapsed.
-  *
-  * @return   Number of milliseconds.
-  *
-  */
-uint32_t loader_port_remaining_time(void);
-
-/**
-  * @brief Asserts bootstrap pins to enter boot mode and toggles reset pin.
-  *
-  * @note  Reset pin should stay asserted for at least 20 milliseconds.
-  */
-void loader_port_enter_bootloader(void);
-
-/**
-  * @brief Toggles reset pin.
-  *
-  * @note  Reset pin should stay asserted for at least 20 milliseconds.
-  */
-void loader_port_reset_target(void);
-
-/**
-  * @brief Function can be defined by user to print debug message.
-  *
-  * @note  Empty weak function is used, otherwise.
-  *
-  */
-void loader_port_debug_print(const char *str);
-
-#ifdef __cplusplus
-}
-#endif
+#include "esp_loader_io.h"

+ 4 - 4
port/esp32_port.c

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -115,7 +115,7 @@ void loader_port_esp32_deinit(void)
 }
 
 
-esp_loader_error_t loader_port_serial_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)
 {
     serial_debug_print(data, size, true);
 
@@ -132,7 +132,7 @@ esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size,
 }
 
 
-esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
+esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
 {
     int read = uart_read_bytes(s_uart_port, data, size, pdMS_TO_TICKS(timeout));
 
@@ -191,7 +191,7 @@ void loader_port_debug_print(const char *str)
     printf("DEBUG: %s\n", str);
 }
 
-esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
+esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
 {
     esp_err_t err = uart_set_baudrate(s_uart_port, baudrate);
     return (err == ESP_OK) ? ESP_LOADER_SUCCESS : ESP_LOADER_ERROR_FAIL;

+ 3 - 3
port/esp32_port.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -15,7 +15,7 @@
 
  #pragma once
 
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/queue.h"
 
@@ -34,7 +34,7 @@ typedef struct
   uint32_t rx_buffer_size;    /*!< Set to zero for default RX buffer size */
   uint32_t tx_buffer_size;    /*!< Set to zero for default TX buffer size */
   uint32_t queue_size;        /*!< Set to zero for default UART queue size */
-  QueueHandle_t *uart_queue;  /*!< Set to NULL, if UART queue handle is not  
+  QueueHandle_t *uart_queue;  /*!< Set to NULL, if UART queue handle is not
                                    necessary. Otherwise, it will be assigned here */
 } loader_esp32_config_t;
 

+ 7 - 7
port/raspberry_port.c

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -13,8 +13,8 @@
  * limitations under the License.
  */
 
-#include "serial_io.h"
-#include "serial_comm.h"
+#include "esp_loader_io.h"
+#include "protocol.h"
 #include <pigpio.h>
 #include "raspberry_port.h"
 
@@ -169,7 +169,7 @@ static esp_loader_error_t change_baudrate(int file_desc, int baudrate)
 
     tcsetattr (file_desc, TCSANOW, &options);
 
-    return ESP_LOADER_SUCCESS; 
+    return ESP_LOADER_SUCCESS;
 }
 
 static void set_timeout(uint32_t timeout)
@@ -231,7 +231,7 @@ esp_loader_error_t loader_port_raspberry_init(const loader_raspberry_config_t *c
 }
 
 
-esp_loader_error_t loader_port_serial_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)
 {
     serial_debug_print(data, size, true);
 
@@ -247,7 +247,7 @@ esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size,
 }
 
 
-esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
+esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
 {
     RETURN_ON_ERROR( read_data(data, size) );
 
@@ -299,7 +299,7 @@ void loader_port_debug_print(const char *str)
     printf("DEBUG: %s\n", str);
 }
 
-esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
+esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
 {
     return change_baudrate(serial, baudrate);
 }

+ 2 - 2
port/raspberry_port.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -16,7 +16,7 @@
 #pragma once
 
 #include <stdint.h>
-#include "serial_io.h"
+#include "esp_loader_io.h"
 
 #ifdef __cplusplus
 extern "C" {

+ 5 - 5
port/stm32_port.c

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -64,7 +64,7 @@ static void serial_debug_print(const uint8_t *data, uint16_t size, bool write) {
 
 static uint32_t s_time_end;
 
-esp_loader_error_t loader_port_serial_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)
 {
     serial_debug_print(data, size, true);
 
@@ -80,7 +80,7 @@ esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size,
 }
 
 
-esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
+esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
 {
     memset(data, 0x22, size);
 
@@ -101,7 +101,7 @@ void loader_port_stm32_init(loader_stm32_config_t *config)
 
 {
     uart = config->huart;
-    gpio_port_io0 = config->port_io0; 
+    gpio_port_io0 = config->port_io0;
     gpio_port_rst = config->port_rst;
     gpio_num_io0 = config->pin_num_io0;
     gpio_num_rst = config->pin_num_rst;
@@ -150,7 +150,7 @@ void loader_port_debug_print(const char *str)
     printf("DEBUG: %s", str);
 }
 
-esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
+esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
 {
     uart->Init.BaudRate = baudrate;
 

+ 2 - 2
port/stm32_port.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -16,7 +16,7 @@
 #pragma once
 
 #include <stdint.h>
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include "stm32f4xx_hal.h"
 
 #ifdef __cplusplus

+ 3 - 3
port/zephyr_port.c

@@ -37,7 +37,7 @@ esp_loader_error_t configure_tty()
     return ESP_LOADER_SUCCESS;
 }
 
-esp_loader_error_t loader_port_serial_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
+esp_loader_error_t loader_port_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
 {
     if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
         return ESP_LOADER_ERROR_FAIL;
@@ -61,7 +61,7 @@ esp_loader_error_t loader_port_serial_read(uint8_t *data, const uint16_t size, c
     return ESP_LOADER_SUCCESS;
 }
 
-esp_loader_error_t loader_port_serial_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
+esp_loader_error_t loader_port_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
 {
     if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
         return ESP_LOADER_ERROR_FAIL;
@@ -126,7 +126,7 @@ uint32_t loader_port_remaining_time(void)
     return (remaining > 0) ? (uint32_t)remaining : 0;
 }
 
-esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
+esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
 {
     struct uart_config uart_config;
 

+ 1 - 1
port/zephyr_port.h

@@ -16,7 +16,7 @@
 
 #pragma once
 
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include <zephyr/kernel.h>
 #include <zephyr/drivers/gpio.h>
 #include <stdint.h>

+ 33 - 1
private_include/serial_comm_prv.h → private_include/protocol.h

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -17,6 +17,7 @@
 
 #include <stdint.h>
 #include <stdbool.h>
+#include "esp_loader.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -194,6 +195,37 @@ typedef struct __attribute__((packed))
     uint32_t status_mask;
 } write_spi_command_t;
 
+esp_loader_error_t loader_flash_begin_cmd(uint32_t offset, uint32_t erase_size, uint32_t block_size, uint32_t blocks_to_write, bool encryption);
+
+esp_loader_error_t loader_flash_data_cmd(const uint8_t *data, uint32_t size);
+
+esp_loader_error_t loader_flash_end_cmd(bool stay_in_loader);
+
+esp_loader_error_t loader_mem_begin_cmd(uint32_t offset, uint32_t size, uint32_t blocks_to_write, uint32_t block_size);
+
+esp_loader_error_t loader_mem_data_cmd(const uint8_t *data, uint32_t size);
+
+esp_loader_error_t loader_mem_end_cmd(uint32_t entrypoint);
+
+esp_loader_error_t loader_mem_begin_cmd(uint32_t offset, uint32_t size, uint32_t blocks_to_write, uint32_t block_size);
+
+esp_loader_error_t loader_mem_data_cmd(const uint8_t *data, uint32_t size);
+
+esp_loader_error_t loader_mem_end_cmd(uint32_t entrypoint);
+
+esp_loader_error_t loader_write_reg_cmd(uint32_t address, uint32_t value, uint32_t mask, uint32_t delay_us);
+
+esp_loader_error_t loader_read_reg_cmd(uint32_t address, uint32_t *reg);
+
+esp_loader_error_t loader_sync_cmd(void);
+
+esp_loader_error_t loader_spi_attach_cmd(uint32_t config);
+
+esp_loader_error_t loader_change_baudrate_cmd(uint32_t baudrate);
+
+esp_loader_error_t loader_md5_cmd(uint32_t address, uint32_t size, uint8_t *md5_out);
+
+esp_loader_error_t loader_spi_parameters(uint32_t total_size);
 
 #ifdef __cplusplus
 }

+ 0 - 54
private_include/serial_comm.h

@@ -1,54 +0,0 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE 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 <stdint.h>
-#include <stdbool.h>
-#include "esp_loader.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-esp_loader_error_t loader_flash_begin_cmd(uint32_t offset, uint32_t erase_size, uint32_t block_size, uint32_t blocks_to_write, bool encryption);
-
-esp_loader_error_t loader_flash_data_cmd(const uint8_t *data, uint32_t size);
-
-esp_loader_error_t loader_flash_end_cmd(bool stay_in_loader);
-
-esp_loader_error_t loader_mem_begin_cmd(uint32_t offset, uint32_t size, uint32_t blocks_to_write, uint32_t block_size);
-
-esp_loader_error_t loader_mem_data_cmd(const uint8_t *data, uint32_t size);
-
-esp_loader_error_t loader_mem_end_cmd(uint32_t entrypoint);
-
-esp_loader_error_t loader_write_reg_cmd(uint32_t address, uint32_t value, uint32_t mask, uint32_t delay_us);
-
-esp_loader_error_t loader_read_reg_cmd(uint32_t address, uint32_t *reg);
-
-esp_loader_error_t loader_sync_cmd(void);
-
-esp_loader_error_t loader_spi_attach_cmd(uint32_t config);
-
-esp_loader_error_t loader_change_baudrate_cmd(uint32_t baudrate);
-
-esp_loader_error_t loader_md5_cmd(uint32_t address, uint32_t size, uint8_t *md5_out);
-
-esp_loader_error_t loader_spi_parameters(uint32_t total_size);
-
-#ifdef __cplusplus
-}
-#endif

+ 28 - 0
private_include/slip.h

@@ -0,0 +1,28 @@
+/* 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 "esp_loader.h"
+#include <stdint.h>
+#include <stdlib.h>
+
+esp_loader_error_t SLIP_receive_data(uint8_t *buff, size_t size);
+
+esp_loader_error_t SLIP_receive_packet(uint8_t *buff, size_t size);
+
+esp_loader_error_t SLIP_send(const uint8_t *data, size_t size);
+
+esp_loader_error_t SLIP_send_delimiter(void);

+ 5 - 6
src/esp_loader.c

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -13,9 +13,8 @@
  * limitations under the License.
  */
 
-#include "serial_comm_prv.h"
-#include "serial_comm.h"
-#include "serial_io.h"
+#include "protocol.h"
+#include "esp_loader_io.h"
 #include "esp_loader.h"
 #include "esp_targets.h"
 #include "md5_hash.h"
@@ -329,7 +328,7 @@ esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_valu
     return loader_write_reg_cmd(address, reg_value, 0xFFFFFFFF, 0);
 }
 
-esp_loader_error_t esp_loader_change_baudrate(uint32_t baudrate)
+esp_loader_error_t esp_loader_change_transmission_rate(uint32_t transmission_rate)
 {
     if (s_target == ESP8266_CHIP) {
         return ESP_LOADER_ERROR_UNSUPPORTED_FUNC;
@@ -337,7 +336,7 @@ esp_loader_error_t esp_loader_change_baudrate(uint32_t baudrate)
 
     loader_port_start_timer(DEFAULT_TIMEOUT);
 
-    return loader_change_baudrate_cmd(baudrate);
+    return loader_change_baudrate_cmd(transmission_rate);
 }
 
 #if MD5_ENABLED

+ 5 - 115
src/serial_comm.c → src/protocol.c

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -13,9 +13,9 @@
  * limitations under the License.
  */
 
-#include "serial_comm_prv.h"
-#include "serial_comm.h"
-#include "serial_io.h"
+#include "protocol.h"
+#include "esp_loader_io.h"
+#include "slip.h"
 #include <stddef.h>
 #include <string.h>
 
@@ -23,23 +23,8 @@
 
 static uint32_t s_sequence_number = 0;
 
-static const uint8_t DELIMITER = 0xC0;
-static const uint8_t C0_REPLACEMENT[2] = {0xDB, 0xDC};
-static const uint8_t DB_REPLACEMENT[2] = {0xDB, 0xDD};
-
 static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, void* resp, uint32_t resp_size);
 
-
-static inline esp_loader_error_t serial_read(uint8_t *buff, size_t size)
-{
-    return loader_port_serial_read(buff, size, loader_port_remaining_time());
-}
-
-static inline esp_loader_error_t serial_write(const uint8_t *buff, size_t size)
-{
-    return loader_port_serial_write(buff, size, loader_port_remaining_time());
-}
-
 static uint8_t compute_checksum(const uint8_t *data, uint32_t size)
 {
     uint8_t checksum = 0xEF;
@@ -51,101 +36,6 @@ static uint8_t compute_checksum(const uint8_t *data, uint32_t size)
     return checksum;
 }
 
-static esp_loader_error_t SLIP_receive_data(uint8_t *buff, uint32_t size)
-{
-    uint8_t ch;
-
-    for (uint32_t i = 0; i < size; i++) {
-        RETURN_ON_ERROR( serial_read(&ch, 1) );
-
-        if (ch == 0xDB) {
-            RETURN_ON_ERROR( serial_read(&ch, 1) );
-            if (ch == 0xDC) {
-                buff[i] = 0xC0;
-            } else if (ch == 0xDD) {
-                buff[i] = 0xDB;
-            } else {
-                return ESP_LOADER_ERROR_INVALID_RESPONSE;
-            }
-        } else {
-            buff[i] = ch;
-        }
-    }
-
-    return ESP_LOADER_SUCCESS;
-}
-
-
-static esp_loader_error_t SLIP_receive_packet(uint8_t *buff, uint32_t size)
-{
-    uint8_t ch;
-
-    // Wait for delimiter
-    do {
-        RETURN_ON_ERROR( serial_read(&ch, 1) );
-    } while (ch != DELIMITER);
-
-    // Workaround: bootloader sends two dummy(0xC0) bytes after response when baud rate is changed.
-    do {
-        RETURN_ON_ERROR( serial_read(&ch, 1) );
-    } while (ch == DELIMITER);
-
-    buff[0] = ch;
-
-    RETURN_ON_ERROR( SLIP_receive_data(&buff[1], size - 1) );
-
-    // Wait for delimiter
-    do {
-        RETURN_ON_ERROR( serial_read(&ch, 1) );
-    } while (ch != DELIMITER);
-
-    return ESP_LOADER_SUCCESS;
-}
-
-
-static esp_loader_error_t SLIP_send(const uint8_t *data, uint32_t size)
-{
-    uint32_t to_write = 0;  // Bytes ready to write as they are
-    uint32_t written = 0;   // Bytes already written
-
-    for (uint32_t i = 0; i < size; i++) {
-        if (data[i] != 0xC0 && data[i] != 0xDB) {
-            to_write++; // Queue this byte for writing
-            continue;
-        }
-
-        // We have a byte that needs encoding, write the queue first
-        if (to_write > 0) {
-            RETURN_ON_ERROR( serial_write(&data[written], to_write) );
-        }
-
-        // Write the encoded byte
-        if (data[i] == 0xC0) {
-            RETURN_ON_ERROR( serial_write(C0_REPLACEMENT, 2) );
-        } else {
-            RETURN_ON_ERROR( serial_write(DB_REPLACEMENT, 2) );
-        }
-
-        // Update to start again after the encoded byte
-        written = i + 1;
-        to_write = 0;
-    }
-
-    // Write the rest of the bytes that didn't need encoding
-    if (to_write > 0) {
-        RETURN_ON_ERROR( serial_write(&data[written], to_write) );
-    }
-
-    return ESP_LOADER_SUCCESS;
-}
-
-
-static esp_loader_error_t SLIP_send_delimiter(void)
-{
-    return serial_write(&DELIMITER, 1);
-}
-
-
 static esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_value)
 {
     response_t response;
@@ -205,7 +95,7 @@ static void log_loader_internal_error(error_code_t error)
         case DEFLATE_ERROR:   loader_port_debug_print("DEFLATE_ERROR"); break;
         default:              loader_port_debug_print("UNKNOWN ERROR"); break;
     }
-    
+
     loader_port_debug_print("\n");
 }
 

+ 125 - 0
src/slip.c

@@ -0,0 +1,125 @@
+/* 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 "slip.h"
+#include "esp_loader_io.h"
+
+static const uint8_t DELIMITER = 0xC0;
+static const uint8_t C0_REPLACEMENT[2] = {0xDB, 0xDC};
+static const uint8_t DB_REPLACEMENT[2] = {0xDB, 0xDD};
+
+static inline esp_loader_error_t peripheral_read(uint8_t *buff, const size_t size)
+{
+    return loader_port_read(buff, size, loader_port_remaining_time());
+}
+
+static inline esp_loader_error_t peripheral_write(const uint8_t *buff, const size_t size)
+{
+    return loader_port_write(buff, size, loader_port_remaining_time());
+}
+
+esp_loader_error_t SLIP_receive_data(uint8_t *buff, const size_t size)
+{
+    uint8_t ch;
+
+    for (uint32_t i = 0; i < size; i++) {
+        RETURN_ON_ERROR( peripheral_read(&ch, 1) );
+
+        if (ch == 0xDB) {
+            RETURN_ON_ERROR( peripheral_read(&ch, 1) );
+            if (ch == 0xDC) {
+                buff[i] = 0xC0;
+            } else if (ch == 0xDD) {
+                buff[i] = 0xDB;
+            } else {
+                return ESP_LOADER_ERROR_INVALID_RESPONSE;
+            }
+        } else {
+            buff[i] = ch;
+        }
+    }
+
+    return ESP_LOADER_SUCCESS;
+}
+
+
+esp_loader_error_t SLIP_receive_packet(uint8_t *buff, const size_t size)
+{
+    uint8_t ch;
+
+    // Wait for delimiter
+    do {
+        RETURN_ON_ERROR( peripheral_read(&ch, 1) );
+    } while (ch != DELIMITER);
+
+    // Workaround: bootloader sends two dummy(0xC0) bytes after response when baud rate is changed.
+    do {
+        RETURN_ON_ERROR( peripheral_read(&ch, 1) );
+    } while (ch == DELIMITER);
+
+    buff[0] = ch;
+
+    RETURN_ON_ERROR( SLIP_receive_data(&buff[1], size - 1) );
+
+    // Wait for delimiter
+    do {
+        RETURN_ON_ERROR( peripheral_read(&ch, 1) );
+    } while (ch != DELIMITER);
+
+    return ESP_LOADER_SUCCESS;
+}
+
+
+esp_loader_error_t SLIP_send(const uint8_t *data, const size_t size)
+{
+    uint32_t to_write = 0;  // Bytes ready to write as they are
+    uint32_t written = 0;   // Bytes already written
+
+    for (uint32_t i = 0; i < size; i++) {
+        if (data[i] != 0xC0 && data[i] != 0xDB) {
+            to_write++; // Queue this byte for writing
+            continue;
+        }
+
+        // We have a byte that needs encoding, write the queue first
+        if (to_write > 0) {
+            RETURN_ON_ERROR( peripheral_write(&data[written], to_write) );
+        }
+
+        // Write the encoded byte
+        if (data[i] == 0xC0) {
+            RETURN_ON_ERROR( peripheral_write(C0_REPLACEMENT, 2) );
+        } else {
+            RETURN_ON_ERROR( peripheral_write(DB_REPLACEMENT, 2) );
+        }
+
+        // Update to start again after the encoded byte
+        written = i + 1;
+        to_write = 0;
+    }
+
+    // Write the rest of the bytes that didn't need encoding
+    if (to_write > 0) {
+        RETURN_ON_ERROR( peripheral_write(&data[written], to_write) );
+    }
+
+    return ESP_LOADER_SUCCESS;
+}
+
+
+esp_loader_error_t SLIP_send_delimiter(void)
+{
+    return peripheral_write(&DELIMITER, 1);
+}

+ 3 - 2
test/CMakeLists.txt

@@ -3,10 +3,11 @@ project(serial_flasher_test)
 
 add_executable( ${PROJECT_NAME}
 	test_main.cpp
-	../src/serial_comm.c
 	../src/esp_loader.c
+	../src/esp_targets.c
 	../src/md5_hash.c
-	../src/esp_targets.c )
+	../src/protocol.c
+	../src/slip.c)
 
 target_include_directories(${PROJECT_NAME} PRIVATE ../include ../private_include ../test ../port)
 

+ 8 - 8
test/qemu_test.cpp

@@ -1,4 +1,4 @@
-/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
+/* Copyright 2018-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.
@@ -16,7 +16,7 @@
 #include "catch.hpp"
 #include "serial_io_mock.h"
 #include "esp_loader.h"
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include <algorithm>
 #include <iostream>
 #include <fstream>
@@ -45,7 +45,7 @@ inline auto file_size_is(ifstream& file)
     file.seekg(0, ios::end);
     file_size = file.tellg();
     file.seekg(0);
-    
+
     return file_size;
 }
 
@@ -59,7 +59,7 @@ void flash_application(ifstream& image)
 
     while(image_size > 0) {
         size_t to_read = min(image_size, sizeof(payload));
-        
+
         image.read((char *)payload, to_read);
 
         ESP_ERR_CHECK( esp_loader_flash_write(payload, to_read) );
@@ -77,7 +77,7 @@ bool file_compare(ifstream& file_1, ifstream& file_2, size_t file_size)
 {
     vector<char> file_data_1(file_size);
     vector<char> file_data_2(file_size);
-    
+
     file_1.read((char*) &file_data_1[0], file_size);
     file_2.read((char*) &file_data_2[0], file_size);
 
@@ -97,16 +97,16 @@ TEST_CASE( "Can write application to flash" )
     REQUIRE ( qemu_image.is_open() );
 
     flash_application(new_image);
-    
+
     auto new_image_size = file_size_is(new_image);
     qemu_image.seekg(APP_START_ADDRESS);
     new_image.seekg(0);
-    
+
     REQUIRE ( file_compare(new_image, qemu_image, new_image_size) );
 
     ESP_ERR_CHECK ( esp_loader_flash_verify() );
 
-    // NOTE: loader_flash_finish() is not called to prevent reset of target 
+    // NOTE: loader_flash_finish() is not called to prevent reset of target
 }
 
 TEST_CASE( "Can write and read register" )

+ 7 - 7
test/serial_io_mock.cpp

@@ -1,4 +1,4 @@
-/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
+/* Copyright 2018-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.
@@ -19,7 +19,7 @@
 #include <algorithm>
 #include <iostream>
 #include <stdio.h>
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include "serial_io_mock.h"
 
 using namespace std;
@@ -30,24 +30,24 @@ static uint32_t receive_delay = 0;
 static int32_t timer = 0;
 
 
-esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config)
+esp_loader_error_t loader_port_mock_init(const loader_serial_config_t *config)
 {
     return ESP_LOADER_SUCCESS;
 }
 
-void loader_port_serial_deinit()
+void loader_port_mock_deinit()
 {
 
 }
 
-esp_loader_error_t loader_port_serial_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)
 {
     copy(&data[0], &data[size], back_inserter(write_buffer));
 
     return ESP_LOADER_SUCCESS;
 }
 
-esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
+esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
 {
     if (read_buffer.size() < size) {
         return ESP_LOADER_ERROR_TIMEOUT;
@@ -55,7 +55,7 @@ esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_
 
     if (receive_delay != 0 && timeout != 0) {
         if (receive_delay > timeout) {
-            receive_delay -= timeout;    
+            receive_delay -= timeout;
             return ESP_LOADER_ERROR_TIMEOUT;
         }
         receive_delay = 0;

+ 3 - 3
test/serial_io_mock.h

@@ -1,4 +1,4 @@
-/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
+/* Copyright 2018-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.
@@ -34,5 +34,5 @@ typedef struct {
     uint32_t dummy;
 } loader_serial_config_t;
 
-esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config);
-void loader_port_serial_deinit();
+esp_loader_error_t loader_port_mock_init(const loader_serial_config_t *config);
+void loader_port_mock_deinit();

+ 7 - 7
test/serial_io_tcp.cpp

@@ -1,4 +1,4 @@
-/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
+/* Copyright 2018-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.
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include "serial_io_mock.h"
 
 #include <sys/socket.h>
@@ -33,7 +33,7 @@ const uint32_t PORT = 5555;
 static int sock = 0;
 ofstream file;
 
-esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config)
+esp_loader_error_t loader_port_mock_init(const loader_serial_config_t *config)
 {
     struct sockaddr_in serv_addr;
 
@@ -66,7 +66,7 @@ esp_loader_error_t loader_port_serial_init(const loader_serial_config_t *config)
 }
 
 
-void loader_port_serial_deinit()
+void loader_port_mock_deinit()
 {
     if (sock != 0) {
         shutdown(sock, 0);
@@ -79,7 +79,7 @@ void loader_port_serial_deinit()
 }
 
 
-esp_loader_error_t loader_port_serial_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)
 {
     uint32_t written = 0;
 
@@ -94,7 +94,7 @@ esp_loader_error_t loader_port_serial_write(const uint8_t *data, uint16_t size,
     return ESP_LOADER_SUCCESS;
 }
 
-esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_t timeout)
+esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
 {
     uint32_t written = 0;
     int bytes_read = 0;
@@ -105,7 +105,7 @@ esp_loader_error_t loader_port_serial_read(uint8_t *data, uint16_t size, uint32_
             cout << "Socket connection lost\n";
             return ESP_LOADER_ERROR_FAIL;
         }
-        
+
         file.write((const char*)&data[written], bytes_read);
         file.flush();
 

+ 8 - 9
test/test.cpp

@@ -1,4 +1,4 @@
-/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
+/* 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.
@@ -14,11 +14,10 @@
  */
 
 #include "catch.hpp"
-#include "serial_comm.h"
-#include "serial_comm_prv.h"
+#include "protocol.h"
 #include "serial_io_mock.h"
 #include "esp_loader.h"
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include <string.h>
 #include <stdio.h>
 #include <array>
@@ -361,18 +360,18 @@ TEST_CASE( "Serial read works correctly" )
     set_read_buffer(&reg_value, sizeof(reg_value));
 
     SECTION( "Read buffer can be read" ) {
-        loader_port_serial_read(readout, sizeof(readout), 0);
+        loader_port_read(readout, sizeof(readout), 0);
         REQUIRE( memcmp(readout, expected, sizeof(readout)) == 0 );
     }
 
     SECTION ( "Read buffer can be read in smaller chunks" ) {
-        loader_port_serial_read(&readout[0], 3, 0);
-        loader_port_serial_read(&readout[3], 3, 0);
+        loader_port_read(&readout[0], 3, 0);
+        loader_port_read(&readout[3], 3, 0);
         REQUIRE( memcmp(readout, expected, sizeof(readout)) == 0 );
     }
 
     SECTION ( "Timeout is returned when requested amount of data is not available" ) {
-        REQUIRE( loader_port_serial_read(readout, sizeof(readout) + 1, 0) == ESP_LOADER_ERROR_TIMEOUT);
+        REQUIRE( loader_port_read(readout, sizeof(readout) + 1, 0) == ESP_LOADER_ERROR_TIMEOUT);
     }
 
     SECTION ( "Read buffer is correctly SLIP encoded " ) {
@@ -384,7 +383,7 @@ TEST_CASE( "Serial read works correctly" )
 
         fill(encoded, &encoded[sizeof(encoded)], 0);
         set_read_buffer(data_to_encode, sizeof(data_to_encode));
-        loader_port_serial_read(encoded, sizeof(encoded), 0);
+        loader_port_read(encoded, sizeof(encoded), 0);
 
         REQUIRE( memcmp(expected, encoded, sizeof(expected)) == 0 );
     }

+ 5 - 5
test/test_main.cpp

@@ -1,4 +1,4 @@
-/* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
+/* Copyright 2018-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.
@@ -17,16 +17,16 @@
 #define CATCH_CONFIG_RUNNER
 
 #include "catch.hpp"
-#include "serial_io.h"
+#include "esp_loader_io.h"
 #include "serial_io_mock.h"
 
 
 int main( int argc, char* argv[] ) {
-    
+
     const loader_serial_config_t dummy_config = { 0 };
 
     // global setup...
-    if( loader_port_serial_init(&dummy_config) != ESP_LOADER_SUCCESS ) {
+    if( loader_port_mock_init(&dummy_config) != ESP_LOADER_SUCCESS ) {
         std::cout << "Serial initialization failed";
         return 0;
     }
@@ -34,7 +34,7 @@ int main( int argc, char* argv[] ) {
     int result = Catch::Session().run( argc, argv );
 
     // global clean-up...
-    loader_port_serial_deinit();
+    loader_port_mock_deinit();
 
     return result;
 }

+ 2 - 1
zephyr/CMakeLists.txt

@@ -13,7 +13,8 @@ if (CONFIG_ESP_SERIAL_FLASHER)
 
     zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/src/esp_loader.c
                 ${ZEPHYR_CURRENT_MODULE_DIR}/src/esp_targets.c
-                ${ZEPHYR_CURRENT_MODULE_DIR}/src/serial_comm.c
+                ${ZEPHYR_CURRENT_MODULE_DIR}/src/protocol.c
+                ${ZEPHYR_CURRENT_MODULE_DIR}/src/slip.c
                 ${ZEPHYR_CURRENT_MODULE_DIR}/src/md5_hash.c
                 ${ZEPHYR_CURRENT_MODULE_DIR}/port/zephyr_port.c
     )