]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC408X/TARGET_LPC4088_DM/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC408X / TARGET_LPC4088_DM / 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 #define ANALOGIN_MEDIAN_FILTER 1
23
24 #define ADC_10BIT_RANGE 0x3FF
25 #define ADC_12BIT_RANGE 0xFFF
26
27 static inline int div_round_up(int x, int y) {
28 return (x + (y - 1)) / y;
29 }
30
31 static const PinMap PinMap_ADC[] = {
32 {P0_25, ADC0_2, 0x01},
33 {P0_26, ADC0_3, 0x01},
34 {NC , NC , 0 }
35 };
36
37 #define ADC_RANGE ADC_12BIT_RANGE
38
39 void analogin_init(analogin_t *obj, PinName pin) {
40 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
41 MBED_ASSERT(obj->adc != (ADCName)NC);
42
43 // ensure power is turned on
44 LPC_SC->PCONP |= (1 << 12);
45
46 uint32_t PCLK = PeripheralClock;
47
48 // calculate minimum clock divider
49 // clkdiv = divider - 1
50 uint32_t MAX_ADC_CLK = 12400000;
51 uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
52
53 // Set the generic software-controlled ADC settings
54 LPC_ADC->CR = (0 << 0) // SEL: 0 = no channels selected
55 | (clkdiv << 8) // CLKDIV:
56 | (0 << 16) // BURST: 0 = software control
57 | (1 << 21) // PDN: 1 = operational
58 | (0 << 24) // START: 0 = no start
59 | (0 << 27); // EDGE: not applicable
60
61 // must enable analog mode (ADMODE = 0)
62 __IO uint32_t *reg = (__IO uint32_t*) (LPC_IOCON_BASE + 4 * pin);
63 *reg &= ~(1 << 7);
64
65 pinmap_pinout(pin, PinMap_ADC);
66 }
67
68 static inline uint32_t adc_read(analogin_t *obj) {
69 // Select the appropriate channel and start conversion
70 LPC_ADC->CR &= ~0xFF;
71 LPC_ADC->CR |= 1 << (int)obj->adc;
72 LPC_ADC->CR |= 1 << 24;
73
74 // Repeatedly get the sample data until DONE bit
75 unsigned int data;
76 do {
77 data = LPC_ADC->GDR;
78 } while ((data & ((unsigned int)1 << 31)) == 0);
79
80 // Stop conversion
81 LPC_ADC->CR &= ~(1 << 24);
82
83 return (data >> 4) & ADC_RANGE; // 12 bit
84 }
85
86 static inline void order(uint32_t *a, uint32_t *b) {
87 if (*a > *b) {
88 uint32_t t = *a;
89 *a = *b;
90 *b = t;
91 }
92 }
93
94 static inline uint32_t adc_read_u32(analogin_t *obj) {
95 uint32_t value;
96 #if ANALOGIN_MEDIAN_FILTER
97 uint32_t v1 = adc_read(obj);
98 uint32_t v2 = adc_read(obj);
99 uint32_t v3 = adc_read(obj);
100 order(&v1, &v2);
101 order(&v2, &v3);
102 order(&v1, &v2);
103 value = v2;
104 #else
105 value = adc_read(obj);
106 #endif
107 return value;
108 }
109
110 uint16_t analogin_read_u16(analogin_t *obj) {
111 uint32_t value = adc_read_u32(obj);
112
113 return (value << 4) | ((value >> 8) & 0x000F); // 12 bit
114 }
115
116 float analogin_read(analogin_t *obj) {
117 uint32_t value = adc_read_u32(obj);
118 return (float)value * (1.0f / (float)ADC_RANGE);
119 }
Imprint / Impressum