mbed library sources for airmote

Fork of mbed-src by mbed official

Committer:
zskdan
Date:
Sun Nov 22 05:08:48 2015 +0000
Revision:
624:15d19c4ec19a
Parent:
621:5d04b41f3685
no prescaling + 1.2 ref for adc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 85:e1a8e879a6a9 1 /* mbed Microcontroller Library
mbed_official 85:e1a8e879a6a9 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 85:e1a8e879a6a9 3 *
mbed_official 85:e1a8e879a6a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 85:e1a8e879a6a9 5 * you may not use this file except in compliance with the License.
mbed_official 85:e1a8e879a6a9 6 * You may obtain a copy of the License at
mbed_official 85:e1a8e879a6a9 7 *
mbed_official 85:e1a8e879a6a9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 85:e1a8e879a6a9 9 *
mbed_official 85:e1a8e879a6a9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 85:e1a8e879a6a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 85:e1a8e879a6a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 85:e1a8e879a6a9 13 * See the License for the specific language governing permissions and
mbed_official 85:e1a8e879a6a9 14 * limitations under the License.
mbed_official 85:e1a8e879a6a9 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
mbed_official 85:e1a8e879a6a9 17 #include "analogin_api.h"
mbed_official 85:e1a8e879a6a9 18 #include "cmsis.h"
mbed_official 85:e1a8e879a6a9 19 #include "pinmap.h"
mbed_official 85:e1a8e879a6a9 20
mbed_official 85:e1a8e879a6a9 21 #define ANALOGIN_MEDIAN_FILTER 1
mbed_official 85:e1a8e879a6a9 22 #define ADC_10BIT_RANGE 0x3FF
mbed_official 85:e1a8e879a6a9 23 #define ADC_RANGE ADC_10BIT_RANGE
mbed_official 85:e1a8e879a6a9 24
mbed_official 85:e1a8e879a6a9 25 static const PinMap PinMap_ADC[] = {
zskdan 624:15d19c4ec19a 26 {p26, ADC0_0, 1},
zskdan 624:15d19c4ec19a 27 {p27, ADC0_0, 2},
mbed_official 85:e1a8e879a6a9 28 {p1, ADC0_0, 4},
mbed_official 85:e1a8e879a6a9 29 {p2, ADC0_0, 8},
mbed_official 85:e1a8e879a6a9 30 {p3, ADC0_0, 16},
mbed_official 85:e1a8e879a6a9 31 {p4, ADC0_0, 32},
mbed_official 85:e1a8e879a6a9 32 {p5, ADC0_0, 64},
mbed_official 85:e1a8e879a6a9 33 {p6, ADC0_0, 128},
mbed_official 300:55638feb26a4 34 {NC, NC, 0}
mbed_official 85:e1a8e879a6a9 35 };
mbed_official 85:e1a8e879a6a9 36
mbed_official 300:55638feb26a4 37 void analogin_init(analogin_t *obj, PinName pin)
mbed_official 300:55638feb26a4 38 {
mbed_official 300:55638feb26a4 39 int analogInputPin = 0;
mbed_official 300:55638feb26a4 40 const PinMap *map = PinMap_ADC;
mbed_official 300:55638feb26a4 41
mbed_official 85:e1a8e879a6a9 42 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *)
mbed_official 227:7bd0639b8911 43 MBED_ASSERT(obj->adc != (ADCName)NC);
mbed_official 300:55638feb26a4 44
mbed_official 85:e1a8e879a6a9 45 while (map->pin != NC) {
mbed_official 300:55638feb26a4 46 if (map->pin == pin) {
mbed_official 85:e1a8e879a6a9 47 analogInputPin = map->function;
mbed_official 85:e1a8e879a6a9 48 break;
mbed_official 85:e1a8e879a6a9 49 }
mbed_official 85:e1a8e879a6a9 50 map++;
mbed_official 85:e1a8e879a6a9 51 }
mbed_official 217:d0ccc61c1fd4 52 obj->adc_pin = (uint8_t)analogInputPin;
mbed_official 300:55638feb26a4 53
mbed_official 227:7bd0639b8911 54 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
mbed_official 85:e1a8e879a6a9 55 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
zskdan 624:15d19c4ec19a 56 //(ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
zskdan 624:15d19c4ec19a 57 (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos) |
zskdan 624:15d19c4ec19a 58 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |
mbed_official 85:e1a8e879a6a9 59 (analogInputPin << ADC_CONFIG_PSEL_Pos) |
mbed_official 227:7bd0639b8911 60 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
mbed_official 85:e1a8e879a6a9 61 }
mbed_official 85:e1a8e879a6a9 62
mbed_official 300:55638feb26a4 63 uint16_t analogin_read_u16(analogin_t *obj)
mbed_official 300:55638feb26a4 64 {
mbed_official 300:55638feb26a4 65 NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk;
mbed_official 300:55638feb26a4 66 NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos;
mbed_official 85:e1a8e879a6a9 67 NRF_ADC->TASKS_START = 1;
mbed_official 300:55638feb26a4 68 while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {
mbed_official 85:e1a8e879a6a9 69 }
mbed_official 300:55638feb26a4 70
mbed_official 85:e1a8e879a6a9 71 return (uint16_t)NRF_ADC->RESULT; // 10 bit
mbed_official 85:e1a8e879a6a9 72 }
mbed_official 85:e1a8e879a6a9 73
mbed_official 300:55638feb26a4 74 float analogin_read(analogin_t *obj)
mbed_official 300:55638feb26a4 75 {
mbed_official 85:e1a8e879a6a9 76 uint16_t value = analogin_read_u16(obj);
mbed_official 85:e1a8e879a6a9 77 return (float)value * (1.0f / (float)ADC_RANGE);
mbed_official 85:e1a8e879a6a9 78 }