diff --git a/boards/xtensa/esp32/common/include/esp32_rgbled.h b/boards/xtensa/esp32/common/include/esp32_rgbled.h new file mode 100644 index 0000000000..e34f237aef --- /dev/null +++ b/boards/xtensa/esp32/common/include/esp32_rgbled.h @@ -0,0 +1,89 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/include/esp32_rgbled.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_RGBLED_H +#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_RGBLED_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define RGB_LED_TIMER TIMER0 + +#define RGB_R_CHANN 0 +#define RGB_G_CHANN 1 +#define RGB_B_CHANN 2 + +/**************************************************************************** + * Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_rgbled_initialize + * + * Description: + * + * + * Input Parameters: + * devname - The name under which the RGB led will be registered + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int esp32_rgbled_initialize(const char *devname); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_RGBLED_H */ diff --git a/boards/xtensa/esp32/common/src/Make.defs b/boards/xtensa/esp32/common/src/Make.defs index 77c84a69e1..c3f39c2f9d 100644 --- a/boards/xtensa/esp32/common/src/Make.defs +++ b/boards/xtensa/esp32/common/src/Make.defs @@ -120,6 +120,10 @@ ifeq ($(CONFIG_LCD_SSD1680),y) CSRCS += esp32_ssd1680.c endif +ifeq ($(CONFIG_RGBLED),y) + CSRCS += esp32_rgbled.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src diff --git a/boards/xtensa/esp32/common/src/esp32_rgbled.c b/boards/xtensa/esp32/common/src/esp32_rgbled.c new file mode 100644 index 0000000000..fae35ac2f6 --- /dev/null +++ b/boards/xtensa/esp32/common/src/esp32_rgbled.c @@ -0,0 +1,152 @@ +/**************************************************************************** + * boards/xtensa/esp32/common/src/esp32_rgbled.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 +#include +#include +#include + +#include "esp32-sparrow-kit.h" +#include "esp32_gpio.h" +#include "esp32_ledc.h" +#include "esp32_rgbled.h" + +#if defined(CONFIG_ESP32_TIMER0) && \ + defined(CONFIG_ESP32_LEDC) && \ + defined(CONFIG_ESP32_LEDC_TIM0) && \ + (CONFIG_ESP32_LEDC_TIM0_CHANNELS >= 3) && \ + defined(CONFIG_PWM_MULTICHAN) && (CONFIG_PWM_NCHANNELS >= 3) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp32_rgbled_initialize + * + * Description: + * Initialize support for RGB LED using PWM. + * + ****************************************************************************/ + +int esp32_rgbled_initialize(const char *devname) +{ + static bool initialized = false; + FAR struct pwm_lowerhalf_s *ledr; + FAR struct pwm_lowerhalf_s *ledg; + FAR struct pwm_lowerhalf_s *ledb; + int ret; + + /* Have we already initialized? */ + + if (!initialized) + { + ledr = esp32_ledc_init(RGB_LED_TIMER); + if (!ledr) + { + lederr("ERROR: Failed to get the ESP32 PWM lower half to LEDR\n"); + return -ENODEV; + } + + /* Initialize LED R */ + + ledr->ops->setup(ledr); + + ledg = esp32_ledc_init(RGB_LED_TIMER); + if (!ledg) + { + lederr("ERROR: Failed to get the ESP32 PWM lower half to LEDG\n"); + return -ENODEV; + } + + /* Initialize LED G */ + + ledg->ops->setup(ledg); + + ledb = esp32_ledc_init(RGB_LED_TIMER); + if (!ledb) + { + lederr("ERROR: Failed to get the ESP32 PWM lower half to LEDB\n"); + return -ENODEV; + } + + /* Initialize LED B */ + + ledb->ops->setup(ledb); + + /* Register the RGB LED diver at */ + + ret = rgbled_register(devname, ledr, ledg, ledb, RGB_R_CHANN, + RGB_G_CHANN, + RGB_B_CHANN); + if (ret < 0) + { + lederr("ERROR: rgbled_register failed: %d\n", ret); + return ret; + } + + int fd = nx_open(devname, O_WRONLY); + + if (fd < 0) + { + lederr("ERROR: rgbled_open failed\n"); + return fd; + } + + /* Turn OFF the LED */ + + ret = nx_write(fd, "#000000", 8); + nx_close(fd); + + if (ret < 0) + { + lederr("ERROR: rgbled_write failed: %d\n", ret); + return ret; + } + + /* Now we are initialized */ + + initialized = true; + } + + return OK; +} + +#else +# error "RGB LED bad configuration" +#endif + diff --git a/boards/xtensa/esp32/esp32-sparrow-kit/configs/nsh/defconfig b/boards/xtensa/esp32/esp32-sparrow-kit/configs/nsh/defconfig index b0101f9130..be810647e9 100644 --- a/boards/xtensa/esp32/esp32-sparrow-kit/configs/nsh/defconfig +++ b/boards/xtensa/esp32/esp32-sparrow-kit/configs/nsh/defconfig @@ -27,11 +27,18 @@ CONFIG_BUILTIN=y CONFIG_DRIVERS_VIDEO=y CONFIG_ESP32_I2C0=y CONFIG_ESP32_I2C0_SDAPIN=21 +CONFIG_ESP32_LEDC=y +CONFIG_ESP32_LEDC_CHANNEL0_PIN=14 +CONFIG_ESP32_LEDC_CHANNEL1_PIN=13 +CONFIG_ESP32_LEDC_CHANNEL2_PIN=15 +CONFIG_ESP32_LEDC_TIM0=y +CONFIG_ESP32_LEDC_TIM0_CHANNELS=3 CONFIG_ESP32_SPI2=y CONFIG_ESP32_SPI2_CLKPIN=18 CONFIG_ESP32_SPI2_CSPIN=5 CONFIG_ESP32_SPI2_MISOPIN=19 CONFIG_ESP32_SPI2_MOSIPIN=23 +CONFIG_ESP32_TIMER0=y CONFIG_ESP32_UART0=y CONFIG_FAT_LCNAMES=y CONFIG_FAT_LFN=y @@ -58,8 +65,14 @@ CONFIG_NSH_LINELEN=64 CONFIG_NSH_MMCSDSPIPORTNO=2 CONFIG_NSH_READLINE=y CONFIG_PREALLOC_TIMERS=4 +CONFIG_PWM_MULTICHAN=y +CONFIG_PWM_NCHANNELS=3 CONFIG_RAM_SIZE=114688 CONFIG_RAM_START=0x20000000 +CONFIG_RGBLED=y +CONFIG_RGBLED_INVERT=y +CONFIG_RGBLED_LIGHTNESS_CORRECTION=y +CONFIG_RGBLED_PWM_FREQ=200 CONFIG_RR_INTERVAL=200 CONFIG_SCHED_WAITPID=y CONFIG_SENSORS=y @@ -69,5 +82,7 @@ CONFIG_START_MONTH=12 CONFIG_START_YEAR=2011 CONFIG_SYSLOG_BUFFER=y CONFIG_SYSTEM_NSH=y +CONFIG_TIMER=y +CONFIG_TIMER_ARCH=y CONFIG_UART0_SERIAL_CONSOLE=y CONFIG_VIDEO_FB=y diff --git a/boards/xtensa/esp32/esp32-sparrow-kit/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-sparrow-kit/src/esp32_bringup.c index edc68e4e74..cf8d36f95c 100644 --- a/boards/xtensa/esp32/esp32-sparrow-kit/src/esp32_bringup.c +++ b/boards/xtensa/esp32/esp32-sparrow-kit/src/esp32_bringup.c @@ -48,6 +48,10 @@ # include #endif +#ifdef CONFIG_RGBLED +# include "esp32_rgbled.h" +#endif + #ifdef CONFIG_TIMER #include #endif @@ -400,6 +404,16 @@ int esp32_bringup(void) } #endif +#ifdef CONFIG_RGBLED + /* Register RGB Driver */ + + ret = esp32_rgbled_initialize("/dev/rgbled0"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: esp32_rgbled_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.