From 583924a9b99e727ae266c62ac1c993f693952711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Mon, 16 Nov 2020 16:52:14 +0100 Subject: [PATCH] Fix corrupted %f printf because of disabled heap newlib requires malloc if printf is used with %f. Since PR 28486, the heap space in RAM is only reserved if explicitly used by the application (independent of the value of CONFIG_HEAP_MEM_POOL_SIZE) in order to free some stack memory. As printf calls malloc internally, this commit forces Zephyr to build with heap support enabled. --- src/setup.cpp | 7 +++++++ zephyr/prj.conf | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/setup.cpp b/src/setup.cpp index 7792394..9406d59 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -96,6 +96,13 @@ void setup() static struct k_timer timestamp_timer; k_timer_init(×tamp_timer, timestamp_inc, NULL); k_timer_start(×tamp_timer, K_MSEC(1000), K_MSEC(1000)); + + /* + * printf from newlib-nano requires malloc, but Zephyr garbage-collects heap management if it + * is not used anywhere in the code. Below dummy calls force Zephyr to build with heap support. + */ + void *temp = k_malloc(4); + k_free(temp); } #endif diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 3e1e69b..573637a 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -1,7 +1,7 @@ # Main application configuration (overrides board-specific settings) -# default is 1024 -CONFIG_HEAP_MEM_POOL_SIZE=1024 +# only very small heap necessary for malloc in printf statements with %f +CONFIG_HEAP_MEM_POOL_SIZE=256 CONFIG_CPLUSPLUS=y