xtensa/esp32s2: Add SPI slave support

This commit is contained in:
Eren Terzioglu 2023-10-12 18:45:28 +03:00 committed by Xiang Xiao
parent 7f48c185c8
commit 9997a858e2
16 changed files with 2188 additions and 145 deletions

View file

@ -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"
@ -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

View file

@ -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)

View file

@ -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 */

View file

@ -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
@ -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

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,74 @@
/****************************************************************************
* boards/xtensa/esp32s2/common/include/esp32s2_board_spidev.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
#ifndef __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SPIDEV_H
#define __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SPIDEV_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: board_spidev_initialize
*
* Description:
* Initialize SPI driver and register the /dev/spi device.
*
* Input Parameters:
* bus - The SPI bus number, used to build the device path as /dev/spiN
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
#ifdef CONFIG_SPI_DRIVER
int board_spidev_initialize(int bus);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SPIDEV_H */

View file

@ -0,0 +1,74 @@
/****************************************************************************
* boards/xtensa/esp32s2/common/include/esp32s2_board_spislavedev.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
#ifndef __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SPISLAVEDEV_H
#define __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SPISLAVEDEV_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: board_spislavedev_initialize
*
* Description:
* Initialize SPI Slave driver and register the /dev/spislv device.
*
* Input Parameters:
* bus - The SPI bus number, used to build the device path as /dev/spislvN
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
#ifdef CONFIG_SPI_SLAVE
int board_spislavedev_initialize(int bus);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32S2_COMMON_INCLUDE_ESP32S2_BOARD_SPISLAVEDEV_H */

View file

@ -24,6 +24,18 @@ ifeq ($(CONFIG_WATCHDOG),y)
CSRCS += esp32s2_board_wdt.c
endif
ifeq ($(CONFIG_ESP32S2_SPI),y)
CSRCS += esp32s2_board_spi.c
endif
ifeq ($(CONFIG_SPI_DRIVER),y)
CSRCS += esp32s2_board_spidev.c
endif
ifeq ($(CONFIG_SPI_SLAVE_DRIVER),y)
CSRCS += esp32s2_board_spislavedev.c
endif
ifeq ($(CONFIG_ESP32S2_SPIFLASH),y)
CSRCS += esp32s2_board_spiflash.c
endif

View file

@ -1,5 +1,5 @@
/****************************************************************************
* boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_spi.c
* boards/xtensa/esp32s2/common/src/esp32s2_board_spi.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -31,7 +31,6 @@
#include <nuttx/spi/spi.h>
#include "esp32s2_gpio.h"
#include "esp32s2-saola-1.h"
/****************************************************************************
* Private Functions
@ -70,7 +69,7 @@ int esp32s2_spi2_cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd)
* data bits are data or a command.
*/
esp32s2_gpiowrite(GPIO_LCD_DC, !cmd);
esp32s2_gpiowrite(CONFIG_ESP32S2_SPI2_MISOPIN, !cmd);
return OK;
}

View file

@ -0,0 +1,81 @@
/****************************************************************************
* boards/xtensa/esp32s2/common/src/esp32s2_board_spidev.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/spi/spi_transfer.h>
#include "esp32s2_spi.h"
#include "esp32s2_board_spidev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_spidev_initialize
*
* Description:
* Initialize SPI driver and register the /dev/spi device.
*
* Input Parameters:
* port - The SPI bus number, used to build the device path as /dev/spiN
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
int board_spidev_initialize(int port)
{
int ret;
struct spi_dev_s *spi;
syslog(LOG_INFO, "Initializing /dev/spi%d...\n", port);
/* Initialize SPI device */
spi = esp32s2_spibus_initialize(port);
if (spi == NULL)
{
syslog(LOG_ERR, "Failed to initialize SPI%d.\n", port);
return -ENODEV;
}
ret = spi_register(spi, port);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to register /dev/spi%d: %d\n", port, ret);
esp32s2_spibus_uninitialize(spi);
}
return ret;
}

View file

@ -0,0 +1,82 @@
/****************************************************************************
* boards/xtensa/esp32s2/common/src/esp32s2_board_spislavedev.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/spi/slave.h>
#include "esp32s2_spi.h"
#include "esp32s2_board_spislavedev.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_spislavedev_initialize
*
* Description:
* Initialize SPI Slave driver and register the /dev/spislv device.
*
* Input Parameters:
* bus - The SPI bus number, used to build the device path as /dev/spislvN
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned
* to indicate the nature of any failure.
*
****************************************************************************/
int board_spislavedev_initialize(int bus)
{
int ret;
struct spi_slave_ctrlr_s *ctrlr;
spiinfo("Initializing /dev/spislv%d...\n", bus);
/* Initialize SPI Slave controller device */
ctrlr = esp32s2_spislave_ctrlr_initialize(bus);
if (ctrlr == NULL)
{
spierr("Failed to initialize SPI%d as slave.\n", bus);
return -ENODEV;
}
ret = spi_slave_register(ctrlr, bus);
if (ret < 0)
{
spierr("Failed to register /dev/spislv%d: %d\n", bus, ret);
esp32s2_spislave_ctrlr_uninitialize(ctrlr);
}
return ret;
}

View file

@ -41,10 +41,6 @@ ifeq ($(CONFIG_I2C_DRIVER),y)
CSRCS += esp32s2_board_i2c.c
endif
ifeq ($(CONFIG_ESP32S2_SPI),y)
CSRCS += esp32s2_board_spi.c
endif
ifeq ($(CONFIG_ARCH_BUTTONS),y)
CSRCS += esp32s2_buttons.c
endif

