]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11U6X/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11U6X / 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 #include "mbed_error.h"
21
22 #if DEVICE_ANALOGIN
23
24 #define ANALOGIN_MEDIAN_FILTER 1
25
26 #define ADC_10BIT_RANGE 0x3FF
27 #define ADC_12BIT_RANGE 0xFFF
28 #define PDRUN_VALID_BITS 0x000025FFL
29 #define PDRUN_RESERVED_ONE 0x0000C800L
30
31 #define ADC_RANGE ADC_12BIT_RANGE
32
33 static const PinMap PinMap_ADC[] = {
34 {P1_9 , ADC_0, 3},
35 {P0_23, ADC_1, 1},
36 {P0_16, ADC_2, 1},
37 {P0_15, ADC_3, 3},
38 {P1_22, ADC_4, 3},
39 {P1_3 , ADC_5, 4},
40 {P0_14, ADC_6, 2},
41 {P0_13, ADC_7, 2},
42 {P0_12, ADC_8, 2},
43 {P0_11, ADC_9, 2},
44 {P1_29, ADC_10,4},
45 {P0_22, ADC_11,1},
46 {NC , NC ,0}
47 };
48
49
50 void analogin_init(analogin_t *obj, PinName pin) {
51 volatile uint32_t tmp;
52 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
53 MBED_ASSERT(obj->adc != (ADCName)NC);
54
55 pinmap_pinout(pin, PinMap_ADC);
56
57 __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + (pin & 0x1FF));
58 // set pin to ADC mode
59 *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode)
60
61 // ADC Powered
62 tmp = (LPC_SYSCON->PDRUNCFG & PDRUN_VALID_BITS);
63 tmp &= ~((1 << 4) & PDRUN_VALID_BITS);
64 LPC_SYSCON->PDRUNCFG = (tmp | PDRUN_RESERVED_ONE);
65
66 // Enable clock for ADC
67 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13);
68
69 // Determine the clock divider for a 500kHz ADC clock during calibration
70 uint32_t clkdiv = (SystemCoreClock / 500000) - 1;
71
72 // Perform a self-calibration
73 LPC_ADC->CTRL = (1UL << 30) | (clkdiv & 0xFF);
74 while ((LPC_ADC->CTRL & (1UL << 30)) != 0);
75
76 // Sampling clock: SystemClock divided by 1
77 LPC_ADC->CTRL = 0;
78 }
79
80 static inline uint32_t adc_read(analogin_t *obj) {
81
82 // select channel
83 LPC_ADC->SEQA_CTRL &= ~(0xFFF);
84 LPC_ADC->SEQA_CTRL |= (1UL << obj->adc);
85
86 // start conversion, sequence enable with async mode
87 LPC_ADC->SEQA_CTRL |= ((1UL << 26) | (1UL << 31) | (1UL << 19));
88
89 // Repeatedly get the sample data until DONE bit
90 volatile uint32_t data;
91 do {
92 data = LPC_ADC->SEQA_GDAT;
93 } while ((data & (1UL << 31)) == 0);
94 data = LPC_ADC->DAT[obj->adc];
95
96 // Stop conversion
97 LPC_ADC->SEQA_CTRL &= ~(1UL << 31);
98
99 return ((data >> 4) & ADC_RANGE);
100 }
101
102 static inline void order(uint32_t *a, uint32_t *b) {
103 if (*a > *b) {
104 uint32_t t = *a;
105 *a = *b;
106 *b = t;
107 }
108 }
109
110 static inline uint32_t adc_read_u32(analogin_t *obj) {
111 uint32_t value;
112 #if ANALOGIN_MEDIAN_FILTER
113 uint32_t v1 = adc_read(obj);
114 uint32_t v2 = adc_read(obj);
115 uint32_t v3 = adc_read(obj);
116 order(&v1, &v2);
117 order(&v2, &v3);
118 order(&v1, &v2);
119 value = v2;
120 #else
121 value = adc_read(obj);
122 #endif
123 return value;
124 }
125
126 uint16_t analogin_read_u16(analogin_t *obj) {
127 uint32_t value = adc_read_u32(obj);
128 return (value << 4) | ((value >> 8) & 0x000F); // 12 bit
129 }
130
131 float analogin_read(analogin_t *obj) {
132 uint32_t value = adc_read_u32(obj);
133 return (float)value * (1.0f / (float)ADC_RANGE);
134 }
135
136 #endif
Imprint / Impressum