forked from nrfconnect/sdk-nrf
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple ADC example. Signed-off-by: Håkon Alseth <haal@nordicsemi.no>
- Loading branch information
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
&led0 { | ||
gpios = <&gpio0 12 GPIO_INT_ACTIVE_LOW>; | ||
status = "enabled"; | ||
}; | ||
|
||
&adc { | ||
status = "ok"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters