Introduction
Programming in the relatively complicated 32-bit Cortex-M devices is different from the 8-bit Atmege AVR microcontrollers. Because STM32 deivces have more registers, those registers are 32-bit long instead of 8-bit, and it has more functions, such as DMA, programming by directly accessing registers is not recommended. Moreover, it is difficult to run the code in different microcontrollers (bad portablity). Therefore, programming STM32 and other cortex-m devices are usually via well-defined libraries.
C provides a good trade-off between coding efficiency, protability, execution effciency and code size.
Moreover, Cortex-M3 support different operating modes, priviledge/unprivileged instruction, and two stack pointer, which means it has some hardware support for a operating system. Then programming style can treat this device as a simple MCU that does not have those features, or play with those features and perphaps with a RTOS. In this note, the device is programmed as a simple MCU that only uses the main stack.
The libraries used to program a STM32 and other cortex-M device are organized as a layered architecture.
Pros and Cons about the layered architecture
- + hide complexity
- + modularity (good for portable)
- - calling overhead: have to go through each layer.
Layers in programming stm32
- mbed-os (provided by mbed (ARM), a suite of API, such as Ethernet, GPIO, ...)
- mbed HAL (collaborated by mbed (ARM) and vendor, defines a hardware abstraction layer to hide the difference of devices' configuration)
- STM32 Core + HAL lib (provided by ST, defines the peripherals registers (e.g. GPIO, flash), data structure
- CMSIS (provided by ARM, defines the registers of the core, interrupts, data structure, compiling convention... e.g. core_cm3.h
As a developer, we can start from any layer.