From e15f211d46bf087eac449fce37236974e2132316 Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Fri, 25 May 2018 17:01:50 +0800 Subject: [PATCH] feat(esp8266): Add "putc" and "vprintf" of version "ets_" --- .../esp8266/include/esp8266/rom_functions.h | 2 +- components/esp8266/include/esp_libc.h | 23 +++++ components/esp8266/ld/eagle.rom.addr.v6.ld | 2 +- components/esp8266/source/ets_printf.c | 20 +++-- components/freertos/port/esp8266/panic.c | 85 ++----------------- 5 files changed, 45 insertions(+), 87 deletions(-) diff --git a/components/esp8266/include/esp8266/rom_functions.h b/components/esp8266/include/esp8266/rom_functions.h index 084cb595..c69e0ba2 100644 --- a/components/esp8266/include/esp8266/rom_functions.h +++ b/components/esp8266/include/esp8266/rom_functions.h @@ -9,7 +9,7 @@ uint32_t Wait_SPI_Idle(); void uart_div_modify(uint32_t uart_no, uint32_t baud_div); void ets_delay_us(uint32_t us); -int ets_vprintf(void (*putc)(char), const char* fmt, va_list ap); +int ets_io_vprintf(int (*putc)(int), const char* fmt, va_list ap); void system_soft_wdt_feed(); diff --git a/components/esp8266/include/esp_libc.h b/components/esp8266/include/esp_libc.h index da49e35d..66f2646e 100644 --- a/components/esp8266/include/esp_libc.h +++ b/components/esp8266/include/esp_libc.h @@ -28,14 +28,37 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { #endif + uint32_t os_random(void); int32_t os_get_random(unsigned char *buf, size_t len); +/** + * @brief put a character to uart or other devices, similar with putc. + * + * @param c : a character. + * + * @return int : the character written as an unsigned char cast to an int or EOF on error. + */ +int ets_putc(int c); + +/** + * @brief Printf the strings to uart or other devices, similar with vprintf, simple than vprintf. + * Can not print float point data format, or longlong data format. + * + * @param const char *fmt : See vprintf. + * + * @param ap : parameter list, see vprintf. + * + * @return int : the length printed to the output device. + */ +int ets_vprintf(const char *fmt, va_list ap); + /** * @brief Printf the strings to uart or other devices, similar with printf, simple than printf. * Can not print float point data format, or longlong data format. diff --git a/components/esp8266/ld/eagle.rom.addr.v6.ld b/components/esp8266/ld/eagle.rom.addr.v6.ld index 90b6dc5c..49f6dd71 100644 --- a/components/esp8266/ld/eagle.rom.addr.v6.ld +++ b/components/esp8266/ld/eagle.rom.addr.v6.ld @@ -57,4 +57,4 @@ PROVIDE ( gpio_input_get = 0x40004cf0 ); PROVIDE ( gpio_pin_wakeup_disable = 0x40004ed4 ); PROVIDE ( gpio_pin_wakeup_enable = 0x40004e90 ); -PROVIDE ( ets_vprintf = 0x40001f00 ); \ No newline at end of file +PROVIDE ( ets_io_vprintf = 0x40001f00 ); \ No newline at end of file diff --git a/components/esp8266/source/ets_printf.c b/components/esp8266/source/ets_printf.c index 47451eea..d3c43013 100644 --- a/components/esp8266/source/ets_printf.c +++ b/components/esp8266/source/ets_printf.c @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include "esp_attr.h" @@ -21,16 +20,27 @@ #include "esp8266/uart_register.h" #include "esp8266/rom_functions.h" -static void IRAM_ATTR uart_tx_one_char(char c) +#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(0)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); + uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(CONFIG_ETS_PUTC_UART)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) break; } - WRITE_PERI_REG(UART_FIFO(0) , c); + WRITE_PERI_REG(UART_FIFO(CONFIG_ETS_PUTC_UART) , c); + + return c; +} + +int IRAM_ATTR ets_vprintf(const char *fmt, va_list ap) +{ + return ets_io_vprintf(ets_putc, fmt, ap); } /* Re-write ets_printf in SDK side, since ets_printf in ROM will use a global @@ -42,7 +52,7 @@ int IRAM_ATTR ets_printf(const char* fmt, ...) int ret; va_start(ap, fmt); - ret = ets_vprintf(uart_tx_one_char, fmt, ap); + ret = ets_vprintf(fmt, ap); va_end(ap); return ret; diff --git a/components/freertos/port/esp8266/panic.c b/components/freertos/port/esp8266/panic.c index 010781bd..3968ac22 100644 --- a/components/freertos/port/esp8266/panic.c +++ b/components/freertos/port/esp8266/panic.c @@ -12,83 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include "esp_attr.h" +#include "esp_libc.h" -#include "esp8266/ets_sys.h" #include "esp8266/eagle_soc.h" -#include "esp8266/uart_register.h" -#include "freertos/portmacro.h" - -/* - * Todo: panic output UART ID, we may add it to 'kconfig' to select target UART. - */ -#define PANIC_UART 0 - -/* - * @brief output a character - * - * @param c a character - * - * @return none - */ -static IRAM_ATTR void panit_putc(char c) -{ - while (1) { - uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(PANIC_UART)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S); - - if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) - break; - } - - WRITE_PERI_REG(UART_FIFO(PANIC_UART) , c); -} - -/* - * @brief output a left-aligned string - * - * @param s string pointer - * @param left_aligned left-aligned bytes - * - * @return none - */ -static IRAM_ATTR void panic_puts(const char *s, int left_aligned) -{ - int bytes = left_aligned - strlen(s); - - while (bytes-- > 0) - panit_putc(' '); - - while (*s) - panit_putc(*s++); -} - -/* - * @brief output a hex value string of 32-bites data - * - * @param hex 32-bites data - * - * @return none - */ -static IRAM_ATTR void panic_puthex(int hex) -{ - char buf[8]; - int bytes = 0; - - while (bytes < 8) { - char c = (uint32_t)hex % 16; - - if (c < 10) - c = c + '0'; - else - c = c - 10 + 'a'; - - buf[bytes++] = c; - hex >>= 4; - } - - while (bytes) - panit_putc(buf[--bytes]); -} +#include "esp8266/ets_sys.h" /* * @brief output xtensa register value map when crash @@ -114,12 +42,9 @@ void IRAM_ATTR panicHandler(void *frame) for (x = 0; x < 20; x += 4) { for (y = 0; y < 4; y++) { - panic_puts(sdesc[x + y], 8); - panic_puts(": 0x", 0); - panic_puthex(regs[x + y + 1]); - panic_puts(" ", 0); + ets_printf("%8s: 0x%08x ", sdesc[x + y], regs[x + y + 1]); } - panit_putc('\n'); + ets_printf("\n"); } /*