]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11UXX/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11UXX / 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 #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform
22
23 #define ANALOGIN_MEDIAN_FILTER 1
24
25 #define ADC_10BIT_RANGE 0x3FF
26 #define ADC_12BIT_RANGE 0xFFF
27
28 static inline int div_round_up(int x, int y) {
29 return (x + (y - 1)) / y;
30 }
31
32 #define LPC_IOCON0_BASE (LPC_IOCON_BASE)
33 #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60)
34
35 #define ADC_RANGE ADC_10BIT_RANGE
36
37 void analogin_init(analogin_t *obj, PinName pin) {
38 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
39 MBED_ASSERT(obj->adc != (ADCName)NC);
40
41 // Power up ADC
42 LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);
43 LPC_SYSCON->SYSAHBCLKCTRL |= ((uint32_t)1 << 13);
44
45 uint32_t pin_number = (uint32_t)pin;
46 __IO uint32_t *reg = (pin_number < 32) ? (__IO uint32_t*)(LPC_IOCON0_BASE + 4 * pin_number) : (__IO uint32_t*)(LPC_IOCON1_BASE + 4 * (pin_number - 32));
47
48 // set pin to ADC mode
49 *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode)
50
51 uint32_t PCLK = SystemCoreClock;
52 uint32_t MAX_ADC_CLK = 4500000;
53 uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
54
55 LPC_ADC->CR = (0 << 0) // no channels selected
56 | (clkdiv << 8) // max of 4.5MHz
57 | (0 << 16) // BURST = 0, software controlled
58 | ( 0 << 17 ); // CLKS = 0, not applicable
59
60 pinmap_pinout(pin, PinMap_ADC);
61 }
62
63 static inline uint32_t adc_read(analogin_t *obj) {
64 // Select the appropriate channel and start conversion
65 LPC_ADC->CR &= ~0xFF;
66 LPC_ADC->CR |= 1 << (int)obj->adc;
67 LPC_ADC->CR |= 1 << 24;
68
69 // Repeatedly get the sample data until DONE bit
70 unsigned int data;
71 do {
72 data = LPC_ADC->GDR;
73 } while ((data & ((unsigned int)1 << 31)) == 0);
74
75 // Stop conversion
76 LPC_ADC->CR &= ~(1 << 24);
77
78 return (data >> 6) & ADC_RANGE; // 10 bit
79 }
80
81 static inline void order(uint32_t *a, uint32_t *b) {
82 if (*a > *b) {
83 uint32_t t = *a;
84 *a = *b;
85 *b = t;
86 }
87 }
88
89 static inline uint32_t adc_read_u32(analogin_t *obj) {
90 uint32_t value;
91 #if ANALOGIN_MEDIAN_FILTER
92 uint32_t v1 = adc_read(obj);
93 uint32_t v2 = adc_read(obj);
94 uint32_t v3 = adc_read(obj);
95 order(&v1, &v2);
96 order(&v2, &v3);
97 order(&v1, &v2);
98 value = v2;
99 #else
100 value = adc_read(obj);
101 #endif
102 return value;
103 }
104
105 uint16_t analogin_read_u16(analogin_t *obj) {
106 uint32_t value = adc_read_u32(obj);
107
108 return (value << 6) | ((value >> 4) & 0x003F); // 10 bit
109 }
110
111 float analogin_read(analogin_t *obj) {
112 uint32_t value = adc_read_u32(obj);
113 return (float)value * (1.0f / (float)ADC_RANGE);
114 }
Imprint / Impressum