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