/* SPI Slave example, receiver (uses SPI Slave driver to communicate with sender) This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "driver/spi_slave.h" #include "driver/gpio.h" /* SPI receiver (slave) example. This example is supposed to work together with the SPI sender. It uses the standard SPI pins (MISO, MOSI, SCLK, CS) to transmit data over in a full-duplex fashion, that is, while the master puts data on the MOSI pin, the slave puts its own data on the MISO pin. This example uses one extra pin: GPIO_HANDSHAKE is used as a handshake pin. After a transmission has been set up and we're ready to send/receive data, this code uses a callback to set the handshake pin high. The sender will detect this and start sending a transaction. As soon as the transaction is done, the line gets set low again. */ /* Pins in use. The SPI Master can use the GPIO mux, so feel free to change these if needed. */ #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 #define GPIO_HANDSHAKE 2 #define GPIO_MOSI 12 #define GPIO_MISO 13 #define GPIO_SCLK 15 #define GPIO_CS 14 #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32H4 #define GPIO_HANDSHAKE 3 #define GPIO_MOSI 7 #define GPIO_MISO 2 #define GPIO_SCLK 6 #define GPIO_CS 10 #elif CONFIG_IDF_TARGET_ESP32C6 #define GPIO_HANDSHAKE 15 #define GPIO_MOSI 19 #define GPIO_MISO 20 #define GPIO_SCLK 18 #define GPIO_CS 9 #elif CONFIG_IDF_TARGET_ESP32H2 #define GPIO_HANDSHAKE 2 #define GPIO_MOSI 5 #define GPIO_MISO 0 #define GPIO_SCLK 4 #define GPIO_CS 1 #elif CONFIG_IDF_TARGET_ESP32S3 #define GPIO_HANDSHAKE 2 #define GPIO_MOSI 11 #define GPIO_MISO 13 #define GPIO_SCLK 12 #define GPIO_CS 10 #endif //CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 #ifdef CONFIG_IDF_TARGET_ESP32 #define RCV_HOST HSPI_HOST #else #define RCV_HOST SPI2_HOST #endif //Called after a transaction is queued and ready for pickup by master. We use this to set the handshake line high. void my_post_setup_cb(spi_slave_transaction_t *trans) { gpio_set_level(GPIO_HANDSHAKE, 1); } //Called after transaction is sent/received. We use this to set the handshake line low. void my_post_trans_cb(spi_slave_transaction_t *trans) { gpio_set_level(GPIO_HANDSHAKE, 0); } //Main application void app_main(void) { int n=0; esp_err_t ret; //Configuration for the SPI bus spi_bus_config_t buscfg={ .mosi_io_num=GPIO_MOSI, .miso_io_num=GPIO_MISO, .sclk_io_num=GPIO_SCLK, .quadwp_io_num = -1, .quadhd_io_num = -1, }; //Configuration for the SPI slave interface spi_slave_interface_config_t slvcfg={ .mode=0, .spics_io_num=GPIO_CS, .queue_size=3, .flags=0, .post_setup_cb=my_post_setup_cb, .post_trans_cb=my_post_trans_cb }; //Configuration for the handshake line gpio_config_t io_conf={ .intr_type=GPIO_INTR_DISABLE, .mode=GPIO_MODE_OUTPUT, .pin_bit_mask=(1<