boot_serial: espressif: split serial adapter implementation for each chip

This commit also fixes array access on serial console read.

Signed-off-by: Almir Okato <almir.okato@espressif.com>
diff --git a/boot/espressif/CMakeLists.txt b/boot/espressif/CMakeLists.txt
index c708da5..5f79d32 100644
--- a/boot/espressif/CMakeLists.txt
+++ b/boot/espressif/CMakeLists.txt
@@ -213,7 +213,6 @@
     ${CMAKE_CURRENT_LIST_DIR}/port/esp_mcuboot.c
     ${CMAKE_CURRENT_LIST_DIR}/port/esp_loader.c
     ${CMAKE_CURRENT_LIST_DIR}/os.c
-    ${CMAKE_CURRENT_LIST_DIR}/serial_adapter.c
     )
 
 if(CONFIG_ESP_MCUBOOT_SERIAL)
@@ -227,7 +226,7 @@
         ${BOOT_SERIAL_DIR}/src/zcbor_common.c
         )
     list(APPEND port_srcs
-        ${CMAKE_CURRENT_LIST_DIR}/serial_adapter.c
+        ${CMAKE_CURRENT_LIST_DIR}/port/${MCUBOOT_TARGET}/serial_adapter.c
         ${MBEDTLS_DIR}/library/base64.c
         )
     list(APPEND CRYPTO_INC
@@ -247,6 +246,7 @@
     ${APP_EXECUTABLE}
     PUBLIC
     ${BOOTUTIL_DIR}/include
+    ${BOOTUTIL_DIR}/src
     ${BOOT_SERIAL_DIR}/include
     ${CRYPTO_INC}
     ${CMAKE_CURRENT_LIST_DIR}/include
diff --git a/boot/espressif/hal/CMakeLists.txt b/boot/espressif/hal/CMakeLists.txt
index 3e9fa6c..c838bc3 100644
--- a/boot/espressif/hal/CMakeLists.txt
+++ b/boot/espressif/hal/CMakeLists.txt
@@ -41,6 +41,7 @@
     ${esp_idf_dir}/components/efuse/${MCUBOOT_TARGET}/include
     ${esp_idf_dir}/components/efuse/private_include
     ${esp_idf_dir}/components/efuse/${MCUBOOT_TARGET}/private_include
+    ${esp_idf_dir}/components/esp_system/include
     ${esp_idf_dir}/components/newlib/platform_include
     )
 
diff --git a/boot/espressif/port/esp32/ld/bootloader.ld b/boot/espressif/port/esp32/ld/bootloader.ld
index 30f08dc..4e3ad89 100644
--- a/boot/espressif/port/esp32/ld/bootloader.ld
+++ b/boot/espressif/port/esp32/ld/bootloader.ld
@@ -40,6 +40,7 @@
     *libhal.a:bootloader_efuse_esp32.*(.literal .text .literal.* .text.*)
     *libhal.a:bootloader_utility.*(.literal .text .literal.* .text.*)
     *libhal.a:bootloader_sha.*(.literal .text .literal.* .text.*)
+    *libhal.a:bootloader_console.*(.literal .text .literal.* .text.*)
     *libhal.a:bootloader_console_loader.*(.literal .text .literal.* .text.*)
     *libhal.a:bootloader_panic.*(.literal .text .literal.* .text.*)
     *libhal.a:bootloader_soc.*(.literal .text .literal.* .text.*)
diff --git a/boot/espressif/serial_adapter.c b/boot/espressif/port/esp32/serial_adapter.c
similarity index 85%
rename from boot/espressif/serial_adapter.c
rename to boot/espressif/port/esp32/serial_adapter.c
index f605665..348ee9a 100644
--- a/boot/espressif/serial_adapter.c
+++ b/boot/espressif/port/esp32/serial_adapter.c
@@ -11,9 +11,12 @@
 #include <esp_rom_sys.h>
 #include <soc/uart_periph.h>
 #include <soc/gpio_struct.h>
+#include <soc/io_mux_reg.h>
+#include <soc/rtc.h>
 #include <hal/gpio_types.h>
 #include <hal/gpio_ll.h>
 #include <hal/uart_ll.h>
+#include <hal/clk_gate_ll.h>
 
 #ifdef CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT
 #define SERIAL_BOOT_GPIO_DETECT     CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT
@@ -88,6 +91,7 @@
                 read_len++;
                 if (read_len == cnt || str[read_len - 1] == '\n') {
                     stop = true;
+                    break;
                 }
             }
         }
@@ -118,10 +122,14 @@
     gpio_ll_output_enable(&GPIO, SERIAL_BOOT_GPIO_TX);
 
     uart_ll_set_mode_normal(serial_boot_uart_dev);
-    uart_ll_set_baudrate(serial_boot_uart_dev, 115200  );
-    uart_ll_set_stop_bits(serial_boot_uart_dev, 1u );
-    uart_ll_set_parity(serial_boot_uart_dev, UART_PARITY_DISABLE );
-    uart_ll_set_rx_tout(serial_boot_uart_dev, 16 );
+    uart_ll_set_baudrate(serial_boot_uart_dev, 115200);
+    uart_ll_set_stop_bits(serial_boot_uart_dev, 1u);
+    uart_ll_set_parity(serial_boot_uart_dev, UART_PARITY_DISABLE);
+    uart_ll_set_rx_tout(serial_boot_uart_dev, 16);
+    uart_ll_set_data_bit_num(serial_boot_uart_dev, UART_DATA_8_BITS);
+    uart_ll_set_tx_idle_num(serial_boot_uart_dev, 0);
+    uart_ll_set_hw_flow_ctrl(serial_boot_uart_dev, UART_HW_FLOWCTRL_DISABLE, 100);
+    periph_ll_enable_clk_clear_rst(PERIPH_UART0_MODULE + SERIAL_BOOT_UART_NUM);
 
     uart_ll_txfifo_rst(serial_boot_uart_dev);
     uart_ll_rxfifo_rst(serial_boot_uart_dev);
@@ -153,14 +161,14 @@
     detected = (pin_value == SERIAL_BOOT_GPIO_DETECT_VAL);
     esp_rom_delay_us(50000);
 
-    if(detected) {
-        if(SERIAL_BOOT_DETECT_DELAY_S > 0) {
+    if (detected) {
+        if (SERIAL_BOOT_DETECT_DELAY_S > 0) {
             /* The delay time is an approximation */
-            for(int i = 0; i < (SERIAL_BOOT_DETECT_DELAY_S * 100); i++) {
+            for (int i = 0; i < (SERIAL_BOOT_DETECT_DELAY_S * 100); i++) {
                 esp_rom_delay_us(10000);
                 pin_value = gpio_ll_get_level(&GPIO, SERIAL_BOOT_GPIO_DETECT);
-                detected =  (pin_value == SERIAL_BOOT_GPIO_DETECT_VAL);
-                if(!detected) {
+                detected = (pin_value == SERIAL_BOOT_GPIO_DETECT_VAL);
+                if (!detected) {
                     break;
                 }
             }