From c780d6fc58c51a2b649bf8da7c59151a3d20f9be Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Mon, 2 Jun 2025 10:56:36 +0200 Subject: [PATCH] Update to latest idf once again --- CMakeLists.txt | 3 +- cores/esp32/esp32-hal-gpio.c | 280 ----------------------------------- cores/esp32/esp32-hal-gpio.h | 96 ------------ cores/esp32/esp32-hal-i2c.c | 111 +++++++++++--- cores/esp32/esp32-hal-spi.c | 123 +++++++++++++-- libraries/Wire/src/Wire.cpp | 5 +- 6 files changed, 210 insertions(+), 408 deletions(-) delete mode 100644 cores/esp32/esp32-hal-gpio.c delete mode 100644 cores/esp32/esp32-hal-gpio.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a8b73e1..11617e8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,5 @@ set(CORE_SRCS cores/esp32/esp32-hal-cpu.c - cores/esp32/esp32-hal-gpio.c cores/esp32/esp32-hal-i2c.c cores/esp32/esp32-hal-matrix.c cores/esp32/esp32-hal-misc.c @@ -22,7 +21,7 @@ set(includedirs set(srcs ${CORE_SRCS} ${LIBRARY_SRCS} ${BLE_SRCS}) set(requires spi_flash mbedtls wifi_provisioning driver) -set(priv_requires nvs_flash bootloader_support espcpputils fmt) +set(priv_requires nvs_flash bootloader_support espcpputils goefmt) idf_component_register(INCLUDE_DIRS ${includedirs} PRIV_INCLUDE_DIRS ${priv_includes} SRCS ${srcs} REQUIRES ${requires} PRIV_REQUIRES ${priv_requires}) set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 23) diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c deleted file mode 100644 index ddffdc75..00000000 --- a/cores/esp32/esp32-hal-gpio.c +++ /dev/null @@ -1,280 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "esp32-hal.h" -#include "esp32-hal-gpio.h" -#include "pins_arduino.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_attr.h" -#include "soc/gpio_reg.h" -#include "soc/io_mux_reg.h" -#include "soc/gpio_struct.h" -#include "soc/rtc_io_reg.h" -#include "soc/rtc_io_periph.h" -#include "soc/rtc_io_struct.h" - -#include "esp_system.h" -#ifdef ESP_IDF_VERSION_MAJOR // IDF 4+ -#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 -#include "esp32/rom/ets_sys.h" -#include "esp32/rom/gpio.h" -#include "esp_intr_alloc.h" -#define GPIO_FUNC 2 -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/ets_sys.h" -#include "esp32s2/rom/gpio.h" -#include "esp_intr_alloc.h" -#include "soc/periph_defs.h" -#define GPIO_FUNC 1 -#else -#error Target CONFIG_IDF_TARGET is not supported -#endif -#else // ESP32 Before IDF 4.0 -#include "rom/ets_sys.h" -#include "rom/gpio.h" -#include "esp_intr.h" -#endif - -#include "driver/rtc_io.h" - -#if CONFIG_IDF_TARGET_ESP32 -const int8_t esp32_adc2gpio[20] = {36, 37, 38, 39, 32, 33, 34, 35, -1, -1, 4, 0, 2, 15, 13, 12, 14, 27, 25, 26}; -#elif CONFIG_IDF_TARGET_ESP32S2 -const int8_t esp32_adc2gpio[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; -#endif - -const DRAM_ATTR esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT]={ -#if CONFIG_IDF_TARGET_ESP32 - {0x44, 11, 11, 1}, - {0x88, -1, -1, -1}, - {0x40, 12, 12, 2}, - {0x84, -1, -1, -1}, - {0x48, 10, 10, 0}, - {0x6c, -1, -1, -1}, - {0x60, -1, -1, -1}, - {0x64, -1, -1, -1}, - {0x68, -1, -1, -1}, - {0x54, -1, -1, -1}, - {0x58, -1, -1, -1}, - {0x5c, -1, -1, -1}, - {0x34, 15, 15, 5}, - {0x38, 14, 14, 4}, - {0x30, 16, 16, 6}, - {0x3c, 13, 13, 3}, - {0x4c, -1, -1, -1}, - {0x50, -1, -1, -1}, - {0x70, -1, -1, -1}, - {0x74, -1, -1, -1}, - {0x78, -1, -1, -1}, - {0x7c, -1, -1, -1}, - {0x80, -1, -1, -1}, - {0x8c, -1, -1, -1}, - {0, -1, -1, -1}, - {0x24, 6, 18, -1}, //DAC1 - {0x28, 7, 19, -1}, //DAC2 - {0x2c, 17, 17, 7}, - {0, -1, -1, -1}, - {0, -1, -1, -1}, - {0, -1, -1, -1}, - {0, -1, -1, -1}, - {0x1c, 9, 4, 8}, - {0x20, 8, 5, 9}, - {0x14, 4, 6, -1}, - {0x18, 5, 7, -1}, - {0x04, 0, 0, -1}, - {0x08, 1, 1, -1}, - {0x0c, 2, 2, -1}, - {0x10, 3, 3, -1} -#elif CONFIG_IDF_TARGET_ESP32S2 - {0x04, 0, -1, -1}, - {0x08, 1, 0, 1}, - {0x0c, 2, 1, 2}, - {0x10, 3, 2, 3}, - {0x14, 4, 3, 4}, - {0x18, 5, 4, 5}, - {0x1c, 6, 5, 6}, - {0x20, 7, 6, 7}, - {0x24, 8, 7, 8}, - {0x28, 9, 8, 9},//FSPI_HD - {0x2c, 10, 9, 10},//FSPI_CS0 / FSPI_D4 - {0x30, 11, 10, 11},//FSPI_D / FSPI_D5 - {0x34, 12, 11, 12},//FSPI_CLK / FSPI_D6 - {0x38, 13, 12, 13},//FSPI_Q / FSPI_D7 - {0x3c, 14, 13, 14},//FSPI_WP / FSPI_DQS - {0x40, 15, 14, -1},//32K+ / RTS0 - {0x44, 16, 15, -1},//32K- / CTS0 - {0x48, 17, 16, -1},//DAC1 / TXD1 - {0x4c, 18, 17, -1},//DAC2 / RXD1 - {0x50, 19, 18, -1},//USB D- / RTS1 - {0x54, 20, 19, -1},//USB D+ / CTS1 - {0x58, 21, -1, -1},//SDA? - { 0, -1, -1, -1},//UNAVAILABLE - { 0, -1, -1, -1},//UNAVAILABLE - { 0, -1, -1, -1},//UNAVAILABLE - { 0, -1, -1, -1},//UNAVAILABLE - {0x6c, -1, -1, -1},//RESERVED SPI_CS1 - {0x70, -1, -1, -1},//RESERVED SPI_HD - {0x74, -1, -1, -1},//RESERVED SPI_WP - {0x78, -1, -1, -1},//RESERVED SPI_CS0 - {0x7c, -1, -1, -1},//RESERVED SPI_CLK - {0x80, -1, -1, -1},//RESERVED SPI_Q - {0x84, -1, -1, -1},//RESERVED SPI_D - {0x88, -1, -1, -1},//FSPI_HD - {0x8c, -1, -1, -1},//FSPI_CS0 - {0x90, -1, -1, -1},//FSPI_D - {0x94, -1, -1, -1},//FSPI_CLK - {0x98, -1, -1, -1},//FSPI_Q - {0x9c, -1, -1, -1},//FSPI_WP - {0xa0, -1, -1, -1},//MTCK - {0xa4, -1, -1, -1},//MTDO - {0xa8, -1, -1, -1},//MTDI - {0xac, -1, -1, -1},//MTMS - {0xb0, -1, -1, -1},//TXD0 - {0xb4, -1, -1, -1},//RXD0 - {0xb8, -1, -1, -1},//SCL? - {0xbc, -1, -1, -1},//INPUT ONLY - {0, -1, -1, -1} -#endif -}; - -extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) -{ - - if(!digitalPinIsValid(pin)) { - return; - } - - int8_t rtc_io = esp32_gpioMux[pin].rtc; - uint32_t rtc_reg = (rtc_io != -1)?rtc_io_desc[rtc_io].reg:0; - if(mode == ANALOG) { - if(!rtc_reg) { - return;//not rtc pin - } -#if CONFIG_IDF_TARGET_ESP32S2 - SENS.sar_io_mux_conf.iomux_clk_gate_en = 1; -#endif - SET_PERI_REG_MASK(rtc_io_desc[rtc_io].reg, (rtc_io_desc[rtc_io].mux)); - SET_PERI_REG_BITS(rtc_io_desc[rtc_io].reg, RTC_IO_TOUCH_PAD1_FUN_SEL_V, 0, rtc_io_desc[rtc_io].func); - - - RTCIO.pin[rtc_io].pad_driver = 0;//OD = 1 - RTCIO.enable_w1tc.w1tc = (1U << rtc_io); - CLEAR_PERI_REG_MASK(rtc_io_desc[rtc_io].reg, rtc_io_desc[rtc_io].ie); - - if (rtc_io_desc[rtc_io].pullup) { - CLEAR_PERI_REG_MASK(rtc_io_desc[rtc_io].reg, rtc_io_desc[rtc_io].pullup); - } - if (rtc_io_desc[rtc_io].pulldown) { - CLEAR_PERI_REG_MASK(rtc_io_desc[rtc_io].reg, rtc_io_desc[rtc_io].pulldown); - } - ESP_REG(DR_REG_IO_MUX_BASE + esp32_gpioMux[pin].reg) = ((uint32_t)GPIO_FUNC << MCU_SEL_S) | ((uint32_t)2 << FUN_DRV_S) | FUN_IE; - return; - } - - //RTC pins PULL settings - if(rtc_reg) { - ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_io_desc[rtc_io].mux); - if(mode & PULLUP) { - ESP_REG(rtc_reg) = (ESP_REG(rtc_reg) | rtc_io_desc[rtc_io].pullup) & ~(rtc_io_desc[rtc_io].pulldown); - } else if(mode & PULLDOWN) { - ESP_REG(rtc_reg) = (ESP_REG(rtc_reg) | rtc_io_desc[rtc_io].pulldown) & ~(rtc_io_desc[rtc_io].pullup); - } else { - ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_io_desc[rtc_io].pullup | rtc_io_desc[rtc_io].pulldown); - } - } - - uint32_t pinFunction = 0, pinControl = 0; - - if(mode & INPUT) { - if(pin < 32) { - GPIO.enable_w1tc = ((uint32_t)1 << pin); - } else { - GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32)); - } - } else if(mode & OUTPUT) { - if(pin >= NUM_OUPUT_PINS){ - return; - } else if(pin < 32) { - GPIO.enable_w1ts = ((uint32_t)1 << pin); - } else { - GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32)); - } - } - - if(mode & PULLUP) { - pinFunction |= FUN_PU; - } else if(mode & PULLDOWN) { - pinFunction |= FUN_PD; - } - - pinFunction |= ((uint32_t)2 << FUN_DRV_S);//what are the drivers? - pinFunction |= FUN_IE;//input enable but required for output as well? - - if(mode & (INPUT | OUTPUT)) { -#if CONFIG_IDF_TARGET_ESP32 - pinFunction |= ((uint32_t)2 << MCU_SEL_S); -#elif CONFIG_IDF_TARGET_ESP32S2 - pinFunction |= ((uint32_t)1 << MCU_SEL_S); -#endif - } else if(mode == SPECIAL) { -#if CONFIG_IDF_TARGET_ESP32 - pinFunction |= ((uint32_t)(((pin)==RX||(pin)==TX)?0:1) << MCU_SEL_S); -#elif CONFIG_IDF_TARGET_ESP32S2 - pinFunction |= ((uint32_t)(((pin)==RX||(pin)==TX)?0:2) << MCU_SEL_S); -#endif - } else { - pinFunction |= ((uint32_t)(mode >> 5) << MCU_SEL_S); - } - - ESP_REG(DR_REG_IO_MUX_BASE + esp32_gpioMux[pin].reg) = pinFunction; - - if(mode & OPEN_DRAIN) { - pinControl = (1 << GPIO_PIN0_PAD_DRIVER_S); - } - - GPIO.pin[pin].val = pinControl; -} - -extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) -{ - if(val) { - if(pin < 32) { - GPIO.out_w1ts = ((uint32_t)1 << pin); - } else if(pin < NUM_OUPUT_PINS) { - GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32)); - } - } else { - if(pin < 32) { - GPIO.out_w1tc = ((uint32_t)1 << pin); - } else if(pin < NUM_OUPUT_PINS) { - GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32)); - } - } -} - -extern int ARDUINO_ISR_ATTR __digitalRead(uint8_t pin) -{ - if(pin < 32) { - return (GPIO.in >> pin) & 0x1; - } else if(pin < GPIO_PIN_COUNT) { - return (GPIO.in1.val >> (pin - 32)) & 0x1; - } - return 0; -} - -extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pinMode"))); -extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite"))); -extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead"))); - diff --git a/cores/esp32/esp32-hal-gpio.h b/cores/esp32/esp32-hal-gpio.h deleted file mode 100644 index ca43e67a..00000000 --- a/cores/esp32/esp32-hal-gpio.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - Arduino.h - Main include file for the Arduino SDK - Copyright (c) 2005-2013 Arduino Team. All right reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef MAIN_ESP32_HAL_GPIO_H_ -#define MAIN_ESP32_HAL_GPIO_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include "stdint.h" -#include "soc/soc_caps.h" - -#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3) -#define NUM_OUPUT_PINS 45 -#define PIN_DAC1 17 -#define PIN_DAC2 18 -#else -#define NUM_OUPUT_PINS 34 -#define PIN_DAC1 25 -#define PIN_DAC2 26 -#endif - -#define LOW 0x0 -#define HIGH 0x1 - -//GPIO FUNCTIONS -#define INPUT 0x01 -#define OUTPUT 0x02 -#define PULLUP 0x04 -#define INPUT_PULLUP 0x05 -#define PULLDOWN 0x08 -#define INPUT_PULLDOWN 0x09 -#define OPEN_DRAIN 0x10 -#define OUTPUT_OPEN_DRAIN 0x12 -#define SPECIAL 0xF0 -#define FUNCTION_1 0x00 -#define FUNCTION_2 0x20 -#define FUNCTION_3 0x40 -#define FUNCTION_4 0x60 -#define FUNCTION_5 0x80 -#define FUNCTION_6 0xA0 -#define ANALOG 0xC0 - -//Interrupt Modes -#define DISABLED 0x00 -#define RISING 0x01 -#define FALLING 0x02 -#define CHANGE 0x03 -#define ONLOW 0x04 -#define ONHIGH 0x05 -#define ONLOW_WE 0x0C -#define ONHIGH_WE 0x0D - -typedef struct { - uint8_t reg; /*!< GPIO register offset from DR_REG_IO_MUX_BASE */ - int8_t rtc; /*!< RTC GPIO number (-1 if not RTC GPIO pin) */ - int8_t adc; /*!< ADC Channel number (-1 if not ADC pin) */ - int8_t touch; /*!< Touch Channel number (-1 if not Touch pin) */ -} esp32_gpioMux_t; - -extern const esp32_gpioMux_t esp32_gpioMux[SOC_GPIO_PIN_COUNT]; -extern const int8_t esp32_adc2gpio[20]; - -#define digitalPinIsValid(pin) ((pin) < SOC_GPIO_PIN_COUNT && esp32_gpioMux[(pin)].reg) -#define digitalPinCanOutput(pin) ((pin) < NUM_OUPUT_PINS && esp32_gpioMux[(pin)].reg) -#define digitalPinToRtcPin(pin) (((pin) < SOC_GPIO_PIN_COUNT)?esp32_gpioMux[(pin)].rtc:-1) -#define digitalPinToAnalogChannel(pin) (((pin) < SOC_GPIO_PIN_COUNT)?esp32_gpioMux[(pin)].adc:-1) -#define digitalPinToTouchChannel(pin) (((pin) < SOC_GPIO_PIN_COUNT)?esp32_gpioMux[(pin)].touch:-1) -#define digitalPinToDacChannel(pin) (((pin) == PIN_DAC1)?0:((pin) == PIN_DAC2)?1:-1) - -void pinMode(uint8_t pin, uint8_t mode); -void digitalWrite(uint8_t pin, uint8_t val); -int digitalRead(uint8_t pin); - -#ifdef __cplusplus -} -#endif - -#endif /* MAIN_ESP32_HAL_GPIO_H_ */ diff --git a/cores/esp32/esp32-hal-i2c.c b/cores/esp32/esp32-hal-i2c.c index 86e74c38..2eab272b 100644 --- a/cores/esp32/esp32-hal-i2c.c +++ b/cores/esp32/esp32-hal-i2c.c @@ -27,7 +27,6 @@ #include "esp_attr.h" #include "esp32-hal-cpu.h" // cpu clock change support 31DEC2018 #include "esp32-hal-log.h" -#include "esp32-hal-gpio.h" #include "esp32-hal-matrix.h" #include "esp32-hal-misc.h" @@ -44,6 +43,8 @@ #include "rom/ets_sys.h" #endif #include "driver/gpio.h" +#include +#define TAG "ARDUINO" #if CONFIG_IDF_TARGET_ESP32 @@ -1432,28 +1433,52 @@ static bool i2cCheckLineState(int8_t sda, int8_t scl){ return false;//return false since there is nothing to do } // if the bus is not 'clear' try the cycling SCL until SDA goes High or 9 cycles - digitalWrite(sda, HIGH); - digitalWrite(scl, HIGH); - pinMode(sda, PULLUP|OPEN_DRAIN|INPUT); - pinMode(scl, PULLUP|OPEN_DRAIN|OUTPUT); + gpio_set_level(sda, 1); + gpio_set_level(scl, 1); + { + const gpio_config_t config = { + .pin_bit_mask = 1ULL << sda, + .mode = GPIO_MODE_INPUT_OUTPUT_OD, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; - if(!digitalRead(sda) || !digitalRead(scl)) { // bus in busy state - log_w("invalid state sda(%d)=%d, scl(%d)=%d", sda, digitalRead(sda), scl, digitalRead(scl)); - digitalWrite(scl, HIGH); + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } + { + const gpio_config_t config = { + .pin_bit_mask = 1ULL << scl, + .mode = GPIO_MODE_OUTPUT_OD, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } + + if(!gpio_get_level(sda) || !gpio_get_level(scl)) { // bus in busy state + log_w("invalid state sda(%d)=%d, scl(%d)=%d", sda, gpio_get_level(sda), scl, gpio_get_level(scl)); + gpio_set_level(scl, 1); for(uint8_t a=0; a<9; a++) { delayMicroseconds(5); - digitalWrite(scl, LOW); + gpio_set_level(scl, 0); delayMicroseconds(5); - digitalWrite(scl, HIGH); - if(digitalRead(sda)){ // bus recovered + gpio_set_level(scl, 1); + if(gpio_get_level(sda)){ // bus recovered log_d("Recovered after %d Cycles",a+1); break; } } } - if(!digitalRead(sda) || !digitalRead(scl)) { // bus in busy state - log_e("Bus Invalid State, TwoWire() Can't init sda=%d, scl=%d",digitalRead(sda),digitalRead(scl)); + if(!gpio_get_level(sda) || !gpio_get_level(scl)) { // bus in busy state + log_e("Bus Invalid State, TwoWire() Can't init sda=%d, scl=%d",gpio_get_level(sda),gpio_get_level(scl)); return false; // bus is busy } return true; @@ -1464,8 +1489,20 @@ i2c_err_t i2cAttachSCL(i2c_t * i2c, int8_t scl) if(i2c == NULL) { return I2C_ERROR_DEV; } - digitalWrite(scl, HIGH); - pinMode(scl, OPEN_DRAIN | PULLUP | INPUT | OUTPUT); + gpio_set_level(scl, 1); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << scl), + .mode = GPIO_MODE_INPUT_OUTPUT_OD, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } pinMatrixOutAttach(scl, I2C_SCL_IDX(i2c->num), false, false); pinMatrixInAttach(scl, I2C_SCL_IDX(i2c->num), false); return I2C_ERROR_OK; @@ -1478,7 +1515,19 @@ i2c_err_t i2cDetachSCL(i2c_t * i2c, int8_t scl) } pinMatrixOutDetach(scl, false, false); pinMatrixInDetach(I2C_SCL_IDX(i2c->num), false, false); - pinMode(scl, INPUT | PULLUP); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << scl), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } return I2C_ERROR_OK; } @@ -1487,8 +1536,20 @@ i2c_err_t i2cAttachSDA(i2c_t * i2c, int8_t sda) if(i2c == NULL) { return I2C_ERROR_DEV; } - digitalWrite(sda, HIGH); - pinMode(sda, OPEN_DRAIN | PULLUP | INPUT | OUTPUT ); + gpio_set_level(sda, 1); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << sda), + .mode = GPIO_MODE_INPUT_OUTPUT_OD, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } pinMatrixOutAttach(sda, I2C_SDA_IDX(i2c->num), false, false); pinMatrixInAttach(sda, I2C_SDA_IDX(i2c->num), false); return I2C_ERROR_OK; @@ -1501,7 +1562,19 @@ i2c_err_t i2cDetachSDA(i2c_t * i2c, int8_t sda) } pinMatrixOutDetach(sda, false, false); pinMatrixInDetach(I2C_SDA_IDX(i2c->num), false, false); - pinMode(sda, INPUT | PULLUP); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << sda), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } return I2C_ERROR_OK; } diff --git a/cores/esp32/esp32-hal-spi.c b/cores/esp32/esp32-hal-spi.c index 2ac55dc0..5c7705f2 100644 --- a/cores/esp32/esp32-hal-spi.c +++ b/cores/esp32/esp32-hal-spi.c @@ -14,6 +14,10 @@ #include +#include +#include +#define TAG "ARDUINO" + #include "esp32-hal-spi.h" #include "esp32-hal.h" #include "freertos/FreeRTOS.h" @@ -45,7 +49,6 @@ #include "rom/gpio.h" #include "esp_intr.h" #endif -#include "esp32-hal-gpio.h" #include "esp32-hal-matrix.h" #include "esp32-hal-cpu.h" @@ -146,7 +149,21 @@ void spiAttachSCK(spi_t * spi, int8_t sck) } #endif } - pinMode(sck, OUTPUT); + + { + const gpio_config_t config = { + .pin_bit_mask = 1ULL << sck, + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } + pinMatrixOutAttach(sck, SPI_CLK_IDX(spi->num), false, false); } @@ -174,7 +191,21 @@ void spiAttachMISO(spi_t * spi, int8_t miso) #endif } SPI_MUTEX_LOCK(); - pinMode(miso, INPUT); + + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << miso), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } + pinMatrixInAttach(miso, SPI_MISO_IDX(spi->num), false); SPI_MUTEX_UNLOCK(); } @@ -202,7 +233,21 @@ void spiAttachMOSI(spi_t * spi, int8_t mosi) } #endif } - pinMode(mosi, OUTPUT); + + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << mosi), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } + pinMatrixOutAttach(mosi, SPI_MOSI_IDX(spi->num), false, false); } @@ -230,7 +275,19 @@ void spiDetachSCK(spi_t * spi, int8_t sck) #endif } pinMatrixOutDetach(sck, false, false); - pinMode(sck, INPUT); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << sck), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } } void spiDetachMISO(spi_t * spi, int8_t miso) @@ -257,7 +314,19 @@ void spiDetachMISO(spi_t * spi, int8_t miso) #endif } pinMatrixInDetach(SPI_MISO_IDX(spi->num), false, false); - pinMode(miso, INPUT); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << miso), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } } void spiDetachMOSI(spi_t * spi, int8_t mosi) @@ -284,7 +353,19 @@ void spiDetachMOSI(spi_t * spi, int8_t mosi) #endif } pinMatrixOutDetach(mosi, false, false); - pinMode(mosi, INPUT); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << mosi), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } } void spiAttachSS(spi_t * spi, uint8_t cs_num, int8_t ss) @@ -314,7 +395,19 @@ void spiAttachSS(spi_t * spi, uint8_t cs_num, int8_t ss) } #endif } - pinMode(ss, OUTPUT); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << ss), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } pinMatrixOutAttach(ss, SPI_SS_IDX(spi->num, cs_num), false, false); spiEnableSSPins(spi, (1 << cs_num)); } @@ -343,7 +436,19 @@ void spiDetachSS(spi_t * spi, int8_t ss) #endif } pinMatrixOutDetach(ss, false, false); - pinMode(ss, INPUT); + { + const gpio_config_t config = { + .pin_bit_mask = (1ULL << ss), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + + const int result = gpio_config(&config); + if (result != ESP_OK) + ESP_LOGE(TAG, "gpio_config() failed %s", esp_err_to_name(result)); + } } void spiEnableSSPins(spi_t * spi, uint8_t cs_mask) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index acfade1b..bb0dd27c 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -30,7 +30,8 @@ extern "C" { #include #include -#include + +#include "goefmt.h" #include "esp32-hal-i2c.h" #include "esp32-hal-log.h" @@ -52,7 +53,7 @@ std::string toString(i2c_err_t val) case I2C_ERROR_NO_BEGIN: return "I2C_ERROR_NO_BEGIN"; default: ESP_LOGW("WIRE", "unknown i2c_err_t(%i)", std::to_underlying(val)); - return fmt::format("Unknown i2c_err_t({})", std::to_underlying(val)); + return goe::format("Unknown i2c_err_t({})", std::to_underlying(val)); } }