Skip to content

Commit

Permalink
xtensa/esp32s2: Add SPI slave support
Browse files Browse the repository at this point in the history
  • Loading branch information
eren-terzioglu authored and xiaoxiang781216 committed Nov 9, 2023
1 parent 7f48c18 commit 9997a85
Show file tree
Hide file tree
Showing 16 changed files with 2,188 additions and 145 deletions.
43 changes: 38 additions & 5 deletions arch/xtensa/src/esp32s2/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -512,22 +512,44 @@ config ESP32S2_SPI_UDCS
---help---
Use user-defined CS.

config ESP32S2_SPI2_SLAVE
bool "SPI2 Slave mode"
default n
depends on SPI_SLAVE && ESP32S2_SPI2
select ESP32S2_GPIO_IRQ
---help---
Configure SPI2 to operate in Slave mode.

config ESP32S2_SPI2_DMA
bool "SPI2 use DMA"
default y
depends on ESP32S2_SPI2

config ESP32S2_SPI3_SLAVE
bool "SPI3 Slave mode"
default n
depends on SPI_SLAVE && ESP32S2_SPI3
select ESP32S2_GPIO_IRQ
---help---
Configure SPI3 to operate in Slave mode.

config ESP32S2_SPI3_DMA
bool "SPI3 use DMA"
default y
depends on ESP32S2_SPI3

config SPI_DMADESC_NUM
int "SPI DMA maximum number of descriptors"
default 2
config ESP32S2_SPI_DMA_BUFSIZE
int "SPI Master GDMA buffer size"
default 2048
depends on ESP32S2_SPI2_DMA || ESP32S2_SPI3_DMA
---help---
Configure the maximum number of out-link/in-link descriptors to
be chained for a SPI DMA transfer.
This is used to calculate and allocate DMA description buffer,
not really allocate TX/RX buffer.

config ESP32S2_SPI_SLAVE_BUFSIZE
int "SPI Slave buffer size"
default 2048
depends on SPI_SLAVE

config ESP32S2_SPI_DMATHRESHOLD
int "SPI DMA threshold"
Expand Down Expand Up @@ -559,6 +581,17 @@ config ESP32S2_SPI2_MISOPIN
default 13
range 0 48

config ESP32S2_SPI2_IO2PIN
int "SPI2 IO2 Pin"
default 14
range 0 48
depends on ESP32S2_SPI_IO_QIO

config ESP32S2_SPI2_IO3PIN
int "SPI2 IO3 Pin"
default 9
range 0 48
depends on ESP32S2_SPI_IO_QIO
endif # ESP32S2_SPI2

if ESP32S2_SPI3
Expand Down
3 changes: 3 additions & 0 deletions arch/xtensa/src/esp32s2/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ endif

ifeq ($(CONFIG_ESP32S2_SPI),y)
CHIP_CSRCS += esp32s2_spi.c
ifeq ($(CONFIG_SPI_SLAVE),y)
CHIP_CSRCS += esp32s2_spi_slave.c
endif
endif

#ifeq ($(CONFIG_ESP32S2_SPIFLASH),y)
Expand Down
6 changes: 5 additions & 1 deletion arch/xtensa/src/esp32s2/esp32s2_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@

/* SPI DMA RX/TX number of descriptors */

#define SPI_DMA_DESC_NUM (CONFIG_SPI_DMADESC_NUM)
# if (CONFIG_ESP32S2_SPI_DMA_BUFSIZE % ESP32S2_DMA_BUFLEN_MAX) > 0
# define SPI_DMA_DESC_NUM (CONFIG_ESP32S2_SPI_DMA_BUFSIZE / ESP32S2_DMA_BUFLEN_MAX + 1)
# else
# define SPI_DMA_DESC_NUM (CONFIG_ESP32S2_SPI_DMA_BUFSIZE / ESP32S2_DMA_BUFLEN_MAX)
# endif

/* SPI DMA reset before exchange */

Expand Down
37 changes: 37 additions & 0 deletions arch/xtensa/src/esp32s2/esp32s2_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ extern "C"

#include <nuttx/spi/spi.h>

#ifdef CONFIG_SPI_SLAVE
# include <nuttx/spi/slave.h>
#endif

#ifdef CONFIG_ESP32S2_SPI2
# define ESP32S2_SPI2 2
#endif
Expand Down Expand Up @@ -139,6 +143,39 @@ int esp32s2_spi3_cmddata(struct spi_dev_s *dev,

int esp32s2_spibus_uninitialize(struct spi_dev_s *dev);

/****************************************************************************
* Name: esp32s2_spislave_ctrlr_initialize
*
* Description:
* Initialize the selected SPI Slave bus.
*
* Input Parameters:
* port - Port number (for hardware that has multiple SPI Slave interfaces)
*
* Returned Value:
* Valid SPI Slave controller structure reference on success;
* NULL on failure.
*
****************************************************************************/

struct spi_slave_ctrlr_s *esp32s2_spislave_ctrlr_initialize(int port);

/****************************************************************************
* Name: esp32s2_spislave_ctrlr_uninitialize
*
* Description:
* Uninitialize an SPI Slave bus.
*
* Input Parameters:
* ctrlr - SPI Slave controller interface instance
*
* Returned Value:
* Zero (OK) is returned on success. Otherwise -1 (ERROR).
*
****************************************************************************/

int esp32s2_spislave_ctrlr_uninitialize(struct spi_slave_ctrlr_s *ctrlr);

#endif /* CONFIG_ESP32S2_SPI */

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 9997a85

Please sign in to comment.