; ARM assembly tutorial for Asmonite, example 02: switch on the LED (Raspberry Pi Pico 2, Arm Cortex-M33).
; Adapted from the ARM-ASM-Tutorial by Niklas Guertler (example SetPin); see LICENSE.txt.
;
; The LED of the Pico 2 is at GPIO 25. Four steps make the pin an output that is high:
;   1. take the blocks IO_BANK0 and PADS_BANK0 out of their reset (RESETS)
;   2. remove the isolation of the pad of GPIO 25 (PADS_BANK0)
;   3. give the pin to the SIO, the simple input/output block of the cores (IO_BANK0)
;   4. switch on the output driver and set the output to 1 (SIO)
; Every address and value is loaded from the table at the end ("ldr r1, name").
.target pico2
.arch thumb
.imagebase 0x20000000

_start:
    ldr  r1, resets_clr
    ldr  r0, reset_bits
    str  r0, [r1]               ; RESETS.RESET: clear bit 6 (IO_BANK0) and bit 9 (PADS_BANK0)
    ldr  r1, resets_done
wait_reset:
    ldr  r2, [r1]               ; RESETS.RESET_DONE: both bits must be 1
    and  r2, r2, r0
    cmp  r2, r0
    bne  wait_reset

    ldr  r1, pad25_clr
    ldr  r0, pad_iso
    str  r0, [r1]               ; PADS_BANK0.GPIO25: clear bit 8 (ISO)

    ldr  r1, gpio25_ctrl
    ldr  r0, funcsel_sio
    str  r0, [r1]               ; IO_BANK0.GPIO25_CTRL: function 5 = SIO

    ldr  r1, sio_oe_set
    ldr  r0, led_bit
    str  r0, [r1]               ; SIO.GPIO_OE_SET: GPIO 25 is an output
    ldr  r1, sio_out_set
    str  r0, [r1]               ; SIO.GPIO_OUT_SET: GPIO 25 is high, the LED is on

    b    $

    align 4
resets_clr:  dd 0x40023000      ; RESETS.RESET (0x40020000), +0x3000: the alias that clears bits
reset_bits:  dd 0x00000240      ; bit 6 IO_BANK0, bit 9 PADS_BANK0
resets_done: dd 0x40020008      ; RESETS.RESET_DONE
pad25_clr:   dd 0x4003b068      ; PADS_BANK0.GPIO25 (0x40038068), +0x3000: clear bits
pad_iso:     dd 0x00000100      ; bit 8 ISO
gpio25_ctrl: dd 0x400280cc      ; IO_BANK0.GPIO25_CTRL
funcsel_sio: dd 5               ; FUNCSEL 5: SIO
sio_oe_set:  dd 0xd0000038      ; SIO.GPIO_OE_SET
sio_out_set: dd 0xd0000018      ; SIO.GPIO_OUT_SET
led_bit:     dd 0x02000000      ; bit 25
