Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Esp32Gpio.hxx File Reference
#include "freertos_drivers/common/GpioWrapper.hxx"
#include "freertos_drivers/esp32/Esp32AdcOneShot.hxx"
#include "os/Gpio.hxx"
#include "utils/logging.h"
#include "utils/macros.h"
#include <driver/gpio.h>
#include <esp_rom_gpio.h>
#include <soc/gpio_struct.h>

Go to the source code of this file.

Classes

class  Esp32Gpio< PIN_NUM, INVERTED >
 Defines a GPIO output pin. More...
 
struct  GpioOutputPin< Base, SAFE_VALUE, INVERT >
 Parametric GPIO output class. More...
 
struct  GpioOutputSafeLow< Defs >
 Defines a GPIO output pin, initialized to be an output pin with low level. More...
 
struct  GpioOutputSafeLowInvert< Defs >
 Defines a GPIO output pin, initialized to be an output pin with low level. More...
 
struct  GpioOutputSafeHigh< Defs >
 Defines a GPIO output pin, initialized to be an output pin with high level. More...
 
struct  GpioOutputSafeHighInvert< Defs >
 Defines a GPIO output pin, initialized to be an output pin with high level. More...
 
struct  GpioInputPin< Base, ACTIVE_HIGH >
 Parametric GPIO input class. More...
 
struct  GpioInputNP< Defs >
 Defines a GPIO input pin. More...
 
struct  GpioInputPU< Defs >
 Defines a GPIO input pin with pull-up. More...
 
struct  GpioInputPD< Defs >
 Defines a GPIO input pin with pull-down enabled. More...
 
struct  GpioInputPUPD< Defs >
 Defines a GPIO input pin with pull-up and pull-down enabled. More...
 

Macros

#define IS_GPIO_OUTPUT(pin)
 Helper macro to test if a pin has been configured for output.
 
#define GPIO_PIN(NAME, BaseClass, NUM)
 Helper macro for defining GPIO pins on the ESP32.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Helper declarations for using GPIO pins via the ESP-IDF APIs.

Author
Mike Dunston
Date
27 March 2020

Definition in file Esp32Gpio.hxx.

Macro Definition Documentation

◆ GPIO_PIN

#define GPIO_PIN (   NAME,
  BaseClass,
  NUM 
)
Value:
struct NAME##Defs \
{ \
static const gpio_num_t PIN_NUM = (gpio_num_t)NUM; \
\
public: \
static gpio_num_t pin() \
{ \
return PIN_NUM; \
} \
}; \
typedef BaseClass<NAME##Defs> NAME##_Pin

Helper macro for defining GPIO pins on the ESP32.

Parameters
NAMEis the basename of the declaration. For NAME==FOO the macro declared FOO_Pin as a structure on which the read-write functions will be available.
BaseClassis the initialization structure, such as LedPin, or GpioOutputSafeHigh or GpioOutputSafeLow.
NUMis the pin number, such as 3 (see below for usable range).

There are multiple variations available for the ESP32: ESP32, WROVER, WROVER-B, PICO, ESP32-Solo, ESP32-S2, ESP32-C3. Each of these have slight differences in the available pins.

ESP32: Valid pin range is 0..39 with the following restrictions:

  • 0 : Bootstrap pin, pull-up resistor on most modules.
  • 1 : UART0 TX, serial console.
  • 2 : Bootstrap pin, pull-down resistor on most modules.
  • 3 : UART0 RX, serial console.
  • 4 : Pull-down resistor on most modules.
  • 5 : Bootstrap pin, pull-up resistor on most modules.
  • 6 - 11 : Used for on-board flash. If you have the PICO-D4 see the section below.
  • 12 : Bootstrap pin, pull-down resistor on most modules.
  • 15 : Bootstrap pin, pull-up resistor on most modules.
  • 24 : Does not exist.
  • 37, 38 : Not exposed on most modules and will have a capacitor connected to 36 and 39 under the metal shielding of the module. The capacitor is typically 270pF.
  • 34 - 39 : These pins are INPUT only. NOTE: ESP32 covers the ESP32-WROOM-32, DOWD, D2WD, S0WD, U4WDH and the ESP32-Solo.

ESP32-PICO-D4: Nearly the same as ESP32 but with the following differences:

  • 9, 10 : Available for use, other modules use these for the on-board flash.
  • 16, 17 : Used for flash and/or PSRAM.

ESP32-PICO-V3: Nearly the same as ESP32-PICO-D4 with the following differences:

  • 7, 8 : Available for use, however validation checks will prevent usage due to no compile time constant available to uniquely identify this variant.
  • 20 : Available for use.
  • 16 - 18, 23: Not available, however validations checks will not prevent usage of GPIO 18 or 23 due to no compile time constant available to uniquely identify this variant.

ESP32-WROVER and WROVER-B: Nearly the same as ESP32 but with the following differences:

  • 16, 17 : Reserved for PSRAM on WROVER/WROVER-B modules.

ESP32-S2: Valid pin range is 0..46 with the following notes:

  • 0 : Bootstrap pin, pull-up resistor on most modules.
  • 18 : Most modules have an RGB LED on this pin.
  • 19 : USB OTG D-, available to use if not using this functionality.
  • 20 : USB OTG D+, available to use if not using this functionality.
  • 22 - 25 : Do not exist.
  • 26 - 32 : Used for on-board flash and/or PSRAM.
  • 43 : UART0 TX, serial console.
  • 44 : UART0 RX, serial console.
  • 45 : Bootstrap pin, pull-down resistor on most modules. Note: ESP32-S2-Kaluga-1 modules have an RGB LED on this pin that can be enabled via a jumper.
  • 46 : Bootstrap pin, pull-down resistor on most modules, INPUT only.

