Page 1 of 1

ESP32C3 Trying to light up a led in assembly

Posted: Wed Sep 17, 2025 11:12 am
by EUtrilla2002
Hi

I'm learning to use registers in assembly. I made this code assuming TRM chapter 5.3 and some ChatGPT

Code: Select all

.section .text
.global app_main
.type app_main, @function

.equ GPIO,        0x60004000
.equ GPIO_OUT_FUNC, 341
.equ IO_MUX_BASE, 0x60000000
.equ IO_MUX_GPIO4_REG, IO_MUX_BASE + 0x0004 + 4*4   # 0x60000014

# =========================
# Set pin as output (GPIO4)
# =========================
gpio_output:
    li a0, 4                 # PIN = 4
    li t0, GPIO

    li t1, GPIO_OUT_FUNC
    add t1, t1, a0
    slli t1, t1, 2
    add t0, t0, t1

    li t1, 0x80             # OEN_SEL + simple GPIO
    sw t1, 0(t0)
    jr ra

# =========================
# Enable output (GPIO4)
# =========================
gpio_output_enable:
    li a0, 4
    li t0, GPIO
    li t1, 0x20              # GPIO_ENABLE_REG offset
    add t0, t0, t1

    li t1, 1
    sll t1, t1, a0
    sw t1, 0(t0)
    jr ra

# =========================
# Configure IO MUX for GPIO4 (FUN_DRV = 3)
# =========================
gpio_io_mux:
    li t0, IO_MUX_GPIO4_REG
    li t1, 0x300041   # FUN_SEL=1, MCU_SEL=1, FUN_DRV=3, WPU/WPD=0
    sw t1, 0(t0)
    jr ra
# =========================
# Write HIGH/LOW
# =========================
gpio_write:
    li t0, GPIO

    beqz a1, write_low
    li t1, 0x08              # GPIO_OUT_W1TS_REG
    j write_value

write_low:
    li t1, 0x0C              # GPIO_OUT_W1TC_REG

write_value:
    add t0, t0, t1
    li t2, 1
    sll t2, t2, a0
    sw t2, 0(t0)
    jr ra

# =========================
# Main (turn LED ON)
# =========================
app_main:
    call gpio_output      # Set pin function
    call gpio_io_mux      # Configura IO MUX primero
    call gpio_output_enable # Habilita salida
    li a0, 4
    li a1, 1
    call gpio_write       # Pone HIGH
However, it does not light up. If i use C code, however, works

i looked up with gdb the following registers
GPIO_OUT_REG : x/w 0x60004004
GPIO_ENABLE_REG: x/w 0x60004020
GPIO_FUNC4_OUT_SEL_CFG_REG: x/w 0x60004564
IO_MUX_GPIO4_REG x/w 0x60000014
GPIO4_PIN_REG x/w 0x60004054

Something seems missing, as my led does not turn on

Re: ESP32C3 Trying to light up a led in assembly

Posted: Wed Sep 17, 2025 5:43 pm
by ahsrabrifat
Change your gpio_output routine to write 0x100 (256) instead of 0x80:

Code: Select all

gpio_output:
    li a0, 4                 # PIN = 4
    li t0, GPIO

    li t1, GPIO_OUT_FUNC
    add t1, t1, a0
    slli t1, t1, 2
    add t0, t0, t1

    li t1, 0x100             # FUNC_OUT_SEL = 256 (GPIO)
    sw t1, 0(t0)
    jr ra
Sequence should be:

Configure IO_MUX (to GPIO, correct drive strength). Configure FUNC_OUT_SEL = 256. Enable GPIO in GPIO_ENABLE_REG. Set/clear pin in W1TS/W1TC. That matches what the ESP-IDF does under the hood.