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@32:4742f6c694e8, 2013-09-30 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Sep 30 20:45:04 2013 +0100
- Revision:
- 32:4742f6c694e8
- Parent:
- 30:91c1d09ada54
- Child:
- 227:7bd0639b8911
Synchronized with git revision e8afb9cd1deecf3a0ef2c140d38e3f2d68a2eb50
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 | 30:91c1d09ada54 | 16 | #include "analogin_api.h" |
mbed_official | 30:91c1d09ada54 | 17 | #include "cmsis.h" |
mbed_official | 30:91c1d09ada54 | 18 | #include "pinmap.h" |
mbed_official | 30:91c1d09ada54 | 19 | #include "error.h" |
mbed_official | 30:91c1d09ada54 | 20 | |
mbed_official | 32:4742f6c694e8 | 21 | static const PinMap PinMap_ADC[] = { |
mbed_official | 32:4742f6c694e8 | 22 | {P0_11, ADC0_0, 2}, |
mbed_official | 32:4742f6c694e8 | 23 | {P1_0 , ADC0_1, 2}, |
mbed_official | 32:4742f6c694e8 | 24 | {P1_1 , ADC0_2, 2}, |
mbed_official | 32:4742f6c694e8 | 25 | {P1_2 , ADC0_3, 2}, |
mbed_official | 32:4742f6c694e8 | 26 | // {P1_3 , ADC0_4, 2}, -- should be mapped to SWDIO only |
mbed_official | 32:4742f6c694e8 | 27 | {P1_4 , ADC0_5, 1}, |
mbed_official | 32:4742f6c694e8 | 28 | {P1_10, ADC0_6, 1}, |
mbed_official | 32:4742f6c694e8 | 29 | {P1_11, ADC0_7, 1}, |
mbed_official | 32:4742f6c694e8 | 30 | {NC , NC , 0} |
mbed_official | 32:4742f6c694e8 | 31 | }; |
mbed_official | 30:91c1d09ada54 | 32 | |
mbed_official | 30:91c1d09ada54 | 33 | #define ANALOGIN_MEDIAN_FILTER 1 |
mbed_official | 30:91c1d09ada54 | 34 | |
mbed_official | 30:91c1d09ada54 | 35 | #define ADC_10BIT_RANGE 0x3FF |
mbed_official | 30:91c1d09ada54 | 36 | #define ADC_12BIT_RANGE 0xFFF |
mbed_official | 30:91c1d09ada54 | 37 | |
mbed_official | 30:91c1d09ada54 | 38 | static inline int div_round_up(int x, int y) { |
mbed_official | 30:91c1d09ada54 | 39 | return (x + (y - 1)) / y; |
mbed_official | 30:91c1d09ada54 | 40 | } |
mbed_official | 30:91c1d09ada54 | 41 | |
mbed_official | 30:91c1d09ada54 | 42 | #define ADC_RANGE ADC_10BIT_RANGE |
mbed_official | 30:91c1d09ada54 | 43 | |
mbed_official | 30:91c1d09ada54 | 44 | void analogin_init(analogin_t *obj, PinName pin) { |
mbed_official | 30:91c1d09ada54 | 45 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 30:91c1d09ada54 | 46 | if (obj->adc == (uint32_t)NC) { |
mbed_official | 30:91c1d09ada54 | 47 | error("ADC pin mapping failed"); |
mbed_official | 30:91c1d09ada54 | 48 | return; |
mbed_official | 30:91c1d09ada54 | 49 | } |
mbed_official | 30:91c1d09ada54 | 50 | |
mbed_official | 30:91c1d09ada54 | 51 | // Power up ADC |
mbed_official | 30:91c1d09ada54 | 52 | LPC_SYSCON->PDRUNCFG &= ~ (1 << 4); |
mbed_official | 30:91c1d09ada54 | 53 | LPC_SYSCON->SYSAHBCLKCTRL |= ((uint32_t)1 << 13); |
mbed_official | 30:91c1d09ada54 | 54 | |
mbed_official | 30:91c1d09ada54 | 55 | uint32_t offset = (uint32_t)pin & 0xff; |
mbed_official | 30:91c1d09ada54 | 56 | __IO uint32_t *reg = (__IO uint32_t*)(LPC_IOCON_BASE + offset); |
mbed_official | 30:91c1d09ada54 | 57 | |
mbed_official | 30:91c1d09ada54 | 58 | // set pin to ADC mode |
mbed_official | 30:91c1d09ada54 | 59 | *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode) |
mbed_official | 30:91c1d09ada54 | 60 | |
mbed_official | 30:91c1d09ada54 | 61 | uint32_t PCLK = SystemCoreClock; |
mbed_official | 30:91c1d09ada54 | 62 | uint32_t MAX_ADC_CLK = 4500000; |
mbed_official | 30:91c1d09ada54 | 63 | uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1; |
mbed_official | 30:91c1d09ada54 | 64 | |
mbed_official | 30:91c1d09ada54 | 65 | LPC_ADC->CR = (0 << 0) // no channels selected |
mbed_official | 30:91c1d09ada54 | 66 | | (clkdiv << 8) // max of 4.5MHz |
mbed_official | 30:91c1d09ada54 | 67 | | (0 << 16) // BURST = 0, software controlled |
mbed_official | 30:91c1d09ada54 | 68 | | ( 0 << 17 ); // CLKS = 0, not applicable |
mbed_official | 30:91c1d09ada54 | 69 | |
mbed_official | 30:91c1d09ada54 | 70 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 30:91c1d09ada54 | 71 | } |
mbed_official | 30:91c1d09ada54 | 72 | |
mbed_official | 30:91c1d09ada54 | 73 | static inline uint32_t adc_read(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 74 | // Select the appropriate channel and start conversion |
mbed_official | 30:91c1d09ada54 | 75 | LPC_ADC->CR &= ~0xFF; |
mbed_official | 30:91c1d09ada54 | 76 | LPC_ADC->CR |= 1 << (int)obj->adc; |
mbed_official | 30:91c1d09ada54 | 77 | LPC_ADC->CR |= 1 << 24; |
mbed_official | 30:91c1d09ada54 | 78 | |
mbed_official | 30:91c1d09ada54 | 79 | // Repeatedly get the sample data until DONE bit |
mbed_official | 30:91c1d09ada54 | 80 | unsigned int data; |
mbed_official | 30:91c1d09ada54 | 81 | do { |
mbed_official | 30:91c1d09ada54 | 82 | data = LPC_ADC->GDR; |
mbed_official | 30:91c1d09ada54 | 83 | } while ((data & ((unsigned int)1 << 31)) == 0); |
mbed_official | 30:91c1d09ada54 | 84 | |
mbed_official | 30:91c1d09ada54 | 85 | // Stop conversion |
mbed_official | 30:91c1d09ada54 | 86 | LPC_ADC->CR &= ~(1 << 24); |
mbed_official | 30:91c1d09ada54 | 87 | |
mbed_official | 30:91c1d09ada54 | 88 | return (data >> 6) & ADC_RANGE; // 10 bit |
mbed_official | 30:91c1d09ada54 | 89 | } |
mbed_official | 30:91c1d09ada54 | 90 | |
mbed_official | 30:91c1d09ada54 | 91 | static inline void order(uint32_t *a, uint32_t *b) { |
mbed_official | 30:91c1d09ada54 | 92 | if (*a > *b) { |
mbed_official | 30:91c1d09ada54 | 93 | uint32_t t = *a; |
mbed_official | 30:91c1d09ada54 | 94 | *a = *b; |
mbed_official | 30:91c1d09ada54 | 95 | *b = t; |
mbed_official | 30:91c1d09ada54 | 96 | } |
mbed_official | 30:91c1d09ada54 | 97 | } |
mbed_official | 30:91c1d09ada54 | 98 | |
mbed_official | 30:91c1d09ada54 | 99 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 100 | uint32_t value; |
mbed_official | 30:91c1d09ada54 | 101 | #if ANALOGIN_MEDIAN_FILTER |
mbed_official | 30:91c1d09ada54 | 102 | uint32_t v1 = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 103 | uint32_t v2 = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 104 | uint32_t v3 = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 105 | order(&v1, &v2); |
mbed_official | 30:91c1d09ada54 | 106 | order(&v2, &v3); |
mbed_official | 30:91c1d09ada54 | 107 | order(&v1, &v2); |
mbed_official | 30:91c1d09ada54 | 108 | value = v2; |
mbed_official | 30:91c1d09ada54 | 109 | #else |
mbed_official | 30:91c1d09ada54 | 110 | value = adc_read(obj); |
mbed_official | 30:91c1d09ada54 | 111 | #endif |
mbed_official | 30:91c1d09ada54 | 112 | return value; |
mbed_official | 30:91c1d09ada54 | 113 | } |
mbed_official | 30:91c1d09ada54 | 114 | |
mbed_official | 30:91c1d09ada54 | 115 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 116 | uint32_t value = adc_read_u32(obj); |
mbed_official | 30:91c1d09ada54 | 117 | |
mbed_official | 30:91c1d09ada54 | 118 | return (value << 6) | ((value >> 4) & 0x003F); // 10 bit |
mbed_official | 30:91c1d09ada54 | 119 | } |
mbed_official | 30:91c1d09ada54 | 120 | |
mbed_official | 30:91c1d09ada54 | 121 | float analogin_read(analogin_t *obj) { |
mbed_official | 30:91c1d09ada54 | 122 | uint32_t value = adc_read_u32(obj); |
mbed_official | 30:91c1d09ada54 | 123 | return (float)value * (1.0f / (float)ADC_RANGE); |
mbed_official | 30:91c1d09ada54 | 124 | } |