From 4314feaa0b8cfef26896ff2d92c7c50134d26973 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Sat, 23 Sep 2023 20:42:43 +0200 Subject: [PATCH] thingy53: add rgbled support --- .../thingy53/configs/rgbled_cpuapp/defconfig | 70 ++++++++ boards/arm/nrf53/thingy53/include/board.h | 18 ++ boards/arm/nrf53/thingy53/src/CMakeLists.txt | 4 + boards/arm/nrf53/thingy53/src/Make.defs | 4 + boards/arm/nrf53/thingy53/src/nrf53_bringup.c | 8 + boards/arm/nrf53/thingy53/src/nrf53_rgbled.c | 164 ++++++++++++++++++ boards/arm/nrf53/thingy53/src/thingy53.h | 10 ++ 7 files changed, 278 insertions(+) create mode 100644 boards/arm/nrf53/thingy53/configs/rgbled_cpuapp/defconfig create mode 100644 boards/arm/nrf53/thingy53/src/nrf53_rgbled.c diff --git a/boards/arm/nrf53/thingy53/configs/rgbled_cpuapp/defconfig b/boards/arm/nrf53/thingy53/configs/rgbled_cpuapp/defconfig new file mode 100644 index 0000000000..dae00a37d5 --- /dev/null +++ b/boards/arm/nrf53/thingy53/configs/rgbled_cpuapp/defconfig @@ -0,0 +1,70 @@ +# +# 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_DEV_CONSOLE is not set +# CONFIG_SYSLOG_DEFAULT is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="thingy53" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_THINGY53=y +CONFIG_ARCH_CHIP="nrf53" +CONFIG_ARCH_CHIP_NRF5340=y +CONFIG_ARCH_CHIP_NRF5340_CPUAPP=y +CONFIG_ARCH_CHIP_NRF53=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_STDARG_H=y +CONFIG_BOARD_LOOPSPERMSEC=5500 +CONFIG_BUILTIN=y +CONFIG_CDCACM=y +CONFIG_CDCACM_CONSOLE=y +CONFIG_CDCACM_RXBUFSIZE=256 +CONFIG_CDCACM_TXBUFSIZE=256 +CONFIG_DEBUG_ASSERTIONS=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_EXAMPLES_RGBLED=y +CONFIG_EXPERIMENTAL=y +CONFIG_FS_PROCFS=y +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_MM_REGIONS=2 +CONFIG_NDEBUG=y +CONFIG_NRF53_HFCLK_XTAL=y +CONFIG_NRF53_I2C2_MASTER=y +CONFIG_NRF53_PWM0=y +CONFIG_NRF53_PWM0_CH0=y +CONFIG_NRF53_PWM0_CH1=y +CONFIG_NRF53_PWM0_CH2=y +CONFIG_NRF53_PWM1=y +CONFIG_NRF53_PWM2=y +CONFIG_NRF53_PWM_MULTICHAN=y +CONFIG_NRF53_SPI1_MASTER=y +CONFIG_NRF53_USBDEV=y +CONFIG_NRF53_USE_HFCLK192M=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=64 +CONFIG_NSH_READLINE=y +CONFIG_NSH_USBCONSOLE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PWM=y +CONFIG_PWM_MULTICHAN=y +CONFIG_PWM_NCHANNELS=3 +CONFIG_RAMLOG=y +CONFIG_RAM_SIZE=524288 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RGBLED=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SPI=y +CONFIG_START_DAY=26 +CONFIG_START_MONTH=3 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y diff --git a/boards/arm/nrf53/thingy53/include/board.h b/boards/arm/nrf53/thingy53/include/board.h index f99d4046e8..5fa53ba302 100644 --- a/boards/arm/nrf53/thingy53/include/board.h +++ b/boards/arm/nrf53/thingy53/include/board.h @@ -127,4 +127,22 @@ #define NRF52_QSPI0_IO0_PIN (GPIO_OUTPUT | GPIO_PORT0 | GPIO_PIN(13)) #define NRF52_QSPI0_IO1_PIN (GPIO_OUTPUT | GPIO_PORT0 | GPIO_PIN(14)) +/* PWM Pins *****************************************************************/ + +/* RGB LEDs: + * RED - P1.08 + * GREEN - P1.06 + * BLUE - P1.07 + */ + +#ifdef CONFIG_PWM_MULTICHAN +# define NRF53_PWM0_CH0_PIN (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT1 | GPIO_PIN(8)) +# define NRF53_PWM0_CH1_PIN (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT1 | GPIO_PIN(6)) +# define NRF53_PWM0_CH2_PIN (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT1 | GPIO_PIN(7)) +#else +# define NRF53_PWM0_CH0_PIN (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT1 | GPIO_PIN(8)) +# define NRF53_PWM1_CH0_PIN (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT1 | GPIO_PIN(6)) +# define NRF53_PWM2_CH0_PIN (GPIO_OUTPUT | GPIO_VALUE_ONE | GPIO_PORT1 | GPIO_PIN(7)) +#endif + #endif /* __BOARDS_ARM_NRF53_THINGY53_INCLUDE_BOARD_H */ diff --git a/boards/arm/nrf53/thingy53/src/CMakeLists.txt b/boards/arm/nrf53/thingy53/src/CMakeLists.txt index f9c66a4372..8f03024b83 100644 --- a/boards/arm/nrf53/thingy53/src/CMakeLists.txt +++ b/boards/arm/nrf53/thingy53/src/CMakeLists.txt @@ -40,6 +40,10 @@ if(CONFIG_USBDEV_COMPOSITE) list(APPEND SRCS nrf53_composite.c) endif() +if(CONFIG_RGBLED) + list(APPEND SRCS nrf53_rgbled.c) +endif() + target_sources(board PRIVATE ${SRCS}) if(CONFIG_ARCH_BOARD_COMMON) diff --git a/boards/arm/nrf53/thingy53/src/Make.defs b/boards/arm/nrf53/thingy53/src/Make.defs index 88fec80f9d..948444c74b 100644 --- a/boards/arm/nrf53/thingy53/src/Make.defs +++ b/boards/arm/nrf53/thingy53/src/Make.defs @@ -42,6 +42,10 @@ ifeq ($(CONFIG_USBDEV_COMPOSITE),y) CSRCS += nrf53_composite.c endif +ifeq ($(CONFIG_RGBLED),y) +CSRCS += nrf53_rgbled.c +endif + DEPPATH += --dep-path board VPATH += :board CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board diff --git a/boards/arm/nrf53/thingy53/src/nrf53_bringup.c b/boards/arm/nrf53/thingy53/src/nrf53_bringup.c index baa7af0b6e..64152d303a 100644 --- a/boards/arm/nrf53/thingy53/src/nrf53_bringup.c +++ b/boards/arm/nrf53/thingy53/src/nrf53_bringup.c @@ -248,6 +248,14 @@ int nrf53_bringup(void) } #endif +#ifdef CONFIG_RGBLED + ret = nrf53_rgbled_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: nrf53_rgbled_init failed: %d\n", ret); + } +#endif + UNUSED(ret); return OK; } diff --git a/boards/arm/nrf53/thingy53/src/nrf53_rgbled.c b/boards/arm/nrf53/thingy53/src/nrf53_rgbled.c new file mode 100644 index 0000000000..8d2adb6d22 --- /dev/null +++ b/boards/arm/nrf53/thingy53/src/nrf53_rgbled.c @@ -0,0 +1,164 @@ +/**************************************************************************** + * boards/arm/nrf53/thingy53/src/nrf53_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 "nrf53_pwm.h" +#include "thingy53.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_PWM_MULTICHAN + +/* PWM0 channels 0, 1 and 2 used */ + +# if !defined(CONFIG_NRF53_PWM0_CH0) || !defined(CONFIG_NRF53_PWM0_CH1) || \ + !defined(CONFIG_NRF53_PWM0_CH2) +# error Invalid configuration for RGBLED driver +# endif + +#else + +/* PWM0 channel 0, PWM1 channel 0 and PWM2 channel 0 used */ + +# if !defined(CONFIG_NRF53_PWM0_CH0) || !defined(CONFIG_NRF53_PWM1_CH0) || \ + !defined(CONFIG_NRF53_PWM2_CH0) +# error Invalid configuration for RGBLED driver +# endif +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf53_rgbled_initialize + * + * Description: + * Configure the RGB LED. + * + ****************************************************************************/ + +int nrf53_rgbled_initialize(void) +{ + static bool initialized = false; + struct pwm_lowerhalf_s *ledr = NULL; + struct pwm_lowerhalf_s *ledg = NULL; + struct pwm_lowerhalf_s *ledb = NULL; + struct pwm_info_s info; + int ret; + + /* Have we already initialized? */ + + if (!initialized) + { + /* Call nrf53_pwminitialize() to get an instance of the PWM interface */ + + ledr = nrf53_pwminitialize(0); + if (!ledr) + { + lederr("ERROR: Failed to get the NRF53 PWM lower half to LEDR\n"); + return -ENODEV; + } + + /* Define frequency and duty cycle */ + +#ifndef CONFIG_PWM_MULTICHAN + info.frequency = 100; + info.duty = 0; +#else + ledg = ledr; + ledb = ledr; + + info.frequency = 100; + info.channels[0].duty = 0; + info.channels[1].duty = 0; + info.channels[2].duty = 0; +#endif + + /* Initialize LED R */ + + ledr->ops->setup(ledr); + ledr->ops->start(ledr, &info); + +#ifndef CONFIG_PWM_MULTICHAN + /* Call nrf53_pwminitialize() to get an instance of the PWM interface */ + + ledg = nrf53_pwminitialize(1); + if (!ledg) + { + lederr("ERROR: Failed to get the NRF53 PWM lower half to LEDG\n"); + return -ENODEV; + } + + /* Initialize LED G */ + + ledg->ops->setup(ledg); + ledg->ops->start(ledg, &info); + + /* Call nrf53_pwminitialize() to get an instance of the PWM interface */ + + ledb = nrf53_pwminitialize(2); + if (!ledb) + { + lederr("ERROR: Failed to get the NRF53 PWM lower half to LEDB\n"); + return -ENODEV; + } + + /* Initialize LED B */ + + ledb->ops->setup(ledb); + ledb->ops->start(ledb, &info); +#endif + + /* Register the RGB LED diver at "/dev/rgbled0" */ + +#ifdef CONFIG_PWM_MULTICHAN + ret = rgbled_register("/dev/rgbled0", ledr, ledg, ledb, 1, 2, 3); +#else + ret = rgbled_register("/dev/rgbled0", ledr, ledg, ledb); +#endif + if (ret < 0) + { + lederr("ERROR: rgbled_register failed: %d\n", ret); + return ret; + } + + /* Now we are initialized */ + + initialized = true; + } + + return OK; +} diff --git a/boards/arm/nrf53/thingy53/src/thingy53.h b/boards/arm/nrf53/thingy53/src/thingy53.h index 98d96f9a20..f67e8867fb 100644 --- a/boards/arm/nrf53/thingy53/src/thingy53.h +++ b/boards/arm/nrf53/thingy53/src/thingy53.h @@ -153,5 +153,15 @@ int nrf53_bringup(void); +/**************************************************************************** + * Name: nrf53_rgbled_initialize + * + * Description: + * Configure the RGB LED. + * + ****************************************************************************/ + +int nrf53_rgbled_initialize(void); + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_NRF53_THINGY53_SRC_THINGY53_H */