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

104 lines
2.0 KiB
C
Raw Normal View History

/*
2016-02-14 17:47:11 +01:00
* Copyright (C) 2015-2016 Freie Universität Berlin
*
2016-02-14 17:47:11 +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.
*/
/**
* @ingroup cpu_lpc11u34
* @{
*
* @file
* @brief Low-level ADC driver implementation
*
2016-02-14 17:47:11 +01:00
* @author Paul Rathgeb <paul.rathgeb@skynet.be>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdint.h>
#include "cpu.h"
2016-02-14 17:47:11 +01:00
#include "mutex.h"
#include "periph/adc.h"
2016-02-14 17:47:11 +01:00
/**
* @brief Mutex to synchronize ADC access from different threads
*/
static mutex_t lock = MUTEX_INIT;
2016-02-14 17:47:11 +01:00
static inline uint32_t *pincfg_reg(adc_t line)
{
2016-02-14 17:47:11 +01:00
int offset = (line < 6) ? (11 + line) : (16 + line);
return ((uint32_t *)(LPC_IOCON) + offset);
}
2016-02-14 17:47:11 +01:00
static inline void prep(void)
{
2016-02-14 17:47:11 +01:00
mutex_lock(&lock);
LPC_SYSCON->PDRUNCFG &= ~(1 << 4);
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13);
}
2016-02-14 17:47:11 +01:00
static inline void done(void)
{
LPC_SYSCON->SYSAHBCLKCTRL &= ~(1 << 13);
LPC_SYSCON->PDRUNCFG |= (1 << 4);
mutex_unlock(&lock);
}
2016-02-14 17:47:11 +01:00
int adc_init(adc_t line)
{
2016-02-14 17:47:11 +01:00
uint32_t *pincfg;
prep();
2016-02-14 17:47:11 +01:00
/* ADC frequency : 3MHz */
LPC_ADC->CR = (15 << 8);
/* configure the connected pin */
pincfg = pincfg_reg(line);
/* Put the pin in its ADC alternate function */
2016-02-14 17:47:11 +01:00
if (line < 5) {
*pincfg |= 2;
}
else {
2016-02-14 17:47:11 +01:00
*pincfg |= 1;
}
/* Configure ADMODE in analog input */
2016-02-14 17:47:11 +01:00
*pincfg &= ~(1 << 7);
2016-02-14 17:47:11 +01:00
done();
return 0;
}
2016-02-14 17:47:11 +01:00
int adc_sample(adc_t line, adc_res_t res)
{
2016-02-14 17:47:11 +01:00
int sample;
/* check if resolution is valid */
if (res < 0xff) {
return -1;
}
2016-02-14 17:47:11 +01:00
/* prepare the device */
prep();
2016-02-14 17:47:11 +01:00
/* set resolution */
LPC_ADC->CR &= ~(0x7 << 17);
LPC_ADC->CR |= res;
/* Start a conversion */
LPC_ADC->CR |= (1 << line) | (1 << 24);
/* Wait for the end of the conversion */
while (!(LPC_ADC->DR[line] & (1 << 31))) {}
/* Read and return result */
sample = (LPC_ADC->DR[line] >> 6);
2016-02-14 17:47:11 +01:00
done();
return sample;
}