Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
mbed-src/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/analogin_api.c@10:22480ac31879, 2014-11-27 (annotated)
- Committer:
- yihui
- Date:
- Thu Nov 27 09:30:36 2014 +0000
- Revision:
- 10:22480ac31879
- Parent:
- 9:05f0b5a3a70a
change to new revision hardware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | /* mbed Microcontroller Library |
yihui | 9:05f0b5a3a70a | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 9:05f0b5a3a70a | 3 | * |
yihui | 9:05f0b5a3a70a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 9:05f0b5a3a70a | 5 | * you may not use this file except in compliance with the License. |
yihui | 9:05f0b5a3a70a | 6 | * You may obtain a copy of the License at |
yihui | 9:05f0b5a3a70a | 7 | * |
yihui | 9:05f0b5a3a70a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 9:05f0b5a3a70a | 9 | * |
yihui | 9:05f0b5a3a70a | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 9:05f0b5a3a70a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 9:05f0b5a3a70a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 9:05f0b5a3a70a | 13 | * See the License for the specific language governing permissions and |
yihui | 9:05f0b5a3a70a | 14 | * limitations under the License. |
yihui | 9:05f0b5a3a70a | 15 | */ |
yihui | 9:05f0b5a3a70a | 16 | #include "mbed_assert.h" |
yihui | 9:05f0b5a3a70a | 17 | #include "analogin_api.h" |
yihui | 9:05f0b5a3a70a | 18 | #include "cmsis.h" |
yihui | 9:05f0b5a3a70a | 19 | #include "pinmap.h" |
yihui | 9:05f0b5a3a70a | 20 | |
yihui | 9:05f0b5a3a70a | 21 | #define ANALOGIN_MEDIAN_FILTER 1 |
yihui | 9:05f0b5a3a70a | 22 | #define ADC_10BIT_RANGE 0x3FF |
yihui | 9:05f0b5a3a70a | 23 | #define ADC_RANGE ADC_10BIT_RANGE |
yihui | 9:05f0b5a3a70a | 24 | |
yihui | 9:05f0b5a3a70a | 25 | static const PinMap PinMap_ADC[] = { |
yihui | 9:05f0b5a3a70a | 26 | {p1, ADC0_0, 4}, |
yihui | 9:05f0b5a3a70a | 27 | {p2, ADC0_0, 8}, |
yihui | 9:05f0b5a3a70a | 28 | {p3, ADC0_0, 16}, |
yihui | 9:05f0b5a3a70a | 29 | {p4, ADC0_0, 32}, |
yihui | 9:05f0b5a3a70a | 30 | {p5, ADC0_0, 64}, |
yihui | 9:05f0b5a3a70a | 31 | {p6, ADC0_0, 128}, |
yihui | 9:05f0b5a3a70a | 32 | {NC, NC, 0} |
yihui | 9:05f0b5a3a70a | 33 | }; |
yihui | 9:05f0b5a3a70a | 34 | |
yihui | 9:05f0b5a3a70a | 35 | void analogin_init(analogin_t *obj, PinName pin) |
yihui | 9:05f0b5a3a70a | 36 | { |
yihui | 9:05f0b5a3a70a | 37 | int analogInputPin = 0; |
yihui | 9:05f0b5a3a70a | 38 | const PinMap *map = PinMap_ADC; |
yihui | 9:05f0b5a3a70a | 39 | |
yihui | 9:05f0b5a3a70a | 40 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); //(NRF_ADC_Type *) |
yihui | 9:05f0b5a3a70a | 41 | MBED_ASSERT(obj->adc != (ADCName)NC); |
yihui | 9:05f0b5a3a70a | 42 | |
yihui | 9:05f0b5a3a70a | 43 | while (map->pin != NC) { |
yihui | 9:05f0b5a3a70a | 44 | if (map->pin == pin) { |
yihui | 9:05f0b5a3a70a | 45 | analogInputPin = map->function; |
yihui | 9:05f0b5a3a70a | 46 | break; |
yihui | 9:05f0b5a3a70a | 47 | } |
yihui | 9:05f0b5a3a70a | 48 | map++; |
yihui | 9:05f0b5a3a70a | 49 | } |
yihui | 9:05f0b5a3a70a | 50 | obj->adc_pin = (uint8_t)analogInputPin; |
yihui | 9:05f0b5a3a70a | 51 | |
yihui | 9:05f0b5a3a70a | 52 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
yihui | 9:05f0b5a3a70a | 53 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
yihui | 10:22480ac31879 | 54 | (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos) | |
yihui | 10:22480ac31879 | 55 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
yihui | 9:05f0b5a3a70a | 56 | (obj->adc_pin << ADC_CONFIG_PSEL_Pos) | |
yihui | 9:05f0b5a3a70a | 57 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
yihui | 9:05f0b5a3a70a | 58 | } |
yihui | 9:05f0b5a3a70a | 59 | |
yihui | 9:05f0b5a3a70a | 60 | void analogin_free(analogin_t *obj) |
yihui | 9:05f0b5a3a70a | 61 | { |
yihui | 9:05f0b5a3a70a | 62 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
yihui | 9:05f0b5a3a70a | 63 | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
yihui | 9:05f0b5a3a70a | 64 | (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) | |
yihui | 9:05f0b5a3a70a | 65 | (0 << ADC_CONFIG_PSEL_Pos) | |
yihui | 9:05f0b5a3a70a | 66 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
yihui | 9:05f0b5a3a70a | 67 | } |
yihui | 9:05f0b5a3a70a | 68 | |
yihui | 9:05f0b5a3a70a | 69 | uint16_t analogin_read_u16(analogin_t *obj) |
yihui | 9:05f0b5a3a70a | 70 | { |
yihui | 9:05f0b5a3a70a | 71 | NRF_ADC->CONFIG &= ~ADC_CONFIG_PSEL_Msk; |
yihui | 9:05f0b5a3a70a | 72 | NRF_ADC->CONFIG |= obj->adc_pin << ADC_CONFIG_PSEL_Pos; |
yihui | 9:05f0b5a3a70a | 73 | NRF_ADC->TASKS_START = 1; |
yihui | 9:05f0b5a3a70a | 74 | while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) { |
yihui | 9:05f0b5a3a70a | 75 | } |
yihui | 9:05f0b5a3a70a | 76 | |
yihui | 9:05f0b5a3a70a | 77 | return (uint16_t)NRF_ADC->RESULT; // 10 bit |
yihui | 9:05f0b5a3a70a | 78 | } |
yihui | 9:05f0b5a3a70a | 79 | |
yihui | 9:05f0b5a3a70a | 80 | float analogin_read(analogin_t *obj) |
yihui | 9:05f0b5a3a70a | 81 | { |
yihui | 9:05f0b5a3a70a | 82 | uint16_t value = analogin_read_u16(obj); |
yihui | 9:05f0b5a3a70a | 83 | return (float)value * (1.0f / (float)ADC_RANGE); |
yihui | 9:05f0b5a3a70a | 84 | } |