mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_NRF51822/analogin_api.c@85:e1a8e879a6a9, 2014-02-07 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Feb 07 18:00:11 2014 +0000
- Revision:
- 85:e1a8e879a6a9
- Child:
- 217:d0ccc61c1fd4
Synchronized with git revision 4b2b368a6a3b1f0fd33d99917981c67436c4aebe
Full URL: https://github.com/mbedmicro/mbed/commit/4b2b368a6a3b1f0fd33d99917981c67436c4aebe/
Who changed what in which revision?
User | Revision | Line number | New 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 | 85:e1a8e879a6a9 | 16 | #include "analogin_api.h" |
mbed_official | 85:e1a8e879a6a9 | 17 | #include "cmsis.h" |
mbed_official | 85:e1a8e879a6a9 | 18 | #include "pinmap.h" |
mbed_official | 85:e1a8e879a6a9 | 19 | #include "error.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[] = { |
mbed_official | 85:e1a8e879a6a9 | 26 | {p1, ADC0_0, 4}, |
mbed_official | 85:e1a8e879a6a9 | 27 | {p2, ADC0_0, 8}, |
mbed_official | 85:e1a8e879a6a9 | 28 | {p3, ADC0_0, 16}, |
mbed_official | 85:e1a8e879a6a9 | 29 | {p4, ADC0_0, 32}, |
mbed_official | 85:e1a8e879a6a9 | 30 | {p5, ADC0_0, 64}, |
mbed_official | 85:e1a8e879a6a9 | 31 | {p6, ADC0_0, 128}, |
mbed_official | 85:e1a8e879a6a9 | 32 | {NC , NC , 0} |
mbed_official | 85:e1a8e879a6a9 | 33 | }; |
mbed_official | 85:e1a8e879a6a9 | 34 | |
mbed_official | 85:e1a8e879a6a9 | 35 | void analogin_init(analogin_t *obj, PinName pin) { |
mbed_official | 85:e1a8e879a6a9 | 36 | int analogInputPin=0; |
mbed_official | 85:e1a8e879a6a9 | 37 | const PinMap *map = PinMap_ADC; |
mbed_official | 85:e1a8e879a6a9 | 38 | |
mbed_official | 85:e1a8e879a6a9 | 39 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *) |
mbed_official | 85:e1a8e879a6a9 | 40 | if (obj->adc == (ADCName)NC) { |
mbed_official | 85:e1a8e879a6a9 | 41 | error("ADC pin mapping failed"); |
mbed_official | 85:e1a8e879a6a9 | 42 | } |
mbed_official | 85:e1a8e879a6a9 | 43 | |
mbed_official | 85:e1a8e879a6a9 | 44 | while (map->pin != NC) { |
mbed_official | 85:e1a8e879a6a9 | 45 | if (map->pin == pin){ |
mbed_official | 85:e1a8e879a6a9 | 46 | analogInputPin = map->function; |
mbed_official | 85:e1a8e879a6a9 | 47 | break; |
mbed_official | 85:e1a8e879a6a9 | 48 | } |
mbed_official | 85:e1a8e879a6a9 | 49 | map++; |
mbed_official | 85:e1a8e879a6a9 | 50 | } |
mbed_official | 85:e1a8e879a6a9 | 51 | |
mbed_official | 85:e1a8e879a6a9 | 52 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
mbed_official | 85:e1a8e879a6a9 | 53 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 54 | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 55 | (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 56 | (analogInputPin << ADC_CONFIG_PSEL_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 57 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 58 | } |
mbed_official | 85:e1a8e879a6a9 | 59 | |
mbed_official | 85:e1a8e879a6a9 | 60 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 61 | NRF_ADC->TASKS_START = 1; |
mbed_official | 85:e1a8e879a6a9 | 62 | while ( ( (NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) |
mbed_official | 85:e1a8e879a6a9 | 63 | { |
mbed_official | 85:e1a8e879a6a9 | 64 | } |
mbed_official | 85:e1a8e879a6a9 | 65 | |
mbed_official | 85:e1a8e879a6a9 | 66 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
mbed_official | 85:e1a8e879a6a9 | 67 | } |
mbed_official | 85:e1a8e879a6a9 | 68 | |
mbed_official | 85:e1a8e879a6a9 | 69 | float analogin_read(analogin_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 70 | uint16_t value = analogin_read_u16(obj); |
mbed_official | 85:e1a8e879a6a9 | 71 | return (float)value * (1.0f / (float)ADC_RANGE); |
mbed_official | 85:e1a8e879a6a9 | 72 | } |