Notes on programming bare metal ARM Cortex-M3 microcontrollers found on Arduino Due boards.

Tools

The tools used in this project are

  • OpenOCD
  • GNU Arm embedded toolchain
  • ST-LINK/V2 in-circuit programmer

Electrical connections

The following diagram outlines the electrical connections between the ST-LINK/V2 programmer and the Arduino Due.

schematic connections

The JTAG port of the ST-LINK/V2 connects to the Serial Wire Debug (SWD) interface of the SAM3X8E chip via the debug pins of the Arduino Due.

After wiring the parts, start OpenOCD (the config file can be found in the source.tar.gz at the end of this page) and establish a telnet session on port 4444. We can program the chip, inspect the chip, etc., over this telnet connection.

The readme file in the source.tar.gz contains the commands for uploading the LED blink program. See the AT91SAM3 flash driver section in the OpenOCD user manual for a complete list of commands for the SAM3X8E.

Memory map

The MCU's memory consists of a 16KB internal ROM, a 512KB flash, and a 96KB SRAM. Both the flash and the SRAM are dual-banked. The linker script must indicate their locations in the MEMORY section. The complete memory map of the MCU can be found in section 7.1 of the datasheet.

According to section 10.6.4 of the datasheet, the ARM core expects a vector table at the start address (see VTOR in the datasheet for relocating the vector table). Therefore, we define the vector table in the main.c file of the sample project. The linker script places the vector table at the first address of the flash.

Section 10.4.2 of the datasheet describes the structure of the vector table. The SAM3X8E uses a descending stack (i.e., the stack grows by decrementing the stack pointer after inserting an item). Hence, we set the stack pointer to SRAM's last (maximum) address.

On startup, program control bypasses the bootloader (more on this later) and jumps straight to the reset vector (the second entry of the vector table). Hence, the reset vector must perform the tasks typically taken care of by bootloaders, such as initializing program memory.

Boot strategy

The ARM core is configured at the hardware level to always boot into address 0x00000000 of the address space. What that address maps to of the physical memories depends on the General Purpose Non-Volatile Memory (GPNVM) bits.

The SAM3X8E contains Atmel's SAM-BA bootloader at address 0x00100000 of its internal ROM. In its default setup, address 0x00000000 maps to this address so that the SAM-BA bootloader, instead of our program, runs on startup.

We alter the GPNVM bits using OpenOCD commands listed in the readme to change the default boot strategy. The GPNVM bits map address 0x00000000 to 0x00080000 of the flash memory, causing program control to bypass the embedded bootloader and execute our program.

Files: source.tar.gz