Making function to turn 2 LEDS ON and OFF on ESP32-WROOM-32 on Assembly in Arduino IDE

Hello Guys! It's my first time writing in this forum. I would like you to help me with a project of mine, if you don't mind. I'm kinda new to these stuff but I wish some of you could help me. I'm using a ESP32-WROOM-32 and I'm using the "esp32" board manager by Espressif Systems (2.0.11) and I need to make a function that has the same output as the "digitalWrite()" function to turn 2 LEDS ON and OFF connected to pins D32 and D33 in Assembly code on Arduino IDE. Thanks in advanced.

Split the project into small pieces.
Why assembly code? Use the C language!
Start by making the communication work, then LED handling.
When both work, make them work in the same code and create the logic links between them making what You want.

A tip for better help: How to get the best out of this forum - Using Arduino / IDE 1.x - Arduino Forum

I understand that C language is way more easier, but this is a school project. It needs to be Assembly.

Is the requirement also that you need to use an ESP32?

I had a quick look at the ESP32 core; something like digitalWrite() seems to be implemented 'under the hood'.

If it has to be an ESP32

  1. I would advise to ask the question in a dedicated Espressif forum.
  2. I think that Espressif has its own IDE and you will probably have to use that.

Just FYI: ESP32 instruction set or assembly (ASM) programming docs? - ESP32 Forum

1 Like

led_control.S:

# led_control.S

.literal GPIO_OUT_W1TC_REG, 0x3FF4400C        # clear PIN (LOW) [REGISTER addr]
.literal GPIO_NUM_2, (1<<2)                   # GPIO 2
.literal GPIO_NUM_4, (1<<4)                   # GPIO 4
.literal GPIO_OUT1_W1TS_REG, 0x3FF44008       # PIN (HIGH) [REGISTER addr]

.global led_green_off
.align 4
led_green_off:
  entry a1, 32

  l32r a14, GPIO_OUT_W1TC_REG
  l32r a15, GPIO_NUM_2
  s32i a15, a14, 0                          

  retw


# Function to turn the green LED on
.global led_green_on
.align 4
led_green_on:
  entry a1, 32

  l32r a14, GPIO_OUT1_W1TS_REG
  l32r a15, GPIO_NUM_2
  s32i a15, a14, 0                          

  retw

# Function to turn the green LED off
.global led_red_off
.align 4
led_red_off:
  entry a1, 32

  l32r a14, GPIO_OUT_W1TC_REG
  l32r a15, GPIO_NUM_4
  s32i a15, a14, 0                          

  retw

# Function to turn the red LED on
.global led_red_on
.align 4

led_red_on:
  entry a1, 32

  l32r a14, GPIO_OUT1_W1TS_REG
  l32r a15, GPIO_NUM_4
  s32i a15, a14, 0                         

  retw

led_control.h:

#ifndef LED_CONTROL_H
#define LED_CONTROL_H

extern "C" {
  void led_green_off();
  void led_green_on();
  void led_red_off();
  void led_red_on();
}

#endif // LED_CONTROL_H

leds_assembly.ino:

#include led_control.h
#include stdio.h

extern void led_green_on(void);
extern void led_green_off(void);
extern void led_red_on(void);
extern void led_red_off(void);

void setup(){
  gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
  gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT);
}

void loop() {
  led_green_on();
  delay(1000);  Wait for 1 second
  led_green_off();
  delay(1000);  Wait for 1 second
  led_red_on();
  delay(1000);  Wait for 1 second
  led_red_off();
  delay(1000);  Wait for 1 second
}

I managed to achieve my goal, I've changed the green led pin to GPIO 2 and red led pin to GPIO 4 and it works. My only doubt is when the green led is turned ON, the blue led inside ESP32 turns on aswell, but that doesn't happen when the red led is turned ON (the codes are the same as you can see in "led_control.S"). How can I make the blue led be turned OFF when green led is turned ON?

Congrats on being persistent and winning :+1:

Any chance that the green LED is on the same pin as the internal blue LED?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.