When developing applications for resource-constrained microcontrollers like the Cortex-M1, choosing the right software libraries is critical. Standard libraries like the C standard library provide rich functionality but have high memory and CPU overhead. Microlibs are optimized libraries designed specifically for microcontrollers. This article compares microlibs and standard libraries for Cortex-M1 development.
The Constraints of Cortex-M1 Microcontrollers
The Cortex-M1 processor is designed for embedded applications requiring minimal silicon area and low power consumption. It has a 32-bit ARMv6-M architecture with a single-issue 3-stage pipeline. Key specifications include:
- 32KB to 64KB of flash memory
- 4KB to 16KB of SRAM
- 2.12 CoreMark/MHz performance
- 0.9 DMIPS/MHz performance
- Up to 50MHz operating frequency
This leads to very tight constraints on code size, RAM usage, and CPU cycles. Applications must be highly optimized for the Cortex-M1. Standard libraries designed for desktop systems are often unsuitable.
The C Standard Library
The C standard library provides functions for string handling, file I/O, memory allocation, and more. It is part of the C standard and included with every C compiler. The library contains over 150 functions to support C programs. Example functions include:
- printf() – formatted output to stdout
- malloc() – dynamic memory allocation
- memcpy() – copy memory block
- strlen() – get string length
The C standard library provides rich functionality out of the box. But it has high overhead costs:
- Large code size – benchmarks show ~30KB compiled size for full stdlib on Cortex-M
- High RAM usage – stdlib depends on heap allocation which consumes RAM
- Poor performance – not optimized for microcontrollers
This overhead is acceptable on desktops and servers, but too heavy for most microcontrollers. The full stdlib is impractical for use on Cortex-M1 chips.
Microlibs, or embedded libraries, are designed for memory and performance constrained microcontroller applications. They provide C standard library functionality optimized for embedded use. Example microlibs include:
- Arm Mbed – general purpose embeddded library
- Microlib – STMicroelectronics Cortex-M microlib
- NanoLib – TI microlib for MSP430 MCUs
- Avr-Libc – Atmel AVR microlib
Microlibs optimize for code size, RAM usage, and performance. Key techniques include:
- Static memory allocation – avoids heap usage
- Inlining – reduces call overhead
- Loop unrolling – improves instruction pipelining
- Assembly optimizations – uses asm for critical paths
By sacrificing functionality and flexibility, microlibs can match the tight resource constraints of microcontrollers. The tradeoffs versus standard libraries include:
- Smaller code size – 1KB to 5KB for microlib vs. 30KB+ for stdlib
- Lower RAM usage – static allocation avoids heap fragmentation
- Higher performance – optimized for microcontroller architecture
- Less functionality – only essential subset of standard functions
- Limited flexibility – configuration fixed at compile time
For the Cortex-M1, the benefits clearly outweigh the costs. Microlibs enable applications that are not possible with the standard library.
There are many high-quality C microlibs available for the Cortex-M series. Here are some key differences between popular options:
- Provided by STMicroelectronics for STM32 devices
- Minimal size – 1KB to 2KB
- Limited functionality – barebones subset of stdlib
- STM32 compiler integration
STM’s microlib is ultra compact to fit STM32 microcontrollers. It sacrifices functionality for a tiny footprint. Useful for simpler applications on smaller MCUs.
- General purpose Cortex-M library
- Moderate size – 4KB to 8KB
- Expanded feature set – filesystem, networking, RTOS
- Nice C++ support and modularity
The Arm Mbed library has more functionality for complex applications. It includes RTOS and stack features missing in smaller microlibs. The larger size requires more resources.
- Barebones microlib for Cortex-M
- Tiny footprint – 1KB to 3KB
- Minimal stdlib functions supported
- Pure C implementation
Microlib is a small standards-compliant C library for Cortex-M microcontrollers. It provides the absolutely essential subset of the C standard library in a compact form. Well suited for simple applications on smaller MCUs like the Cortex-M1.
The C standard library provides full-featured out-of-the-box functionality for C programs. However, its large resource footprint makes it impractical for most microcontrollers. Microlibs are optimized embedded libraries designed specifically for memory and CPU constrained microcontroller applications.
For Cortex-M1 development, microlibs are clearly the best choice to manage the tight code size, RAM, and performance constraints. Arm Mbed offers more advanced functionality at the cost of more resources. STM’s microlib and Microlib provide ultra compact C standard libraries for simpler needs. In summary, microlibs enable smaller, faster, and more efficient Cortex-M1 applications.