1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/nrf51/periph/adc.c

82 lines
1.6 KiB
C
Raw Normal View History

2015-10-25 14:30:14 +01:00
/*
* Copyright (C) 2014-2016 Freie Universität Berlin
* 2015 Ludwig Knüpfer
2015-10-25 14:30:14 +01:00
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
2015-10-25 14:30:14 +01:00
*/
/**
* @ingroup cpu_nrf51
* @ingroup drivers_periph_adc
2015-10-25 14:30:14 +01:00
* @{
*
* @file
* @brief Low-level ADC driver implementation
2015-10-25 14:30:14 +01:00
*
* @}
*/
#include "cpu.h"
#include "mutex.h"
2015-10-25 14:30:14 +01:00
#include "periph/adc.h"
#include "periph_conf.h"
/**
* @brief Lock to prevent concurrency issues when used from different threads
*/
static mutex_t lock;
2015-10-25 14:30:14 +01:00
static inline void prep(void)
2015-10-25 14:30:14 +01:00
{
mutex_lock(&lock);
2015-10-25 14:30:14 +01:00
NRF_ADC->POWER = 1;
NRF_ADC->ENABLE = 1;
}
2015-10-25 14:30:14 +01:00
static inline void done(void)
{
NRF_ADC->ENABLE = 0;
NRF_ADC->POWER = 0;
mutex_unlock(&lock);
}
2015-10-25 14:30:14 +01:00
int adc_init(adc_t line)
{
if (line >= ADC_NUMOF) {
return -1;
2015-10-25 14:30:14 +01:00
}
return 0;
}
int32_t adc_sample(adc_t line, adc_res_t res)
2015-10-25 14:30:14 +01:00
{
int val;
2015-10-25 14:30:14 +01:00
/* check if resolution is valid */
if (res > 2) {
return -1;
2015-10-25 14:30:14 +01:00
}
/* prepare device */
prep();
/* set resolution, line, and use 1/3 input and ref voltage scaling */
NRF_ADC->CONFIG = ((ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << 5) |
(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << 2) |
(1 << (adc_config[line] + 8)) |
res);
2015-10-25 14:30:14 +01:00
/* start conversion */
NRF_ADC->TASKS_START = 1;
/* wait for conversion to be complete */
while (NRF_ADC->BUSY == 1) {}
/* get result */
val = (int)NRF_ADC->RESULT;
2015-10-25 14:30:14 +01:00
/* free device */
done();
2015-10-25 14:30:14 +01:00
return val;
2015-10-25 14:30:14 +01:00
}