1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "mbed_assert.h"
17 #include "analogin_api.h"
21 #define ANALOGIN_MEDIAN_FILTER 1
22 #define ADC_10BIT_RANGE 0x3FF
23 #define ADC_RANGE ADC_10BIT_RANGE
25 static const PinMap PinMap_ADC[] = {
35 void analogin_init(analogin_t *obj, PinName pin)
37 int analogInputPin = 0;
38 const PinMap *map = PinMap_ADC;
40 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
41 MBED_ASSERT(obj->adc != (ADCName)NC);
43 while (map->pin != NC) {
44 if (map->pin == pin) {
45 analogInputPin = map->function;
50 obj->adc_pin = (uint8_t)analogInputPin;
52 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
53 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
54 (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
55 (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
56 (analogInputPin << ADC_CONFIG_PSEL_Pos) |
57 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
60 uint16_t analogin_read_u16(analogin_t *obj)
62 NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
63 NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
64 NRF_ADC->TASKS_START = 1;
65 while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
68 return (uint16_t)NRF_ADC->RESULT; // 10 bit
71 float analogin_read(analogin_t *obj)
73 uint16_t value = analogin_read_u16(obj);
74 return (float)value * (1.0f / (float)ADC_RANGE);