#include <stdint.h>
#include <rom/ets_sys.h>
// ESP32 GPIO register addresses
#define GPIO_OUT_REG (*(volatile uint32_t*)0x3FF44004)
#define GPIO_OUT_W1TS_REG (*(volatile uint32_t*)0x3FF44008)
#define GPIO_OUT_W1TC_REG (*(volatile uint32_t*)0x3FF4400C)
#define GPIO_ENABLE_REG (*(volatile uint32_t*)0x3FF44020)
#define GPIO_IN_REG (*(volatile uint32_t*)0x3FF4403C)
#define LED_PIN 2
#define SWITCH_PIN 4
void delay_ms(uint32_t ms) {
while (ms--) {
ets_delay_us(1000);
}
}
void app_main(void) {
// Set GPIO2 as output
GPIO_ENABLE_REG |= (1u << LED_PIN);
// Set GPIO4 as input
GPIO_ENABLE_REG &= ~(1u << SWITCH_PIN);
while (1) {
// Read GPIO4
if (GPIO_IN_REG & (1u << SWITCH_PIN)) {
// If switch input = 1, turn LED ON
GPIO_OUT_W1TS_REG = (1u << LED_PIN);
} else {
// If switch input = 0, turn LED OFF
GPIO_OUT_W1TC_REG = (1u << LED_PIN);
}
delay_ms(50);
}
}