Skip to content

Commit

Permalink
samples: nrf9160: add adc example
Browse files Browse the repository at this point in the history
Add a simple ADC example.

Signed-off-by: Håkon Alseth <haal@nordicsemi.no>
  • Loading branch information
Rallare committed Feb 8, 2019
1 parent 8541380 commit 0e5d440
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
12 changes: 12 additions & 0 deletions samples/nrf9160/adc/CMakeLists.txt
@@ -0,0 +1,12 @@
#
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
#

cmake_minimum_required(VERSION 3.8.2)

include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(NONE)

target_sources(app PRIVATE src/main.c)
8 changes: 8 additions & 0 deletions samples/nrf9160/adc/nrf9160_pca10090.overlay
@@ -0,0 +1,8 @@
&led0 {
gpios = <&gpio0 12 GPIO_INT_ACTIVE_LOW>;
status = "enabled";
};

&adc {
status = "ok";
};
21 changes: 21 additions & 0 deletions samples/nrf9160/adc/prj.conf
@@ -0,0 +1,21 @@
CONFIG_BSD_LIBRARY=y
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_NETWORKING=y
CONFIG_NET_BUF_USER_DATA_SIZE=1
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_NET_RAW_MODE=y
CONFIG_TRUSTED_EXECUTION_NONSECURE=y
CONFIG_LOG=n
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_HEAP_MEM_POOL_SIZE=1024
# LTE link control
CONFIG_LTE_LINK_CONTROL=n
CONFIG_ADC=y
CONFIG_ADC_0=y
CONFIG_ADC_NRFX_SAADC=y
108 changes: 108 additions & 0 deletions samples/nrf9160/adc/src/main.c
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
*/

#include <net/socket.h>
#include <nrf9160.h>
#include <stdio.h>
#include <string.h>
#include <uart.h>
#include <adc.h>
#include <zephyr.h>

struct device *adc_dev;

#if defined(CONFIG_BOARD_NRF52_PCA10040) || \
defined(CONFIG_BOARD_NRF52840_PCA10056) || \
defined(CONFIG_BOARD_NRF9160_PCA10090) || \
defined(CONFIG_BOARD_NRF52840_BLIP)

#include <hal/nrf_saadc.h>
#define ADC_DEVICE_NAME DT_ADC_0_NAME
#define ADC_RESOLUTION 10
#define ADC_GAIN ADC_GAIN_1_6
#define ADC_REFERENCE ADC_REF_INTERNAL
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)
#define ADC_1ST_CHANNEL_ID 0
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0
#define ADC_2ND_CHANNEL_ID 2
#define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2

#endif

static const struct adc_channel_cfg m_1st_channel_cfg = {
.gain = ADC_GAIN,
.reference = ADC_REFERENCE,
.acquisition_time = ADC_ACQUISITION_TIME,
.channel_id = ADC_1ST_CHANNEL_ID,
#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
.input_positive = ADC_1ST_CHANNEL_INPUT,
#endif
};

#define BUFFER_SIZE 1
static s16_t m_sample_buffer[BUFFER_SIZE];

static int adc_sample(void)
{
int ret;

const struct adc_sequence sequence = {
.channels = BIT(ADC_1ST_CHANNEL_ID),
.buffer = m_sample_buffer,
.buffer_size = sizeof(m_sample_buffer),
.resolution = ADC_RESOLUTION,
};

if (!adc_dev) {
return -1;
}

ret = adc_read(adc_dev, &sequence);
printk("ADC read err: %d\n", ret);

/* Print the AIN0 values */
for (int i = 0; i < BUFFER_SIZE; i++) {
float adc_voltage = 0;
adc_voltage = (float)(((float)m_sample_buffer[i] / 1023.0f) *
3600.0f);
printk("ADC raw value: %d\n", m_sample_buffer[i]);
printf("Measured voltage: %f mV\n", adc_voltage);
}

return ret;
}

int main(void)
{
int err;

printk("nrf91 saadc sampling AIN0 (P0.13)\n");
printk("Example requires secure_boot to have ");
printk("SAADC set to non-secure!\n");
printk("If not; BusFault/UsageFault will be triggered\n");

adc_dev = device_get_binding("ADC_0");
if (!adc_dev) {
printk("device_get_binding ADC_0 failed\n");
}
err = adc_channel_setup(adc_dev, &m_1st_channel_cfg);
if (err) {
printk("Error in adc setup: %d\n", err);
}

/* Trigger offset calibration
* As this generates a _DONE and _RESULT event
* the first result will be incorrect.
*/
NRF_SAADC_NS->TASKS_CALIBRATEOFFSET = 1;
while (1) {
err = adc_sample();
if (err) {
printk("Error in adc sampling: %d\n", err);
}
k_sleep(500);
}
}
3 changes: 3 additions & 0 deletions samples/nrf9160/secure_boot/src/main.c
Expand Up @@ -335,6 +335,9 @@ static void secure_boot_config_peripherals(void)
/* Configure TIMER2 as non-secure */
secure_boot_config_peripheral(
NRFX_PERIPHERAL_ID_GET(NRF_TIMER2_S), 0);
/* Configure SAADC as non-secure */
secure_boot_config_peripheral(
NRFX_PERIPHERAL_ID_GET(NRF_SAADC_S), 0);
}

static void secure_boot_config(void)
Expand Down

0 comments on commit 0e5d440

Please sign in to comment.