|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 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 |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "analogin_api.h" |
| 18 | +#include "cmsis.h" |
| 19 | +#include "pinmap.h" |
| 20 | +#include "error.h" |
| 21 | + |
| 22 | +#define ANALOGIN_MEDIAN_FILTER 1 |
| 23 | + |
| 24 | +#define ADC_10BIT_RANGE 0x3FF |
| 25 | +#define ADC_12BIT_RANGE 0xFFF |
| 26 | + |
| 27 | +#define ADC_RANGE ADC_12BIT_RANGE |
| 28 | + |
| 29 | +static const PinMap PinMap_ADC[] = { |
| 30 | + {P0_8 , ADC0_0, 0}, |
| 31 | + {P0_7 , ADC0_1, 0}, |
| 32 | + {P0_6 , ADC0_2, 0}, |
| 33 | + {P0_5 , ADC0_3, 0}, |
| 34 | + {P0_4 , ADC0_4, 0}, |
| 35 | + {P0_3 , ADC0_5, 0}, |
| 36 | + {P0_2 , ADC0_6, 0}, |
| 37 | + {P0_1 , ADC0_7, 0}, |
| 38 | + {P1_0 , ADC0_8, 0}, |
| 39 | + {P0_31, ADC0_9, 0}, |
| 40 | + {P0_0 , ADC0_10,0}, |
| 41 | + {P0_30, ADC0_11,0}, |
| 42 | + {P1_1 , ADC1_0, 0}, |
| 43 | + {P0_9 , ADC1_1, 0}, |
| 44 | + {P0_10, ADC1_2, 0}, |
| 45 | + {P0_11, ADC1_3, 0}, |
| 46 | + {P1_2 , ADC1_4, 0}, |
| 47 | + {P1_3 , ADC1_5, 0}, |
| 48 | + {P0_13, ADC1_6, 0}, |
| 49 | + {P0_14, ADC1_7, 0}, |
| 50 | + {P0_15, ADC1_8, 0}, |
| 51 | + {P0_16, ADC1_9, 0}, |
| 52 | + {P1_4 , ADC1_10,0}, |
| 53 | + {P1_5 , ADC1_11,0}, |
| 54 | +}; |
| 55 | + |
| 56 | +void analogin_init(analogin_t *obj, PinName pin) { |
| 57 | + obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
| 58 | + if (obj->adc == (uint32_t)NC) { |
| 59 | + error("ADC pin mapping failed"); |
| 60 | + } |
| 61 | + uint32_t port = (pin >> 5); |
| 62 | + // enable clock for GPIOx |
| 63 | + LPC_SYSCON->SYSAHBCLKCTRL0 |= (1UL << (14 + port)); |
| 64 | + // pin enable |
| 65 | + LPC_SWM->PINENABLE0 &= ~(1UL << obj->adc); |
| 66 | + // configure GPIO as input |
| 67 | + LPC_GPIO_PORT->DIR[port] &= ~(1UL << (pin & 0x1F)); |
| 68 | + |
| 69 | + // power up ADC |
| 70 | + if (obj->adc < ADC1_0) |
| 71 | + { |
| 72 | + // ADC0 |
| 73 | + LPC_SYSCON->PDRUNCFG &= ~(1 << 10); |
| 74 | + LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 27); |
| 75 | + } |
| 76 | + else { |
| 77 | + // ADC1 |
| 78 | + LPC_SYSCON->PDRUNCFG &= ~(1 << 11); |
| 79 | + LPC_SYSCON->SYSAHBCLKCTRL0 |= (1 << 28); |
| 80 | + } |
| 81 | + |
| 82 | + // select IRC as async. clock, divided by 1 |
| 83 | + LPC_SYSCON->ADCASYNCCLKSEL = 0; |
| 84 | + LPC_SYSCON->ADCASYNCCLKDIV = 1; |
| 85 | + |
| 86 | + __IO LPC_ADC0_Type *adc_reg = (obj->adc < ADC1_0) ? (__IO LPC_ADC0_Type*)(LPC_ADC0) : (__IO LPC_ADC0_Type*)(LPC_ADC1); |
| 87 | + |
| 88 | + // start calibration |
| 89 | + adc_reg->CTRL |= (1UL << 30); |
| 90 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 91 | + |
| 92 | + // asynchronous mode |
| 93 | + adc_reg->CTRL = (1UL << 8); |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +static inline uint32_t adc_read(analogin_t *obj) { |
| 98 | + |
| 99 | + __IO LPC_ADC0_Type *adc_reg = (obj->adc < ADC1_0) ? (__IO LPC_ADC0_Type*)(LPC_ADC0) : (__IO LPC_ADC0_Type*)(LPC_ADC1); |
| 100 | + |
| 101 | + // select channel |
| 102 | + adc_reg->SEQA_CTRL &= ~(0xFFF); |
| 103 | + adc_reg->SEQA_CTRL |= (1UL << (obj->adc & 0x1F)); |
| 104 | + |
| 105 | + // start conversion and sequence enable |
| 106 | + adc_reg->SEQA_CTRL |= ((1UL << 26) | (1UL << 31)); |
| 107 | + |
| 108 | + // Repeatedly get the sample data until DONE bit |
| 109 | + volatile uint32_t data; |
| 110 | + do { |
| 111 | + data = adc_reg->SEQA_GDAT; |
| 112 | + } while ((data & (1UL << 31)) == 0); |
| 113 | + |
| 114 | + // Stop conversion |
| 115 | + adc_reg->SEQA_CTRL &= ~(1UL << 31); |
| 116 | + |
| 117 | + return ((data >> 4) & ADC_RANGE); |
| 118 | +} |
| 119 | + |
| 120 | +static inline void order(uint32_t *a, uint32_t *b) { |
| 121 | + if (*a > *b) { |
| 122 | + uint32_t t = *a; |
| 123 | + *a = *b; |
| 124 | + *b = t; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +static inline uint32_t adc_read_u32(analogin_t *obj) { |
| 129 | + uint32_t value; |
| 130 | +#if ANALOGIN_MEDIAN_FILTER |
| 131 | + uint32_t v1 = adc_read(obj); |
| 132 | + uint32_t v2 = adc_read(obj); |
| 133 | + uint32_t v3 = adc_read(obj); |
| 134 | + order(&v1, &v2); |
| 135 | + order(&v2, &v3); |
| 136 | + order(&v1, &v2); |
| 137 | + value = v2; |
| 138 | +#else |
| 139 | + value = adc_read(obj); |
| 140 | +#endif |
| 141 | + return value; |
| 142 | +} |
| 143 | + |
| 144 | +uint16_t analogin_read_u16(analogin_t *obj) { |
| 145 | + uint32_t value = adc_read_u32(obj); |
| 146 | + return (value << 4) | ((value >> 8) & 0x000F); // 12 bit |
| 147 | +} |
| 148 | + |
| 149 | +float analogin_read(analogin_t *obj) { |
| 150 | + uint32_t value = adc_read_u32(obj); |
| 151 | + return (float)value * (1.0f / (float)ADC_RANGE); |
| 152 | +} |
0 commit comments