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