]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC13XX/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC13XX / 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_11, ADC0_0, 0x02},
33     {P0_12, ADC0_1, 0x02},
34     {P0_13, ADC0_2, 0x02},
35     {P0_14, ADC0_3, 0x02},
36     {P0_15, ADC0_4, 0x02},
37     {P0_16, ADC0_5, 0x01},
38     {P0_22, ADC0_6, 0x01},
39     {P0_23, ADC0_7, 0x01},
40     {NC   , NC    , 0   }
41 };
42
43 #define LPC_IOCON0_BASE (LPC_IOCON_BASE)
44 #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60)
45
46 #define ADC_RANGE    ADC_10BIT_RANGE
47
48 void analogin_init(analogin_t *obj, PinName pin) {
49     obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
50     MBED_ASSERT(obj->adc != (ADCName)NC);
51     
52     // Power up ADC
53     LPC_SYSCON->PDRUNCFG &= ~ (1 << 4);
54     LPC_SYSCON->SYSAHBCLKCTRL |= ((uint32_t)1 << 13);
55
56     uint32_t pin_number = (uint32_t)pin;
57     __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));
58
59     // set pin to ADC mode
60     *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode)
61
62     uint32_t PCLK = SystemCoreClock;
63     uint32_t MAX_ADC_CLK = 4500000;
64     uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
65
66     LPC_ADC->CR = (0 << 0)      // no channels selected
67                 | (clkdiv << 8) // max of 4.5MHz
68                 | (0 << 16)     // BURST = 0, software controlled
69                 | ( 0 << 17 );  // CLKS = 0, not applicable
70     
71     pinmap_pinout(pin, PinMap_ADC);
72 }
73
74 static inline uint32_t adc_read(analogin_t *obj) {
75     // Select the appropriate channel and start conversion
76     LPC_ADC->CR &= ~0xFF;
77     LPC_ADC->CR |= 1 << (int)obj->adc;
78     LPC_ADC->CR |= 1 << 24;
79     
80     // Repeatedly get the sample data until DONE bit
81     unsigned int data;
82     do {
83         data = LPC_ADC->GDR;
84     } while ((data & ((unsigned int)1 << 31)) == 0);
85     
86     // Stop conversion
87     LPC_ADC->CR &= ~(1 << 24);
88     
89     return (data >> 6) & ADC_RANGE; // 10 bit
90 }
91
92 static inline void order(uint32_t *a, uint32_t *b) {
93     if (*a > *b) {
94         uint32_t t = *a;
95         *a = *b;
96         *b = t;
97     }
98 }
99
100 static inline uint32_t adc_read_u32(analogin_t *obj) {
101     uint32_t value;
102 #if ANALOGIN_MEDIAN_FILTER
103     uint32_t v1 = adc_read(obj);
104     uint32_t v2 = adc_read(obj);
105     uint32_t v3 = adc_read(obj);
106     order(&v1, &v2);
107     order(&v2, &v3);
108     order(&v1, &v2);
109     value = v2;
110 #else
111     value = adc_read(obj);
112 #endif
113     return value;
114 }
115
116 uint16_t analogin_read_u16(analogin_t *obj) {
117     uint32_t value = adc_read_u32(obj);
118     
119     return (value << 6) | ((value >> 4) & 0x003F); // 10 bit
120 }
121
122 float analogin_read(analogin_t *obj) {
123     uint32_t value = adc_read_u32(obj);
124     return (float)value * (1.0f / (float)ADC_RANGE);
125 }