Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active April 21, 2024 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todbot/56900f340d2f54885737c0853f0426bb to your computer and use it in GitHub Desktop.
Save todbot/56900f340d2f54885737c0853f0426bb to your computer and use it in GitHub Desktop.
Read button and control LED on SAMD21 in assembly
.cpu cortex-m0
.thumb
.thumb_func
.global _start
_start:
stacktop: .word 0x20001000
.word reset
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.thumb_func
reset:
bl main
b hang
.thumb_func
hang: b .
.end
@todbot
Copy link
Author

todbot commented Apr 21, 2024

The linker script flash.ld is:

MEMORY
{
    ram : ORIGIN = 0x00000000, LENGTH = 0x1000
}

SECTIONS
{
    .text : { *(.text*) } > ram
    .rodata : { *(.rodata*) } > ram
    .bss : { *(.bss*) } > ram
}

And the Makefile is:

NAME=led_button

AS=arm-none-eabi-as
CC=arm-none-eabi-gcc
LD=arm-none-eabi-ld
OBJCOPY=arm-none-eabi-objcopy
OBJDUMP=arm-none-eabi-objdump
GDB=arm-none-eabi-gdb

# seems that -mcpu not needed for asflags, maybe mthumb too?
ASFLAGS = -g --warn --fatal-warnings -mthumb -mcpu=cortex-m0 

LDFLAGS = -T flash.ld -Map=$(NAME).map

all : $(NAME).bin 

# does both startup.s and main.s
%.o: %.s
	$(AS) $(ASFLAGS) -o $@ $<

$(NAME).elf : flash.ld startup.o main.o
	$(LD) $(LDFLAGS) -o $(NAME).elf startup.o main.o

$(NAME).bin : $(NAME).elf
	$(OBJDUMP) -D $(NAME).elf > $(NAME).list
	$(OBJCOPY) -O binary $(NAME).elf $(NAME).bin 

program: program-stlink

program-dap: $(NAME).bin
	openocd -f interface/cmsis-dap.cfg -f target/at91samdXX.cfg \
			-c "init; targets; halt; " \
			-c "program $(NAME).bin verify " \
			-c "reset"

program-stlink: $(NAME).bin
	openocd -f interface/stlink.cfg -c "set CHIPNAME at91samd21g18a; set CPUTAPID 0x0bc11477" \
			-f target/at91samdXX.cfg -c "init; targets; halt " \
			-c "program $(NAME).bin verify" \
			-c "reset"

gdb-basic: $(NAME).elf
	$(GDB)  -iex "target extended-remote localhost:3333" \
			$(NAME).elf \

gdb: $(NAME).elf
	$(GDB)  -iex "target extended-remote localhost:3333" \
			$(NAME).elf \
			-ex "layout asm" -ex "layout regs" 

clean:
	rm -f *.bin *.elf *hex *.list *map 
	rm -f *.o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment