mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC11U6X/analogin_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 285:31249416b6f9
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 186:2e805bf06ee4 | 1 | /* mbed Microcontroller Library |
mbed_official | 186:2e805bf06ee4 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 186:2e805bf06ee4 | 3 | * |
mbed_official | 186:2e805bf06ee4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 186:2e805bf06ee4 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 186:2e805bf06ee4 | 6 | * You may obtain a copy of the License at |
mbed_official | 186:2e805bf06ee4 | 7 | * |
mbed_official | 186:2e805bf06ee4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 186:2e805bf06ee4 | 9 | * |
mbed_official | 186:2e805bf06ee4 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 186:2e805bf06ee4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 186:2e805bf06ee4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 186:2e805bf06ee4 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 186:2e805bf06ee4 | 14 | * limitations under the License. |
mbed_official | 186:2e805bf06ee4 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 186:2e805bf06ee4 | 17 | #include "analogin_api.h" |
mbed_official | 186:2e805bf06ee4 | 18 | #include "cmsis.h" |
mbed_official | 186:2e805bf06ee4 | 19 | #include "pinmap.h" |
mbed_official | 285:31249416b6f9 | 20 | #include "mbed_error.h" |
mbed_official | 186:2e805bf06ee4 | 21 | |
mbed_official | 193:cff126de7aaf | 22 | #if DEVICE_ANALOGIN |
mbed_official | 186:2e805bf06ee4 | 23 | |
mbed_official | 186:2e805bf06ee4 | 24 | #define ANALOGIN_MEDIAN_FILTER 1 |
mbed_official | 186:2e805bf06ee4 | 25 | |
mbed_official | 186:2e805bf06ee4 | 26 | #define ADC_10BIT_RANGE 0x3FF |
mbed_official | 186:2e805bf06ee4 | 27 | #define ADC_12BIT_RANGE 0xFFF |
mbed_official | 186:2e805bf06ee4 | 28 | #define PDRUN_VALID_BITS 0x000025FFL |
mbed_official | 186:2e805bf06ee4 | 29 | #define PDRUN_RESERVED_ONE 0x0000C800L |
mbed_official | 186:2e805bf06ee4 | 30 | |
mbed_official | 186:2e805bf06ee4 | 31 | #define ADC_RANGE ADC_12BIT_RANGE |
mbed_official | 186:2e805bf06ee4 | 32 | |
mbed_official | 186:2e805bf06ee4 | 33 | static const PinMap PinMap_ADC[] = { |
mbed_official | 186:2e805bf06ee4 | 34 | {P1_9 , ADC_0, 3}, |
mbed_official | 186:2e805bf06ee4 | 35 | {P0_23, ADC_1, 1}, |
mbed_official | 186:2e805bf06ee4 | 36 | {P0_16, ADC_2, 1}, |
mbed_official | 186:2e805bf06ee4 | 37 | {P0_15, ADC_3, 3}, |
mbed_official | 186:2e805bf06ee4 | 38 | {P1_22, ADC_4, 3}, |
mbed_official | 186:2e805bf06ee4 | 39 | {P1_3 , ADC_5, 4}, |
mbed_official | 186:2e805bf06ee4 | 40 | {P0_14, ADC_6, 2}, |
mbed_official | 186:2e805bf06ee4 | 41 | {P0_13, ADC_7, 2}, |
mbed_official | 186:2e805bf06ee4 | 42 | {P0_12, ADC_8, 2}, |
mbed_official | 186:2e805bf06ee4 | 43 | {P0_11, ADC_9, 2}, |
mbed_official | 186:2e805bf06ee4 | 44 | {P1_29, ADC_10,4}, |
mbed_official | 186:2e805bf06ee4 | 45 | {P0_22, ADC_11,1}, |
mbed_official | 186:2e805bf06ee4 | 46 | {NC , NC ,0} |
mbed_official | 186:2e805bf06ee4 | 47 | }; |
mbed_official | 186:2e805bf06ee4 | 48 | |
mbed_official | 186:2e805bf06ee4 | 49 | |
mbed_official | 186:2e805bf06ee4 | 50 | void analogin_init(analogin_t *obj, PinName pin) { |
mbed_official | 186:2e805bf06ee4 | 51 | volatile uint32_t tmp; |
mbed_official | 186:2e805bf06ee4 | 52 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 227:7bd0639b8911 | 53 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mbed_official | 227:7bd0639b8911 | 54 | |
mbed_official | 186:2e805bf06ee4 | 55 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 186:2e805bf06ee4 | 56 | |
mbed_official | 193:cff126de7aaf | 57 | __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + (pin & 0x1FF)); |
mbed_official | 193:cff126de7aaf | 58 | // set pin to ADC mode |
mbed_official | 193:cff126de7aaf | 59 | *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode) |
mbed_official | 193:cff126de7aaf | 60 | |
mbed_official | 186:2e805bf06ee4 | 61 | // ADC Powered |
mbed_official | 186:2e805bf06ee4 | 62 | tmp = (LPC_SYSCON->PDRUNCFG & PDRUN_VALID_BITS); |
mbed_official | 186:2e805bf06ee4 | 63 | tmp &= ~((1 << 4) & PDRUN_VALID_BITS); |
mbed_official | 186:2e805bf06ee4 | 64 | LPC_SYSCON->PDRUNCFG = (tmp | PDRUN_RESERVED_ONE); |
mbed_official | 186:2e805bf06ee4 | 65 | |
mbed_official | 186:2e805bf06ee4 | 66 | // Enable clock for ADC |
mbed_official | 186:2e805bf06ee4 | 67 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 13); |
mbed_official | 193:cff126de7aaf | 68 | |
mbed_official | 186:2e805bf06ee4 | 69 | // Start ADC self-calibration |
mbed_official | 193:cff126de7aaf | 70 | LPC_ADC->CTRL = (1UL << 30); |
mbed_official | 186:2e805bf06ee4 | 71 | do { |
mbed_official | 186:2e805bf06ee4 | 72 | tmp = LPC_ADC->CTRL; |
mbed_official | 186:2e805bf06ee4 | 73 | } while ((tmp & (1UL << 30)) != 0); |
mbed_official | 186:2e805bf06ee4 | 74 | |
mbed_official | 222:96162ccfbf43 | 75 | LPC_ADC->CTRL = 1; // Sampling clock: SystemClock divided by 1 |
mbed_official | 186:2e805bf06ee4 | 76 | } |
mbed_official | 186:2e805bf06ee4 | 77 | |
mbed_official | 186:2e805bf06ee4 | 78 | static inline uint32_t adc_read(analogin_t *obj) { |
mbed_official | 186:2e805bf06ee4 | 79 | |
mbed_official | 186:2e805bf06ee4 | 80 | // select channel |
mbed_official | 186:2e805bf06ee4 | 81 | LPC_ADC->SEQA_CTRL &= ~(0xFFF); |
mbed_official | 186:2e805bf06ee4 | 82 | LPC_ADC->SEQA_CTRL |= (1UL << obj->adc); |
mbed_official | 186:2e805bf06ee4 | 83 | |
mbed_official | 186:2e805bf06ee4 | 84 | // start conversion, sequence enable with async mode |
mbed_official | 193:cff126de7aaf | 85 | LPC_ADC->SEQA_CTRL |= ((1UL << 26) | (1UL << 31) | (1UL << 19)); |
mbed_official | 186:2e805bf06ee4 | 86 | |
mbed_official | 186:2e805bf06ee4 | 87 | // Repeatedly get the sample data until DONE bit |
mbed_official | 186:2e805bf06ee4 | 88 | volatile uint32_t data; |
mbed_official | 186:2e805bf06ee4 | 89 | do { |
mbed_official | 186:2e805bf06ee4 | 90 | data = LPC_ADC->SEQA_GDAT; |
mbed_official | 186:2e805bf06ee4 | 91 | } while ((data & (1UL << 31)) == 0); |
mbed_official | 186:2e805bf06ee4 | 92 | data = LPC_ADC->DAT[obj->adc]; |
mbed_official | 186:2e805bf06ee4 | 93 | |
mbed_official | 186:2e805bf06ee4 | 94 | // Stop conversion |
mbed_official | 186:2e805bf06ee4 | 95 | LPC_ADC->SEQA_CTRL &= ~(1UL << 31); |
mbed_official | 186:2e805bf06ee4 | 96 | |
mbed_official | 186:2e805bf06ee4 | 97 | return ((data >> 4) & ADC_RANGE); |
mbed_official | 186:2e805bf06ee4 | 98 | } |
mbed_official | 186:2e805bf06ee4 | 99 | |
mbed_official | 186:2e805bf06ee4 | 100 | static inline void order(uint32_t *a, uint32_t *b) { |
mbed_official | 186:2e805bf06ee4 | 101 | if (*a > *b) { |
mbed_official | 186:2e805bf06ee4 | 102 | uint32_t t = *a; |
mbed_official | 186:2e805bf06ee4 | 103 | *a = *b; |
mbed_official | 186:2e805bf06ee4 | 104 | *b = t; |
mbed_official | 186:2e805bf06ee4 | 105 | } |
mbed_official | 186:2e805bf06ee4 | 106 | } |
mbed_official | 186:2e805bf06ee4 | 107 | |
mbed_official | 186:2e805bf06ee4 | 108 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
mbed_official | 186:2e805bf06ee4 | 109 | uint32_t value; |
mbed_official | 186:2e805bf06ee4 | 110 | #if ANALOGIN_MEDIAN_FILTER |
mbed_official | 186:2e805bf06ee4 | 111 | uint32_t v1 = adc_read(obj); |
mbed_official | 186:2e805bf06ee4 | 112 | uint32_t v2 = adc_read(obj); |
mbed_official | 186:2e805bf06ee4 | 113 | uint32_t v3 = adc_read(obj); |
mbed_official | 186:2e805bf06ee4 | 114 | order(&v1, &v2); |
mbed_official | 186:2e805bf06ee4 | 115 | order(&v2, &v3); |
mbed_official | 186:2e805bf06ee4 | 116 | order(&v1, &v2); |
mbed_official | 186:2e805bf06ee4 | 117 | value = v2; |
mbed_official | 186:2e805bf06ee4 | 118 | #else |
mbed_official | 186:2e805bf06ee4 | 119 | value = adc_read(obj); |
mbed_official | 186:2e805bf06ee4 | 120 | #endif |
mbed_official | 186:2e805bf06ee4 | 121 | return value; |
mbed_official | 186:2e805bf06ee4 | 122 | } |
mbed_official | 186:2e805bf06ee4 | 123 | |
mbed_official | 186:2e805bf06ee4 | 124 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 186:2e805bf06ee4 | 125 | uint32_t value = adc_read_u32(obj); |
mbed_official | 186:2e805bf06ee4 | 126 | return (value << 4) | ((value >> 8) & 0x000F); // 12 bit |
mbed_official | 186:2e805bf06ee4 | 127 | } |
mbed_official | 186:2e805bf06ee4 | 128 | |
mbed_official | 186:2e805bf06ee4 | 129 | float analogin_read(analogin_t *obj) { |
mbed_official | 186:2e805bf06ee4 | 130 | uint32_t value = adc_read_u32(obj); |
mbed_official | 186:2e805bf06ee4 | 131 | return (float)value * (1.0f / (float)ADC_RANGE); |
mbed_official | 186:2e805bf06ee4 | 132 | } |
mbed_official | 186:2e805bf06ee4 | 133 | |
mbed_official | 186:2e805bf06ee4 | 134 | #endif |