diff --git a/boards/arm/nrf52/nrf52840-dk/configs/adc/defconfig b/boards/arm/nrf52/nrf52840-dk/configs/adc/defconfig new file mode 100644 index 00000000000..6256edb3d57 --- /dev/null +++ b/boards/arm/nrf52/nrf52840-dk/configs/adc/defconfig @@ -0,0 +1,53 @@ +# +# 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_FPU is not set +# CONFIG_NSH_DISABLE_IFCONFIG is not set +# CONFIG_NSH_DISABLE_PS is not set +CONFIG_ADC=y +CONFIG_ANALOG=y +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="nrf52840-dk" +CONFIG_ARCH_BOARD_NRF52840_DK=y +CONFIG_ARCH_CHIP="nrf52" +CONFIG_ARCH_CHIP_NRF52840=y +CONFIG_ARCH_CHIP_NRF52=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_STDARG_H=y +CONFIG_BOARD_LOOPSPERMSEC=5500 +CONFIG_BUILTIN=y +CONFIG_EXAMPLES_ADC=y +CONFIG_EXAMPLES_ADC_NSAMPLES=4 +CONFIG_EXAMPLES_ADC_SWTRIG=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_MAX_TASKS=16 +CONFIG_MM_REGIONS=2 +CONFIG_NFILE_DESCRIPTORS=8 +CONFIG_NFILE_STREAMS=8 +CONFIG_NRF52_SAADC=y +CONFIG_NRF52_UART0=y +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=65535 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SDCLONE_DISABLE=y +CONFIG_START_DAY=26 +CONFIG_START_MONTH=3 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/boards/arm/nrf52/nrf52840-dk/configs/pwm/defconfig b/boards/arm/nrf52/nrf52840-dk/configs/pwm/defconfig new file mode 100644 index 00000000000..23caee845a3 --- /dev/null +++ b/boards/arm/nrf52/nrf52840-dk/configs/pwm/defconfig @@ -0,0 +1,57 @@ +# +# 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_FPU is not set +# CONFIG_NSH_DISABLE_IFCONFIG is not set +# CONFIG_NSH_DISABLE_PS is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="nrf52840-dk" +CONFIG_ARCH_BOARD_NRF52840_DK=y +CONFIG_ARCH_CHIP="nrf52" +CONFIG_ARCH_CHIP_NRF52840=y +CONFIG_ARCH_CHIP_NRF52=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_STDARG_H=y +CONFIG_BOARD_LOOPSPERMSEC=5500 +CONFIG_BUILTIN=y +CONFIG_EXAMPLES_PWM=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_MAX_TASKS=16 +CONFIG_MM_REGIONS=2 +CONFIG_NFILE_DESCRIPTORS=8 +CONFIG_NFILE_STREAMS=8 +CONFIG_NRF52_PWM0=y +CONFIG_NRF52_PWM0_CH0=y +CONFIG_NRF52_PWM0_CH1=y +CONFIG_NRF52_PWM0_CH2=y +CONFIG_NRF52_PWM0_CH3=y +CONFIG_NRF52_PWM_MULTICHAN=y +CONFIG_NRF52_UART0=y +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_PWM=y +CONFIG_PWM_MULTICHAN=y +CONFIG_PWM_NCHANNELS=4 +CONFIG_RAM_SIZE=65535 +CONFIG_RAM_START=0x20000000 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_WAITPID=y +CONFIG_SDCLONE_DISABLE=y +CONFIG_START_DAY=26 +CONFIG_START_MONTH=3 +CONFIG_SYMTAB_ORDEREDBYNAME=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_USER_ENTRYPOINT="nsh_main" diff --git a/boards/arm/nrf52/nrf52840-dk/include/board.h b/boards/arm/nrf52/nrf52840-dk/include/board.h index de5cfce11a3..d4ab9b4ddc0 100644 --- a/boards/arm/nrf52/nrf52840-dk/include/board.h +++ b/boards/arm/nrf52/nrf52840-dk/include/board.h @@ -152,4 +152,32 @@ #define BOARD_I2C0_SCL_PIN (GPIO_OUTPUT | GPIO_PORT0 | GPIO_PIN(27)) #define BOARD_I2C0_SDA_PIN (GPIO_INPUT | GPIO_PORT0 | GPIO_PIN(26)) +/* PWM Pins *****************************************************************/ + +/* PWM 0: + * PWM0 CH0 - P1.1 + * PWM0 CH1 - P1.2 + * PWM0 CH2 - P1.3 + * PWM0 CH3 - P1.4 + */ + +#define NRF52_PWM0_CH0_PIN (GPIO_OUTPUT | GPIO_PORT1 | GPIO_PIN(1)) +#define NRF52_PWM0_CH1_PIN (GPIO_OUTPUT | GPIO_PORT1 | GPIO_PIN(2)) +#define NRF52_PWM0_CH2_PIN (GPIO_OUTPUT | GPIO_PORT1 | GPIO_PIN(3)) +#define NRF52_PWM0_CH3_PIN (GPIO_OUTPUT | GPIO_PORT1 | GPIO_PIN(4)) + +/* ADC Pins *****************************************************************/ + +/* ADC + * ADC CH0 - P0.03 - AIN1 + * ADC CH1 - P0.04 - AIN2 + * ADC CH2 - P0.28 - AIN4 + * ADC CH3 - P0.29 - AIN5 + */ + +#define NRF52_ADC_CH0_PIN (GPIO_INPUT | GPIO_PORT0 | GPIO_PIN(3)) +#define NRF52_ADC_CH1_PIN (GPIO_INPUT | GPIO_PORT0 | GPIO_PIN(4)) +#define NRF52_ADC_CH2_PIN (GPIO_INPUT | GPIO_PORT0 | GPIO_PIN(28)) +#define NRF52_ADC_CH3_PIN (GPIO_INPUT | GPIO_PORT0 | GPIO_PIN(29)) + #endif /* __BOARDS_ARM_NRF52_NRF52840_DK_INCLUDE_BOARD_H */ diff --git a/boards/arm/nrf52/nrf52840-dk/src/Makefile b/boards/arm/nrf52/nrf52840-dk/src/Makefile index f02bab60f02..3c9cc0a249a 100644 --- a/boards/arm/nrf52/nrf52840-dk/src/Makefile +++ b/boards/arm/nrf52/nrf52840-dk/src/Makefile @@ -81,4 +81,12 @@ CSRCS += nrf52_timer.c endif endif +ifeq ($(CONFIG_PWM),y) +CSRCS += nrf52_pwm.c +endif + +ifeq ($(CONFIG_ADC),y) +CSRCS += nrf52_adc.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52840-dk.h b/boards/arm/nrf52/nrf52840-dk/src/nrf52840-dk.h index 93f28eec5ef..a5500368f93 100644 --- a/boards/arm/nrf52/nrf52840-dk/src/nrf52840-dk.h +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52840-dk.h @@ -180,5 +180,29 @@ int nrf52_lpwaninitialize(void); int nrf52_timer_driver_setup(FAR const char *devpath, int timer); #endif +/**************************************************************************** + * Name: nrf52_pwm_setup + * + * Description: + * Initialize PWM driver. + * + ****************************************************************************/ + +#ifdef CONFIG_PWM +int nrf52_pwm_setup(void); +#endif + +/**************************************************************************** + * Name: nrf52_adc_setup + * + * Description: + * Initialize ADC driver. + * + ****************************************************************************/ + +#ifdef CONFIG_ADC +int nrf52_adc_setup(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_NRF52_NRF52840_DK_SRC_NRF52840_DK_H */ diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52_adc.c b/boards/arm/nrf52/nrf52840-dk/src/nrf52_adc.c new file mode 100644 index 00000000000..c9f7dfa0bc0 --- /dev/null +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52_adc.c @@ -0,0 +1,200 @@ +/**************************************************************************** + * boards/arm/nrf52/nrf52840-dk/src/nrf52_adc.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 "nrf52_gpio.h" +#include "nrf52_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Only one channel supported if TIMER triger enabled */ + +#ifdef CONFIG_NRF52_SAADC_TIMER +# define ADC_NCHANNELS (1) +#else +# define ADC_NCHANNELS (4) +#endif + +/**************************************************************************** + * Private data + ****************************************************************************/ + +/* ADC pins configuration */ + +static uint32_t g_adc_pins[ADC_NCHANNELS] = +{ + NRF52_ADC_CH0_PIN, /* AIN1 */ +#if (ADC_NCHANNELS == 4) + NRF52_ADC_CH1_PIN, /* AIN2 */ + NRF52_ADC_CH2_PIN, /* AIN4 */ + NRF52_ADC_CH3_PIN, /* AIN5 */ +#endif +}; + +/* ADC channels configuration */ + +static struct nrf52_adc_channel_s g_adc_chanlist[ADC_NCHANNELS] = +{ + /* Channel 0 */ + + { + .p_psel = NRF52_ADC_IN_IN1, + .n_psel = 0, +#ifdef CONFIG_NRF52_SAADC_LIMITS + .limith = 0, + .limitl = 0, +#endif + .resp = NRF52_ADC_RES_BYPASS, + .resn = NRF52_ADC_RES_BYPASS, + .gain = NRF52_ADC_GAIN_1, + .refsel = NRF52_ADC_REFSEL_INTERNAL, + .tacq = NRF52_ADC_TACQ_40US, + .mode = NRF52_ADC_MODE_SE, + .burst = NRF52_ADC_BURST_DISABLE, + }, + +#if (ADC_NCHANNELS == 4) + /* Channel 1 */ + + { + .p_psel = NRF52_ADC_IN_IN2, + .n_psel = 0, +#ifdef CONFIG_NRF52_SAADC_LIMITS + .limith = 0, + .limitl = 0, +#endif + .resp = NRF52_ADC_RES_BYPASS, + .resn = NRF52_ADC_RES_BYPASS, + .gain = NRF52_ADC_GAIN_1, + .refsel = NRF52_ADC_REFSEL_INTERNAL, + .tacq = NRF52_ADC_TACQ_40US, + .mode = NRF52_ADC_MODE_SE, + .burst = NRF52_ADC_BURST_DISABLE, + }, + + /* Channel 2 */ + + { + .p_psel = NRF52_ADC_IN_IN4, + .n_psel = 0, +#ifdef CONFIG_NRF52_SAADC_LIMITS + .limith = 0, + .limitl = 0, +#endif + .resp = NRF52_ADC_RES_BYPASS, + .resn = NRF52_ADC_RES_BYPASS, + .gain = NRF52_ADC_GAIN_1, + .refsel = NRF52_ADC_REFSEL_INTERNAL, + .tacq = NRF52_ADC_TACQ_40US, + .mode = NRF52_ADC_MODE_SE, + .burst = NRF52_ADC_BURST_DISABLE, + }, + + /* Channel 3 */ + + { + .p_psel = NRF52_ADC_IN_IN5, + .n_psel = 0, +#ifdef CONFIG_NRF52_SAADC_LIMITS + .limith = 0, + .limitl = 0, +#endif + .resp = NRF52_ADC_RES_BYPASS, + .resn = NRF52_ADC_RES_BYPASS, + .gain = NRF52_ADC_GAIN_1, + .refsel = NRF52_ADC_REFSEL_INTERNAL, + .tacq = NRF52_ADC_TACQ_40US, + .mode = NRF52_ADC_MODE_SE, + .burst = NRF52_ADC_BURST_DISABLE, + } +#endif +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf52_adc_setup + * + * Description: + * Initialize ADC and register the ADC device. + * + ****************************************************************************/ + +int nrf52_adc_setup(void) +{ + static bool initialized = false; + struct adc_dev_s *adc = NULL; + int ret = OK; + int i = 0; + + /* Have we already initialized? */ + + if (!initialized) + { + /* Configure ADC pins */ + + for (i = 0; i < ADC_NCHANNELS; i += 1) + { + nrf52_gpio_config(g_adc_pins[i]); + } + + /* Call nrf52_adcinitialize() to get an instance of the ADC interface */ + + adc = nrf52_adcinitialize(g_adc_chanlist, ADC_NCHANNELS); + if (!adc) + { + aerr("ERROR: Failed to get the NRF52 ADC lower half\n"); + ret = -ENODEV; + goto errout; + } + + /* Register the ADC driver at "/dev/adc0" */ + + ret = adc_register("/dev/adc0", adc); + if (ret < 0) + { + aerr("ERROR: adc_register failed: %d\n", ret); + goto errout; + } + + /* Now we are initialized */ + + initialized = true; + } + +errout: + return ret; +} diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c b/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c index 1d08e254858..34bc0005e1a 100644 --- a/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52_bringup.c @@ -192,6 +192,30 @@ int nrf52_bringup(void) } #endif +#ifdef CONFIG_PWM + /* Configure PWM driver */ + + ret = nrf52_pwm_setup(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize PWM driver: %d\n", + ret); + } +#endif + +#ifdef CONFIG_ADC + /* Configure ADC driver */ + + ret = nrf52_adc_setup(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize ADC driver: %d\n", + ret); + } +#endif + UNUSED(ret); return OK; } diff --git a/boards/arm/nrf52/nrf52840-dk/src/nrf52_pwm.c b/boards/arm/nrf52/nrf52840-dk/src/nrf52_pwm.c new file mode 100644 index 00000000000..75ded836ac8 --- /dev/null +++ b/boards/arm/nrf52/nrf52840-dk/src/nrf52_pwm.c @@ -0,0 +1,90 @@ +/**************************************************************************** + * boards/arm/nrf52/nrf52840-dk/src/nrf52_pwm.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 "nrf52_pwm.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NRF52_PWM (0) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf52_pwm_setup + * + * Description: + * Initialize PWM and register the PWM device. + * + ****************************************************************************/ + +int nrf52_pwm_setup(void) +{ + static bool initialized = false; + struct pwm_lowerhalf_s *pwm = NULL; + int ret = OK; + + /* Have we already initialized? */ + + if (!initialized) + { + /* Call nrf52_pwminitialize() to get an instance of the PWM interface */ + + pwm = nrf52_pwminitialize(NRF52_PWM); + if (!pwm) + { + aerr("ERROR: Failed to get the NRF52 PWM lower half\n"); + ret = -ENODEV; + goto errout; + } + + /* Register the PWM driver at "/dev/pwm0" */ + + ret = pwm_register("/dev/pwm0", pwm); + if (ret < 0) + { + aerr("ERROR: pwm_register failed: %d\n", ret); + goto errout; + } + + /* Now we are initialized */ + + initialized = true; + } + +errout: + return ret; +}