esp32: Add support to gesture and APA102 as LCD

This commit is contained in:
Alan Carvalho de Assis 2023-10-22 16:34:19 -03:00 committed by Xiang Xiao
parent 1883b91e3c
commit 41a6adf93f
6 changed files with 463 additions and 0 deletions

View File

@ -0,0 +1,74 @@
/****************************************************************************
* boards/xtensa/esp32/common/include/esp32_board_apds9960.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_ESP32_COMMON_INCLUDE_ESP32_BOARD_APDS9960_H
#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_APDS9960_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_apds9960_initialize
*
* Description:
* Initialize and register the APDS9960 gesture sensor.
*
* Input Parameters:
* devno - The device number, used to build the device path as /dev/gestN
* busno - The I2C bus number
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
#ifdef CONFIG_SENSORS_APDS9960
int board_apds9960_initialize(int devno, int busno);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_APDS9960_H */

View File

@ -72,6 +72,10 @@ ifeq ($(CONFIG_ESP32_WIFI),y)
CSRCS += esp32_board_wlan.c
endif
ifeq ($(CONFIG_SENSORS_APDS9960),y)
CSRCS += esp32_board_apds9960.c
endif
ifeq ($(CONFIG_SENSORS_BMP180),y)
CSRCS += esp32_bmp180.c
endif
@ -116,6 +120,10 @@ ifeq ($(CONFIG_LCD_ST7789),y)
CSRCS += esp32_st7789.c
endif
ifeq ($(CONFIG_LCD_APA102),y)
CSRCS += esp32_board_apa102_lcd.c
endif
ifeq ($(CONFIG_LCD_SSD1306_I2C),y)
CSRCS += esp32_ssd1306.c
endif

View File

@ -0,0 +1,108 @@
/****************************************************************************
* boards/xtensa/esp32/common/src/esp32_board_apa102_lcd.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 <stdbool.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/board.h>
#include <nuttx/spi/spi.h>
#include <nuttx/lcd/lcd.h>
#include <nuttx/lcd/apa102.h>
#include "esp32_gpio.h"
#include "esp32_spi.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define LCD_SPI_PORTNO 2 /* On SPI2 */
#ifndef CONFIG_LCD_CONTRAST
# define CONFIG_LCD_CONTRAST 60
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static struct spi_dev_s *g_spidev;
static struct lcd_dev_s *g_lcddev;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_lcd_initialize
****************************************************************************/
int board_lcd_initialize(void)
{
g_spidev = esp32_spibus_initialize(LCD_SPI_PORTNO);
if (!g_spidev)
{
lcderr("ERROR: Failed to initialize SPI port %d\n", LCD_SPI_PORTNO);
return -ENODEV;
}
return OK;
}
/****************************************************************************
* Name: board_lcd_getdev
****************************************************************************/
struct lcd_dev_s *board_lcd_getdev(int lcddev)
{
g_lcddev = apa102_initialize(g_spidev, lcddev);
if (!g_lcddev)
{
lcderr("ERROR: Failed to bind SPI port 1 to LCD %d\n", lcddev);
}
else
{
lcdinfo("SPI port 1 bound to LCD %d\n", lcddev);
return g_lcddev;
}
return NULL;
}
/****************************************************************************
* Name: board_lcd_uninitialize
****************************************************************************/
void board_lcd_uninitialize(void)
{
/* TO-FIX */
}

View File

