Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Created December 12, 2015 23:43
Show Gist options
  • Save postspectacular/045a694803d896dd0065 to your computer and use it in GitHub Desktop.
Save postspectacular/045a694803d896dd0065 to your computer and use it in GitHub Desktop.
ARM ASM blink example, linker & build scripts
.syntax unified
.cpu cortex-m4
.thumb
// configuration values for STM32F4x
// see: http://www.st.com/web/en/resource/technical/document/reference_manual/DM00031020.pdf
#define AHB1PERIPH_BASE 0x40020000
#define RCC_BASE AHB1PERIPH_BASE + 0x3800
#define RCC_AHB1_ENR RCC_BASE + 0x30
#define GPIOD_BASE AHB1PERIPH_BASE + 0x0c00
#define GPIOD_MODER GPIOD_BASE
#define GPIOD_ODR GPIOD_BASE + 0x14
#define GPIOD_BIT 8
#define LED_PIN 15
#define LED_MODER 1 << (2 * LED_PIN)
#define LED_BIT 1 << LED_PIN
#define DELAY 0x80000
.section .text
.weak _start
.type _start, %function
_start:
ldr r0, =RCC_AHB1_ENR // get contents of AHB1 bus clock enable register
ldr r1, [r0]
orr r1, GPIOD_BIT // set bit to enable clock for GPIO port D
str r1, [r0]
ldr r0, =GPIOD_MODER // configure LED pin as outout
ldr r1, =LED_MODER
str r1, [r0]
ldr r0, =GPIOD_ODR // load address of GPIOD output data register
.blink:
movw r1, LED_BIT
str r1, [r0] // setting LED bit in GPIOD_ODR
bl .delay // jump to delay function below
eors r1, r1 // clear r1 register
str r1, [r0] // turn LED off
bl .delay // wait again
b .blink // infinite loop
.delay:
ldr r2, =DELAY // load DELAY value
1: subs r2, r2, #1 // count down to 0 = wait
bne 1b
bx lr // return to caller
// partial, but mandatory ARM Cortex M interrupt table
// here we only specify the reset handler, which also is our main program
.section .isr_vector_table, "a", %progbits
.type isr_vectors, %object
isr_vectors:
.word _estack // stack start address (defined in linker script)
.word _start // address of the reset handler
.size isr_vectors, . - isr_vectors
arm-none-eabi-gcc -Wall -Wextra -o blinky.elf -nostdlib -mcpu=cortex-m4 -mthumb -Tstm32f401-mem.ld -Tsections.ld -Wa,-adhnls blinky.S \
&& arm-none-eabi-objcopy -O binary blinky.elf blinky.bin \
&& st-flash --reset write blinky.bin 0x08000000
ENTRY(_start)
SECTIONS {
.isr_vector_table : {
*(.isr_vector_table)
} > FLASH
.text : {
*(.text)
} > FLASH
}
MEMORY {
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 256K
RAM (rxw) : ORIGIN = 0x20000000, LENGTH = 64K
}
_estack = ORIGIN(RAM) + LENGTH(RAM);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment