From 4744810bfd13a29b7ad7015bba2bdb69c3e54f1c Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Mon, 29 Aug 2022 18:44:44 -0300 Subject: [PATCH] esp32c3/boards: Add support APA102/APDS0060 for Shift Game --- .../configs/shiftgame/defconfig | 74 +++++++ .../esp32c3-devkit-rust-1/src/Makefile | 12 ++ .../src/esp32c3-devkit-rust-1.h | 25 +++ .../src/esp32c3_apa102.c | 109 +++++++++++ .../src/esp32c3_apds9960.c | 183 ++++++++++++++++++ .../src/esp32c3_board_spi.c | 83 ++++++++ .../src/esp32c3_bringup.c | 21 ++ 7 files changed, 507 insertions(+) create mode 100644 boards/risc-v/esp32c3/esp32c3-devkit-rust-1/configs/shiftgame/defconfig create mode 100644 boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apa102.c create mode 100644 boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apds9960.c create mode 100644 boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_board_spi.c diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/configs/shiftgame/defconfig b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/configs/shiftgame/defconfig new file mode 100644 index 00000000000..d325c34036d --- /dev/null +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/configs/shiftgame/defconfig @@ -0,0 +1,74 @@ +# +# 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_ESP32C3_UART0 is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +# CONFIG_NSH_CMDPARMS is not set +CONFIG_ARCH="risc-v" +CONFIG_ARCH_BOARD="esp32c3-devkit-rust-1" +CONFIG_ARCH_BOARD_ESP32C3_DEVKIT_RUST1=y +CONFIG_ARCH_CHIP="esp32c3" +CONFIG_ARCH_CHIP_ESP32C3=y +CONFIG_ARCH_CHIP_ESP32C3MINI1=y +CONFIG_ARCH_INTERRUPTSTACK=1536 +CONFIG_ARCH_RISCV=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARD_LOOPSPERMSEC=15000 +CONFIG_BUILTIN=y +CONFIG_DEBUG_ERROR=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_I2C=y +CONFIG_DEBUG_I2C_ERROR=y +CONFIG_DEBUG_I2C_WARN=y +CONFIG_DEBUG_INFO=y +CONFIG_DEBUG_WARN=y +CONFIG_DEV_ZERO=y +CONFIG_DRIVERS_VIDEO=y +CONFIG_ESP32C3_DMA=y +CONFIG_ESP32C3_GPIO_IRQ=y +CONFIG_ESP32C3_I2C0=y +CONFIG_ESP32C3_I2C0_SCLPIN=8 +CONFIG_ESP32C3_I2C0_SDAPIN=10 +CONFIG_ESP32C3_SPI2=y +CONFIG_ESP32C3_SPI2_CLKPIN=4 +CONFIG_ESP32C3_SPI2_CSPIN=0 +CONFIG_ESP32C3_SPI2_DMA=y +CONFIG_ESP32C3_SPI2_MISOPIN=6 +CONFIG_ESP32C3_SPI2_MOSIPIN=5 +CONFIG_ESP32C3_USBSERIAL=y +CONFIG_EXAMPLES_FB=y +CONFIG_FS_PROCFS=y +CONFIG_GAMES_SHIFT=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_LCD=y +CONFIG_LCD_APA102=y +CONFIG_LCD_APA102_FREQUENCY=10000000 +CONFIG_LCD_DEV=y +CONFIG_LCD_FRAMEBUFFER=y +CONFIG_LCD_NOGETRUN=y +CONFIG_LIBC_PERROR_STDOUT=y +CONFIG_LIBC_STRERROR=y +CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_READLINE=y +CONFIG_NSH_STRERROR=y +CONFIG_PREALLOC_TIMERS=0 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SENSORS=y +CONFIG_SENSORS_APDS9960=y +CONFIG_START_DAY=29 +CONFIG_START_MONTH=11 +CONFIG_START_YEAR=2019 +CONFIG_SYSTEM_NSH=y +CONFIG_VIDEO_FB=y diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/Makefile b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/Makefile index 2b6fe94ba76..f6fd0d9e9fd 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/Makefile +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/Makefile @@ -41,6 +41,18 @@ ifeq ($(CONFIG_BOARDCTL_IOCTL),y) CSRCS += esp32c3_ioctl.c endif +ifeq ($(CONFIG_ESP32C3_SPI),y) +CSRCS += esp32c3_board_spi.c +endif + +ifeq ($(CONFIG_SENSORS_APDS9960),y) +CSRCS += esp32c3_apds9960.c +endif + +ifeq ($(CONFIG_LCD_APA102),y) +CSRCS += esp32c3_apa102.c +endif + SCRIPTIN = $(SCRIPTDIR)$(DELIM)flat.template.ld SCRIPTOUT = $(SCRIPTDIR)$(DELIM)esp32c3_out.ld diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3-devkit-rust-1.h b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3-devkit-rust-1.h index afdb07f48c6..7fe5bd301e3 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3-devkit-rust-1.h +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3-devkit-rust-1.h @@ -27,6 +27,10 @@ #include +#ifdef CONFIG_VIDEO_FB + #include +#endif + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -278,5 +282,26 @@ int esp32c3_pwm_setup(void); int board_adc_init(void); #endif +/**************************************************************************** + * Name: 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. + * Initialize the APDS-9960 sensor + * + ****************************************************************************/ + +#ifdef CONFIG_SENSORS_APDS9960 +int apds9960_initialize(int devno, int busno); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_RISCV_ESP32C3_ESP32C3_DEVKIT_RUST1_SRC_ESP32C3_DEVKIT_RUST1_H */ diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apa102.c b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apa102.c new file mode 100644 index 00000000000..e21a32ed561 --- /dev/null +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apa102.c @@ -0,0 +1,109 @@ +/**************************************************************************** + * boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apa102.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 +#include +#include +#include + +#include "esp32c3_gpio.h" +#include "esp32c3_spi.h" +#include "esp32c3-devkit-rust-1.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 = esp32c3_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 */ +} + diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apds9960.c b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apds9960.c new file mode 100644 index 00000000000..086d1d5382e --- /dev/null +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_apds9960.c @@ -0,0 +1,183 @@ +/**************************************************************************** + * boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_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 + +#include +#include +#include + +#include +#include +#include + +#include "esp32c3.h" +#include "esp32c3_i2c.h" +#include "esp32c3_gpio.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Use IO1 as APDS9960 INT */ + +#define GPIO_APDS9960_INT 1 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +struct esp32c3_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 esp32c3_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 = ESP32C3_PIN2IRQ(GPIO_APDS9960_INT); + + sninfo("apds9960_irq_attach\n"); + + flags = enter_critical_section(); + + /* Configure the pins that will be used as interrupt input */ + + esp32c3_configgpio(GPIO_APDS9960_INT, INPUT_FUNCTION_1 | PULLUP); + + /* Make sure the interrupt is disabled */ + + esp32c3_gpioirqdisable(irq); + + ret = irq_attach(irq, isr, arg); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: apds9960_irq_attach() failed: %d\n", ret); + return ret; + } + + /* Setup interrupt for Falling Edge */ + + esp32c3_gpioirqenable(irq, FALLING); + + leave_critical_section(flags); + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: 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 apds9960_initialize(int devno, int busno) +{ + struct i2c_master_s *i2c; + char devpath[12]; + int ret; + + sninfo("Initializing APDS9960!\n"); + + /* Initialize I2C */ + + i2c = esp32c3_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; +} diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_board_spi.c b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_board_spi.c new file mode 100644 index 00000000000..55fb02aa6f4 --- /dev/null +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_board_spi.c @@ -0,0 +1,83 @@ +/**************************************************************************** + * boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_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 "esp32c3_gpio.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32c3_spi2_status + ****************************************************************************/ + +#ifdef CONFIG_ESP32C3_SPI2 + +uint8_t esp32c3_spi2_status(struct spi_dev_s *dev, uint32_t devid) +{ + uint8_t status = 0; + + return status; +} + +#endif + +/**************************************************************************** + * Name: esp32c3_spi2_cmddata + ****************************************************************************/ + +#if defined(CONFIG_ESP32C3_SPI2) && defined(CONFIG_SPI_CMDDATA) + +int esp32c3_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. + */ + + esp32c3_gpiowrite(CONFIG_ESP32C3_SPI2_MISOPIN, !cmd); + + return OK; + } + + spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" : + "data"); + + return -ENODEV; +} + +#endif diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_bringup.c index 556edd328ed..72c0f5be475 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_bringup.c +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/src/esp32c3_bringup.c @@ -134,6 +134,27 @@ int esp32c3_bringup(void) } #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 = 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.