|
// samd21 blink LED on PA10 on Trinket M0, controlled by button on D3 (PA07) |
|
// adapted / stolen from: |
|
// https://github.com/LifeWithDavid/RaspberryPiPico-BareMetalAdventures/blob/main/Chapter%2004/assembly.s |
|
// with much help from https://www.whynotestflight.com/excuses/its-alive-samd21e18a-assembly-no-sdk/ |
|
|
|
.cpu cortex-m0 |
|
.syntax unified |
|
.thumb |
|
|
|
.global main // 'main' used in startup.s |
|
|
|
.section .text |
|
|
|
main: |
|
|
|
movs r1, #1 // r1 will be our button pin mask, load up r1 with 1 |
|
lsls r1, r1, #BUT_PINNUM // shift the one to button pin's position |
|
|
|
movs r2, #1 // r2 will be our LED pin mask |
|
lsls r2, r2, #LED_PINNUM // shift the 1 to led pin's position |
|
|
|
ldr r0, =PORT // load PORT base address into r0, used for all future load/stores |
|
str r2, [r0, #DIRSET] // set bit in PORT_DIRSET to make output |
|
|
|
ldr r3, =INPUT_ENABLE // get PULLEN, INEN, WRPINCFG for upper 16 bits of WRCONFIG |
|
orrs r3, r3, r1 // "add" the button pinmask in r1 (probably better way to do this?) |
|
|
|
str r3, [r0, #WRCONFIG] // store button pin mask + flags it to WRCONFIG, i.e. set pin to input w/ pullup |
|
str r1, [r0, #OUTSET] // write to OUTSET to enable pullup for button pin |
|
|
|
todloop: |
|
ldr r4, [r0, #IN] // read in PORT_IN to read pin states, including our button |
|
ands r4, r4, r1 // pick out button bit (r1 has button number mask) |
|
bne led_off |
|
|
|
led_on: |
|
str r2, [r0, #OUTSET] // turn LED on with OUTSET (r2 has led pin mask) |
|
b todloop |
|
|
|
led_off: |
|
str r2, [r0, #OUTCLR] // turn LED off with OUTCLR (r2 has led pin mask) |
|
b todloop |
|
|
|
|
|
.data |
|
.equ LED_PINNUM, 10 // PA10 = where LED is on Trinket M0 |
|
.equ BUT_PINNUM, 7 // PA07 = D3 = where my button is |
|
|
|
.equ INPUT_ENABLE, 0x40060000 // sets bits WRPINCFG, PULLEN, INEN |
|
|
|
|
|
.equ PORT, 0x41004400 // Section 23.7 of SAMD21 datasheet |
|
|
|
.equ DIR, 0x00 |
|
.equ DIRCLR, 0x04 |
|
.equ DIRSET, 0x08 |
|
.equ DIRTGL, 0x0C |
|
.equ OUT, 0x10 |
|
.equ OUTCLR, 0x14 |
|
.equ OUTSET, 0x18 |
|
.equ OUTTGL, 0x1C |
|
.equ IN, 0x20 |
|
.equ CTRL, 0x24 |
|
.equ WRCONFIG, 0x28 |
|
|
|
.equ PORT_DIRCLR, PORT + DIRCLR |
|
.equ PORT_DIRSET, PORT + DIRSET |
|
.equ PORT_DIRTGL, PORT + DIRTGL |
|
|
|
.equ PORT_OUT, PORT + OUT |
|
.equ PORT_OUTCLR, PORT + OUTCLR |
|
.equ PORT_OUTSET, PORT + OUTSET |
|
.equ PORT_OUTTGL, PORT + OUTTGL |
|
|
|
.equ PORT_IN, PORT + IN |
|
.equ PORT_CTRL, PORT + CTRL |
|
.equ PORT_WRCONFIG, PORT + WRCONFIG |
The linker script
flash.ld
is:And the Makefile is: