Léo Dupuy
Léo Dupuy

Reputation: 1

LEDC PWM in RISC-V assembly on ESP32-C3 using ESP-IDF

I've started a personal project. The idea is to output PWM on pin 8 (GPIO8) of a ESP32-C3 using LEDC peripheral on an ESP32-C3 using assembly language (RISC-V) with ESP-IDF. I've started by configuring GPIO, LEDC and enabling LEDC_CLK (according to the ESP32-C3 technical documentation) but no output have been observed.

The first thing I did was to use simple output (using GPIO_OUTPUT_REG) and it worked! Then I changed the GPIO Matrix output signal for a given pin by using the function ledc_ls_sig_out3. At this point nothing worked correctly: for instance changing the value of LEDC_IDLE_LV_CH (default output of LEDC) did not impact the output.

I put my code on my GitHub. You can find the code right below. Simply put it in a ESP-IDF project for ESP32-C3.

.global init_pwm

# Use CH3 + TIMER3
# Timer @1250Hz

init_pwm : 

    addi sp, sp, -4
    sw ra, 0(sp)

    # -----------------------------------------------------------------------------

    #### Base : 0x600C_0000 - System registers

    # SYSTEM_PERIP_RST_EN0_REG (0x0018) -> 0x0800 : RESET SYS_LEDC_CLK
    li a2, 0x600C0018
    li t5, 0x00000800
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    li a2, 0x600C0018
    li t5, 0x00000000
    lw t1, 0(a2)
    and t1, t1, t5
    sw t1, 0(a2)

    # SYSTEM_PERIP_CLK_EN0_REG (0x0010) -> 0x0800 : enable SYS_LEDC_CLK
    li a2, 0x600C0010
    li t6, 0x00000800
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # -------------------------------------------------------------
    # Base LEDC_PWM : 0x6001_9000

    # Register : LEDC_CONF_REG : 0x00D0 (general)  : APB_CLK as LEDC Timer
    li t5, 0x80000001
    li a2, 0x600190D0
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # Register : LEDC_TIMER3_CONF_REG : 0x00B8
    li t5, 0x0000F906
    li a2, 0x600190B8
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # Register : LEDC_CH3_HPOINT_REG : 0x00040
    li t5, 0x0000010
    li a2, 0x60019040
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # Register : LEDC_CH3_DUTY_REG : 0x0044
    li t5, 0x00000080
    li a2, 0x60019044
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # Register : LEDC_CH3_CONF0_REG : 0x003C
    li t5, 0x0000000f
    li a2, 0x6001903C
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # Register : LEDC_CH3_CONF1_REG : 0x0048
    li t5, 0x00000000
    li a2, 0x60019048
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # ---- UPDATE -----
    # Register : LEDC_CH3_CONF0_REG : 0x0000 (register 0)
    li t5, 0x00000010
    li a2, 0x6001903C
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # Register : LEDC_TIMER3_CONF_REG : 0x00B8
    li t5, 0x02000000
    li a2, 0x600190B8
    lw t1, 0(a2)
    or t1, t1, t5
    sw t1, 0(a2)

    # ----------------------------------------------------------------------
    # Base GPIO : 0x6000_4000

         # Register : GPIO_FUNC8_OUT_SEL_CFG_REG (n: 0-21) (0x0554+4*n) : valeur sur 48 (ledc_ls_sig_out1 ) + 9ème bit sur 0 (to enable peripheral output)
    li a2, 0x60004574
    li t5, 0xfffff800
    li t6, 0x00000030
        lw t1, 0(a2)
        and t1, t1, t5
    or t1, t1, t6
        sw t1, 0(a2)

    # Register : IO_MUX_GPIO8_REG (0x0004+4*8)
    li t5, 0x1000
        li t6, 0xfffff000
    li a2, 0x60009024
        lw t1, 0(a2)
    or t1, t1, t5
        and t1, t1, t6
        sw t1, 0(a2)

    # Register : GPIO_ENABLE_W1TC_REG (0x0028) : Clear ENABLE Reg

    # Register : GPIO_ENABLE_W1TS_REG (0x0024) : SET ENABLE
    li t5, 0x0100
    li a2, 0x60004024
        lw t1, 0(a2)
        or t1, t1, t5
        sw t1, 0(a2)

    # Register : GPIO_OUT_W1TS_REG (0x0008)
    li t5, 0x0100
    li a2, 0x60004000
        lw t1, 0(a2)
        or t1, t1, t5
        sw t1, 0(a2)

    # Register : GPIO_OUT_W1TC_REG (0x000C)

        addi sp, sp, 4
    jalr ra
void init_pwm(void);

void app_main(void)
{
    init_pwm();
}
idf_component_register(SRCS "main.c" "pwm.S"
                    INCLUDE_DIRS ".")

I would like to know if there is specific registers to program to enable LEDC or to let LEDC access some memory or if the project is a bit too tough.

Upvotes: 0

Views: 62

Answers (0)

Related Questions