]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/analogin_api.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11XX_11CXX / 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 static const PinMap PinMap_ADC[] = {
23 {P0_11, ADC0_0, 2},
24 {P1_0 , ADC0_1, 2},
25 {P1_1 , ADC0_2, 2},
26 {P1_2 , ADC0_3, 2},
27 // {P1_3 , ADC0_4, 2}, -- should be mapped to SWDIO only
28 {P1_4 , ADC0_5, 1},
29 {P1_10, ADC0_6, 1},
30 {P1_11, ADC0_7, 1},
31 {NC , NC , 0}
32 };
33
34 #define ANALOGIN_MEDIAN_FILTER 1
35
36 #define ADC_10BIT_RANGE 0x3FF
37 #define ADC_12BIT_RANGE 0xFFF
38
39 static inline int div_round_up(int x, int y) {
40 return (x + (y - 1)) / y;
41 }
42
43 #define ADC_RANGE ADC_10BIT_RANGE
44
45 void analogin_init(analogin_t *obj, PinName pin) {
46 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
47 MBED_ASSERT(obj->adc != (uint32_t)NC);
48
49 // Power up ADC
50 LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);
51 LPC_SYSCON->SYSAHBCLKCTRL |= ((uint32_t)1 << 13);
52
53 uint32_t offset = (uint32_t)pin & 0xff;
54 __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + offset);
55
56 // set pin to ADC mode
57 *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode)
58
59 uint32_t PCLK = SystemCoreClock;
60 uint32_t MAX_ADC_CLK = 4500000;
61 uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
62
63 LPC_ADC->CR = (0 << 0) // no channels selected
64 | (clkdiv << 8) // max of 4.5MHz
65 | (0 << 16) // BURST = 0, software controlled
66 | ( 0 << 17 ); // CLKS = 0, not applicable
67
68 pinmap_pinout(pin, PinMap_ADC);
69 }
70
71 static inline uint32_t adc_read(analogin_t *obj) {
72 // Select the appropriate channel and start conversion
73 LPC_ADC->CR &= ~0xFF;
74 LPC_ADC->CR |= 1 << (int)obj->adc;
75 LPC_ADC->CR |= 1 << 24;
76
77 // Repeatedly get the sample data until DONE bit
78 unsigned int data;
79 do {
80 data = LPC_ADC->GDR;
81 } while ((data & ((unsigned int)1 << 31)) == 0);
82
83 // Stop conversion
84 LPC_ADC->CR &= ~(1 << 24);
85
86 return (data >> 6) & ADC_RANGE; // 10 bit
87 }
88
89 static inline void order(uint32_t *a, uint32_t *b) {
90 if (*a > *b) {
91 uint32_t t = *a;
92 *a = *b;
93 *b = t;
94 }
95 }
96
97 static inline uint32_t adc_read_u32(analogin_t *obj) {
98 uint32_t value;
99 #if ANALOGIN_MEDIAN_FILTER
100 uint32_t v1 = adc_read(obj);
101 uint32_t v2 = adc_read(obj);
102 uint32_t v3 = adc_read(obj);
103 order(&v1, &v2);
104 order(&v2, &v3);
105 order(&v1, &v2);
106 value = v2;
107 #else
108 value = adc_read(obj);
109 #endif
110 return value;
111 }
112
113 uint16_t analogin_read_u16(analogin_t *obj) {
114 uint32_t value = adc_read_u32(obj);
115
116 return (value << 6) | ((value >> 4) & 0x003F); // 10 bit
117 }
118
119 float analogin_read(analogin_t *obj) {
120 uint32_t value = adc_read_u32(obj);
121 return (float)value * (1.0f / (float)ADC_RANGE);
122 }
Imprint / Impressum