esp32s2-saola-1: Add SPI example using max6675

This commit is contained in:
Alan Carvalho de Assis 2022-10-18 19:09:15 -03:00 committed by Xiang Xiao
parent 0e4c2fed93
commit 11ca921b71
6 changed files with 285 additions and 0 deletions

View File

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

View File

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

View File

@ -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 <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/sensors/max6675.h>
#include <nuttx/spi/spi.h>
#include <stdio.h>
#include <debug.h>
#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

View File

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

View File

@ -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 <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <debug.h>
#include <nuttx/spi/spi.h>
#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

View File

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