RETRO: Size of firmware

I’ve noticed that both the RETRO and LPC1768 mbed platforms consume about 10kb of Flash just to blink an LED. The same program on a FRDM-KL25Z mbed platform consumes just 1.8 kb.

Could someone explain why? The extra 8kb of Flash freed would be very useful.

That is probably the size of the mbed library. I guess you can drop it and do all the required initialization plumbing yourself.

1 Like

@ Experimenter - As @ Architect said, that is the base size of the mbed libraries with a reference to the GPIO libraries. As you reference more libraries, PWM, Timers etc. the size grows quite quickly.

The cost of having nice abstractions to work with.

1 Like

Be aware that the same program was used.
No PWM, Timers, etc. just DigitalOut and wait()l.

#include “mbed.h”

int main() {
DigitalOut led1(LED_RED);
while (true) {
led1 = !led1;
wait(.5);
}
}

Nothing was done to the FRDM-KL25Z to make it only 1.8k.

@ Experimenter - You would need to take a look at how the libraries are put together. I have not looked at much outside of the LPC11u24 libraries so I could not speak to the specifics of the FRDM-KL25Z.

Some possible guesses would be that there is either a difference in the target HAL implementation or maybe the compiler options on the backend vary per platform in such away that the optimizations differ, the online compiler is a huge black box to me…

Code size to blink an LED on mbuino:
mbed library - 10,300 bytes
mbed-src library - 10,124 bytes
mbed-src with a 1 character change - 2,868 bytes.

Still more than 1.8k but a lot better than it was.

What was the change?

In the mbed-src library find the devices.h file for the mbuino (targets/TARGET_NXT/TARGET_LPC11UXX/TARGET_OC_MBUINO/devices.h) and on line 55 change #define DEVICE_STDIO_MESSAGES from 1 to 0

As with all NXP parts the mbuino defaults to support for stdout, stdin and stderr on UART0, disable this and the stdio library is no longer linked in and the code size drops.

However do keep in mind that if you use printf or the related functions anywhere in your code then this saving will go away again.

4 Likes

P.S.
setting DEVICE_ERROR_PATTERN to 0 seems to save about 400 bytes more.

Having a quick play with the other options in that file most seem to be required for the code to build and the ones that are optional don’t seem to have any impact final code size.

3 Likes

@ AndyA - Great tips. Thanks for sharing. Will be handy for the next RETRO contest. :wink:

:dance:
Thanks for the fantastic work!

“Every byte is sacred, every byte is great, if a byte gets wasted you may get irate.”

1 Like

Thanks for the tips. I found that I had to convert the mbed gear build not quite a library yet into a library in order to access any source files. I right clicked on the mbed gear icon then chose Convert To Library. After it finished I was able to find the TARGET_OC_MBUINO\TARGET_NXP\TARGET_LPC11UXX\TARGET_OC_MBUINO\device.h file. After that was really easy. Just set the components you don’t want from 1 to 0 and then rebuild.

However something to note for new developers just getting started with Mbed and the MBuino platform who are used to working with Harvard architecture big embedded contollers and AVR micros (and not M0 ARMs). Arrays that are declared as const get stored in Flash. Without the const they are stored in RAM. A Duh moment for sure however… Programmers working with AVR might not think about it because they have to specifically declare the array to be located in Flash (using the PROGMEM macro) and working with a Harvard Architecture embedded controller that runs programs out of RAM completely don’t think about storing variables in Flash. I just assumed since there was not any PROGMEM type thing for the ARM that it would store the array in Flash automatically. It does … but then moves it to RAM though upon startup.

1 Like

@ cfavreau

Your procedure didn’t work for me. I noticed no change in code size for the rebuilds.

For me, I had to remove the mbed library from my project and import the official mbed-src library from the Import Library…, From Library Wizard… context menu.

Then the mods worked a treat.

The path to device.h was slightly different being …/hal/TARGET_NXP/TARGET_LPC11UXX/TARGET_OC_MBUINO/

actually …/targets/hal/TARGET_NXP/TARGET_LPC11UXX/TARGET_OC_MBUINO/