Skip to content

Commit 33f3049

Browse files
authored
Merge pull request #12233 from bulislaw/minimal_printf_default
Enable minimal-printf by default for all builds
2 parents e307695 + 74884bb commit 33f3049

File tree

11 files changed

+74
-37
lines changed

11 files changed

+74
-37
lines changed

TESTS/mbed_drivers/c_strings/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,15 @@ Case cases[] = {
116116
Case("C strings: %u %d integer formatting", test_case_c_string_u_d, greentea_failure_handler),
117117
Case("C strings: %x %E integer formatting", test_case_c_string_x_X, greentea_failure_handler),
118118
#if !defined(__NEWLIB_NANO)
119-
//In build tools, GCC with Newlib-nano linker option "-u _printf_float" is not configured
120-
//to enable printf floating format. So disabling floating format test case.
119+
// Newlib-nano linker option "-u _printf_float" is not set to enable floating point support.
120+
#if (defined(MBED_MINIMAL_PRINTF) && MBED_CONF_PLATFORM_MINIMAL_PRINTF_ENABLE_FLOATING_POINT) || !defined(MBED_MINIMAL_PRINTF)
121121
Case("C strings: %f %f float formatting", test_case_c_string_f_f, greentea_failure_handler),
122+
#endif
122123
#ifndef MBED_MINIMAL_PRINTF
123124
Case("C strings: %e %E float formatting", test_case_c_string_e_E, greentea_failure_handler),
124125
Case("C strings: %g %g float formatting", test_case_c_string_g_g, greentea_failure_handler),
125-
#endif
126-
#endif
126+
#endif // MBED_MINIMAL_PRINTF
127+
#endif // !defined(__NEWLIB_NANO)
127128
};
128129

129130
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)

TESTS/mbed_drivers/reset_reason/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static cmd_status_t handle_command(const char *key, const char *value)
7575
int raw_reason_hex_str_len = snprintf(raw_reason_hex_str,
7676
sizeof raw_reason_hex_str, "%08lx", raw_reason);
7777

