From 964e027860709d904938e4a627a003411f5bb8c9 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Thu, 6 Sep 2018 20:40:02 +0800 Subject: [PATCH] feat(bootloader): Add configration for output console UART --- .../subproject/main/esp8266.bootloader.rom.ld | 5 +-- .../bootloader_support/src/bootloader_init.c | 33 +++++++++++++++++++ components/esp8266/Kconfig | 17 ++++++++++ components/esp8266/source/ets_printf.c | 10 +++--- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/components/bootloader/subproject/main/esp8266.bootloader.rom.ld b/components/bootloader/subproject/main/esp8266.bootloader.rom.ld index 948b8a9c..a9e48559 100644 --- a/components/bootloader/subproject/main/esp8266.bootloader.rom.ld +++ b/components/bootloader/subproject/main/esp8266.bootloader.rom.ld @@ -1,6 +1,7 @@ PROVIDE ( ets_memcpy = 0x400018b4 ); -PROVIDE ( ets_printf = 0x400024cc ); PROVIDE ( SPIRead = 0x40004b1c ); -PROVIDE ( xthal_get_ccount = 0x4000dd38 ); \ No newline at end of file +PROVIDE ( xthal_get_ccount = 0x4000dd38 ); +PROVIDE ( uart_div_modify = 0x400039d8 ); +PROVIDE ( ets_io_vprintf = 0x40001f00 ); \ No newline at end of file diff --git a/components/bootloader_support/src/bootloader_init.c b/components/bootloader_support/src/bootloader_init.c index c876f0f4..d757e796 100644 --- a/components/bootloader_support/src/bootloader_init.c +++ b/components/bootloader_support/src/bootloader_init.c @@ -547,6 +547,15 @@ void __assert_func(const char *file, int line, const char *func, const char *exp #include "esp_flash_partitions.h" #include "bootloader_flash.h" +#include "esp8266/uart_register.h" +#include "esp8266/eagle_soc.h" +#include "esp8266/gpio_register.h" +#include "esp8266/pin_mux_register.h" +#include "esp8266/rom_functions.h" + +#define CONFIG_CONSOLE_UART_BAUDRATE 74880 +#define BOOTLOADER_CONSOLE_CLK_FREQ 52 * 1000 * 1000 + extern int _bss_start; extern int _bss_end; extern int _data_start; @@ -558,6 +567,28 @@ static esp_err_t bootloader_main(); static void print_flash_info(const esp_image_header_t* pfhdr); static void update_flash_config(const esp_image_header_t* pfhdr); +static void uart_console_configure(void) +{ +#if CONFIG_CONSOLE_UART_NUM == 1 + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); + + CLEAR_PERI_REG_MASK(UART_CONF1(CONFIG_CONSOLE_UART_NUM), UART_RX_FLOW_EN); + CLEAR_PERI_REG_MASK(UART_CONF0(CONFIG_CONSOLE_UART_NUM), UART_TX_FLOW_EN); + + uart_div_modify(CONFIG_CONSOLE_UART_NUM, BOOTLOADER_CONSOLE_CLK_FREQ / CONFIG_CONSOLE_UART_BAUDRATE); + + WRITE_PERI_REG(UART_CONF0(CONFIG_CONSOLE_UART_NUM), + 0 // None parity + | (1 << 4) // 1-bit stop + | (3 << 2) // 8-bit data + | 0 // None flow control + | 0); // None Inverse + + SET_PERI_REG_MASK(UART_CONF0(CONFIG_CONSOLE_UART_NUM), UART_RXFIFO_RST | UART_TXFIFO_RST); + CLEAR_PERI_REG_MASK(UART_CONF0(CONFIG_CONSOLE_UART_NUM), UART_RXFIFO_RST | UART_TXFIFO_RST); +#endif +} + esp_err_t bootloader_init() { //Clear bss @@ -571,6 +602,8 @@ esp_err_t bootloader_init() static esp_err_t bootloader_main() { + uart_console_configure(); + esp_image_header_t fhdr; if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &fhdr, sizeof(esp_image_header_t), true) != ESP_OK) { ESP_LOGE(TAG, "failed to load bootloader header!"); diff --git a/components/esp8266/Kconfig b/components/esp8266/Kconfig index 84cbd91f..4985ade7 100644 --- a/components/esp8266/Kconfig +++ b/components/esp8266/Kconfig @@ -35,6 +35,23 @@ config SOC_FULL_ICACHE Enable this option, full 32 KB iram instead of 16 KB iram will be used as icache, so the heap use can use may reduce a lot. +choice CONSOLE_UART_NUM + prompt "UART peripheral to use for console output (0-1)" + default CONSOLE_UART_CUSTOM_NUM_0 + help + Configrate output console UART for "ets_printf", "printf", "ESP_LOGX" and so on. + +config CONSOLE_UART_CUSTOM_NUM_0 + bool "UART0" +config CONSOLE_UART_CUSTOM_NUM_1 + bool "UART1" +endchoice + +config CONSOLE_UART_NUM + int + default 0 if CONSOLE_UART_CUSTOM_NUM_0 + default 1 if CONSOLE_UART_CUSTOM_NUM_1 + endmenu menu WIFI diff --git a/components/esp8266/source/ets_printf.c b/components/esp8266/source/ets_printf.c index d3c43013..06e90c46 100644 --- a/components/esp8266/source/ets_printf.c +++ b/components/esp8266/source/ets_printf.c @@ -14,26 +14,24 @@ #include +#include "sdkconfig.h" + #include "esp_attr.h" #include "esp8266/eagle_soc.h" #include "esp8266/uart_register.h" #include "esp8266/rom_functions.h" -#ifndef CONFIG_ETS_PUTC_UART -#define CONFIG_ETS_PUTC_UART 0 -#endif - int IRAM_ATTR ets_putc(int c) { while (1) { - uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(CONFIG_ETS_PUTC_UART)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); + uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(CONFIG_CONSOLE_UART_NUM)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) break; } - WRITE_PERI_REG(UART_FIFO(CONFIG_ETS_PUTC_UART) , c); + WRITE_PERI_REG(UART_FIFO(CONFIG_CONSOLE_UART_NUM) , c); return c; }