# using the systick timer as time base for the blink program # MCU seems to start with 8MHz sysclk, not 24MHz # 2025-07-02 mchris .org 0x00000000 .file "blink.s" .section .text .balign 4 .global main .global Coldstart R32_RCC_APB2PCENR = 0x40021018 R32_GPIOD_CFGLR = 0x40011400 R32_GPIOD_BSHR = 0x40011410 # systick registers absolute addresses R32_STK_CTLR = 0xE000F000 R32_STK_SR = 0xE000F004 R32_STK_CNTL = 0xE000F008 R32_STK_CMPLR = 0xE000F010 # systick registers relative addresses SYSTICK_CONTROL = 0x00 SYSTICK_STATUS = 0x04 SYSTICK_COUNTER = 0x08 SYSTICK_RELOAD = 0x10 main: Coldstart: nop Port_Setup_Clock: li x10,R32_RCC_APB2PCENR lw x11,0(x10) li x7,(1<<5) or x11,x11,x7 sw x11,0(x10) Port_Setup_Direction: li x10,R32_GPIOD_CFGLR lw x11,0(x10) li x7,0xfff0ffff and x11,x11,x7 li x7,0x00030000 or x11,x11,x7 sw x11,0(x10) li x10,R32_GPIOD_BSHR InitSystick: li t1, R32_STK_CTLR # load systick counter base address R32_STK_CTLR li t2, 1000000 # compare value sysclk/systickDiv/reloadValue/2ticksOnOff=24MHz/8/1000000/2=1.5Hz blink should be # .. it is much slower: 1/3 Hz is the sysclock wrong? sw t2, SYSTICK_RELOAD(t1) # store reload value in compare register sw x0, SYSTICK_COUNTER(t1) # set counter to 0 li t2, 1+8 # Enable the timer: set R32_STK_CTLR = 1 auto and +8 for auto reload, systick clk=HCLK/8 sw t2, SYSTICK_CONTROL(t1) mainLoop: Led_PD4_ON: li x11,0b00000000000000000000000000010000 sw x11,0(x10) call waitForTick Led_PD4_OFF: li x11,0b00000000000100000000000000000000 sw x11,0(x10) call waitForTick j mainLoop waitForTick: lw t2, SYSTICK_STATUS(t1) # load systick flag beq t2,x0,waitForTick sw x0,SYSTICK_STATUS(t1) # clear systick flag ret ebreak