Skip to main content Skip to local navigation

Exploring RISC-V: Espressif IDE & ESP32-C3

Here is a short post on a successful attempt to write a C program in the Espressif IDE on the EECS Linux computers (LAS 1006). I have an ESP32-C3 development board plugged in and have the IDE loaded with the boilerplate code inserted when I choose a simple example from the menu.

I then modified the project to ensure that the target (see the target / aim symbol at the top of the page) points to the /dev/ttyUSB0 CP2102N USB to UART bridge.

From there I

  1. added a new text file and called it return4bytes.S (apparently the .S works better than .s or .asm)
  2. added an entry in the CMakeLists.txt file (in the Main folder)
idf_component_register(
    SRCS main.c  return4bytes.S     # list the source files of this component
    INCLUDE_DIRS        # optional, add here public include directories
    PRIV_INCLUDE_DIRS   # optional, add here private include directories
    REQUIRES            # optional, list the public requirements (component names)
    PRIV_REQUIRES       # optional, list the private requirements
)

The assembler file is

# =========== TEXT: Code section ===============
.text
.global return4bytes

# Function returns 0xABCD
# No inputs.  Just one output: 0xABCD
return4bytes:
    li a0, 0xABCD    # load immediate 0xABCD into return register a0
    ret              # return to caller

and then I modified the C file to include the prototype for the assembler file and to call it:

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

// Reference: https://www.yorku.ca/professor/drsmith/2025/12/11/exploring-risc-v-options-the-rp2350-part-7-casm-passing-values/
extern int return4bytes(void);  // prototype of the assembler routine

void app_main(void)
{
	/* Call the assembler routine and accept the output. */
	int result = return4bytes();   // call the assembler routine
	
    while (true) {
        printf("Hello from app_main! Here is the value: 0x%x\n",result);
		sleep(1);
    }
}

and it worked.

One of the important details is that the assembler file is capable of returning a value to the C's printf() call. This is key as it will allow students to write assembler code and then we can have them return a particular value back from, say, a particular register, and that can be printed out.

Espressif IDE running a combined C+ASM program that outputs a string to the terminal