From 11ca921b71d3ce0f3fd4d7158f59e98bbbdfde20 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Tue, 18 Oct 2022 19:09:15 -0300 Subject: [PATCH] esp32s2-saola-1: Add SPI example using max6675 --- .../esp32s2/common/include/esp32s2_max6675.h | 36 +++++ boards/xtensa/esp32s2/common/src/Make.defs | 4 + .../esp32s2/common/src/esp32s2_max6675.c | 103 ++++++++++++++ .../esp32s2/esp32s2-saola-1/src/Make.defs | 4 + .../esp32s2-saola-1/src/esp32s2_board_spi.c | 126 ++++++++++++++++++ .../esp32s2-saola-1/src/esp32s2_bringup.c | 12 ++ 6 files changed, 285 insertions(+) create mode 100644 boards/xtensa/esp32s2/common/include/esp32s2_max6675.h create mode 100644 boards/xtensa/esp32s2/common/src/esp32s2_max6675.c create mode 100644 boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_spi.c diff --git a/boards/xtensa/esp32s2/common/include/esp32s2_max6675.h b/boards/xtensa/esp32s2/common/include/esp32s2_max6675.h new file mode 100644 index 0000000000..92c15c58e5 --- /dev/null +++ b/boards/xtensa/esp32s2/common/include/esp32s2_max6675.h @@ -0,0 +1,36 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/include/esp32s2_max6675.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. + * + ****************************************************************************/ + +/**************************************************************************** + * Name: board_max6675_initialize + * + * Description: + * Initialize and register the MAX6675 Temperature Sensor driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/tempN + * busno - The SPI bus number + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_max6675_initialize(int devno, int busno); diff --git a/boards/xtensa/esp32s2/common/src/Make.defs b/boards/xtensa/esp32s2/common/src/Make.defs index 9be45d4cbf..3508237018 100644 --- a/boards/xtensa/esp32s2/common/src/Make.defs +++ b/boards/xtensa/esp32s2/common/src/Make.defs @@ -24,6 +24,10 @@ ifeq ($(CONFIG_WATCHDOG),y) CSRCS += esp32s2_board_wdt.c endif +ifeq ($(CONFIG_SENSORS_MAX6675),y) + CSRCS += esp32s2_max6675.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src) diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_max6675.c b/boards/xtensa/esp32s2/common/src/esp32s2_max6675.c new file mode 100644 index 0000000000..bc28ba8d2f --- /dev/null +++ b/boards/xtensa/esp32s2/common/src/esp32s2_max6675.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/common/src/esp32s2_max6675.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 +#include "esp32s2_spi.h" + +#ifdef CONFIG_SENSORS_MAX6675 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_max6675_initialize + * + * Description: + * Initialize and register the MAX6675 Temperature Sensor driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/tempN + * busno - The SPI bus number + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int board_max6675_initialize(int devno, int busno) +{ + struct spi_dev_s *spi; + char devpath[12]; + int ret; + + spi = esp32s2_spibus_initialize(busno); + + if (!spi) + { + return -ENODEV; + } + + /* Then register the barometer sensor */ + + snprintf(devpath, sizeof(devpath), "/dev/temp%d", devno); + ret = max6675_register(devpath, spi); + if (ret < 0) + { + snerr("ERROR: Error registering MAX6675\n"); + } + + return ret; +} + +#endif diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/src/Make.defs b/boards/xtensa/esp32s2/esp32s2-saola-1/src/Make.defs index cb7977835d..d6a841d922 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/Make.defs +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/Make.defs @@ -49,6 +49,10 @@ ifeq ($(CONFIG_SENSORS_BMP180),y) CSRCS += esp32s2_bmp180.c endif +ifeq ($(CONFIG_ESP32S2_SPI),y) +CSRCS += esp32s2_board_spi.c +endif + SCRIPTIN = $(SCRIPTDIR)$(DELIM)esp32s2.template.ld SCRIPTOUT = $(SCRIPTDIR)$(DELIM)esp32s2_out.ld diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_spi.c b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_spi.c new file mode 100644 index 0000000000..cf05947957 --- /dev/null +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_board_spi.c @@ -0,0 +1,126 @@ +/**************************************************************************** + * boards/xtensa/esp32s2/esp32s2-saola-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 + +#include +#include +#include + +#include + +#include "esp32s2_gpio.h" +#include "esp32s2-saola-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 diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c index d8607faaa9..861dca693e 100644 --- a/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/src/esp32s2_bringup.c @@ -62,6 +62,10 @@ # include "esp32s2_board_wdt.h" #endif +#ifdef CONFIG_SENSORS_MAX6675 +# include "esp32s2_max6675.h" +#endif + #include "esp32s2-saola-1.h" /**************************************************************************** @@ -217,6 +221,14 @@ int esp32s2_bringup(void) } #endif +#ifdef CONFIG_SENSORS_MAX6675 + ret = board_max6675_initialize(0, 2); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: MAX6675 initialization failed: %d\n", ret); + } +#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.