From 26e825b0dedba6cab9695e43ddb00c8801f6e46f Mon Sep 17 00:00:00 2001 From: Wu Jian Gang Date: Thu, 24 May 2018 11:47:21 +0800 Subject: [PATCH] feat(esp8266): Add ets_printf in SDK side --- .../esp8266/include/esp8266/rom_functions.h | 3 +- components/esp8266/include/esp_libc.h | 12 +++++ components/esp8266/ld/eagle.rom.addr.v6.ld | 2 +- components/esp8266/source/ets_printf.c | 49 +++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 components/esp8266/source/ets_printf.c diff --git a/components/esp8266/include/esp8266/rom_functions.h b/components/esp8266/include/esp8266/rom_functions.h index 32a0827b..084cb595 100644 --- a/components/esp8266/include/esp8266/rom_functions.h +++ b/components/esp8266/include/esp8266/rom_functions.h @@ -2,13 +2,14 @@ #define _ROM_FUNCTIONS_H #include +#include 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_printf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); +int ets_vprintf(void (*putc)(char), 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 92467def..da49e35d 100644 --- a/components/esp8266/include/esp_libc.h +++ b/components/esp8266/include/esp_libc.h @@ -36,6 +36,18 @@ extern "C" { uint32_t os_random(void); int32_t os_get_random(unsigned char *buf, size_t len); +/** + * @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. + * + * @param const char *fmt : See printf. + * + * @param ... : See printf. + * + * @return int : the length printed to the output device. + */ +int ets_printf(const char *fmt, ...); + #ifndef os_printf #define os_printf printf #endif diff --git a/components/esp8266/ld/eagle.rom.addr.v6.ld b/components/esp8266/ld/eagle.rom.addr.v6.ld index 4fc9d36f..90b6dc5c 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_printf = 0x400024cc ); \ No newline at end of file +PROVIDE ( ets_vprintf = 0x40001f00 ); \ No newline at end of file diff --git a/components/esp8266/source/ets_printf.c b/components/esp8266/source/ets_printf.c new file mode 100644 index 00000000..47451eea --- /dev/null +++ b/components/esp8266/source/ets_printf.c @@ -0,0 +1,49 @@ +// Copyright 2018-2019 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. + +#include +#include + +#include "esp_attr.h" + +#include "esp8266/eagle_soc.h" +#include "esp8266/uart_register.h" +#include "esp8266/rom_functions.h" + +static void IRAM_ATTR uart_tx_one_char(char c) +{ + while (1) { + uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(0)) & (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); +} + +/* Re-write ets_printf in SDK side, since ets_printf in ROM will use a global + * variable which address is in heap region of SDK side. If use ets_printf in ROM, + * this variable maybe re-write when heap alloc and modification.*/ +int IRAM_ATTR ets_printf(const char* fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = ets_vprintf(uart_tx_one_char, fmt, ap); + va_end(ap); + + return ret; +}