diff --git a/components/cjson/CMakeLists.txt b/components/cjson/CMakeLists.txt index e3e0b554..afc69285 100644 --- a/components/cjson/CMakeLists.txt +++ b/components/cjson/CMakeLists.txt @@ -4,7 +4,3 @@ set(COMPONENT_ADD_INCLUDEDIRS cJSON) set(COMPONENT_REQUIRES newlib) register_component() - -if(CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL) -target_compile_definitions(${COMPONENT_LIB} PRIVATE -DCJSON_SPRINTF_FLOAT=1) -endif() diff --git a/components/cjson/cJSON/cJSON.c b/components/cjson/cJSON/cJSON.c index af1e4500..64186107 100644 --- a/components/cjson/cJSON/cJSON.c +++ b/components/cjson/cJSON/cJSON.c @@ -56,14 +56,6 @@ #pragma GCC visibility pop #endif -#ifndef CJSON_SPRINTF_FLOAT -#define CJSON_SPRINTF_FLOAT 0 -#endif - -#if !CJSON_SPRINTF_FLOAT -#include -#endif - #include "cJSON.h" /* define our own boolean type */ @@ -501,7 +493,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out } else { -#if CJSON_SPRINTF_FLOAT double test; /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ @@ -513,51 +504,6 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out /* If not, print with 17 decimal places of precision */ length = sprintf((char*)number_buffer, "%1.17g", d); } -#else - long long d64 = (long long)d; - long d32 = (long)d; - - /** - * Check if 32-bit type data is overflow. - */ - assert(d32 == d64 && "Library newlib of nano mode does not support double or long-long format, please enable newlib normal mode."); - - length = sprintf((char*)number_buffer, "%ld", d32); - - if ((double)d32 != d) { - size_t precision = 14; - unsigned char *pbuf = number_buffer; - - if (d < 0.0) { - d = (double)d32 - d + 0.00000000000001; - } else { - d = d - (double)d32; - } - - pbuf = &number_buffer[length]; - *pbuf++ = '.'; - length++; - - while (d > 0.0 && precision--) { - d *= 10.0; - unsigned char tmp = (unsigned char)d; - - *pbuf++ = tmp + '0'; - length++; - - d -= (double)tmp; - } - - pbuf = &number_buffer[length - 1]; - - while (*pbuf == '0') { - pbuf--; - length--; - } - - *++pbuf = 0; - } -#endif } /* sprintf failed or buffer overrun occured */ diff --git a/components/cjson/component.mk b/components/cjson/component.mk index 9b2da22a..e404f24c 100644 --- a/components/cjson/component.mk +++ b/components/cjson/component.mk @@ -4,7 +4,3 @@ COMPONENT_ADD_INCLUDEDIRS += cJSON COMPONENT_SRCDIRS := cJSON - -ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL -CFLAGS += -DCJSON_SPRINTF_FLOAT=1 -endif diff --git a/components/newlib/CMakeLists.txt b/components/newlib/CMakeLists.txt index e0669b9c..9a002cba 100644 --- a/components/newlib/CMakeLists.txt +++ b/components/newlib/CMakeLists.txt @@ -2,10 +2,12 @@ set(COMPONENT_SRCDIRS newlib/port) set(COMPONENT_ADD_INCLUDEDIRS newlib/include newlib/port/include) -if(CONFIG_NEWLIB_NANO_FORMAT) +if(CONFIG_NEWLIB_LIBRARY_LEVEL_NANO) set(LIBC c_nano) -else() +elseif(CONFIG_NEWLIB_LIBRARY_LEVEL_NORMAL) set(LIBC c) +elseif(CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO) + set(LIBC c_fnano) endif() set(LIBM m) @@ -14,6 +16,10 @@ set(COMPONENT_PRIV_REQUIRES "vfs" "lwip") # for sys/ioctl.h register_component() +if(CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO) + target_link_libraries(${COMPONENT_LIB} PUBLIC "-u _printf_float" "-u _scanf_float") +endif() + target_compile_definitions(${COMPONENT_LIB} PUBLIC -D_CLOCKS_PER_SEC_=CONFIG_FREERTOS_HZ -D_POSIX_THREADS=1 -D_UNIX98_THREAD_MUTEX_ATTRIBUTES=1 ) diff --git a/components/newlib/Kconfig b/components/newlib/Kconfig index 527130f4..14d11fc5 100644 --- a/components/newlib/Kconfig +++ b/components/newlib/Kconfig @@ -11,7 +11,7 @@ config NEWLIB_ENABLE choice NEWLIB_LIBRARY_LEVEL prompt "newlib level" - default NEWLIB_LIBRARY_LEVEL_NANO + default NEWLIB_LIBRARY_LEVEL_FLOAT_NANO depends on NEWLIB_ENABLE help Choose newlib library level. @@ -26,15 +26,29 @@ config NEWLIB_LIBRARY_LEVEL_NANO bool "nano" help The newlib library which has been compiled with so-called "nano" - formatting option. This option doesn't support 64-bit integer formats and C99 - features, such as positional arguments. + formatting option. This option doesn't support 64-bit integer formats, C99 + features and float formats, such as positional arguments. For more details about "nano" formatting option, please see newlib readme file, search for '--enable-newlib-nano-formatted-io': https://sourceware.org/newlib/README - If you do not need 64-bit integer formatting support or C99 features, select this - option. + If you do not need 64-bit integer formatting support, C99 features and float, + select this option. + +config NEWLIB_LIBRARY_LEVEL_FLOAT_NANO + bool "float nano" + help + The newlib library which has been compiled with so-called "float nano" + formatting option. This option doesn't support 64-bit integer formats and C99 + features, but support float formats, such as positional arguments. + + For more details about "nano" formatting option, please see newlib readme file, + search for '--enable-newlib-nano-formatted-io': + https://sourceware.org/newlib/README + + If you do not need 64-bit integer formatting support and C99 features, but need float formats, + select this option. endchoice diff --git a/components/newlib/component.mk b/components/newlib/component.mk index d3110c83..9a3a4851 100644 --- a/components/newlib/component.mk +++ b/components/newlib/component.mk @@ -16,6 +16,12 @@ ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_NANO LIBC_PATH := $(COMPONENT_PATH)/newlib/lib/libc_nano.a LIBM_PATH := $(COMPONENT_PATH)/newlib/lib/libm.a ADD_NEW_NEWLIB := 1 +else +ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO +LIBC_PATH := $(COMPONENT_PATH)/newlib/lib/libc_fnano.a +LIBM_PATH := $(COMPONENT_PATH)/newlib/lib/libm.a +ADD_NEW_NEWLIB := 1 +endif endif endif @@ -23,6 +29,9 @@ ifeq ($(ADD_NEW_NEWLIB),1) COMPONENT_ADD_INCLUDEDIRS += newlib/include newlib/port/include COMPONENT_SRCDIRS += newlib/port COMPONENT_ADD_LDFLAGS := $(LIBC_PATH) $(LIBM_PATH) -lnewlib +ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO +COMPONENT_ADD_LDFLAGS := $(COMPONENT_ADD_LDFLAGS) -u _printf_float -u _scanf_float +endif COMPONENT_ADD_LINKER_DEPS := $(LIBC_PATH) $(LIBM_PATH) endif diff --git a/components/newlib/newlib/lib/libc_fnano.a b/components/newlib/newlib/lib/libc_fnano.a new file mode 100644 index 00000000..4d972b58 Binary files /dev/null and b/components/newlib/newlib/lib/libc_fnano.a differ