- fix F411 F334 systeminit when HSI used - portinout always read IDR regardless of port direction
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/analogin_api.c@300:55638feb26a4, 2014-08-27 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Aug 27 08:45:06 2014 +0100
- Revision:
- 300:55638feb26a4
- Parent:
- 270:e2babe29baf8
Synchronized with git revision 90467175c04ad81c20cdf7b2ddb86c54f5f3dcbb
Full URL: https://github.com/mbedmicro/mbed/commit/90467175c04ad81c20cdf7b2ddb86c54f5f3dcbb/
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 | 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[] = { |
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 | 300:55638feb26a4 | 32 | {NC, NC, 0} |
mbed_official | 85:e1a8e879a6a9 | 33 | }; |
mbed_official | 85:e1a8e879a6a9 | 34 | |
mbed_official | 300:55638feb26a4 | 35 | void analogin_init(analogin_t *obj, PinName pin) |
mbed_official | 300:55638feb26a4 | 36 | { |
mbed_official | 300:55638feb26a4 | 37 | int analogInputPin = 0; |
mbed_official | 300:55638feb26a4 | 38 | const PinMap *map = PinMap_ADC; |
mbed_official | 300:55638feb26a4 | 39 | |
mbed_official | 85:e1a8e879a6a9 | 40 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *) |
mbed_official | 227:7bd0639b8911 | 41 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mbed_official | 300:55638feb26a4 | 42 | |
mbed_official | 85:e1a8e879a6a9 | 43 | while (map->pin != NC) { |
mbed_official | 300:55638feb26a4 | 44 | if (map->pin == pin) { |
mbed_official | 85:e1a8e879a6a9 | 45 | analogInputPin = map->function; |
mbed_official | 85:e1a8e879a6a9 | 46 | break; |
mbed_official | 85:e1a8e879a6a9 | 47 | } |
mbed_official | 85:e1a8e879a6a9 | 48 | map++; |
mbed_official | 85:e1a8e879a6a9 | 49 | } |
mbed_official | 217:d0ccc61c1fd4 | 50 | obj->adc_pin = (uint8_t)analogInputPin; |
mbed_official | 300:55638feb26a4 | 51 | |
mbed_official | 227:7bd0639b8911 | 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 | 300:55638feb26a4 | 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 | 227:7bd0639b8911 | 57 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 58 | } |
mbed_official | 85:e1a8e879a6a9 | 59 | |
mbed_official | 300:55638feb26a4 | 60 | uint16_t analogin_read_u16(analogin_t *obj) |
mbed_official | 300:55638feb26a4 | 61 | { |
mbed_official | 300:55638feb26a4 | 62 | NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
mbed_official | 300:55638feb26a4 | 63 | NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos; |
mbed_official | 85:e1a8e879a6a9 | 64 | NRF_ADC->TASKS_START = 1; |
mbed_official | 300:55638feb26a4 | 65 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) { |
mbed_official | 85:e1a8e879a6a9 | 66 | } |
mbed_official | 300:55638feb26a4 | 67 | |
mbed_official | 85:e1a8e879a6a9 | 68 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
mbed_official | 85:e1a8e879a6a9 | 69 | } |
mbed_official | 85:e1a8e879a6a9 | 70 | |
mbed_official | 300:55638feb26a4 | 71 | float analogin_read(analogin_t *obj) |
mbed_official | 300:55638feb26a4 | 72 | { |
mbed_official | 85:e1a8e879a6a9 | 73 | uint16_t value = analogin_read_u16(obj); |
mbed_official | 85:e1a8e879a6a9 | 74 | return (float)value * (1.0f / (float)ADC_RANGE); |
mbed_official | 85:e1a8e879a6a9 | 75 | } |