@ -0,0 +1,185 @@
/****************************************************************************
* boards/xtensa/esp32/common/src/esp32_board_apds9960.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 <errno.h>
#include <debug.h>
#include <stdio.h>
#include <nuttx/spi/spi.h>
#include <nuttx/sensors/apds9960.h>
#include <arch/board/board.h>
#include "esp32_i2c.h"
#include "esp32_gpio.h"
#include "esp32_board_apds9960.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Use IO21 as APDS9960 INT */
#define GPIO_APDS9960_INT 21
/****************************************************************************
* Public Functions
****************************************************************************/
struct esp32_apds9960config_s
{
/* Configuration structure as seen by the APDS-9960 driver */
struct apds9960_config_s config;
/* Additional private definitions only known to this driver */
void *arg; /* Argument to pass to the interrupt handler */
xcpt_t isr; /* ISR Handler */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int apds9960_irq_attach(struct apds9960_config_s *state,
xcpt_t isr, void *arg);
/****************************************************************************
* Private Data
****************************************************************************/
/* A reference to a structure of this type must be passed to the APDS-9960
* driver. This structure provides information about the configuration
* of the APDS-9960 and provides some board-specific hooks.
*
* Memory for this structure is provided by the caller. It is not copied
* by the driver and is presumed to persist while the driver is active. The
* memory must be writable because, under certain circumstances, the driver
* may modify frequency or X plate resistance values.
*/
static struct esp32_apds9960config_s g_apds9960config =
{
.config =
{
.irq_attach = apds9960_irq_attach,
},
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* Attach the APDS-9960 interrupt handler to the GPIO interrupt */
static int apds9960_irq_attach(struct apds9960_config_s *state,
xcpt_t isr, void *arg)
{
irqstate_t flags;
int ret;
int irq = ESP32_PIN2IRQ(GPIO_APDS9960_INT);
sninfo("apds9960_irq_attach\n");
flags = enter_critical_section();
/* Configure the pins that will be used as interrupt input */
esp32_configgpio(GPIO_APDS9960_INT, INPUT_FUNCTION_3 | PULLUP);
/* Make sure the interrupt is disabled */
esp32_gpioirqdisable(irq);
ret = irq_attach(irq, isr, arg);
if (ret < 0)
{
leave_critical_section(flags);
syslog(LOG_ERR, "ERROR: apds9960_irq_attach() failed: %d\n", ret);
return ret;
}
/* Setup interrupt for Falling Edge */
esp32_gpioirqenable(irq, FALLING);
leave_critical_section(flags);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_apds9960_initialize
*
* Description:
* Initialize and register the APDS9960 gesture sensor.
*
* Input Parameters:
* devno - The device number, used to build the device path as /dev/gestN
* busno - The I2C bus number
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int board_apds9960_initialize(int devno, int busno)
{
struct i2c_master_s *i2c;
char devpath[12];
int ret;
sninfo("Initializing APDS9960!\n");
/* Initialize I2C */
i2c = esp32_i2cbus_initialize(busno);
if (i2c == NULL)
{
return -ENODEV;
}
/* Save this i2c in the config */
g_apds9960config.config.i2c_dev = i2c;
g_apds9960config.config.i2c_addr = APDS9960_I2C_ADDR;
/* Then register the gesture sensor */
snprintf(devpath, sizeof(devpath), "/dev/gest%d", devno);
ret = apds9960_register(devpath, &g_apds9960config.config);
if (ret < 0)
{
snerr("ERROR: Failed registering APDS-9960!\n");
}
return ret;
}

View File

@ -0,0 +1,59 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_LEDS is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32-devkitc"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32_DEVKITC=y
CONFIG_ARCH_CHIP="esp32"
CONFIG_ARCH_CHIP_ESP32=y
CONFIG_ARCH_CHIP_ESP32WROVER=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DRIVERS_VIDEO=y
CONFIG_ESP32_I2C0=y
CONFIG_ESP32_SPI2=y
CONFIG_ESP32_UART0=y
CONFIG_EXAMPLES_APDS9960=y
CONFIG_EXAMPLES_FB=y
CONFIG_FS_PROCFS=y
CONFIG_GAMES_SHIFT=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INTELHEX_BINARY=y
CONFIG_LCD=y
CONFIG_LCD_APA102=y
CONFIG_LCD_FRAMEBUFFER=y
CONFIG_LCD_NOGETRUN=y
CONFIG_MM_REGIONS=3
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_SENSORS=y
CONFIG_SENSORS_APDS9960=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSLOG_BUFFER=y
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_VIDEO_FB=y

View File

@ -100,6 +100,10 @@
# include "esp32_tca9548a.h"
#endif
#ifdef CONFIG_SENSORS_APDS9960
#include "esp32_board_apds9960.h"
#endif
#ifdef CONFIG_SENSORS_BMP180
# include "esp32_bmp180.h"
#endif
@ -137,6 +141,10 @@
# include <nuttx/lcd/lcd_dev.h>
#endif
#ifdef CONFIG_VIDEO_FB
# include <nuttx/video/fb.h>
#endif
#ifdef CONFIG_RTC_DRIVER
# include "esp32_rtc_lowerhalf.h"
#endif
@ -669,6 +677,27 @@ int esp32_bringup(void)
# endif
#endif
#ifdef CONFIG_VIDEO_FB
/* Initialize and register the framebuffer driver */
ret = fb_register(0, 0);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: fb_register() failed: %d\n", ret);
}
#endif
#ifdef CONFIG_SENSORS_APDS9960
/* Register the APDS-9960 gesture sensor */
ret = board_apds9960_initialize(0, 0);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: board_apds9960_initialize() 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.