From 54a91a27db835a54ff90ba8a5064506763049b9f Mon Sep 17 00:00:00 2001 From: Manuel Bleichenbacher Date: Sat, 26 Mar 2022 20:06:03 +0100 Subject: [PATCH] Improve SPI robustness (fix incompatibility with ESP32-S2) --- Kconfig | 2 +- examples/deep_sleep/main/main.cpp | 4 +- examples/deep_sleep_in_c/main/main.c | 4 +- examples/hello_world/main/main.cpp | 4 +- examples/hello_world_in_c/main/main.c | 4 +- examples/mac_address/main/main.cpp | 4 +- examples/monitoring/main/main.cpp | 4 +- examples/power_off/main/main.cpp | 4 +- examples/power_off_in_c/main/main.c | 4 +- examples/provisioning/main/main.cpp | 4 +- include/TheThingsNetwork.h | 2 +- include/ttn.h | 2 +- src/hal/hal_esp32.c | 59 ++++++++++++++++----------- 13 files changed, 56 insertions(+), 45 deletions(-) diff --git a/Kconfig b/Kconfig index a9387a1..a595c28 100644 --- a/Kconfig +++ b/Kconfig @@ -51,7 +51,7 @@ endchoice config TTN_SPI_FREQ int "SPI frequency (in Hz)" - default 10000000 + default 2000000 help SPI frequency to communicate between ESP32 and SX127x radio chip diff --git a/examples/deep_sleep/main/main.cpp b/examples/deep_sleep/main/main.cpp index 7165903..19330a9 100644 --- a/examples/deep_sleep/main/main.cpp +++ b/examples/deep_sleep/main/main.cpp @@ -35,8 +35,8 @@ const char *devEui = "????????????????"; const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/deep_sleep_in_c/main/main.c b/examples/deep_sleep_in_c/main/main.c index c3b3c0d..7f668a8 100644 --- a/examples/deep_sleep_in_c/main/main.c +++ b/examples/deep_sleep_in_c/main/main.c @@ -36,8 +36,8 @@ const char *devEui = "????????????????"; const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/hello_world/main/main.cpp b/examples/hello_world/main/main.cpp index 40f37f8..5e151e7 100644 --- a/examples/hello_world/main/main.cpp +++ b/examples/hello_world/main/main.cpp @@ -32,8 +32,8 @@ const char *devEui = "????????????????"; const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/hello_world_in_c/main/main.c b/examples/hello_world_in_c/main/main.c index 04dad5d..23889eb 100644 --- a/examples/hello_world_in_c/main/main.c +++ b/examples/hello_world_in_c/main/main.c @@ -32,8 +32,8 @@ const char *devEui = "????????????????"; const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/mac_address/main/main.cpp b/examples/mac_address/main/main.cpp index 1b44a99..f4ab45e 100644 --- a/examples/mac_address/main/main.cpp +++ b/examples/mac_address/main/main.cpp @@ -33,8 +33,8 @@ const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/monitoring/main/main.cpp b/examples/monitoring/main/main.cpp index eed0fdc..099eb9e 100644 --- a/examples/monitoring/main/main.cpp +++ b/examples/monitoring/main/main.cpp @@ -34,8 +34,8 @@ const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/power_off/main/main.cpp b/examples/power_off/main/main.cpp index a5c4ed8..3c75acd 100644 --- a/examples/power_off/main/main.cpp +++ b/examples/power_off/main/main.cpp @@ -33,8 +33,8 @@ const char *devEui = "????????????????"; const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/power_off_in_c/main/main.c b/examples/power_off_in_c/main/main.c index e9de5f1..e1face1 100644 --- a/examples/power_off_in_c/main/main.c +++ b/examples/power_off_in_c/main/main.c @@ -34,8 +34,8 @@ const char *devEui = "????????????????"; const char *appKey = "????????????????????????????????"; // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/examples/provisioning/main/main.cpp b/examples/provisioning/main/main.cpp index cff9ad2..462a13a 100644 --- a/examples/provisioning/main/main.cpp +++ b/examples/provisioning/main/main.cpp @@ -24,8 +24,8 @@ // Pins and other resources -#define TTN_SPI_HOST HSPI_HOST -#define TTN_SPI_DMA_CHAN 1 +#define TTN_SPI_HOST SPI2_HOST +#define TTN_SPI_DMA_CHAN SPI_DMA_DISABLED #define TTN_PIN_SPI_SCLK 5 #define TTN_PIN_SPI_MOSI 27 #define TTN_PIN_SPI_MISO 19 diff --git a/include/TheThingsNetwork.h b/include/TheThingsNetwork.h index 4ba0431..53c10f2 100644 --- a/include/TheThingsNetwork.h +++ b/include/TheThingsNetwork.h @@ -442,7 +442,7 @@ class TheThingsNetwork * Before calling this member function, the SPI bus needs to be configured using `spi_bus_initialize()`. * Additionally, `gpio_install_isr_service()` must have been called to initialize the GPIO ISR handler service. * - * @param spi_host The SPI bus/peripherial to use (`SPI_HOST`, `HSPI_HOST` or `VSPI_HOST`). + * @param spi_host The SPI bus/peripherial to use (`SPI1_HOST`, `SPI2_HOST`, `SPI3_HOST`, `FSPI_HOST`, `HSPI_HOST`, or `VSPI_HOST`). * @param nss The GPIO pin number connected to the radio chip's NSS pin (serving as the SPI chip select) * @param rxtx The GPIO pin number connected to the radio chip's RXTX pin (@ref TTN_NOT_CONNECTED if not * connected) diff --git a/include/ttn.h b/include/ttn.h index 8ca50f2..263ee56 100644 --- a/include/ttn.h +++ b/include/ttn.h @@ -440,7 +440,7 @@ extern "C" * * Call this function after @ref ttn_init() and before all other TTN functions. * - * @param spi_host The SPI bus/peripherial to use (`SPI_HOST`, `HSPI_HOST` or `VSPI_HOST`). + * @param spi_host The SPI bus/peripherial to use (`SPI1_HOST`, `SPI2_HOST`, `SPI3_HOST`, `FSPI_HOST`, `HSPI_HOST`, or `VSPI_HOST`). * @param nss The GPIO pin number connected to the radio chip's NSS pin (serving as the SPI chip select) * @param rxtx The GPIO pin number connected to the radio chip's RXTX pin (@ref TTN_NOT_CONNECTED if not * connected) diff --git a/src/hal/hal_esp32.c b/src/hal/hal_esp32.c index 6ea4f03..9f7d116 100755 --- a/src/hal/hal_esp32.c +++ b/src/hal/hal_esp32.c @@ -110,37 +110,42 @@ void IRAM_ATTR qio_irq_handler(void *arg) void init_io(void) { - // pin_nss and pin_dio0 and pin_dio1 are required + // pin_nss, pin_dio0 and pin_dio1 are required ASSERT(pin_nss != LMIC_UNUSED_PIN); ASSERT(pin_dio0 != LMIC_UNUSED_PIN); ASSERT(pin_dio1 != LMIC_UNUSED_PIN); - gpio_reset_pin(pin_nss); - gpio_set_level(pin_nss, 0); - gpio_set_direction(pin_nss, GPIO_MODE_OUTPUT); + gpio_config_t output_pin_config = { + .pin_bit_mask = BIT64(pin_nss), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = false, + .pull_down_en = false, + .intr_type = GPIO_INTR_DISABLE + }; if (pin_rx_tx != LMIC_UNUSED_PIN) - { - gpio_reset_pin(pin_rx_tx); - gpio_set_level(pin_rx_tx, 0); - gpio_set_direction(pin_rx_tx, GPIO_MODE_OUTPUT); - } + output_pin_config.pin_bit_mask |= BIT64(pin_rx_tx); if (pin_rst != LMIC_UNUSED_PIN) - { - gpio_reset_pin(pin_rst); + output_pin_config.pin_bit_mask |= BIT64(pin_rst); + + gpio_config(&output_pin_config); + + gpio_set_level(pin_nss, 1); + if (pin_rx_tx != LMIC_UNUSED_PIN) + gpio_set_level(pin_rx_tx, 0); + if (pin_rst != LMIC_UNUSED_PIN) gpio_set_level(pin_rst, 0); - gpio_set_direction(pin_rst, GPIO_MODE_OUTPUT); - } // DIO pins with interrupt handlers - gpio_reset_pin(pin_dio0); - gpio_set_direction(pin_dio0, GPIO_MODE_INPUT); - gpio_set_intr_type(pin_dio0, GPIO_INTR_POSEDGE); - - gpio_reset_pin(pin_dio1); - gpio_set_direction(pin_dio1, GPIO_MODE_INPUT); - gpio_set_intr_type(pin_dio1, GPIO_INTR_POSEDGE); + gpio_config_t input_pin_config = { + .pin_bit_mask = BIT64(pin_dio0) | BIT64(pin_dio1), + .mode = GPIO_MODE_INPUT, + .pull_up_en = false, + .pull_down_en = true, + .intr_type = GPIO_INTR_POSEDGE, + }; + gpio_config(&input_pin_config); ESP_LOGI(TAG, "IO initialized"); } @@ -172,7 +177,6 @@ void hal_pin_rst(u1_t val) gpio_set_direction(pin_rst, GPIO_MODE_OUTPUT); #else // keep pin floating - gpio_set_level(pin_rst, val); gpio_set_direction(pin_rst, GPIO_MODE_INPUT); #endif } @@ -207,13 +211,12 @@ void init_spi(void) { // init device spi_device_interface_config_t spi_config = { - .mode = 1, + .mode = 0, .clock_speed_hz = CONFIG_TTN_SPI_FREQ, .command_bits = 0, .address_bits = 8, - .spics_io_num = pin_nss, + .spics_io_num = -1, .queue_size = 1, - .cs_ena_posttrans = 2 }; esp_err_t ret = spi_bus_add_device(spi_host, &spi_config, &spi_handle); @@ -224,16 +227,22 @@ void init_spi(void) void hal_spi_write(u1_t cmd, const u1_t *buf, size_t len) { + gpio_set_level(pin_nss, 0); + memset(&spi_transaction, 0, sizeof(spi_transaction)); spi_transaction.addr = cmd; spi_transaction.length = 8 * len; spi_transaction.tx_buffer = buf; esp_err_t err = spi_device_transmit(spi_handle, &spi_transaction); ESP_ERROR_CHECK(err); + + gpio_set_level(pin_nss, 1); } void hal_spi_read(u1_t cmd, u1_t *buf, size_t len) { + gpio_set_level(pin_nss, 0); + memset(buf, 0, len); memset(&spi_transaction, 0, sizeof(spi_transaction)); spi_transaction.addr = cmd; @@ -243,6 +252,8 @@ void hal_spi_read(u1_t cmd, u1_t *buf, size_t len) spi_transaction.rx_buffer = buf; esp_err_t err = spi_device_transmit(spi_handle, &spi_transaction); ESP_ERROR_CHECK(err); + + gpio_set_level(pin_nss, 1); }