Просмотр исходного кода

zephyr: add initial support

Brings Zephyr support for this feature.

Signed-off-by: Dirk Kaar <kaar@kt-elektronik.de>
Dirk Kaar 3 лет назад
Родитель
Сommit
3982a207a3
7 измененных файлов с 268 добавлено и 2 удалено
  1. 24 1
      README.md
  2. 157 0
      port/zephyr_port.c
  3. 39 0
      port/zephyr_port.h
  4. 1 1
      src/esp_loader.c
  5. 26 0
      zephyr/CMakeLists.txt
  6. 16 0
      zephyr/Kconfig
  7. 5 0
      zephyr/module.yml

+ 24 - 1
README.md

@@ -9,6 +9,7 @@ Supported **host** microcontrollers:
 - STM32
 - STM32
 - Raspberry Pi
 - Raspberry Pi
 - ESP32
 - ESP32
+- MCU running Zephyr OS
 
 
 Supported **target** microcontrollers:
 Supported **target** microcontrollers:
 
 
@@ -38,7 +39,7 @@ Following functions are part of serial_io.h header for convenience, however, use
 - loader_port_debug_print()
 - loader_port_debug_print()
 
 
 Prototypes of all function mentioned above can be found in [serial_io.h](include/serial_io.h).
 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) and [STM32 port](port/stm32_port.c) are available.
+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.
 
 
 ## Configuration
 ## Configuration
 
 
@@ -104,6 +105,28 @@ set(STM32_CHIP          STM32F407VG)
 set(PORT                STM32)
 set(PORT                STM32)
 ```
 ```
 
 
+### Zephyr support
+
+The Zephyr port is ready to be integrated into your Zephyr app as a Zephyr module. In the manifest file (west.yml), add:
+
+```
+    - name: esp-flasher
+      url: https://github.com/espressif/esp-serial-flasher
+      revision: <insert current revision here>
+      path: modules/lib/esp_flasher
+```
+
+And add
+
+```
+CONFIG_ESP_SERIAL_FLASHER=y
+CONFIG_CONSOLE_GETCHAR=y
+```
+
+to your project configuration `prj.conf`.
+
+For your C/C++ source code, you can use the example code provided in `examples/zephyr_example` as a starting point.
+
 ## Licence
 ## Licence
 
 
 Code is distributed under Apache 2.0 license.
 Code is distributed under Apache 2.0 license.

+ 157 - 0
port/zephyr_port.c

