#include <stdint.h>
#include <stdio.h>
#include <rom/ets_sys.h>
/* =========================================================
ESP32 GPIO Register Addresses
These addresses come from the ESP32 memory map.
We use them to control GPIO pins directly.
========================================================= */
#define GPIO_ENABLE_REG 0x3FF44020
#define GPIO_OUT_REG 0x3FF44004
#define GPIO_IN_REG 0x3FF4403C
/* =========================================================
Pin Connections
========================================================= */
#define RED_LED_PIN 2
#define YELLOW_LED_PIN 4
#define GREEN_LED_PIN 5
#define SWITCH_PIN 12
/* =========================================================
Timing Configuration
========================================================= */
#define SHORT_DELAY_US 300000
#define LONG_DELAY_US 700000
#define BLINK_TIMES 3
/* =========================================================
Application Constants and State
========================================================= */
const uint8_t SWITCH_ACTIVE = 1;
uint8_t system_mode = 0;
/* =========================================================
GPIO Output Enable
This function configures a GPIO pin as output.
It sets the selected bit in GPIO_ENABLE_REG.
========================================================= */
void gpio_enable_output(uint8_t pin)
{
volatile uint32_t *gpio_enable;
gpio_enable = (volatile uint32_t *)GPIO_ENABLE_REG;
*gpio_enable |= (1UL << pin);
}
/* =========================================================
GPIO Set HIGH
This function makes one GPIO output HIGH.
It does not affect the other GPIO pins.
========================================================= */
void gpio_set_high(uint8_t pin)
{
volatile uint32_t *gpio_out;
gpio_out = (volatile uint32_t *)GPIO_OUT_REG;
*gpio_out |= (1UL << pin);
}
/* =========================================================
GPIO Set LOW
This function makes one GPIO output LOW.
It does not affect the other GPIO pins.
========================================================= */
void gpio_set_low(uint8_t pin)
{
volatile uint32_t *gpio_out;
gpio_out = (volatile uint32_t *)GPIO_OUT_REG;
*gpio_out &= ~(1UL << pin);
}
/* =========================================================
GPIO Read Input
This function reads one selected GPIO pin.
It returns 1 if the pin is HIGH.
It returns 0 if the pin is LOW.
========================================================= */
uint8_t gpio_read_pin(uint8_t pin)
{
volatile uint32_t *gpio_in;
gpio_in = (volatile uint32_t *)GPIO_IN_REG;
uint32_t input_value = *gpio_in;
uint32_t pin_mask = (1UL << pin);
if ((input_value & pin_mask) != 0)
{
return 1;
}
return 0;
}
/* =========================================================
Turn All LEDs OFF
========================================================= */
void turn_all_leds_off(void)
{
gpio_set_low(RED_LED_PIN);
gpio_set_low(YELLOW_LED_PIN);
gpio_set_low(GREEN_LED_PIN);
}
/* =========================================================
Normal Mode
Green LED ON only.
========================================================= */
void show_normal_mode(void)
{
turn_all_leds_off();
gpio_set_high(GREEN_LED_PIN);
}
/* =========================================================
Warning Mode
Yellow LED blinks three times.
Then Red LED turns ON.
========================================================= */
void show_warning_mode(void)
{
turn_all_leds_off();
for (uint8_t blink_counter = 0; blink_counter < BLINK_TIMES; blink_counter++)
{
gpio_set_high(YELLOW_LED_PIN);
ets_delay_us(SHORT_DELAY_US);
gpio_set_low(YELLOW_LED_PIN);
ets_delay_us(SHORT_DELAY_US);
}
gpio_set_high(RED_LED_PIN);
ets_delay_us(LONG_DELAY_US);
}
/* =========================================================
Update System Mode
If switch is ON, system enters warning mode.
If switch is OFF, system returns to normal mode.
========================================================= */
void update_system_mode(void)
{
uint8_t switch_state = gpio_read_pin(SWITCH_PIN);
if (switch_state == SWITCH_ACTIVE)
{
system_mode = 1;
}
else
{
system_mode = 0;
}
}
/* =========================================================
Display Current Mode
========================================================= */
void display_current_mode(void)
{
if (system_mode == 0)
{
show_normal_mode();
}
else
{
show_warning_mode();
}
}
/* =========================================================
Print Debug Information
========================================================= */
void print_debug_info(void)
{
uint8_t switch_state = gpio_read_pin(SWITCH_PIN);
printf("Switch = %u | System Mode = %u\n",
switch_state,
system_mode);
}
/* =========================================================
Main Application
========================================================= */
void app_main(void)
{
gpio_enable_output(RED_LED_PIN);
gpio_enable_output(YELLOW_LED_PIN);
gpio_enable_output(GREEN_LED_PIN);
turn_all_leds_off();
while (1)
{
update_system_mode();
display_current_mode();
print_debug_info();
ets_delay_us(SHORT_DELAY_US);
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4