The ARM Memory map


  • AHB-Advanced high performance bus.
  • APB-Advanced peripheral bus.
  • AMBA-Advanced micro-controller bus architecture.

The memory map is a way to access peripherals and other micro-controller features via a memory write or read. The memory map on 32 bit ARM micro-controllers is 4GB (2^32 unsigned) and contiguous.

The reason it is this large is to allow room for expansion and not limit the micro-controller vendors. Examples include adding extra peripherals to a chip revision. The above definitions define buses on the ARM micro-controllers. If the APB is set to 0.5GB, the vendor only has to worry about the APB space but can add numerous peripherals to this bus.

The image below shows the memory map obtained from the data-sheet of the STM32F446RE,note the memory map may be different for different micro-controllers:


The 4GB memory map is split into eight 512MB sections. To link the memory map to the physcal chip we need the functional block diagram. The image below is part of the block diagram, which shows how the buses are connected to the CPU core:


Top section of block diagram


Bottom section of block diagram

The bus matrix is the core of the whole system that controls which master(Cortex core,DMA,USB OTG) controls the slaves(AHB,flash and SRAM).


By definition from the datasheet:The 32-bit multi-AHB bus matrix interconnects all the masters (CPU, DMAs, USB HS) and the slaves Flash memory, RAM, QuadSPI, FMC, AHB and APB peripherals and ensures a seamless and efficient operation even when several high-speed peripherals work simultaneously.

We shall focus on the GPIO as an example of how the memory map works.


We would like to write to PB13 above maybe to turn on an led (Port B pin 13/ GPIO B pin 13). We thus look for GPIOB in the memory map


We can thus see GPIOB is addressable from 0x40020400-0x400207FF . That is great but how do we choose pin 13?

We thus turn from the datasheet to the technical reference manual(TRM).


As from the above,each GPIO port has 10, 32 bit registers that just determine how the GPIO port works. Remember that this is on the STM32F446RE. It may be different on a different microcontroller even from the same vendor.

Now we turn to the GPIO register map:


We stop temporarily here and recall,

  • AHB1 location  starts at 0x4002 0000. We take this as our base  location(AHB1_BASE) as it is referenced from 0x0000 0000. This is absolute referencing.
  • GPIOB location starts at AHB1_BASE + 0x400, we shall call this GPIOB_BASE. This is relative referencing.

We can now reference GPIOB registers as offsets from GPIOB. Notice the Image above from the reference manual lists the registers as offsets so we can define GPIOB_MODER as GPIOB+0x0 as it is at offset 0.


Notice that for different GPIOx, the offsets are the same from the base whether it is GPIOA or GPIOB. We can thus define a GPIO as a structure and reference it via a pointer. This is how it is defined in the STM32CUBEF4 drivers:

typedef struct
__IO uint32_t GPIOx_MODER;
__IO uint32_t GPIOx_OTYPER;
__IO uint32_t GPIOx_OSPEEDER;
__IO uint32_t GPIOx_IDR;
__IO uint32_t GPIOx_ODR;
__IO uint32_t GPIOx_BSRR;
__IO uint32_t GPIOx_LCKR;
__IO uint32_t GPIOx_AFRL;
} GPIO_TypeDef;



The __IO modifier is expanded as:

#ifdef __cplusplus
#define __I volatile
/*!<defines ’read only’ permissions */
#define __I volatile const /*!<defines ’read only’ permissions */
#define __O volatile /*!<defines ’write only’ permissions */
#define __IO volatile /*!<defines ’read / write’ permissions */

Due to the nature of structures in ANSI C, the structure elements are placed in contiguous memory locations in memory. The structure elements must have the same type. Thus the structure is a perfect data structure for this application.

I usually use eclipse to program arm microcontrollers. A good feature is that it allows one to transparently see macro expansions.


We thus check GPIOB macro expansion:


Peripheral base is defined as:

#define PERIPH_BASE ((uint32_t)0x40000000)

The peripheral base can be seen in the memory map below. All peripherals are on the right block.


AHB1 is offset at 0x20000 from the peripheral base

#define AHB1PERIPH_BASE (PERIPH_BASE + 0x20000)

Remembering that GPIOB is offset at 0x400 from AHB1PERIPH_BASE


#define GPIOB_BASE (AHB1PERIPH_BASE + 0x0400)



Finally to link the struct defining the GPIOx egisters and the definitions above:

#define GPIOB ((GPIO_TypeDef *) GPIOB_BASE)



