Report
4 years, 9 months ago.

Serial object requires 19.1KB Flash usage

Hello All, I recently compiled a simple program that instantiates a serial object and calls printf a couple of times. the code size for it was a huge 19.1KB using the online compiler. That's a lot of program memory usage for sending something over the UART

I then built the mbed SDK and compiled the same program using it with ARM-GCC. I got about 33KB when enabling float support and scanf or about 17KB when disabling them.

Does this sound right? Is there anything that I can do to bring down the program size of a simple hello world program?

I'm using the Arch Pro platform and the code is shown below:

hellombed.cpp

#include "mbed.h"              
 
Serial pc(USBTX, USBRX); // tx, rx
 
int main() {
    pc.printf("Hello World!\n");
    while(1){
        pc.printf("Heartbeat!\n");
        wait(1);
        }
}

/media/uploads/halherta/hellombed.png

Question relating to:

Comment on this question

2 Answers

4 years, 9 months ago.

As Erik has explained, your problem lies with the use of streams that are automatically imported when using a serial port object. Attached to this is the 'printf()' function as well as formatting and number conversion (even floating point support which is as big package). To avoid it, you might be able to use the 'RawSerial' object instead. This comes with own implementations of 'putc()' and 'puts()'. It is also safe to use in interrupts.

It's not that simple. In my testing with a modified mbed-src library for stm32f030f4 (where program size is very important), above example program size is 14316B. But the same program without line "Serial pc(USBTX, USBRX); tx, rx" and use "printf" instead "pc.printf" has a size 9184B. The size difference is more than 5kB (!!!) but both programs work the same way (the same result in more complex use printf e.g. number conversion).

posted by Nothing Special 23 Nov 2014
4 years, 9 months ago.

The easiest way is not to worry about it, especially on something like the Arch Pro. Smaller targets use a different ARM lib in the online compiler (which also should be avaialble using Keil), which uses less space. The reason in the end is that apparantly compilers still can't understand which functions are actually used in the program, and then the printf, scanf, etc standard functions take alot of space.

However this is a one-time thing, it isn't that a second Serial object will again take that much space. Which is why I wouldn't worry about it.

Thanks Guys! I guess I will not worry about it for now. In the future I'll write my own simple printf c function for each platform that I'm using. I guess when you have 256KB of flash one can afford to take a bit of a hit on the program memory.

I followed 'Nothing Specials' advice and used 'printf' instead of 'pc.printf'. In the online compiler I'm still getting 17.8KB but with the mbed SDK (and a custom makefile w/o float support) I'm getting around 10.8KB.

posted by Ziryab 23 Nov 2014