periph_cpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Gunar Schorcht
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include "sdkconfig.h"
24 #include "hal/ledc_types.h"
25 #include "hal/spi_types.h"
26 #include "soc/ledc_struct.h"
27 #include "soc/periph_defs.h"
28 #include "soc/soc_caps.h"
29 
30 #include "modules.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
39 #define STDIO_UART_DEV CONFIG_ESP_CONSOLE_UART_NUM
40 
45 #define PROVIDES_PM_SET_LOWEST
46 #define PROVIDES_PM_RESTART
47 #define PROVIDES_PM_OFF
48 #define PROVIDES_PM_LAYERED_OFF
49 
53 #define PM_NUM_MODES (3U)
54 
59 #define ESP_PM_MODEM_SLEEP (2U)
60 #define ESP_PM_LIGHT_SLEEP (1U)
61 #define ESP_PM_DEEP_SLEEP (0U)
69 #define CPUID_LEN (6U)
70 
82 #define HAVE_GPIO_T
83 typedef unsigned int gpio_t;
90 #define GPIO_UNDEF (0xffffffff)
97 #define GPIO_PIN(x, y) ((x << 5) | y)
104 #define PORT_GPIO (0)
111 #define GPIO_PIN_NUMOF (SOC_GPIO_PIN_COUNT)
114 #ifndef DOXYGEN
120 #define HAVE_GPIO_FLANK_T
121 typedef enum {
122  GPIO_NONE,
123  GPIO_RISING,
124  GPIO_FALLING,
125  GPIO_BOTH,
126  GPIO_LOW,
127  GPIO_HIGH
128 } gpio_flank_t;
136 #define HAVE_GPIO_MODE_T
137 typedef enum {
138  GPIO_IN,
139  GPIO_IN_PD,
140  GPIO_IN_PU,
141  GPIO_OUT,
142  GPIO_OD,
143  GPIO_OD_PU,
144  GPIO_IN_OUT,
145  GPIO_IN_OD,
146  GPIO_IN_OD_PU
147 } gpio_mode_t;
150 /* BEGIN: GPIO LL overwrites */
151 
152 #if SOC_GPIO_PIN_COUNT > 32
153 
154 #define GPIO_PORT_NUMOF 2
155 #define GPIO_PORT_0 0
156 #define GPIO_PORT_1 1
157 #define GPIO_PORT_0_PIN_NUMOF (32)
158 #define GPIO_PORT_1_PIN_NUMOF (SOC_GPIO_PIN_COUNT - 32)
159 #define GPIO_PORT_PIN_NUMOF(p) ((p == GPIO_PORT_0) ? GPIO_PORT_0_PIN_NUMOF \
160  : GPIO_PORT_1_PIN_NUMOF)
161 #else
162 
163 #define GPIO_PORT_NUMOF 1
164 #define GPIO_PORT_0 0
165 #define GPIO_PORT_0_PIN_NUMOF (SOC_GPIO_PIN_COUNT)
166 #define GPIO_PORT_PIN_NUMOF(p) ((p == GPIO_PORT_0) ? GPIO_PORT_0_PIN_NUMOF : 0)
167 
168 #endif
169 
170 #define HAVE_GPIO_PORT_T
171 typedef uintptr_t gpio_port_t;
172 
173 #define HAVE_GPIO_SLEW_T
174 typedef enum {
175  GPIO_SLEW_SLOWEST = 0,
176  GPIO_SLEW_SLOW = 0,
177  GPIO_SLEW_FAST = 0,
178  GPIO_SLEW_FASTEST = 0,
179 } gpio_slew_t;
180 
181 #define HAVE_GPIO_PULL_STRENGTH_T
182 typedef enum {
183  GPIO_PULL_WEAKEST = 0,
184  GPIO_PULL_WEAK = 0,
185  GPIO_PULL_STRONG = 0,
188 
189 /*
190  * This include is placed here by intention to avoid type name conflicts.
191  * Having the macros HAVE_GPIO_* defined before including this file allows to
192  * use these macros in `hal/gpio_types.h` to decide whether to use the
193  * ESP-IDF types when compiling ESP-IDF modules or to use the RIOT types
194  * when compiling RIOT source code.
195  */
196 #include "hal/gpio_types.h"
197 
198 #define HAVE_GPIO_PULL_T
199 typedef gpio_pull_mode_t gpio_pull_t;
200 #define GPIO_PULL_UP GPIO_PULLUP_ONLY
201 #define GPIO_PULL_DOWN GPIO_PULLDOWN_ONLY
202 #define GPIO_PULL_KEEP GPIO_PULLUP_PULLDOWN
203 
207 #define HAVE_GPIO_DRIVE_STRENGTH_T
208 typedef enum {
209  GPIO_DRIVE_WEAKEST = 0,
210  GPIO_DRIVE_WEAK = 1,
211  GPIO_DRIVE_STRONG = 2,
214 
215 /*
216  * @brief Map former enumeration values the new enumeration values for compatibility.
217  */
218 #define GPIO_DRIVE_5 GPIO_DRIVE_WEAKEST
219 #define GPIO_DRIVE_10 GPIO_DRIVE_WEAK
220 #define GPIO_DRIVE_20 GPIO_DRIVE_STRONG
221 #define GPIO_DRIVE_30 GPIO_DRIVE_STRONGEST
223 #define HAVE_GPIO_STATE_T
224 typedef enum {
228  GPIO_INPUT,
231 } gpio_state_t;
232 
233 #define HAVE_GPIO_CONF_T
234 typedef union gpio_conf_esp32 gpio_conf_t;
235 
236 #endif /* ndef DOXYGEN */
237 
243  uint8_t bits;
244  struct {
276  bool initial_value : 1;
277  };
278 };
279 
280 /* END: GPIO LL overwrites */
347 #define ADC_NUMOF_MAX (SOC_ADC_CHANNEL_NUM(0) + SOC_ADC_CHANNEL_NUM(1))
348 
390 #if defined(SOC_DAC_SUPPORTED) || DOXYGEN
391 #define DAC_NUMOF_MAX (SOC_DAC_PERIPH_NUM)
392 #endif
393 
442 #ifndef DOXYGEN
449 #define HAVE_I2C_SPEED_T
450 typedef enum {
451  I2C_SPEED_LOW = 0,
456 } i2c_speed_t;
458 #endif /* ndef DOXYGEN */
459 
463 typedef struct {
464  uint8_t module;
465  i2c_speed_t speed;
466  gpio_t scl;
467  gpio_t sda;
468  bool scl_pullup;
469  bool sda_pullup;
470 } i2c_conf_t;
471 
475 #define I2C_NUMOF_MAX (SOC_I2C_NUM)
476 
477 #define PERIPH_I2C_NEED_READ_REG
478 #define PERIPH_I2C_NEED_READ_REGS
479 #define PERIPH_I2C_NEED_WRITE_REG
480 #define PERIPH_I2C_NEED_WRITE_REGS
572 typedef struct {
573  uint8_t module;
574  ledc_mode_t group;
575  ledc_timer_t timer;
576  uint8_t ch_numof;
577  const gpio_t *gpios;
578 } pwm_config_t;
579 
583 #define PWM_NUMOF_MAX (4)
584 
588 #define PWM_CH_NUMOF_MAX (SOC_LEDC_CHANNEL_NUM)
589 
612 typedef struct {
613  uint8_t channel;
614  gpio_t gpio;
616 
622 #define RMT_CH_NUMOF_MAX (SOC_RMT_CHANNELS_PER_GROUP)
623 
634 #define RNG_DATA_REG_ADDR (WDEV_RND_REG)
649 #define RTT_FREQUENCY (32768UL)
650 
654 #define RTT_MAX_VALUE (0xFFFFFFFFUL)
655 
682 typedef enum {
683 #if IS_USED(SOC_SDMMC_USE_GPIO_MATRIX) || DOXYGEN
685 #endif
687 } sdmmc_slot_t;
688 
700 typedef struct {
702  gpio_t cd;
703  gpio_t wp;
704 #if IS_USED(SOC_SDMMC_USE_GPIO_MATRIX) || DOXYGEN
705  gpio_t clk;
706  gpio_t cmd;
707  gpio_t dat0;
708  gpio_t dat1;
709  gpio_t dat2;
710  gpio_t dat3;
711 #if IS_USED(MODULE_PERIPH_SMMC_8BIT) || DOXYGEN
712  gpio_t dat4;
713  gpio_t dat5;
714  gpio_t dat6;
715  gpio_t dat7;
716 #endif /* IS_USED(MODULE_PERIPH_SMMC_8BIT) */
717 #else /* IS_USED(SOC_SDMMC_USE_IOMUX) */
718  uint8_t bus_width;
719 #endif
720 } sdmmc_conf_t;
721 
725 #define SDMMC_CPU_DMA_REQUIREMENTS __attribute__((aligned(SDMMC_CPU_DMA_ALIGNMENT)))
726 
730 #define SDMMC_CPU_DMA_ALIGNMENT 4
731 
809 #ifndef DOXYGEN
814 #define HAVE_SPI_CLK_T
815 typedef enum {
816  SPI_CLK_100KHZ = 100000,
817  SPI_CLK_400KHZ = 400000,
818  SPI_CLK_1MHZ = 1000000,
819  SPI_CLK_5MHZ = 5000000,
820  SPI_CLK_10MHZ = 10000000
821 } spi_clk_t;
828 #define spi_pin_mosi(dev) spi_config[dev].mosi
829 #define spi_pin_miso(dev) spi_config[dev].miso
830 #define spi_pin_clk(dev) spi_config[dev].sck
833 #endif /* !DOXYGEN */
834 
838 typedef spi_host_device_t spi_ctrl_t;
839 
840 /*
841  * In former ESP-IDF versions, SPI interfaces were identified by the alias
842  * names `FSPI`, `HSPI` and `VSPI`, which are sometimes also used in data
843  * sheets. These alias names have been declared obsolete in ESP-IDF. For
844  * source code compatibility reasons these alias names are defined here.
845  */
846 #if defined(CPU_FAM_ESP32)
847 #define HSPI SPI2_HOST
848 #define VSPI SPI3_HOST
849 #elif defined(CPU_FAM_ESP32S2)
850 #define FSPI SPI2_HOST
851 #define HSPI SPI3_HOST
852 #else
853 #define FSPI SPI2_HOST
854 #endif
855 
859 typedef struct {
861  gpio_t sck;
862  gpio_t mosi;
863  gpio_t miso;
864  gpio_t cs;
865 } spi_conf_t;
866 
870 #define SPI_NUMOF_MAX (SOC_SPI_PERIPH_NUM - 1)
871 
872 #define PERIPH_SPI_NEEDS_TRANSFER_BYTE
873 #define PERIPH_SPI_NEEDS_TRANSFER_REG
874 #define PERIPH_SPI_NEEDS_TRANSFER_REGS
902 #ifndef MODULE_ESP_HW_COUNTER
909 #define TIMER_NUMOF (SOC_TIMER_GROUP_TOTAL_TIMERS - 1)
910 #define TIMER_CHANNEL_NUMOF (1)
911 #endif
912 
914 #define TIMER_SYSTEM_GROUP 0 /* formerly TIMER_GROUP_0 */
916 #define TIMER_SYSTEM_INDEX 0 /* formerly TIMER_0 */
918 #define TIMER_SYSTEM_INT_SRC ETS_TG0_T0_LEVEL_INTR_SOURCE
919 
925 #define PERIPH_TIMER_PROVIDES_SET
926 
972 typedef struct {
973  gpio_t txd;
974  gpio_t rxd;
975 } uart_conf_t;
976 
977 #ifndef DOXYGEN
981 typedef enum {
982  UART_STOP_BITS_1 = 0x1,
983  UART_STOP_BITS_1_5 = 0x2,
984  UART_STOP_BITS_2 = 0x3,
986 
987 #define HAVE_UART_STOP_BITS_T
988 
992 #define UART_MODE_UNSUPPORTED 0xf0
993 
997 typedef enum {
998  UART_PARITY_NONE = 0x0,
999  UART_PARITY_EVEN = 0x2,
1000  UART_PARITY_ODD = 0x3,
1001  UART_PARITY_MARK = UART_MODE_UNSUPPORTED | 0,
1002  UART_PARITY_SPACE = UART_MODE_UNSUPPORTED | 1,
1003 } uart_parity_t;
1004 
1005 #define UART_PARITY_DISABLE UART_PARITY_NONE
1006 #define HAVE_UART_PARITY_T
1007 
1008 #endif /* !DOXYGEN */
1009 
1013 #define UART_NUMOF_MAX (SOC_UART_NUM)
1023 #include "usbdev_synopsys_dwc2.h"
1024 
1028 #define USBDEV_NUMOF_MAX (SOC_USB_PERIPH_NUM)
1031 #ifdef __cplusplus
1032 }
1033 #endif
1034 
1038 #if defined(CPU_FAM_ESP32)
1039 #include "periph_cpu_esp32.h"
1040 #elif defined(CPU_FAM_ESP32C3)
1041 #include "periph_cpu_esp32c3.h"
1042 #elif defined(CPU_FAM_ESP32S2)
1043 #include "periph_cpu_esp32s2.h"
1044 #elif defined(CPU_FAM_ESP32S3)
1045 #include "periph_cpu_esp32s3.h"
1046 #else
1047 #error "ESP32x family implementation missing"
1048 #endif
1049 
1050 #ifdef MODULE_PERIPH_CAN
1051 #include "can_esp.h"
1052 #endif
1053 
gpio_flank_t
Definition: periph_cpu.h:179
@ GPIO_OUT
select GPIO MASK as output
Definition: periph_cpu.h:164
@ GPIO_IN
select GPIO MASK as input
Definition: periph_cpu.h:163
i2c_speed_t
Definition: periph_cpu.h:275
spi_clk_t
Definition: periph_cpu.h:351
sdmmc_slot_t
SDIO/SDMMC slots.
Definition: periph_cpu.h:682
@ SDMMC_SLOT_1
SD/MMC host controller slot 1.
Definition: periph_cpu.h:686
@ SDMMC_SLOT_0
SD/MMC host controller slot 0 (not usable on ESP32 variant)
Definition: periph_cpu.h:684
spi_ctrl_t
SPI controllers that can be used for peripheral interfaces.
Definition: periph_cpu.h:264
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
gpio_pull_t
Enumeration of pull resistor configurations.
Definition: gpio_ll.h:257
gpio_pull_strength_t
Enumeration of pull resistor values.
Definition: gpio_ll.h:275
gpio_state_t
Enumeration of GPIO states (direction)
Definition: gpio_ll.h:165
gpio_slew_t
Enumeration of slew rate settings.
Definition: gpio_ll.h:339
gpio_drive_strength_t
Enumeration of drive strength options.
Definition: gpio_ll.h:306
typedef gpio_conf_t
GPIO pin configuration.
Definition: gpio_ll.h:423
uintptr_t gpio_port_t
GPIO port type.
Definition: gpio_ll.h:95
@ GPIO_PULL_WEAKEST
Use the weakest (highest Ohm value) resistor.
Definition: gpio_ll.h:276
@ GPIO_PULL_WEAK
Use a weak pull resistor.
Definition: gpio_ll.h:277
@ GPIO_PULL_STRONG
Use a strong pull resistor.
Definition: gpio_ll.h:278
@ GPIO_PULL_STRONGEST
Use the strongest pull resistor.
Definition: gpio_ll.h:279
@ GPIO_OUTPUT_OPEN_SOURCE
Use pin as output in open emitter configuration.
Definition: gpio_ll.h:202
@ GPIO_USED_BY_PERIPHERAL
The GPIO pin is used by a peripheral.
Definition: gpio_ll.h:221
@ GPIO_OUTPUT_OPEN_DRAIN
Use pin as output in open collector configuration.
Definition: gpio_ll.h:189
@ GPIO_OUTPUT_PUSH_PULL
Use pin as output in push-pull configuration.
Definition: gpio_ll.h:176
@ GPIO_DISCONNECT
Disconnect pin from all peripherals.
Definition: gpio_ll.h:249
@ GPIO_INPUT
Use pin as input.
Definition: gpio_ll.h:208
@ GPIO_SLEW_SLOWEST
let the output voltage level rise/fall as slow as possible
Definition: gpio_ll.h:340
@ GPIO_SLEW_FAST
let the output voltage level rise/fall fast
Definition: gpio_ll.h:343
@ GPIO_SLEW_SLOW
let the output voltage level rise/fall slowly
Definition: gpio_ll.h:342
@ GPIO_SLEW_FASTEST
let the output voltage level rise/fall as fast as possible
Definition: gpio_ll.h:344
@ GPIO_DRIVE_STRONG
Use a strong drive strength.
Definition: gpio_ll.h:309
@ GPIO_DRIVE_WEAK
Use a weak drive strength.
Definition: gpio_ll.h:308
@ GPIO_DRIVE_STRONGEST
Use the strongest drive strength.
Definition: gpio_ll.h:310
@ GPIO_DRIVE_WEAKEST
Use the weakest drive strength.
Definition: gpio_ll.h:307
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:91
@ GPIO_FALLING
emit interrupt on falling flank
Definition: periph_cpu.h:109
@ GPIO_RISING
emit interrupt on rising flank
Definition: periph_cpu.h:110
@ GPIO_BOTH
emit interrupt on both flanks
Definition: periph_cpu.h:111
@ GPIO_OD
configure as output in open-drain mode without pull resistor
Definition: gpio.h:123
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:125
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:120
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: periph_cpu.h:277
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: periph_cpu.h:279
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: periph_cpu.h:276
@ I2C_SPEED_HIGH
high speed mode: ~3400 kbit/s
Definition: periph_cpu.h:281
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:278
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: periph_cpu.h:356
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: periph_cpu.h:355
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: periph_cpu.h:353
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: periph_cpu.h:354
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: periph_cpu.h:352
@ UART_PARITY_SPACE
space parity
Definition: periph_cpu.h:506
@ UART_PARITY_NONE
no parity
Definition: periph_cpu.h:502
@ UART_PARITY_EVEN
even parity
Definition: periph_cpu.h:503
@ UART_PARITY_ODD
odd parity
Definition: periph_cpu.h:504
@ UART_PARITY_MARK
mark parity
Definition: periph_cpu.h:505
@ UART_STOP_BITS_2
2 stop bits
Definition: periph_cpu.h:535
@ UART_STOP_BITS_1
1 stop bit
Definition: periph_cpu.h:534
Common macros and compiler attributes/pragmas configuration.
ESP32 specific peripheral configuration.
ESP32-C3 specific peripheral configuration.
ESP32-S2 specific peripheral configuration.
ESP32-S3 specific peripheral configuration.
RIOT-OS modification of the bootloader SDK configuration.
uart_parity_t
Definition of possible parity modes.
Definition: periph_cpu.h:501
uart_stop_bits_t
Definition of possible stop bits lengths.
Definition: periph_cpu.h:533
I2C configuration structure.
Definition: periph_cpu.h:298
bool scl_pullup
Pullup enabled for SCL pin.
Definition: periph_cpu.h:468
bool sda_pullup
Pullup enabled for SDA pin.
Definition: periph_cpu.h:469
gpio_t sda
GPIO used as SDA pin.
Definition: periph_cpu.h:467
gpio_t scl
GPIO used as SCL pin.
Definition: periph_cpu.h:466
uint8_t module
I2C module identifier.
Definition: periph_cpu.h:464
PWM configuration structure type.
Definition: periph_cpu.h:572
ledc_mode_t group
LEDC channel group used (low/high speed)
Definition: periph_cpu.h:574
ledc_timer_t timer
LEDC timer used by this device.
Definition: periph_cpu.h:575
uint8_t module
LEDC module identifier.
Definition: periph_cpu.h:573
uint8_t ch_numof
Number of channels used by this device.
Definition: periph_cpu.h:576
const gpio_t * gpios
GPIOs used as channels of this device.
Definition: periph_cpu.h:577
RMT channel configuration.
Definition: periph_cpu.h:612
uint8_t channel
channel index
Definition: periph_cpu.h:613
gpio_t gpio
GPIO used as RMT channel.
Definition: periph_cpu.h:614
SDMMC slot configuration.
Definition: periph_cpu.h:700
gpio_t wp
Write Protect pin (must be GPIO_UNDEF if not connected)
Definition: periph_cpu.h:703
gpio_t dat1
DAT[1] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:708
gpio_t dat3
DAT[3] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:710
gpio_t clk
CLK pin (must be defined)
Definition: periph_cpu.h:705
gpio_t dat0
DAT[0] pin (must be defined)
Definition: periph_cpu.h:707
gpio_t dat4
DAT[4] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:712
gpio_t cmd
CMD pin (must be defined)
Definition: periph_cpu.h:706
gpio_t dat6
DAT[6] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:714
gpio_t dat7
DAT[7] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:715
gpio_t dat2
DAT[2] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:709
gpio_t dat5
DAT[5] pin (GPIO_UNDEF if not connected)
Definition: periph_cpu.h:713
sdmmc_slot_t slot
SDMMC slot used [ SDMMC_SLOT_0 | SDMMC_SLOT_1].
Definition: periph_cpu.h:701
gpio_t cd
Card Detect pin (must be GPIO_UNDEF if not connected)
Definition: periph_cpu.h:702
SPI device configuration.
Definition: periph_cpu.h:336
gpio_t mosi
GPIO used as MOSI pin.
Definition: periph_cpu.h:862
spi_ctrl_t ctrl
SPI controller used for the interface.
Definition: periph_cpu.h:860
gpio_t cs
GPIO used as CS0 pin.
Definition: periph_cpu.h:864
gpio_t miso
GPIO used as MISO pin.
Definition: periph_cpu.h:863
gpio_t sck
GPIO used as SCK pin.
Definition: periph_cpu.h:861
UART device configuration.
Definition: periph_cpu.h:217
gpio_t txd
GPIO used as TxD pin.
Definition: periph_cpu.h:973
gpio_t rxd
GPIO used as RxD pin.
Definition: periph_cpu.h:974
GPIO pin configuration for ESP32/ESP32Cx/ESP32Sx MCUs.
Definition: periph_cpu.h:242
bool initial_value
Initial value of the output.
Definition: periph_cpu.h:276
gpio_state_t state
State of the pin.
Definition: periph_cpu.h:248
gpio_drive_strength_t drive_strength
Drive strength of the GPIO.
Definition: periph_cpu.h:262
gpio_pull_t pull
Pull resistor configuration.
Definition: periph_cpu.h:252
uint8_t bits
the raw bits
Definition: periph_cpu.h:243
Low level USB FS/HS driver definitions for MCUs with Synopsys DWC2 IP core.