diff --git a/boards/common/esp32/include/periph_conf_common.h b/boards/common/esp32/include/periph_conf_common.h index c1d2bcb2c4..d6595b369c 100644 --- a/boards/common/esp32/include/periph_conf_common.h +++ b/boards/common/esp32/include/periph_conf_common.h @@ -46,6 +46,11 @@ extern "C" { #define ADC_GPIOS { } #endif +/** + * @brief Static array with declared ADC channels + */ +static const gpio_t adc_channels[] = ADC_GPIOS; + /** * @brief Number of GPIOs declared as ADC channels * @@ -54,7 +59,7 @@ extern "C" { * * @note ADC_NUMOF definition must not be changed. */ -#define ADC_NUMOF (adc_chn_num) +#define ADC_NUMOF (sizeof(adc_channels) / sizeof(adc_channels[0])) /** @} */ /** diff --git a/boards/esp32-olimex-evb/Makefile.features b/boards/esp32-olimex-evb/Makefile.features index 1ae2939448..1598352c10 100644 --- a/boards/esp32-olimex-evb/Makefile.features +++ b/boards/esp32-olimex-evb/Makefile.features @@ -2,7 +2,9 @@ include $(RIOTBOARD)/common/esp32/Makefile.features # additional features provided by the board (no ADC and no DAC) -FEATURES_PROVIDED += periph_adc +ifneq (,$(filter olimex_esp32_gateway,$(USEMODULE))) + FEATURES_PROVIDED += periph_adc +endif FEATURES_PROVIDED += periph_dac FEATURES_PROVIDED += periph_i2c FEATURES_PROVIDED += periph_pwm diff --git a/cpu/esp32/include/periph_cpu.h b/cpu/esp32/include/periph_cpu.h index 61262de422..fea86d734f 100644 --- a/cpu/esp32/include/periph_cpu.h +++ b/cpu/esp32/include/periph_cpu.h @@ -40,20 +40,83 @@ extern "C" { #define CPUID_LEN (7U) /** - * @brief Available ports on the ESP32 + * @name GPIO configuration * @{ */ -#define PORT_GPIO 0 /**< port GPIO */ + +/** + * @brief Override the default gpio_t type definition + * + * This is required here to have gpio_t defined in this file. + * @{ + */ +#define HAVE_GPIO_T +typedef unsigned int gpio_t; +/** @} */ + +/** + * @brief Definition of a fitting UNDEF value + * @{ + */ +#define GPIO_UNDEF (0xffffffff) +/** @} */ + +/** + * @brief Define a CPU specific GPIO pin generator macro + * @{ + */ +#define GPIO_PIN(x, y) ((x & 0) | y) +/** @} */ + +/** + * @brief Available GPIO ports on ESP32 + * @{ + */ +#define PORT_GPIO (0) /** @} */ /** * @brief Define CPU specific number of GPIO pins * @{ */ -#define GPIO_PIN_NUMOF 40 -#ifndef GPIO_PIN_COUNT -#define GPIO_PIN_COUNT GPIO_PIN_NUMOF -#endif +#define GPIO_PIN_NUMOF (40) +/** @} */ + +/** + * @brief Override mode flank selection values + * + * @{ + */ +#define HAVE_GPIO_FLANK_T +typedef enum { + GPIO_NONE = 0, + GPIO_RISING = 1, /**< emit interrupt on rising flank */ + GPIO_FALLING = 2, /**< emit interrupt on falling flank */ + GPIO_BOTH = 3, /**< emit interrupt on both flanks */ + GPIO_LOW = 4, /**< emit interrupt on low level */ + GPIO_HIGH = 5 /**< emit interrupt on low level */ +} gpio_flank_t; + +/** @} */ + +/** + * @brief Override GPIO modes + * + * @{ + */ +#define HAVE_GPIO_MODE_T +typedef enum { + GPIO_IN, /**< input */ + GPIO_IN_PD, /**< input with pull-down */ + GPIO_IN_PU, /**< input with pull-up */ + GPIO_OUT, /**< output */ + GPIO_OD, /**< open-drain output */ + GPIO_OD_PU, /**< open-drain output with pull-up */ + GPIO_IN_OUT, /**< input and output */ + GPIO_IN_OD, /**< input and open-drain output */ + GPIO_IN_OD_PU /**< input and open-drain output */ +} gpio_mode_t; +/** @} */ /** @} */ /** @@ -100,42 +163,6 @@ extern "C" { #define GPIO39 (GPIO_PIN(PORT_GPIO,39)) /** @} */ -/** - * @brief Override mode flank selection values - * - * @{ - */ -#define HAVE_GPIO_FLANK_T -typedef enum { - GPIO_NONE = 0, - GPIO_RISING = 1, /**< emit interrupt on rising flank */ - GPIO_FALLING = 2, /**< emit interrupt on falling flank */ - GPIO_BOTH = 3, /**< emit interrupt on both flanks */ - GPIO_LOW = 4, /**< emit interrupt on low level */ - GPIO_HIGH = 5 /**< emit interrupt on low level */ -} gpio_flank_t; - -/** @} */ - -/** - * @brief Override GPIO modes - * - * @{ - */ -#define HAVE_GPIO_MODE_T -typedef enum { - GPIO_IN, /**< input */ - GPIO_IN_PD, /**< input with pull-down */ - GPIO_IN_PU, /**< input with pull-up */ - GPIO_OUT, /**< output */ - GPIO_OD, /**< open-drain output */ - GPIO_OD_PU, /**< open-drain output with pull-up */ - GPIO_IN_OUT, /**< input and output */ - GPIO_IN_OD, /**< input and open-drain output */ - GPIO_IN_OD_PU /**< input and open-drain output */ -} gpio_mode_t; -/** @} */ - /** * @name ADC configuration * @@ -216,9 +243,6 @@ typedef enum { */ #define ADC_NUMOF_MAX 16 -/** Number of ADC channels determined from ADC_GPIOS */ -extern const unsigned adc_chn_num; - /** @} */ /** diff --git a/cpu/esp32/periph/adc.c b/cpu/esp32/periph/adc.c index 7554a8a3ac..e511fe54e6 100644 --- a/cpu/esp32/periph/adc.c +++ b/cpu/esp32/periph/adc.c @@ -35,12 +35,6 @@ #define ENABLE_DEBUG (0) #include "debug.h" -/** Map of RIOT ADC to GPIOs */ -static const uint32_t adc_pins[] = ADC_GPIOS; - -/** number of ADC channels */ -const unsigned adc_chn_num = (sizeof(adc_pins) / sizeof(adc_pins[0])); - /* declaration of external functions */ extern void _adc1_ctrl_init(void); extern void _adc2_ctrl_init(void); @@ -55,7 +49,7 @@ extern const gpio_t _gpio_rtcio_map[]; int adc_init(adc_t line) { - CHECK_PARAM_RET (line < adc_chn_num, -1) + CHECK_PARAM_RET (line < ADC_NUMOF, -1) if (!_adc_module_initialized) { /* do some configuration checks */ @@ -66,7 +60,7 @@ int adc_init(adc_t line) _adc_module_initialized = true; } - uint8_t rtcio = _gpio_rtcio_map[adc_pins[line]]; + uint8_t rtcio = _gpio_rtcio_map[adc_channels[line]]; if (_adc_hw[rtcio].adc_ctrl == ADC1_CTRL) { _adc1_ctrl_init(); @@ -158,10 +152,10 @@ int adc_init(adc_t line) int adc_sample(adc_t line, adc_res_t res) { - CHECK_PARAM_RET (line < adc_chn_num, -1) + CHECK_PARAM_RET (line < ADC_NUMOF, -1) CHECK_PARAM_RET (res <= ADC_RES_12BIT, -1) - uint8_t rtcio = _gpio_rtcio_map[adc_pins[line]]; + uint8_t rtcio = _gpio_rtcio_map[adc_channels[line]]; if (_adc_hw[rtcio].adc_ctrl == ADC1_CTRL) { /* set the resolution for the measurement */ @@ -202,9 +196,9 @@ int adc_sample(adc_t line, adc_res_t res) int adc_set_attenuation(adc_t line, adc_attenuation_t atten) { - CHECK_PARAM_RET (line < adc_chn_num, -1) + CHECK_PARAM_RET (line < ADC_NUMOF, -1) - uint8_t rtcio = _gpio_rtcio_map[adc_pins[line]]; + uint8_t rtcio = _gpio_rtcio_map[adc_channels[line]]; if (_adc_hw[rtcio].adc_ctrl == ADC1_CTRL) { SENS.sar_atten1 &= ~(0x3 << (_adc_hw[rtcio].adc_channel << 1)); @@ -221,8 +215,8 @@ int adc_vref_to_gpio25 (void) { /* determine ADC line for GPIO25 */ adc_t line = ADC_UNDEF; - for (unsigned i = 0; i < adc_chn_num; i++) { \ - if (adc_pins[i] == GPIO25) { \ + for (unsigned i = 0; i < ADC_NUMOF; i++) { \ + if (adc_channels[i] == GPIO25) { \ line = i; break; } @@ -235,7 +229,7 @@ int adc_vref_to_gpio25 (void) if (adc_init(line) == 0) { - uint8_t rtcio = _gpio_rtcio_map[adc_pins[line]]; + uint8_t rtcio = _gpio_rtcio_map[adc_channels[line]]; RTCCNTL.bias_conf.dbg_atten = 0; RTCCNTL.test_mux.dtest_rtc = 1; RTCCNTL.test_mux.ent_rtc = 1; @@ -252,10 +246,10 @@ int adc_vref_to_gpio25 (void) static bool _adc_conf_check(void) { - for (unsigned i = 0; i < adc_chn_num; i++) { - if (_gpio_rtcio_map[adc_pins[i]] == RTCIO_NA) { + for (unsigned i = 0; i < ADC_NUMOF; i++) { + if (_gpio_rtcio_map[adc_channels[i]] == RTCIO_NA) { LOG_TAG_ERROR("adc", "GPIO%d cannot be used as ADC line\n", - adc_pins[i]); + adc_channels[i]); return false; } } @@ -279,8 +273,8 @@ void adc_print_config(void) { ets_printf("\tADC\t\tpins=[ "); #if defined(ADC_GPIOS) - for (unsigned i = 0; i < adc_chn_num; i++) { - ets_printf("%d ", adc_pins[i]); + for (unsigned i = 0; i < ADC_NUMOF; i++) { + ets_printf("%d ", adc_channels[i]); } #endif /* defined(ADC_GPIOS) */ ets_printf("]\n");