@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2022 KT-Elektronik, Klaucke und Partner GmbH
+ *
+ * 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 "zephyr_port.h"
+#include <zephyr/drivers/uart.h>
+#include <zephyr/console/tty.h>
+
+static const struct device *uart_dev;
+static struct gpio_dt_spec enable_spec;
+static struct gpio_dt_spec boot_spec;
+
+static struct tty_serial tty;
+static char tty_rx_buf[CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE];
+static char tty_tx_buf[CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE];
+
+esp_loader_error_t configure_tty()
+{
+    if (tty_init(&tty, uart_dev) < 0 ||
+        tty_set_rx_buf(&tty, tty_rx_buf, sizeof(tty_rx_buf)) < 0 ||
+        tty_set_tx_buf(&tty, tty_tx_buf, sizeof(tty_tx_buf)) < 0) {
+        return ESP_LOADER_ERROR_FAIL;
+    }
+
+    return ESP_LOADER_SUCCESS;
+}
+
+esp_loader_error_t loader_port_serial_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
+{
+    ssize_t bytes_read = 0;
+
+    if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
+        return ESP_LOADER_ERROR_FAIL;
+    }
+
+    tty_set_rx_timeout(&tty, timeout);
+    while (bytes_read < size) {
+        uint16_t chunk = CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE;
+        if (size < CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE) {
+            chunk = size;
+        }
+
+        ssize_t read = tty_read(&tty, &data[bytes_read], chunk);
+        if (read < 0) {
+            return ESP_LOADER_ERROR_TIMEOUT;
+        } else {
+            bytes_read += read;
+        }
+    }
+
+    if (bytes_read != size) return ESP_LOADER_ERROR_FAIL;
+
+    return ESP_LOADER_SUCCESS;
+}
+
+esp_loader_error_t loader_port_serial_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
+{
+    ssize_t bytes_wrote = 0;
+
+    if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
+        return ESP_LOADER_ERROR_FAIL;
+    }
+
+    tty_set_tx_timeout(&tty, timeout);
+    while (bytes_wrote < size) {
+        uint16_t chunk = CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE;
+        if (size < CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE) {
+            chunk = size;
+        }
+
+        ssize_t read = tty_write(&tty, &data[bytes_wrote], chunk);
+        if (read < 0) {
+            return ESP_LOADER_ERROR_TIMEOUT;
+        } else {
+            bytes_wrote += read;
+        }
+    }
+
+    if (bytes_wrote != size) return ESP_LOADER_ERROR_FAIL;
+
+    return ESP_LOADER_SUCCESS;
+}
+
+esp_loader_error_t loader_port_zephyr_init(const loader_zephyr_config_t *config)
+{
+    uart_dev = config->uart_dev;
+    enable_spec = config->enable_spec;
+    boot_spec = config->boot_spec;
+    return configure_tty();
+}
+
+void loader_port_reset_target(void)
+{
+    gpio_pin_set_dt(&enable_spec, false);
+    loader_port_delay_ms(CONFIG_SERIAL_FLASHER_RESET_HOLD_TIME_MS);
+    gpio_pin_set_dt(&enable_spec, true);
+}
+
+void loader_port_enter_bootloader(void)
+{
+    gpio_pin_set_dt(&boot_spec, false);
+    loader_port_reset_target();
+    loader_port_delay_ms(CONFIG_SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
+    gpio_pin_set_dt(&boot_spec, true);
+}
+
+void loader_port_delay_ms(uint32_t ms)
+{
+    k_msleep(ms);
+}
+
+static uint64_t s_time_end;
+
+void loader_port_start_timer(uint32_t ms)
+{
+    s_time_end = sys_clock_timeout_end_calc(Z_TIMEOUT_MS(ms));
+}
+
+uint32_t loader_port_remaining_time(void)
+{
+    int64_t remaining = k_ticks_to_ms_floor64(s_time_end - k_uptime_ticks());
+    return (remaining > 0) ? (uint32_t)remaining : 0;
+}
+
+esp_loader_error_t loader_port_change_baudrate(uint32_t baudrate)
+{
+    struct uart_config uart_config;
+
+    if (!device_is_ready(uart_dev)) {
+        return ESP_LOADER_ERROR_FAIL;
+    }
+
+    if (uart_config_get(uart_dev, &uart_config) != 0) {
+        return ESP_LOADER_ERROR_FAIL;
+    }
+    uart_config.baudrate = baudrate;
+
+    if (uart_configure(uart_dev, &uart_config) != 0) {
+        return ESP_LOADER_ERROR_FAIL;
+    }
+
+    /* bitrate-change can require tty re-configuration */
+    return configure_tty();
+}
+

+ 39 - 0
port/zephyr_port.h

@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2022 KT-Elektronik, Klaucke und Partner GmbH
+ *
+ * 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 "serial_io.h"
+#include <zephyr/kernel.h>
+#include <zephyr/drivers/gpio.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+    const struct device *uart_dev;
+    const struct gpio_dt_spec enable_spec;
+    const struct gpio_dt_spec boot_spec;
+} loader_zephyr_config_t;
+
+esp_loader_error_t loader_port_zephyr_init(const loader_zephyr_config_t *config);
+
+#ifdef __cplusplus
+}
+#endif
+

+ 1 - 1
src/esp_loader.c

@@ -365,4 +365,4 @@ esp_loader_error_t esp_loader_flash_verify(void)
 void esp_loader_reset_target(void)
 void esp_loader_reset_target(void)
 {
 {
     loader_port_reset_target();
     loader_port_reset_target();
-}
+}

+ 26 - 0
zephyr/CMakeLists.txt

@@ -0,0 +1,26 @@
+cmake_minimum_required(VERSION 3.5)
+
+if (CONFIG_ESP_SERIAL_FLASHER)
+    zephyr_include_directories(
+        "${ZEPHYR_CURRENT_MODULE_DIR}/include"
+        "${ZEPHYR_CURRENT_MODULE_DIR}/port"
+        "${ZEPHYR_CURRENT_MODULE_DIR}/private_include"
+    )
+
+    zephyr_interface_library_named(esp_flasher)
+
+    zephyr_library()
+
+    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/md5_hash.c
+                ${ZEPHYR_CURRENT_MODULE_DIR}/port/zephyr_port.c
+    )
+
+    zephyr_library_link_libraries(esp_flasher)
+
+    if(DEFINED MD5_ENABLED OR CONFIG_SERIAL_FLASHER_MD5_ENABLED)
+        target_compile_definitions(esp_flasher INTERFACE -DMD5_ENABLED=1)
+    endif()
+endif()

+ 16 - 0
zephyr/Kconfig

@@ -0,0 +1,16 @@
+config ESP_SERIAL_FLASHER
+    bool "Enable ESP serial flasher library"
+    default y
+    select CONSOLE_SUBSYS
+    help
+      Select this option to enable the ESP serial flasher library.
+
+config ESP_SERIAL_FLASHER_UART_BUFSIZE
+    int "ESP Serial Flasher UART buffer size"
+    default 512
+    help
+      Buffer size for UART TX and RX packets
+
+if ESP_SERIAL_FLASHER
+    rsource "../Kconfig"
+endif

+ 5 - 0
zephyr/module.yml

@@ -0,0 +1,5 @@
+name: esp-flasher
+
+build:
+  cmake: zephyr
+  kconfig: zephyr/Kconfig