GPIO B thus is macro expanded to:

  • GPIOB ((GPIO_TypeDef *) (AHB1PERIPH_BASE + 0x0400))
  • GPIOB ((GPIO_TypeDef *) ((PERIPH_BASE + 0x20000) + 0x0400))
  • GPIOB ((GPIO_TypeDef *) ((((uint32_t)0x40000000) + 0x20000) + 0x0400))
  • GPIOB ((GPIO_TypeDef *) ((0x40000000 + 0x20000) + 0x0400))


The U suffix after the memory locations such as 0x4000000U are to force the compiler to regard the literal as an unsigned integer. This makes the code more portable across compilers as the drivers are to be used with the customers preferred compiler which could be Keil from arm or the open source bare metal arm toolchain gcc-arm-none-eabi.

Pin 13 is defined as:

#define pin13 ((uint16_t)0x4000U)


To toggle a pin:

void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
/* Check the parameters */


Understanding the convoluted world of ARM cortex-M documentation.


ARM processors are usually powerful and most likely you are reading this on a device with an ARM cortex application processor(Your phone).

Usually when one starts out programming ARM micro controllers , it gets very intimidating to start programming so I will give a few hints to the rules of thumb I use to wade through the multitude of documentation available. This applies to almost all Cortex M micro controllers across various vendors.

  1. Look through ARM micro controller vendor’s portfolio of chips such as those from NXP, STM32 etc . Base your decision on the peripherals the micro-controller offers versus its price.Peripherals include HDMI controllers, USB controllers, Infared controllers(IRDA) depending on the application.
  2. Look though the micro controller data sheet second. It is a relatively short manual usually of about 100-200 pages that talks of the physical properties of the chip including the peripherals. This should be enough to make you decide what chip you want.
  3. Next document to look at from the micro controller vendor is the errata which lists uncorrected errors in the data sheet revisions and also in the technical reference manual(TRM) below. It may also list changes on the micro controller itself including peripheral limitations. Reading this is a must for mass product development.
  4. Look for drivers, (they may be in C) that expose a common API by which to communicate with and control the hardware using the firmware you will write. Examples are the current STM32cube and the older standard peripheral library(spl) by ST Micro(Works only on STM devices) or the open source libopencm3 (Works across numerous ARM vendors)
  5. Setup tool chain, IDE and link with the drivers.
  6. Set up debug interface via GDB and openocd.
  7. After your mind is made up and you have set up a tool chain on your computer that can program the chosen chip, the next reference should be the technical reference manual that describes the register layout of each peripheral and the different modes allowed(~1000pages). Use this only for reference to check peripheral descriptions, operating modes or if you need a way to access a register using assembly if a C function isn’t available or it isn’t prudent to use C.
  8. Use the driver library documentation and reference if the documentation is in line with the technical reference manual.
  9. Program away!

Here is example documentation for the stm32f446RE:

Just  remember the tool chain and IDE may be the most painful to setup but you only have to do it once!



Guide to choosing the right ingredients for an electronic product/device.

When making business decisions about what to have in a product, the decision factor is not binary whereby the decision is based on expensive/affordable among binary options but is a mix of various factors . The factors also depend on geographical location among other factors.

They include:

  • Access to manufacturers and vendors

    An electronic product hinges heavily on the ability to source the cheapest parts that deliver the expected performance for the expected lifetime of the product thus one needs to source and compare from various vendors. The Internet has been of great help to this.

  • Ability to access design and prototyping tools

    Design tools such as Electronic computer aided design(ECAD) and mechanical computer aided design(MCAD)  tools are the link between the designer and the vendors and manufacturers. Such tools output industry standard file outputs that can be understood by all parties allowing one to work with a manufacturer and send design files using the Internet. Point to note is that some of these tools are free while others are non free.

  • Tax complexity and nature of economy

    The tax complexity affects the selling price of goods and this determines the minimum price point you would theoretically be able to sell in order to break even. The problem is further aggravated in developing countries whereby the purchasing power of a people is low and you thus have an upper limit at which one can sell  each product. The nature of the economy(Manufacturing or service) determines the legislations that are passed that affect businesses as a nation wide legislation that positively affects service sectors doesn’t necessarily affect manufacturing sector positively. complexity-fs8

