diff --git a/boards/arm/nrf53/thingy53/src/CMakeLists.txt b/boards/arm/nrf53/thingy53/src/CMakeLists.txt index 7d4769cfe49..6343b3aae82 100644 --- a/boards/arm/nrf53/thingy53/src/CMakeLists.txt +++ b/boards/arm/nrf53/thingy53/src/CMakeLists.txt @@ -50,6 +50,10 @@ if(CONFIG_RGBLED) list(APPEND SRCS nrf53_rgbled.c) endif() +if(CONFIG_ADC) + list(APPEND SRCS nrf53_adc.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 75c4b581370..98481e0de5f 100644 --- a/boards/arm/nrf53/thingy53/src/Make.defs +++ b/boards/arm/nrf53/thingy53/src/Make.defs @@ -52,6 +52,10 @@ ifeq ($(CONFIG_RGBLED),y) CSRCS += nrf53_rgbled.c endif +ifeq ($(CONFIG_ADC),y) +CSRCS += nrf53_adc.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_adc.c b/boards/arm/nrf53/thingy53/src/nrf53_adc.c new file mode 100644 index 00000000000..66b051808ca --- /dev/null +++ b/boards/arm/nrf53/thingy53/src/nrf53_adc.c @@ -0,0 +1,146 @@ +/**************************************************************************** + * boards/arm/nrf53/thingy53/src/nrf53_adc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "nrf53_gpio.h" +#include "nrf53_adc.h" + +#include "thingy53.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Only one channel supported if TIMER trigger enabled */ + +#define ADC_NCHANNELS (1) + +/**************************************************************************** + * Private data + ****************************************************************************/ + +/* ADC pins configuration */ + +static uint32_t g_adc_pins[ADC_NCHANNELS] = +{ + NRF53_ADC_CH0_PIN, /* AIN2 */ +}; + +/* ADC channels configuration */ + +static struct nrf53_adc_channel_s g_adc_chanlist[ADC_NCHANNELS] = +{ + /* Channel 0 */ + + { + .p_psel = NRF53_ADC_IN_IN2, /* AIN2 / P0.06 - BAT_MEAS */ + .n_psel = 0, +#ifdef CONFIG_NRF53_SAADC_LIMITS + .limith = 0, + .limitl = 0, +#endif + .resp = NRF53_ADC_RES_BYPASS, + .resn = NRF53_ADC_RES_BYPASS, + .gain = NRF53_ADC_GAIN_1, + .refsel = NRF53_ADC_REFSEL_INTERNAL, + .tacq = NRF53_ADC_TACQ_40US, + .mode = NRF53_ADC_MODE_SE, + .burst = NRF53_ADC_BURST_DISABLE, + }, +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf53_adc_setup + * + * Description: + * Initialize ADC and register the ADC device. + * + ****************************************************************************/ + +int nrf53_adc_setup(void) +{ + static bool initialized = false; + struct adc_dev_s *adc = NULL; + int ret = OK; + int i = 0; + + /* Enable battery measurement. + * TODO: this should be controlled by power management logic + */ + + nrf53_gpio_config(GPIO_BATT_EN); + nrf53_gpio_write(GPIO_BATT_EN, true); + + /* Have we already initialized? */ + + if (!initialized) + { + /* Configure ADC pins */ + + for (i = 0; i < ADC_NCHANNELS; i += 1) + { + nrf53_gpio_config(g_adc_pins[i]); + } + + /* Call nrf53_adcinitialize() to get an instance of the ADC interface */ + + adc = nrf53_adcinitialize(g_adc_chanlist, ADC_NCHANNELS); + if (!adc) + { + aerr("ERROR: Failed to get the NRF53 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/nrf53/thingy53/src/nrf53_bringup.c b/boards/arm/nrf53/thingy53/src/nrf53_bringup.c index f10c5c64c5a..0ce83f60cca 100644 --- a/boards/arm/nrf53/thingy53/src/nrf53_bringup.c +++ b/boards/arm/nrf53/thingy53/src/nrf53_bringup.c @@ -211,6 +211,7 @@ int nrf53_bringup(void) #if defined(CONFIG_RNDIS) && !defined(CONFIG_RNDIS_COMPOSITE) uint8_t mac[6]; + mac[0] = 0xa0; /* TODO */ mac[1] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 0)) & 0xff; mac[2] = (CONFIG_NETINIT_MACADDR_1 >> (8 * 3)) & 0xff; @@ -248,6 +249,18 @@ int nrf53_bringup(void) } #endif +#ifdef CONFIG_ADC + /* Configure ADC driver */ + + ret = nrf53_adc_setup(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: Failed to initialize ADC driver: %d\n", + ret); + } +#endif + /* Initialize on-board sensors */ ret = nrf53_sensors_init(); diff --git a/boards/arm/nrf53/thingy53/src/thingy53.h b/boards/arm/nrf53/thingy53/src/thingy53.h index 4cae9e27118..58bcc0d4bb9 100644 --- a/boards/arm/nrf53/thingy53/src/thingy53.h +++ b/boards/arm/nrf53/thingy53/src/thingy53.h @@ -217,5 +217,17 @@ int nrf53_i2ctool(void); void nrf53_spidev_initialize(void); #endif +/**************************************************************************** + * Name: nrf53_adc_setup + * + * Description: + * Initialize ADC driver. + * + ****************************************************************************/ + +#ifdef CONFIG_ADC +int nrf53_adc_setup(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_NRF53_THINGY53_SRC_THINGY53_H */