.syntax unified @ Specify unified assembly syntax
.cpu cortex-m0plus @ Specify CPU type is Cortex M0+
.thumb @ Specify thumb assembly for RP2040
.global main_asm @ Provide program starting address to the linker
.align 4 @ Specify code alignment
.equ SLEEP_TIME, 500 @ Specify the sleep time (in ms)
.equ LED_GPIO_PIN, 0 @ Specify the pin that the onboard LED is connected to
.equ ADC_GPIO_PIN, 26 @ Specify the pin that the potentiometer is connected to
.equ LED_GPIO_OUT, 1 @ Specify the direction of the GPIO pin
.equ LED_GPIO_IN, 0 @ Specify the direction of the GPIO pin
.equ LED_VALUE_ON, 1 @ Specify the value that turns the LED "on"
.equ LED_VALUE_OFF, 0 @ Specify the value that turns the LED "off"
.equ ADC_BASE, 0x4004C000 @ Base address of the ADC registers
.equ ADC_CS, 0x00 @ Offset of the Control and Status Register
.equ ADC_RESULT, 0x04 @ Offset of the Result Register
.equ ADC_CS_EN, 0x01 @ Bit 0: Enable ADC
.equ ADC_CS_START_MANY, 0x08 @ Bit 3: Start Many conversion
.equ ADC_CH0, 0x00 @ Channel 0 (GPIO26) selection bits
.equ ADC_READY_BIT_MASK, 0x0100
@ Entry point to the ASM portion of the program
main_asm:
movs r5, #0
init_gpio:
movs r0, #LED_GPIO_PIN @ Load r0 with the value of LED_GPIO_PIN, which is 25
adds r0, r0, r5
bl asm_gpio_init @ Call asm_gpio_init with r0=25 (GPIO 25)
movs r0, #LED_GPIO_PIN @ Load r0 with the value of LED_GPIO_PIN, which is 25
adds r0, r0, r5
movs r1, #LED_GPIO_OUT @ Load r1 with 1, indicating output direction
bl asm_gpio_set_dir @ Call asm_gpio_set_dir with r0=25, r1=1
adds r5, r5, #1
cmp r5, #10
bne init_gpio
movs r0, #ADC_GPIO_PIN
bl asm_gpio_init
movs r0, #ADC_GPIO_PIN
movs r1, #LED_GPIO_IN
bl asm_gpio_set_dir
ldr r1, =ADC_BASE @ Load the base address of the ADC
ldr r0, [r1, #ADC_CS] @ Load the current value of the CS register
adds r0, r0, #ADC_CS_EN @ Set the EN bit to enable the ADC
str r0, [r1, #ADC_CS] @ Write back the value to the CS register
ldr r1, [r0, #ADC_CS] @ Load the current value of the CS register
ldr r2, [r0, #ADC_RESULT] @ Load the result from the RESULT register
poll_adc:
movs r7, #9
movs r4, #0
set_leds:
cmp r4, r7 @ Compare LED counter with scaled result
bge loop
movs r0, #LED_GPIO_IN @ Load GPIO pin number
adds r0, r0, r4
movs r1, #LED_VALUE_ON @ Set pin high
bl asm_gpio_put @ Turn on LED
adds r4, r4, #1
b set_leds
loop:
b loop
@ Set data alignment
.data
.align 4