Fig: Simple illustration of cost-benefit variation vs products sold.

  • Device mobility

    Device mobility is very important especially in developing countries where the mobile sector is very vibrant. The smaller and more portable the device the more favorable is its use. According to a statistical report by Gallup group[1] “Nearly two-thirds (65%) of households in 23 countries in sub-Saharan Africa had at least one mobile phone in 2013, with median growth of 27% since 2008 and median annual growth of 5%.” To put it succinctly in my words- “In a mobile world, go mobile or die!”

  • Ready access to the needed, affordable skill set

    The ability to access local skills that one can use when developing a local/ international product. Such knowledge hinges heavily on the local quality of training in the country and the incentives given to the labor market to invest in their skills for this market.

  • Marketing and shifts in marketing practices

    Go mainstream marketing or go on-line marketing?That is a common question asked. It honestly depends on the market one wishes to serve and the ability to receive statistics that one can directly relate to internal business performance. On-line marketing offers this advantages apart from the normal “Daily reach number” quoted by mainstream marketing avenues.

  • References

  1. Africa Continues Going Mobile by Bob Tortora. Gallup group. May 1, 2014.

Getting started with STM32 ARM microcontrollers


  1. Those moving from the arduino and would like to experiment with other micro controllers.
  2. Hobbyists new to the field

32 bit micro controllers are different beast from the lowly 8 bit micro controllers. The 8 bit micro controllers include the PIC 8 bit series, AVR series from microchip, the notable 8051 among others. A very notable use of the 8 bit microcontrollers among hobbyists is the arduino.

To have a feel of the arduino capabilities without saying much, here are a few videos of projects completed by other people using arduino.


A good comparison and summary of the 8 bit vs 32 bit battle, which to choose? can be found in this hackaday article. I would suggest reading that article first. Now comes the big question:

If the arduino works so well then why move on to a more complex platform?

  • Control-“With great power comes great responsibility”

There is a direct linear relationship between device complexity and degree of control.

This means that complexity increases with the number of items one has to handle. The STM32 among other 32 bit arm platforms allows you to have full control of your system which may be more beneficial in a professional setting as opposed to  a hobby one though it increases complexity. In the STM32, the most notable of this is setting the clocks on the device.

Prior to using a device peripheral, say an on chip LCD TFT controller, one has to set up its clock (Normally given in Megahertz) prior to using the device among other settings. This helps in controlling the power consumption of the device as the power consumed . A good reference of the power dissipation in CMOS circuits can be found in this Texas Instruments article


Fig: Current draw from the power rail against frequency for AHC (Advanced high speed CMOS). From Texas instruments.

Here is a sample image of the clock distribution settings one can control in the STM32.


  • Abundance of peripherals and capability

Peripherals are the items that give the microcontroller its ability to control the outside world among other functions. This is the first thing to consider when choosing a microcontroller for system design.I shall make a direct comparison of the peripherals available on the STM32 and arduino. In this case I shall use the STM32 Nucleo F446RE vs Arduino Mega.

STM32 nucleo


  • Core: ARM® 32-bit Cortex®-M4 CPU with FPU, Adaptive real-time accelerator (ART Accelerator™) allowing 0-wait state execution from Fl ash memory, frequency up to 180 MHz, MPU, 225 DMIPS/1.25 DMIPS/MHz (Dhrystone 2.1), and DSP instructions
  • Memories
    • 512 kB of Flash memory
    • 128 KB of SRAM
    • Flexible external memory controller with up to 16-bit data bus: SRAM,PSRAM,SDRAM/LPSDR SDRAM, Flash NOR/NAND memories
    • Dual mode Quad SPI interface
  • LCD parallel interface, 8080/6800 modes
  • Clock, reset and supply management
    • 1.7 V to 3.6 V application supply and I/Os
    • POR, PDR, PVD and BOR
    • 4-to-26 MHz crystal oscillator
    • Internal 16 MHz factory-trimmed RC (1% accuracy)
    • 32 kHz oscillator for RTC with calibration
    • Internal 32 kHz RC with calibration
  • Low power
    • Sleep, Stop and Standby modes
    • VBAT supply for RTC, 20×32 bit backup registers + optional 4 KB backup SRAM
  • 3×12-bit, 2.4 MSPS ADC: up to 24 channels and 7.2 MSPS in triple interleaved mode
  • 2×12-bit D/A converters
  • General-purpose DMA: 16-stream DMA controller with FIFOs and burst support
  • Up to 17 timers: 2x watchdog, 1x SysTick timer and up to twelve 16-bit and two 32-bit timers up to 180 MHz, each with up to 4 IC/OC/PWM or pulse counter
  • Debug mode
    • SWD & JTAG interfaces
    • Cortex®-M4 Trace Macrocell™
  • Up to 114 I/O ports with interrupt capability
    • Up to 111 fast I/Os up to 90 MHz
    • Up to 112 5 V-tolerant I/Os
  • Up to 20 communication interfaces
    • SPDIF-Rx
    • Up to 4 × I2 C interfaces (SMBus/PMBus)
    • Up to 4 USARTs/2 UARTs (11.25 Mbit/s, ISO7816 interface, LIN, IrDA, modem control)
    • Up to 4 SPIs (45 Mbits/s), 3 with muxed I2 S for audio class accuracy via internal audio PLL or external clock
    • 2 x SAI (serial audio interface)
    • 2 × CAN (2.0B Active)
    • SDIO interface
    • Consumer electronics control (CEC) I/F
  • Advanced connectivity
    • USB 2.0 full-speed device/host/OTG controller with on-chip PHY
    • USB 2.0 high-speed/full-speed device/host/OTG controller with dedicated DMA, on-chip full-speed PHY and ULPI
    • Dedicated USB power rail enabling on-chip PHYs operation throughout the entire MCU power supply range
  • 8- to 14-bit parallel camera interface up to 54 Mbytes/s
  • CRC calculation unit
  • RTC: subsecond accuracy, hardware calendar
  • 96-bit unique ID

