๐ฏ ESP32-C6 GPIO implementation with hardware-optimized features
Overview
EspGpio is the ESP32-C6 implementation of the BaseGpio interface,
providing comprehensive GPIO functionality specifically optimized for ESP32-C6 microcontrollers
running ESP-IDF v5.5+.
It offers both basic and advanced GPIO features with hardware-specific optimizations.
Features
ESP32-C6 Optimized - Full support for ESP32-C6 GPIO capabilities
#include"inc/mcu/esp32/EspGpio.h"// Create output pin for LED (active low, strong drive)EspGpioled_pin(GPIO_NUM_2,hf_gpio_direction_t::HF_GPIO_DIRECTION_OUTPUT,hf_gpio_active_state_t::HF_GPIO_ACTIVE_LOW,hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING,hf_gpio_drive_cap_t::HF_GPIO_DRIVE_CAP_STRONG);voidsetup_led(){if(!led_pin.EnsureInitialized()){printf("Failed to initialize LED pin\n");return;}// LED starts offled_pin.SetInactive();}voidblink_led(){led_pin.SetActive();// Turn onvTaskDelay(pdMS_TO_TICKS(500));led_pin.SetInactive();// Turn offvTaskDelay(pdMS_TO_TICKS(500));}
// Global flag for button statevolatileboolbutton_pressed=false;// Interrupt callback functionvoidIRAM_ATTRbutton_isr_handler(BaseGpio*gpio,hf_gpio_interrupt_trigger_ttrigger,void*user_data){button_pressed=true;// Set flag for main loop}// Create input pin for buttonEspGpiobutton_pin(GPIO_NUM_0,hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT,hf_gpio_active_state_t::HF_GPIO_ACTIVE_LOW,hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_UP);voidsetup_button(){// Initialize pinif(!button_pin.EnsureInitialized()){printf("Failed to initialize button pin\n");return;}// Configure interrupthf_gpio_err_tresult=button_pin.ConfigureInterrupt(hf_gpio_interrupt_trigger_t::HF_GPIO_INTERRUPT_TRIGGER_FALLING_EDGE,button_isr_handler,nullptr);if(result==hf_gpio_err_t::GPIO_SUCCESS){button_pin.EnableInterrupt();printf("Button interrupt configured\n");}else{printf("Failed to configure button interrupt: %s\n",HfGpioErrToString(result));}}voidcheck_button(){if(button_pressed){button_pressed=false;// Clear flagprintf("Button was pressed!\n");// Debouncing - check if still pressed after delayvTaskDelay(pdMS_TO_TICKS(50));if(button_pin.IsActive()){printf("Button press confirmed\n");// Handle button press action}}}
// High-speed digital output for motor controlEspGpiomotor_step_pin(GPIO_NUM_4,hf_gpio_direction_t::HF_GPIO_DIRECTION_OUTPUT,hf_gpio_active_state_t::HF_GPIO_ACTIVE_HIGH,hf_gpio_output_mode_t::HF_GPIO_OUTPUT_MODE_PUSH_PULL,hf_gpio_pull_mode_t::HF_GPIO_PULL_MODE_FLOATING,hf_gpio_drive_cap_t::HF_GPIO_DRIVE_CAP_STRONGEST);voidsetup_motor_control(){motor_step_pin.EnsureInitialized();// Configure for fastest switchingmotor_step_pin.SetSlewRate(hf_gpio_slew_rate_t::HF_GPIO_SLEW_RATE_FAST);motor_step_pin.SetDriveCapability(hf_gpio_drive_cap_t::HF_GPIO_DRIVE_CAP_STRONGEST);}voidgenerate_step_pulses(intnum_steps,intdelay_us){for(inti=0;i<num_steps;i++){motor_step_pin.SetActive();esp_rom_delay_us(delay_us);motor_step_pin.SetInactive();esp_rom_delay_us(delay_us);}}
// Configure pin for deep sleep compatibilityEspGpiowake_pin(GPIO_NUM_0,hf_gpio_direction_t::HF_GPIO_DIRECTION_INPUT);wake_pin.EnsureInitialized();// Configure as wake sourceesp_sleep_enable_ext0_wakeup(GPIO_NUM_0,0);// Wake on low level
Performance Optimization
1
2
3
4
5
// For time-critical applications, cache the ESP GPIO numbergpio_num_tesp_pin=gpio_pin.GetEspGpioNum();// Use direct ESP-IDF calls for maximum speed (if needed)gpio_set_level(esp_pin,1);// Direct register access
Error Handling
All EspGpio methods return appropriate error codes from the hf_gpio_err_t enumeration.
Common ESP32-specific errors include:
GPIO_ERR_INVALID_PIN - Pin number not available on ESP32-C6
GPIO_ERR_PIN_NOT_AVAILABLE - Pin reserved for special functions