Skip to content

Commit

Permalink
Fix corrupted %f printf because of disabled heap
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinjaeger committed Nov 18, 2020
1 parent e5e27c1 commit 583924a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ void setup()
static struct k_timer timestamp_timer;
k_timer_init(&timestamp_timer, timestamp_inc, NULL);
k_timer_start(&timestamp_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
4 changes: 2 additions & 2 deletions zephyr/prj.conf
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 583924a

Please sign in to comment.