Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active April 15, 2024 01:07
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/119582ba35fae1503ec36d96d9ae366f to your computer and use it in GitHub Desktop.
Save todbot/119582ba35fae1503ec36d96d9ae366f to your computer and use it in GitHub Desktop.
samd21 blink LED on PA06 on Trinket M0
@todbot
Copy link
Author

todbot commented Apr 14, 2024

More standard way might be loading the base address for PORT DIR and PORT OUT and using SET/CLR offsets:

// samd21 blink LED on PA10 on Trinket M0
// adapted / stolen from:
// https://github.com/LifeWithDavid/RaspberryPiPico-BareMetalAdventures/blob/main/Chapter%2004/assembly.s
    
.cpu cortex-m0
.syntax unified
.thumb

.global main                // used in startup.s
    
.section .text
    
main:

    movs    r2, #1              // load up r2 with 1
    lsls    r2, r2, #PINNUM     // shift the 1 to pinnum's position

    ldr     r0, =PORT_DIR       // load DIR base address into r0
    str     r2, [r0, #8]        // set bit 10 in PORT_DIRSET  (make output)

    ldr     r0, =PORT_OUT       // base addr for PORT (+4 for CLR, +8 for SET
    
todloop:
    str     r2, [r0, #4]        // set bit 10 in OUTCLR (r2 has (1<<10))
    ldr     r3, =DELAY1
    bl      delay               // call subroutine

    str     r2, [r0, #8]        // set bit 10 in OUTSET  (r2 has (1<<10))
    ldr     r3, =DELAY2
    bl      delay               // call subroutine
    
    b       todloop             // continue forever
    
delay:
    subs    r3, #1              // subtract one from reg 3  (not SUB, SUBS)
    bne     delay               // loop back if not zero
    bx      lr                  // return from subroutine

	//mov r0, r0          // to word align data below?

// data must be 4-byte aligned? how to achieve?
.data
    .equ PINNUM,       10

    .equ DELAY1,        180000
    .equ DELAY2,        140000
    
    .equ PORT_DIR,      0x41004400
    .equ PORT_DIRCLR,   0x41004404
    .equ PORT_DIRSET,   0x41004408
    .equ PORT_DIRTGL,   0x4100440C
    
    .equ PORT_OUT,      0x41004410
    .equ PORT_OUTCLR,   0x41004414
    .equ PORT_OUTSET,   0x41004418
    .equ PORT_OUTTGL,   0x4100441C

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