View file

@ -1,128 +0,0 @@
/****************************************************************************
* boards/xtensa/esp32s2/esp32s2-kaluga-1/src/esp32s2_board_spi.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <nuttx/spi/spi.h>
#include <arch/board/board.h>
#include "esp32s2_gpio.h"
#include "esp32s2-kaluga-1.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: esp32s2_spi2_status
****************************************************************************/
#ifdef CONFIG_ESP32S2_SPI2
uint8_t esp32s2_spi2_status(struct spi_dev_s *dev, uint32_t devid)
{
uint8_t status = 0;
return status;
}
#endif
/****************************************************************************
* Name: esp32s2_spi2_cmddata
****************************************************************************/
#if defined(CONFIG_ESP32S2_SPI2) && defined(CONFIG_SPI_CMDDATA)
int esp32s2_spi2_cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd)
{
if (devid == SPIDEV_DISPLAY(0))
{
/* This is the Data/Command control pad which determines whether the
* data bits are data or a command.
*/
esp32s2_gpiowrite(GPIO_LCD_DC, !cmd);
return OK;
}
spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
"data");
return -ENODEV;
}
#endif
/****************************************************************************
* Name: esp32s2_spi3_status
****************************************************************************/
#ifdef CONFIG_ESP32S2_SPI3
uint8_t esp32s2_spi3_status(struct spi_dev_s *dev, uint32_t devid)
{
uint8_t status = 0;
return status;
}
#endif
/****************************************************************************
* Name: esp32s2_spi3_cmddata
****************************************************************************/
#if defined(CONFIG_ESP32S2_SPI3) && defined(CONFIG_SPI_CMDDATA)
int esp32s2_spi3_cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd)
{
if (devid == SPIDEV_DISPLAY(0))
{
/* This is the Data/Command control pad which determines whether the
* data bits are data or a command.
*/
esp32s2_gpiowrite(CONFIG_ESP32S2_SPI3_MISOPIN, !cmd);
return OK;
}
spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" :
"data");
return -ENODEV;
}
#endif

View file

@ -74,6 +74,16 @@
# include <nuttx/lcd/lcd_dev.h>
#endif
#ifdef CONFIG_SPI_DRIVER
# include "esp32s2_spi.h"
# include "esp32s2_board_spidev.h"
#endif
#ifdef CONFIG_SPI_SLAVE_DRIVER
# include "esp32s2_spi.h"
# include "esp32s2_board_spislavedev.h"
#endif
#include "esp32s2-kaluga-1.h"
/****************************************************************************
@ -138,6 +148,33 @@ int esp32s2_bringup(void)
}
#endif
#ifdef CONFIG_ESP32S2_SPI2
# ifdef CONFIG_SPI_DRIVER
ret = board_spidev_initialize(ESP32S2_SPI2);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize SPI%d driver: %d\n",
ESP32S2_SPI2, ret);
}
# elif defined(CONFIG_SPI_SLAVE_DRIVER) && defined(CONFIG_ESP32S2_SPI2_SLAVE)
ret = board_spislavedev_initialize(ESP32S2_SPI2);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize SPI%d Slave driver: %d\n",
ESP32S2_SPI2, ret);
}
# endif
#endif
#if defined(CONFIG_SPI_SLAVE_DRIVER) && defined(CONFIG_ESP32S2_SPI3_SLAVE)
ret = board_spislavedev_initialize(ESP32S2_SPI3);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize SPI%d Slave driver: %d\n",
ESP32S2_SPI3, ret);
}
#endif
/* Register the timer drivers */
#ifdef CONFIG_TIMER

View file

@ -49,10 +49,6 @@ ifeq ($(CONFIG_SENSORS_BMP180),y)
CSRCS += esp32s2_bmp180.c
endif
ifeq ($(CONFIG_ESP32S2_SPI),y)
CSRCS += esp32s2_board_spi.c
endif
ifeq ($(CONFIG_ARCH_BUTTONS),y)
CSRCS += esp32s2_buttons.c
endif

View file

@ -74,6 +74,16 @@
# include "esp32s2_max6675.h"
#endif
#ifdef CONFIG_SPI_DRIVER
# include "esp32s2_spi.h"
# include "esp32s2_board_spidev.h"
#endif
#ifdef CONFIG_SPI_SLAVE_DRIVER
# include "esp32s2_spi.h"
# include "esp32s2_board_spislavedev.h"
#endif
#include "esp32s2-saola-1.h"
/****************************************************************************
@ -162,6 +172,33 @@ int esp32s2_bringup(void)
}
#endif
#ifdef CONFIG_ESP32S2_SPI2
# ifdef CONFIG_SPI_DRIVER
ret = board_spidev_initialize(ESP32S2_SPI2);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize SPI%d driver: %d\n",
ESP32S2_SPI2, ret);
}
# elif defined(CONFIG_SPI_SLAVE_DRIVER) && defined(CONFIG_ESP32S2_SPI2_SLAVE)
ret = board_spislavedev_initialize(ESP32S2_SPI2);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize SPI%d Slave driver: %d\n",
ESP32S2_SPI2, ret);
}
# endif
#endif
#if defined(CONFIG_SPI_SLAVE_DRIVER) && defined(CONFIG_ESP32S2_SPI3_SLAVE)
ret = board_spislavedev_initialize(ESP32S2_SPI3);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize SPI%d Slave driver: %d\n",
ESP32S2_SPI3, ret);
}
#endif
/* Register the timer drivers */
#ifdef CONFIG_TIMER