ESP32-C3: Valid pin range is 0..21 with the following notes:

  • 8 : Bootstrap pin, most modules have an RGB LED on this pin.
  • 9 : Bootstrap pin, connected to built-in pull-up resistor, may also have an external pull-up resistor.
  • 11 - 17 : Used for flash and/or PSRAM.
  • 18 : USB CDC-ACM D- / JTAG, available to use if not using this functionality.
  • 19 : USB CDC ACM D+ / JTAG, available to use if not using this functionality.
  • 20 : UART0 RX, serial console.
  • 21 : UART0 TX, serial console.

ESP8685: This is an ESP32-C3 in a smaller package without SPI pins exposed.

ESP32-S3: Valid pin range is 0..48 with the following notes:

  • 0 : Bootstrap pin, pull-up resistor on most modules.
  • 18 : Most modules have an RGB LED on this pin.
  • 19 : USB OTG D-, available to use if not using this functionality.
  • 20 : USB OTG D+, available to use if not using this functionality.
  • 22 - 25 : Do not exist.
  • 26 - 32 : Used for on-board flash and/or PSRAM.
  • 33 - 37 : Used for on-board flash and/or PSRAM if Octal SPI mode is enabled.
  • 39 - 42 : JTAG interface (pins in order: MTCK, MTDO, MTDI, MTMS)
  • 43 : UART0 TX, serial console.
  • 44 : UART0 RX, serial console.
  • 45 : Bootstrap pin, pull-down resistor on most modules.
  • 46 : Bootstrap pin, pull-down resistor on most modules.
  • 48 : Most modules have an RGB LED on this pin.

Pins marked as having a pull-up or pull-down resistor are typically 10kOhm.

Any pins marked as a bootstrap pin can alter the behavior of the ESP32 if they are not in the default/expected state on startup. Consult the schematic and/or datasheet for more details.

The built in pull-up/pull-down resistor for all ESP32 variants are typically 45kOhm.

SoC datasheet references: ESP32: https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf ESP32-WROVER: https://www.espressif.com/sites/default/files/documentation/esp32-wrover_datasheet_en.pdf ESP32-WROVER-B: https://www.espressif.com/sites/default/files/documentation/esp32-wrover-b_datasheet_en.pdf ESP32-PICO-D4: https://www.espressif.com/sites/default/files/documentation/esp32-pico-d4_datasheet_en.pdf ESP32-PICO-V3: https://www.espressif.com/sites/default/files/documentation/esp32-pico-v3_datasheet_en.pdf ESP32-S2: https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf ESP32-S2-WROVER: https://www.espressif.com/sites/default/files/documentation/esp32-s2-wrover_esp32-s2-wrover-i_datasheet_en.pdf ESP32-C3: https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf ESP32-S3: https://www.espressif.com/sites/default/files/documentation/esp32-s3_datasheet_en.pdf ESP8685: https://www.espressif.com/sites/default/files/documentation/esp8685_datasheet_en.pdf

SoC technical references: ESP32: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf ESP32-S2: https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf ESP32-C3: https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf ESP32-S3: https://www.espressif.com/sites/default/files/documentation/esp32-s3_technical_reference_manual_en.pdf

Module schematic references: DevKitC v4: https://dl.espressif.com/dl/schematics/esp32_devkitc_v4-sch-20180607a.pdf DevKitC v2: https://dl.espressif.com/dl/schematics/ESP32-Core-Board-V2_sch.pdf WROVER KIT v4.1: https://dl.espressif.com/dl/schematics/ESP-WROVER-KIT_V4_1.pdf WROVER KIT v3: https://dl.espressif.com/dl/schematics/ESP-WROVER-KIT_SCH-3.pdf WROVER KIT v2: https://dl.espressif.com/dl/schematics/ESP-WROVER-KIT_SCH-2.pdf WROVER KIT v1: https://dl.espressif.com/dl/schematics/ESP32-DevKitJ-v1_sch.pdf PICO KIT v4.1: https://dl.espressif.com/dl/schematics/esp32-pico-kit-v4.1_schematic.pdf PICO KIT v3: https://dl.espressif.com/dl/schematics/esp32-pico-kit-v3_schematic.pdf ESP32-S2-Saola-1: https://dl.espressif.com/dl/schematics/ESP32-S2-SAOLA-1_V1.1_schematics.pdf ESP32-S2-Kaluga-1: https://dl.espressif.com/dl/schematics/ESP32-S2-Kaluga-1_V1_3_SCH_20200526A.pdf ESP32-C3-DevKitM-1: https://dl.espressif.com/dl/schematics/SCH_ESP32-C3-DEVKITM-1_V1_20200915A.pdf

NOTE: The WROVER KIT v1 is also known as DevKitJ and is RED colored PCB that supports both WROVER and WROOM-32 modules.

Example: GPIO_PIN(FOO, GpioOutputSafeLow, 3); ... FOO_Pin::set(true);

Definition at line 558 of file Esp32Gpio.hxx.

◆ IS_GPIO_OUTPUT

#define IS_GPIO_OUTPUT (   pin)
Value:
(GPIO_IS_VALID_OUTPUT_GPIO(pin) && \
(pin < 32) ? GPIO.enable & BIT(pin & 31) : \
GPIO.enable1.data & BIT(pin & 31))

Helper macro to test if a pin has been configured for output.

This is necessary since ESP-IDF does not expose gpio_get_direction(pin).

Definition at line 59 of file Esp32Gpio.hxx.