Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_NRF51822/analogin_api.c@227:7bd0639b8911, 2014-06-11 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 11 16:00:09 2014 +0100
- Revision:
- 227:7bd0639b8911
- Parent:
- 217:d0ccc61c1fd4
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af
Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/
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 | 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 | 227:7bd0639b8911 | 40 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mbed_official | 85:e1a8e879a6a9 | 41 | |
mbed_official | 85:e1a8e879a6a9 | 42 | while (map->pin != NC) { |
mbed_official | 85:e1a8e879a6a9 | 43 | if (map->pin == pin){ |
mbed_official | 85:e1a8e879a6a9 | 44 | analogInputPin = map->function; |
mbed_official | 85:e1a8e879a6a9 | 45 | break; |
mbed_official | 85:e1a8e879a6a9 | 46 | } |
mbed_official | 85:e1a8e879a6a9 | 47 | map++; |
mbed_official | 85:e1a8e879a6a9 | 48 | } |
mbed_official | 217:d0ccc61c1fd4 | 49 | obj->adc_pin = (uint8_t)analogInputPin; |
mbed_official | 85:e1a8e879a6a9 | 50 | |
mbed_official | 227:7bd0639b8911 | 51 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
mbed_official | 85:e1a8e879a6a9 | 52 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 53 | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling<< ADC_CONFIG_INPSEL_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 54 | (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) | |
mbed_official | 85:e1a8e879a6a9 | 55 | (analogInputPin << ADC_CONFIG_PSEL_Pos) | |
mbed_official | 227:7bd0639b8911 | 56 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 57 | } |
mbed_official | 85:e1a8e879a6a9 | 58 | |
mbed_official | 85:e1a8e879a6a9 | 59 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 217:d0ccc61c1fd4 | 60 | NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
mbed_official | 217:d0ccc61c1fd4 | 61 | NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos; |
mbed_official | 85:e1a8e879a6a9 | 62 | NRF_ADC->TASKS_START = 1; |
mbed_official | 85:e1a8e879a6a9 | 63 | while ( ( (NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) |
mbed_official | 85:e1a8e879a6a9 | 64 | { |
mbed_official | 85:e1a8e879a6a9 | 65 | } |
mbed_official | 85:e1a8e879a6a9 | 66 | |
mbed_official | 85:e1a8e879a6a9 | 67 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
mbed_official | 85:e1a8e879a6a9 | 68 | } |
mbed_official | 85:e1a8e879a6a9 | 69 | |
mbed_official | 85:e1a8e879a6a9 | 70 | float analogin_read(analogin_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 71 | uint16_t value = analogin_read_u16(obj); |
mbed_official | 85:e1a8e879a6a9 | 72 | return (float)value * (1.0f / (float)ADC_RANGE); |
mbed_official | 85:e1a8e879a6a9 | 73 | } |