From 857414e95dccd8e54922439b2e03a596c1d35beb Mon Sep 17 00:00:00 2001 From: Sara Souza Date: Fri, 23 Jul 2021 09:42:28 -0300 Subject: [PATCH] xtensa/esp32: expose SPI2 as a char driver --- arch/xtensa/src/esp32/esp32_spi.c | 4 +- arch/xtensa/src/esp32/esp32_spi.h | 8 +++ boards/xtensa/esp32/common/src/Make.defs | 4 ++ .../esp32/common/src/esp32_board_spidev.c | 72 +++++++++++++++++++ .../esp32/esp32-devkitc/src/esp32-devkitc.h | 19 +++++ .../esp32/esp32-devkitc/src/esp32_bringup.c | 15 ++++ 6 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 boards/xtensa/esp32/common/src/esp32_board_spidev.c diff --git a/arch/xtensa/src/esp32/esp32_spi.c b/arch/xtensa/src/esp32/esp32_spi.c index 2be5a3462a..0175c01b2c 100644 --- a/arch/xtensa/src/esp32/esp32_spi.c +++ b/arch/xtensa/src/esp32/esp32_spi.c @@ -1440,12 +1440,12 @@ FAR struct spi_dev_s *esp32_spibus_initialize(int port) switch (port) { #ifdef CONFIG_ESP32_SPI2 - case 2: + case ESP32_SPI2: priv = &esp32_spi2_priv; break; #endif #ifdef CONFIG_ESP32_SPI3 - case 3: + case ESP32_SPI3: priv = &esp32_spi3_priv; break; #endif diff --git a/arch/xtensa/src/esp32/esp32_spi.h b/arch/xtensa/src/esp32/esp32_spi.h index afd3946b8c..20da8e8e59 100644 --- a/arch/xtensa/src/esp32/esp32_spi.h +++ b/arch/xtensa/src/esp32/esp32_spi.h @@ -46,6 +46,14 @@ extern "C" #include +#ifdef CONFIG_ESP32_SPI2 +# define ESP32_SPI2 2 +#endif + +#ifdef CONFIG_ESP32_SPI3 +# define ESP32_SPI3 3 +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/boards/xtensa/esp32/common/src/Make.defs b/boards/xtensa/esp32/common/src/Make.defs index 928dbe0960..b95231dffd 100644 --- a/boards/xtensa/esp32/common/src/Make.defs +++ b/boards/xtensa/esp32/common/src/Make.defs @@ -34,6 +34,10 @@ ifeq ($(CONFIG_ESP32_SPI),y) CSRCS += esp32_board_spi.c endif +ifeq ($(CONFIG_SPI_DRIVER),y) + CSRCS += esp32_board_spidev.c +endif + ifeq ($(CONFIG_ESP32_WIRELESS),y) CSRCS += esp32_board_wlan.c endif diff --git a/boards/xtensa/esp32/common/src/esp32_board_spidev.c b/boards/xtensa/esp32/common/src/esp32_board_spidev.c new file mode 100644 index 0000000000..1dad836ada --- /dev/null +++ b/boards/xtensa/esp32/common/src/esp32_board_spidev.c @@ -0,0 +1,72 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/src/esp32_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 + +#include +#include +#include + +#include + +#include "esp32_spi.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_spidev_initialize + * + * Description: + * Initialize and register SPI driver for the specified SPI port. + * + ****************************************************************************/ + +int board_spidev_initialize(int port) +{ + int ret; + FAR struct spi_dev_s *spi; + + spiinfo("Initializing /dev/spi%d...\n", port); + + /* Initialize SPI device */ + + spi = esp32_spibus_initialize(port); + if (spi == NULL) + { + spierr("Failed to initialize SPI%d.\n", port); + return -ENODEV; + } + + ret = spi_register(spi, port); + if (ret < 0) + { + spierr("Failed to register /dev/spi%d: %d\n", port, ret); + + esp32_spibus_uninitialize(spi); + } + + return ret; +} diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h index 01fb366ac3..688de84d0c 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h @@ -136,5 +136,24 @@ void esp32_spiflash_encrypt_test(void); int esp32_gpio_init(void); #endif +/**************************************************************************** + * 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 + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_XTENSA_ESP32_ESP32_DEVKITC_SRC_ESP32_DEVKITC_H */ diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c index ca844a6c3a..73c100e4b8 100644 --- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c @@ -94,6 +94,10 @@ # include "esp32_rtc_lowerhalf.h" #endif +#ifdef CONFIG_SPI_DRIVER +# include "esp32_spi.h" +#endif + #include "esp32-devkitc.h" /**************************************************************************** @@ -390,6 +394,17 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_SPI_DRIVER +# ifdef CONFIG_ESP32_SPI2 + ret = board_spidev_initialize(ESP32_SPI2); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to initialize SPI%d driver: %d\n", + ESP32_SPI2, ret); + } +# endif +#endif + /* If we got here then perhaps not all initialization was successful, but * at least enough succeeded to bring-up NSH with perhaps reduced * capabilities.