Arduino Mega-R3




8-Bit Microcontroller
•Advanced RISC Architecture
– 135 Powerful Instructions – Most Single Clock Cycle Execution
– 32 × 8 General Purpose Working Registers
– Fully Static Operation
– Up to 16 MIPS Throughput at 16MHz
– On-Chip 2-cycle Multiplier
•High Endurance Non-volatile Memory Segments
– 64K/128K/256KBytes of In-System Self-Programmable Flash
– 4Kbytes EEPROM
– 8Kbytes Internal SRAM
– Write/Erase Cycles:10,000 Flash/100,000 EEPROM
– Optional Boot Code Section with Independent Lock Bits
• In-System Programming by On-chip Boot Program
• True Read-While-Write Operation
– Programming Lock for Software Security
• Endurance: Up to 64Kbytes Optional External Memory Space
JTAG (IEEE®std. 1149.1 compliant) Interface
– Boundary-scan Capabilities According to the JTAG Standard
– Extensive On-chip Debug Support
– Programming of Flash, EE
PROM, Fuses, and Lock Bits through the JTAG Interface
•Peripheral Features
– Two 8-bit Timer/Counters with Separate Prescaler and Compare Mode
– Four 16-bit Timer/Counter with Separate Prescaler,
Compare- and Capture Mode
– Real Time Counter with Separate Oscillator
– Four 8-bit PWM Channels
– Six/Twelve PWM Channels with Programmable Resolution from 2 to 16 Bits
(ATmega1281/2561, ATmega640/1280/2560)
– Output Compare Modulator
– 8/16-channel, 10-bit ADC (ATmeg
a1281/2561, ATmega640/1280/2560)
– Two/Four Programmable Serial USART (ATmega1281/256
1, ATmega640/1280/2560)
– Master/Slave SPI Serial Interface
– Byte Oriented 2-wire Serial Interface
– Programmable Watchdog Timer with
Separate On-chip Oscillator
– On-chip Analog Comparator
– Interrupt and Wake-up on Pin Change
•Special Microcontroller Features
– Power-on Reset and Programmable Brown-out Detection
– Internal Calibrated Oscillator
– External and Internal Interrupt Sources
– Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby,
and Extended Standby


As can be seen the STM32 has greater advanced peripherals out of the box such as USB, ethernet among others.

  • Greater computational capability per Watt


  • Availability of cheap debugging tools on the STM32 and other ARM platforms compared to the arduino.

This gives one the ability to see what the program is doing and instructions it is performing in real time. Even the 15 dollar Nucleo board above has an on board debugger.

  • Code portability across vendor devices

Because of (CMSIS)Cortex Micro controller Software Interface Standard. Code is more readily portable among devices belonging to different vendors. A good case for this is the recent semiconductor company mergers that have been shaking up the industry. In case you were to use an Atmel micro controller found in the arduino and Microchip acquired Atmel. If Microchip decided that it would EOL(End of life) a product line that is  in your 10 thousand products, that is a bite in your posterior! Ouch. Back to design! Of course this may not happen but it is good to be open to the possibility.

As opposed to this, the same arm core that is in the STM32 can be found readily available in other vendors products such as NXP, TI among others.

Final thoughts

The STM32 is a great device for moving onto more complex designs where the time and cost complexity of having an 8 bit solution is outweighed by the benefits of a 32 bit solution at the expense of greater complexity but more control.

Grab an STM32 Nucleo from mouser when you can.

Note: I am not affiliated to any of the aforementioned companies.