]>
git.gir.st - tmk_keyboard.git/blob - 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
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "mbed_assert.h"
17 #include "analogin_api.h"
20 #include "mbed_error.h"
21 #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform
23 #define ANALOGIN_MEDIAN_FILTER 1
25 #define ADC_10BIT_RANGE 0x3FF
26 #define ADC_12BIT_RANGE 0xFFF
28 static inline int div_round_up(int x
, int y
) {
29 return (x
+ (y
- 1)) / y
;
32 #define LPC_IOCON0_BASE (LPC_IOCON_BASE)
33 #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60)
35 #define ADC_RANGE ADC_10BIT_RANGE
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
);
42 LPC_SYSCON
->PDRUNCFG
&= ~ (1 << 4);
43 LPC_SYSCON
->SYSAHBCLKCTRL
|= ((uint32_t)1 << 13);
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));
48 // set pin to ADC mode
49 *reg
&= ~(1 << 7); // set ADMODE = 0 (analog mode)
51 uint32_t PCLK
= SystemCoreClock
;
52 uint32_t MAX_ADC_CLK
= 4500000;
53 uint32_t clkdiv
= div_round_up(PCLK
, MAX_ADC_CLK
) - 1;
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
60 pinmap_pinout(pin
, PinMap_ADC
);
63 static inline uint32_t adc_read(analogin_t
*obj
) {
64 // Select the appropriate channel and start conversion
66 LPC_ADC
->CR
|= 1 << (int)obj
->adc
;
67 LPC_ADC
->CR
|= 1 << 24;
69 // Repeatedly get the sample data until DONE bit
73 } while ((data
& ((unsigned int)1 << 31)) == 0);
76 LPC_ADC
->CR
&= ~(1 << 24);
78 return (data
>> 6) & ADC_RANGE
; // 10 bit
81 static inline void order(uint32_t *a
, uint32_t *b
) {
89 static inline uint32_t adc_read_u32(analogin_t
*obj
) {
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
);
100 value
= adc_read(obj
);
105 uint16_t analogin_read_u16(analogin_t
*obj
) {
106 uint32_t value
= adc_read_u32(obj
);
108 return (value
<< 6) | ((value
>> 4) & 0x003F); // 10 bit
111 float analogin_read(analogin_t
*obj
) {
112 uint32_t value
= adc_read_u32(obj
);
113 return (float)value
* (1.0f
/ (float)ADC_RANGE
);