78-
if (raw_reason_hex_str_len != (sizeof raw_reason_hex_str) - 1) {
78+
if (raw_reason_hex_str_len < 0) {
7979
TEST_ASSERT_MESSAGE(0, "Failed to compose raw reset reason hex string.");
8080
return CMD_STATUS_ERROR;
8181
}
@@ -134,7 +134,7 @@ void test_reset_reason()
134134
hal_reset_reason_get_capabilities(&rrcap);
135135
char msg_value[11];
136136
int str_len = snprintf(msg_value, sizeof msg_value, "%08lx,%01x", rrcap.reasons, MSG_VALUE_WATCHDOG_STATUS);
137-
if (str_len != (sizeof msg_value) - 1) {
137+
if (str_len < 0) {
138138
printf("Failed to compose a value string to be sent to host.");
139139
GREENTEA_TESTSUITE_RESULT(0);
140140
return;

TESTS/mbed_drivers/watchdog_reset/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms)
9292
{
9393
char msg_value[12];
9494
int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", tcdata->start_index + tcdata->index, delay_ms);
95-
if (str_len != (sizeof msg_value) - 1) {
95+
if (str_len < 0) {
9696
utest_printf("Failed to compose a value string to be sent to host.");
9797
return false;
9898
}

TESTS/mbed_hal/reset_reason/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static cmd_status_t handle_command(const char *key, const char *value)
7575
int raw_reason_hex_str_len = snprintf(raw_reason_hex_str,
7676
sizeof raw_reason_hex_str, "%08lx", raw_reason);
7777

78-
if (raw_reason_hex_str_len != (sizeof raw_reason_hex_str) - 1) {
78+
if (raw_reason_hex_str_len < 0) {
7979
TEST_ASSERT_MESSAGE(0, "Failed to compose raw reset reason hex string.");
8080
return CMD_STATUS_ERROR;
8181
}
@@ -129,7 +129,7 @@ void test_reset_reason()
129129
hal_reset_reason_get_capabilities(&rrcap);
130130
char msg_value[11];
131131
int str_len = snprintf(msg_value, sizeof msg_value, "%08lx,%01x", rrcap.reasons, MSG_VALUE_WATCHDOG_STATUS);
132-
if (str_len != (sizeof msg_value) - 1) {
132+
if (str_len < 0) {
133133
printf("Failed to compose a value string to be sent to host.");
134134
GREENTEA_TESTSUITE_RESULT(0);
135135
return;

TESTS/mbed_hal/watchdog_reset/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms)
9090
{
9191
char msg_value[12];
9292
int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", tcdata->start_index + tcdata->index, delay_ms);
93-
if (str_len != (sizeof msg_value) - 1) {
93+
if (str_len < 0) {
9494
utest_printf("Failed to compose a value string to be sent to host.");
9595
return false;
9696
}

TESTS/mbed_hal/watchdog_timing/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms)
5959
{
6060
char msg_value[12];
6161
int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", tcdata->start_index + tcdata->index, delay_ms);
62-
if (str_len != (sizeof msg_value) - 1) {
62+
if (str_len < 0) {
6363
utest_printf("Failed to compose a value string to be sent to host.");
6464
return false;
6565
}
@@ -110,7 +110,7 @@ void test_timing()
110110

111111
int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", current_case.start_index + current_case.index,
112112
(uint32_t) current_ts);
113-
if (str_len != (sizeof msg_value) - 1) {
113+
if (str_len < 0) {
114114
utest_printf("Failed to compose a value string to be sent to host.");
115115
return;
116116
}

TESTS/mbed_timing_fpga_ci_test_shield/watchdog/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms)
119119
{
120120
char msg_value[12];
121121
int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", tcdata->start_index + tcdata->index, delay_ms);
122-
if (str_len != (sizeof msg_value) - 1) {
122+
if (str_len < 0) {
123123
utest_printf("Failed to compose a value string to be sent to host.");
124124
return false;
125125
}

platform/mbed_lib.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@
140140
"value": true
141141
},
142142
"minimal-printf-enable-64-bit": {
143-
"help": "Enable printing 64 bit integers when using mprintf profile",
143+
"help": "Enable printing 64 bit integers when using minimal printf library",
144144
"value": true
145145
},
146146
"minimal-printf-enable-floating-point": {
147-
"help": "Enable floating point printing when using mprintf profile",
147+
"help": "Enable floating point printing when using minimal printf library",
148148
"value": false
149149
},
150150
"minimal-printf-set-floating-point-max-decimals": {
151-
"help": "Maximum number of decimals to be printed",
151+
"help": "Maximum number of decimals to be printed when using minimal printf library",
152152
"value": 6
153153
}
154154
},

platform/source/minimal-printf/README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Minimal printf and snprintf
22

33

4-
Library supports both printf and snprintf in 1252 bytes of flash.
4+
Library supports both printf and snprintf in around 1300 bytes of flash.
55

66
Prints directly to stdio/UART without using malloc. All flags and precision modifiers are ignored.
77
There is no error handling if a writing error occurs.
@@ -20,6 +20,10 @@ Supports:
2020
* %s: string.
2121
* %p: pointer (e.g. 0x00123456).
2222

23+
Note that support for:
24+
* 64b modifiers is only present when `minimal-printf-enable-64-bit` config is set to `true` (default).
25+
* Floating point parameters is only present when `minimal-printf-enable-floating-point` config is set to `true` (disabled by default).
26+
2327
Unrecognized format specifiers are treated as ordinary characters.
2428

2529
Floating point limitations:
@@ -28,8 +32,7 @@ Floating point limitations:
2832

2933
## Usage
3034

31-
32-
To replace the standard implementation of the printf functions with the ones in this library:
35+
As of Mbed OS 6.0 this is enabled by default. To replace the standard implementation of the printf functions with the ones in this library for older versions of Mbed:
3336

3437
Modify your application configuration file to override the parameter `target.printf_lib` with the value `minimal-printf` as shown below:
3538

@@ -43,6 +46,17 @@ Modify your application configuration file to override the parameter `target.pri
4346
}
4447
```
4548

49+
If your application requires more advanced functionality, you'll need to revert to using standard version of printf/snprintf. Please note that it will result in significant ROM usage increase. In case you are using minimal version of standard C library advanced functionality may not be present.
50+
51+
Modify your application configuration in `mbed_app.json` file to override the parameter `target.printf_lib` with the value `std` as shown below:
52+
53+
```json
54+
"target_overrides": {
55+
"*": {
56+
"target.printf_lib": "std"
57+
}
58+
}
59+
```
4660

4761
## Configuration
4862

@@ -54,27 +68,24 @@ Minimal printf is configured by the following parameters defined in `platform/mb
5468
"name": "platform",
5569
"config": {
5670
"minimal-printf-enable-64-bit": {
57-
"help": "Enable printing 64 bit integers when using minimal-printf profile",
71+
"help": "Enable printing 64 bit integers when using minimal printf library",
5872
"value": true
5973
},
6074
"minimal-printf-enable-floating-point": {
61-
"help": "Enable floating point printing when using minimal-printf profile",
75+
"help": "Enable floating point printing when using minimal printf library",
6276
"value": false
6377
},
6478
"minimal-printf-set-floating-point-max-decimals": {
65-
"help": "Maximum number of decimals to be printed",
79+
"help": "Maximum number of decimals to be printed when using minimal printf library",
6680
"value": 6
6781
}
68-
}
82+
}
6983
}
7084
```
7185

72-
By default, 64 bit integers support is enabled.
73-
74-
If your target does not require some options then you can override the default configuration in your application `mbed_app.json` and achieve further memory optimisation (see next section for size comparison numbers).
75-
76-
In mbed_app.json:
86+
By default, 64 bit integers support is enabled, but floating point support is disabled to increase memory savings.
7787

88+
If your application needs to override the default configuration add following section to your `mbed_app.json`:
7889
```json
7990
"target_overrides": {
8091
"*": {

platform/source/minimal-printf/mbed_printf_implementation.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
2+
* Copyright (c) 2017-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -122,17 +122,22 @@ static void mbed_minimal_putchar(char *buffer, size_t length, int *result, char
122122
{
123123
/* only continue if 'result' doesn't overflow */
124124
if ((*result >= 0) && (*result <= INT_MAX - 1)) {
125-
/* write data only if there's enough space */
126-
if ((size_t)*result < length) {
127-
if (buffer) {
125+
if (buffer) {
126+
/* write data only if there's enough space */
127+
if ((size_t)*result < length) {
128128
buffer[*result] = data;
129+
}
130+
131+
/* increment 'result' even if data was not written. This ensures that
132+
'mbed_minimal_formatted_string' returns the correct value. */
133+
*result += 1;
134+
} else {
135+
if (fputc(data, stream) == EOF) {
136+
*result = EOF;
129137
} else {
130-
fputc(data, stream);
138+
*result += 1;
131139
}
132140
}
133-
/* increment 'result' even if data was not written. This ensures that
134-
'mbed_minimal_formatted_string' returns the correct value. */
135-
*result += 1;
136141
}
137142
}
138143

@@ -503,6 +508,16 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
503508
/* use 64 bit storage type for readout */
504509
value = va_arg(arguments, MBED_SIGNED_STORAGE);
505510
} else
511+
#else
512+
/* If 64 bit is not enabled, print %ll[di] rather than truncated value */
513+
if (length_modifier == LENGTH_LL) {
514+
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
515+
if (next == '%') {
516+
// Continue printing loop after `%`
517+
index = next_index;
518+
}
519+
continue;
520+
}
506521
#endif
507522
{
508523
/* use native storage type (which can be 32 or 64 bit) */
@@ -552,6 +567,16 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
552567
/* use 64 bit storage type for readout */
553568
value = va_arg(arguments, MBED_UNSIGNED_STORAGE);
554569
} else
570+
#else
571+
/* If 64 bit is not enabled, print %ll[uxX] rather than truncated value */
572+
if (length_modifier == LENGTH_LL) {
573+
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
574+
if (next == '%') {
575+
// Continue printing loop after `%`
576+
index = next_index;
577+
}
578+
continue;
579+
}
555580
#endif
556581
{
557582
/* use native storage type (which can be 32 or 64 bit) */

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"c_lib": "std",
2020
"bootloader_supported": false,
2121
"static_memory_defines": true,
22-
"printf_lib": "std",
22+
"printf_lib": "minimal-printf",
2323
"supported_c_libs": {
2424
"arm": [
2525
"std"

0 commit comments

Comments
 (0)