mbed library sources modified for open wear
Dependents: openwear-lifelogger-example
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/analogin_api.c@286:31249416b6f9, 2014-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 15 16:30:08 2014 +0100
- Revision:
- 286:31249416b6f9
- Parent:
- 251:de9a1e4ffd79
Synchronized with git revision 601712595f49bbd2a2771c52cf3e84c9c7a591af
Full URL: https://github.com/mbedmicro/mbed/commit/601712595f49bbd2a2771c52cf3e84c9c7a591af/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 30:91c1d09ada54 | 1 | /* mbed Microcontroller Library |
mbed_official | 30:91c1d09ada54 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 30:91c1d09ada54 | 3 | * |
mbed_official | 30:91c1d09ada54 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 30:91c1d09ada54 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 30:91c1d09ada54 | 6 | * You may obtain a copy of the License at |
mbed_official | 30:91c1d09ada54 | 7 | * |
mbed_official | 30:91c1d09ada54 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 30:91c1d09ada54 | 9 | * |
mbed_official | 30:91c1d09ada54 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 30:91c1d09ada54 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 30:91c1d09ada54 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 30:91c1d09ada54 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 30:91c1d09ada54 | 14 | * limitations under the License. |
mbed_official | 30:91c1d09ada54 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 30:91c1d09ada54 | 17 | #include "analogin_api.h" |
mbed_official | 30:91c1d09ada54 | 18 | #include "cmsis.h" |
mbed_official | 30:91c1d09ada54 | 19 | #include "pinmap.h" |
mbed_official | 286:31249416b6f9 | 20 | #include "mbed_error.h" |
mbed_official | 30:91c1d09ada54 | 21 | |
mbed_official | 32:4742f6c694e8 | 22 | static const PinMap PinMap_ADC[] = { |
mbed_official | 32:4742f6c694e8 | 23 | {P0_11, ADC0_0, 2}, |
mbed_official | 32:4742f6c694e8 | 24 | {P1_0 , ADC0_1, 2}, |
mbed_official | 32:4742f6c694e8 | 25 | {P1_1 , ADC0_2, 2}, |
mbed_official | 32:4742f6c694e8 | 26 | {P1_2 , ADC0_3, 2}, |
mbed_official | 32:4742f6c694e8 | 27 | // {P1_3 , ADC0_4, 2}, -- should be mapped to SWDIO only |
mbed_official | 32:4742f6c694e8 | 28 | {P1_4 , ADC0_5, 1}, |
mbed_official | 32:4742f6c694e8 | 29 | {P1_10, ADC0_6, 1}, |
mbed_official | 32:4742f6c694e8 | 30 | {P1_11, ADC0_7, 1}, |
mbed_official | 32:4742f6c694e8 | 31 | {NC , NC , 0} |
mbed_official | 32:4742f6c694e8 | 32 | }; |
mbed_official | 30:91c1d09ada54 | 33 | |
mbed_official | 30:91c1d09ada54 | 34 | #define ANALOGIN_MEDIAN_FILTER 1 |
mbed_official | 30:91c1d09ada54 | 35 | |
mbed_official | 30:91c1d09ada54 | 36 | #define ADC_10BIT_RANGE 0x3FF |
mbed_official | 30:91c1d09ada54 | 37 | #define ADC_12BIT_RANGE 0xFFF |
mbed_official | 30:91c1d09ada54 | 38 | |
mbed_official | 30:91c1d09ada54 | 39 | static inline int div_round_up(int x, int y) { |
mbed_official | 30:91c1d09ada54 | 40 | return (x + (y - 1)) / y; |
mbed_official | 30:91c1d09ada54 | 41 | } |
mbed_official | 30:91c1d09ada54 | 42 | |
mbed_official | 30:91c1d09ada54 | 43 | #define ADC_RANGE ADC_10BIT_RANGE |
mbed_official | 30:91c1d09ada54 | 44 | |
mbed_official | 30:91c1d09ada54 | 45 | void analogin_init(analogin_t *obj, PinName pin) { |
mbed_official | 30:91c1d09ada54 | 46 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 227:7bd0639b8911 | 47 | MBED_ASSERT(obj->adc != (uint32_t)NC); |
mbed_official | 30:91c1d09ada54 | 48 | |
mbed_official | 30:91c1d09ada54 | 49 | // Power up ADC |
mbed_official | 30:91c1d09ada54 | 50 | LPC_SYSCON->PDRUNCFG &= ~ (1 << 4); |
mbed_official | 30:91c1d09ada54 | 51 | LPC_SYSCON->SYSAHBCLKCTRL |= ((uint32_t)1 << 13); |
mbed_official | 30:91c1d09ada54 | 52 | |
mbed_official | 30:91c1d09ada54 | 53 | uint32_t offset = (uint32_t)pin & 0xff; |
mbed_official | 30:91c1d09ada54 | 54 | __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + offset); |
mbed_official | 30:91c1d09ada54 | 55 | |
mbed_official | 30:91c1d09ada54 | 56 | // set pin to ADC mode |
mbed_official | 30:91c1d09ada54 | 57 | *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode) |
mbed_official | 30:91c1d09ada54 | 58 | |
mbed_official | 30:91c1d09ada54 | 59 | uint32_t PCLK = SystemCoreClock; |
mbed_official | 30:91c1d09ada54 | 60 | uint32_t MAX_ADC_CLK = 4500000; |
mbed_official | 30:91c1d09ada54 | 61 | uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1; |
mbed_official | 30:91c1d09ada54 | 62 | |
mbed_official | 30:91c1d09ada54 | 63 | LPC_ADC->CR = (0 << 0) // no channels selected |
mbed_official | 30:91c1d09ada54 | 64 | | (clkdiv << 8) // max of 4.5MHz |
mbed_official | 30:91c1d09ada54 | 65 | | (0 << 16) // BURST = 0, software controlled |
mbed_official | 30:91c1d09ada54 | 66 | | ( 0 << 17 ); // CLKS = 0, not applicable |
mbed_official | 30:91c1d09ada54 | 67 | |
mbed_official | 30:91c1d09ada54 | 68 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 30:91c1d09ada54 | 69 | } |
mbed_official | 30:91c1d09ada54 | 70 | |
mbed_official | 30:91c1d09ada54 | 71 | static inline uint32_t adc_read(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 72 | // Select the appropriate channel and start conversion |
mbed_official | 30:91c1d09ada54 | 73 | LPC_ADC->CR &= ~0xFF; |
mbed_official | 30:91c1d09ada54 | 74 | LPC_ADC->CR |= 1 << (int)obj->adc; |
mbed_official | 30:91c1d09ada54 | 75 | LPC_ADC->CR |= 1 << 24; |
mbed_official | 30:91c1d09ada54 | 76 | |
mbed_official | 30:91c1d09ada54 | 77 | // Repeatedly get the sample data until DONE bit |
mbed_official | 30:91c1d09ada54 | 78 | unsigned int data; |
mbed_official | 30:91c1d09ada54 | 79 | do { |
mbed_official | 30:91c1d09ada54 | 80 | data = LPC_ADC->GDR; |
mbed_official | 30:91c1d09ada54 | 81 | } while ((data & ((unsigned int)1 << 31)) == 0); |
mbed_official | 30:91c1d09ada54 | 82 | |
mbed_official | 30:91c1d09ada54 | 83 | // Stop conversion |
mbed_official | 30:91c1d09ada54 | 84 | LPC_ADC->CR &= ~(1 << 24); |
mbed_official | 30:91c1d09ada54 | 85 | |
mbed_official | 30:91c1d09ada54 | 86 | return (data >> 6) & ADC_RANGE; // 10 bit |
mbed_official | 30:91c1d09ada54 | 87 | } |
mbed_official | 30:91c1d09ada54 | 88 | |
mbed_official | 30:91c1d09ada54 | 89 | static inline void order(uint32_t *a, uint32_t *b) { |
mbed_official | 30:91c1d09ada54 | 90 | if (*a > *b) { |
mbed_official | 30:91c1d09ada54 | 91 | uint32_t t = *a; |
mbed_official | 30:91c1d09ada54 | 92 | *a = *b; |
mbed_official | 30:91c1d09ada54 | 93 | *b = t; |
mbed_official | 30:91c1d09ada54 | 94 | } |
mbed_official | 30:91c1d09ada54 | 95 | } |
mbed_official | 30:91c1d09ada54 | 96 | |
mbed_official | 30:91c1d09ada54 | 97 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 98 | uint32_t value; |
mbed_official | 30:91c1d09ada54 | 99 | #if ANALOGIN_MEDIAN_FILTER |
mbed_official | 30:91c1d09ada54 | 100 | uint32_t v1 = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 101 | uint32_t v2 = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 102 | uint32_t v3 = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 103 | order(&v1, &v2); |
mbed_official | 30:91c1d09ada54 | 104 | order(&v2, &v3); |
mbed_official | 30:91c1d09ada54 | 105 | order(&v1, &v2); |
mbed_official | 30:91c1d09ada54 | 106 | value = v2; |
mbed_official | 30:91c1d09ada54 | 107 | #else |
mbed_official | 30:91c1d09ada54 | 108 | value = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 109 | #endif |
mbed_official | 30:91c1d09ada54 | 110 | return value; |
mbed_official | 30:91c1d09ada54 | 111 | } |
mbed_official | 30:91c1d09ada54 | 112 | |
mbed_official | 30:91c1d09ada54 | 113 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 114 | uint32_t value = adc_read_u32(obj); |
mbed_official | 30:91c1d09ada54 | 115 | |
mbed_official | 30:91c1d09ada54 | 116 | return (value << 6) | ((value >> 4) & 0x003F); // 10 bit |
mbed_official | 30:91c1d09ada54 | 117 | } |
mbed_official | 30:91c1d09ada54 | 118 | |
mbed_official | 30:91c1d09ada54 | 119 | float analogin_read(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 120 | uint32_t value = adc_read_u32(obj); |
mbed_official | 30:91c1d09ada54 | 121 | return (float)value * (1.0f / (float)ADC_RANGE); |
mbed_official | 30:91c1d09ada54 | 122 | } |