mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC43XX/analogin_api.c@165:2dd56e6daeec, 2017-05-23 (annotated)
- Committer:
- ranaumarnaeem
- Date:
- Tue May 23 12:54:50 2017 +0000
- Revision:
- 165:2dd56e6daeec
- Parent:
- 149:156823d33999
jhjg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | * |
<> | 144:ef7eb2e8f9f7 | 16 | * Ported to NXP LPC43XX by Micromint USA <support@micromint.com> |
<> | 144:ef7eb2e8f9f7 | 17 | */ |
<> | 144:ef7eb2e8f9f7 | 18 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include "analogin_api.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 21 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 22 | #include "mbed_error.h" |
<> | 144:ef7eb2e8f9f7 | 23 | #include "gpio_api.h" |
<> | 144:ef7eb2e8f9f7 | 24 | |
<> | 144:ef7eb2e8f9f7 | 25 | #define ANALOGIN_MEDIAN_FILTER 1 |
<> | 144:ef7eb2e8f9f7 | 26 | |
<> | 144:ef7eb2e8f9f7 | 27 | static inline int div_round_up(int x, int y) { |
<> | 144:ef7eb2e8f9f7 | 28 | return (x + (y - 1)) / y; |
<> | 144:ef7eb2e8f9f7 | 29 | } |
<> | 144:ef7eb2e8f9f7 | 30 | |
<> | 144:ef7eb2e8f9f7 | 31 | static const PinMap PinMap_ADC[] = { |
<> | 144:ef7eb2e8f9f7 | 32 | {P4_3, ADC0_0, 0}, |
<> | 144:ef7eb2e8f9f7 | 33 | {P4_1, ADC0_1, 0}, |
<> | 144:ef7eb2e8f9f7 | 34 | {PF_8, ADC0_2, 0}, |
<> | 144:ef7eb2e8f9f7 | 35 | {P7_5, ADC0_3, 0}, |
<> | 144:ef7eb2e8f9f7 | 36 | {P7_4, ADC0_4, 0}, |
<> | 144:ef7eb2e8f9f7 | 37 | {PF_10, ADC0_5, 0}, |
<> | 144:ef7eb2e8f9f7 | 38 | {PB_6, ADC0_6, 0}, |
<> | 144:ef7eb2e8f9f7 | 39 | {PC_3, ADC1_0, 0}, |
<> | 144:ef7eb2e8f9f7 | 40 | {PC_0, ADC1_1, 0}, |
<> | 144:ef7eb2e8f9f7 | 41 | {PF_9, ADC1_2, 0}, |
<> | 144:ef7eb2e8f9f7 | 42 | {PF_6, ADC1_3, 0}, |
<> | 144:ef7eb2e8f9f7 | 43 | {PF_5, ADC1_4, 0}, |
<> | 144:ef7eb2e8f9f7 | 44 | {PF_11, ADC1_5, 0}, |
<> | 144:ef7eb2e8f9f7 | 45 | {P7_7, ADC1_6, 0}, |
<> | 144:ef7eb2e8f9f7 | 46 | {PF_7, ADC1_7, 0}, |
<> | 148:21d94c44109e | 47 | {adc0_0, ADC_pin0_0, 0}, |
<> | 148:21d94c44109e | 48 | {adc0_1, ADC_pin0_1, 0}, |
<> | 148:21d94c44109e | 49 | {adc0_2, ADC_pin0_2, 0}, |
<> | 148:21d94c44109e | 50 | {adc0_3, ADC_pin0_3, 0}, |
<> | 148:21d94c44109e | 51 | {adc0_4, ADC_pin0_4, 0}, |
<> | 148:21d94c44109e | 52 | {adc0_5, ADC_pin0_5, 0}, |
<> | 148:21d94c44109e | 53 | {adc0_6, ADC_pin0_6, 0}, |
<> | 148:21d94c44109e | 54 | {adc0_7, ADC_pin0_7, 0}, |
<> | 148:21d94c44109e | 55 | {adc1_0, ADC_pin1_0, 0}, |
<> | 148:21d94c44109e | 56 | {adc1_1, ADC_pin1_1, 0}, |
<> | 148:21d94c44109e | 57 | {adc1_2, ADC_pin1_2, 0}, |
<> | 148:21d94c44109e | 58 | {adc1_3, ADC_pin1_3, 0}, |
<> | 148:21d94c44109e | 59 | {adc1_4, ADC_pin1_4, 0}, |
<> | 148:21d94c44109e | 60 | {adc1_5, ADC_pin1_5, 0}, |
<> | 148:21d94c44109e | 61 | {adc1_6, ADC_pin1_6, 0}, |
<> | 148:21d94c44109e | 62 | {adc1_7, ADC_pin1_7, 0}, |
<> | 144:ef7eb2e8f9f7 | 63 | {NC, NC, 0 } |
<> | 144:ef7eb2e8f9f7 | 64 | }; |
<> | 144:ef7eb2e8f9f7 | 65 | |
<> | 144:ef7eb2e8f9f7 | 66 | void analogin_init(analogin_t *obj, PinName pin) { |
<> | 144:ef7eb2e8f9f7 | 67 | ADCName name; |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | name = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 70 | MBED_ASSERT(obj->adc != (LPC_ADC_T *)NC); |
<> | 148:21d94c44109e | 71 | |
<> | 148:21d94c44109e | 72 | // Set ADC number |
<> | 148:21d94c44109e | 73 | if(name < ADC1_0) { |
<> | 148:21d94c44109e | 74 | obj->num = 0; |
<> | 148:21d94c44109e | 75 | } else if(name < ADC_pin0_0 && name > ADC0_6) { |
<> | 148:21d94c44109e | 76 | obj->num = 1; |
<> | 148:21d94c44109e | 77 | } else if(name < ADC_pin1_1 && name > ADC1_7) { |
<> | 148:21d94c44109e | 78 | obj->num = 0; |
<> | 148:21d94c44109e | 79 | } else if(name > ADC_pin0_7) { |
<> | 148:21d94c44109e | 80 | obj->num = 1; |
<> | 148:21d94c44109e | 81 | } |
<> | 144:ef7eb2e8f9f7 | 82 | |
<> | 148:21d94c44109e | 83 | //ADC register and channel |
<> | 144:ef7eb2e8f9f7 | 84 | obj->ch = name % (ADC0_7 + 1); |
<> | 144:ef7eb2e8f9f7 | 85 | obj->adc = (LPC_ADC_T *) (obj->num > 0) ? LPC_ADC1 : LPC_ADC0; |
<> | 144:ef7eb2e8f9f7 | 86 | |
<> | 148:21d94c44109e | 87 | // Reset pin function to GPIO if it is a GPIO pin. for adc only pins it is not necessary |
<> | 148:21d94c44109e | 88 | if(name < ADC_pin0_0) { |
<> | 148:21d94c44109e | 89 | gpio_set(pin); |
<> | 148:21d94c44109e | 90 | // Select ADC on analog function select register in SCU |
<> | 148:21d94c44109e | 91 | LPC_SCU->ENAIO[obj->num] |= (1 << obj->ch); |
<> | 148:21d94c44109e | 92 | } else { |
<> | 148:21d94c44109e | 93 | LPC_SCU->ENAIO[obj->num] &= ~(1 << obj->ch); |
<> | 148:21d94c44109e | 94 | } |
<> | 144:ef7eb2e8f9f7 | 95 | |
<> | 144:ef7eb2e8f9f7 | 96 | // Calculate minimum clock divider |
<> | 144:ef7eb2e8f9f7 | 97 | // clkdiv = divider - 1 |
<> | 144:ef7eb2e8f9f7 | 98 | uint32_t PCLK = SystemCoreClock; |
<> | 144:ef7eb2e8f9f7 | 99 | uint32_t adcRate = 400000; |
<> | 144:ef7eb2e8f9f7 | 100 | uint32_t clkdiv = div_round_up(PCLK, adcRate) - 1; |
<> | 144:ef7eb2e8f9f7 | 101 | |
<> | 144:ef7eb2e8f9f7 | 102 | // Set the generic software-controlled ADC settings |
<> | 144:ef7eb2e8f9f7 | 103 | obj->adc->CR = (0 << 0) // SEL: 0 = no channels selected |
<> | 144:ef7eb2e8f9f7 | 104 | | (clkdiv << 8) // CLKDIV: |
<> | 144:ef7eb2e8f9f7 | 105 | | (0 << 16) // BURST: 0 = software control |
<> | 144:ef7eb2e8f9f7 | 106 | | (1 << 21) // PDN: 1 = operational |
<> | 144:ef7eb2e8f9f7 | 107 | | (0 << 24) // START: 0 = no start |
<> | 144:ef7eb2e8f9f7 | 108 | | (0 << 27); // EDGE: not applicable |
<> | 144:ef7eb2e8f9f7 | 109 | } |
<> | 144:ef7eb2e8f9f7 | 110 | |
<> | 144:ef7eb2e8f9f7 | 111 | static inline uint32_t adc_read(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 112 | uint32_t temp; |
<> | 144:ef7eb2e8f9f7 | 113 | uint8_t channel = obj->ch; |
<> | 144:ef7eb2e8f9f7 | 114 | LPC_ADC_T *pADC = obj->adc; |
<> | 144:ef7eb2e8f9f7 | 115 | |
<> | 144:ef7eb2e8f9f7 | 116 | // Select the appropriate channel and start conversion |
<> | 144:ef7eb2e8f9f7 | 117 | pADC->CR |= ADC_CR_CH_SEL(channel); |
<> | 144:ef7eb2e8f9f7 | 118 | temp = pADC->CR & ~ADC_CR_START_MASK; |
<> | 144:ef7eb2e8f9f7 | 119 | pADC->CR = temp | (ADC_CR_START_MODE_SEL(ADC_START_NOW)); |
<> | 144:ef7eb2e8f9f7 | 120 | |
<> | 144:ef7eb2e8f9f7 | 121 | // Wait for DONE bit and read data |
<> | 144:ef7eb2e8f9f7 | 122 | while (!(pADC->STAT & ADC_CR_CH_SEL(channel))); |
<> | 144:ef7eb2e8f9f7 | 123 | temp = pADC->DR[channel]; |
<> | 144:ef7eb2e8f9f7 | 124 | |
<> | 144:ef7eb2e8f9f7 | 125 | // Deselect channel and return result |
<> | 144:ef7eb2e8f9f7 | 126 | pADC->CR &= ~ADC_CR_START_MASK; |
<> | 144:ef7eb2e8f9f7 | 127 | pADC->CR &= ~ADC_CR_CH_SEL(channel); |
<> | 144:ef7eb2e8f9f7 | 128 | return ADC_DR_RESULT(temp); |
<> | 144:ef7eb2e8f9f7 | 129 | } |
<> | 144:ef7eb2e8f9f7 | 130 | |
<> | 144:ef7eb2e8f9f7 | 131 | static inline void order(uint32_t *a, uint32_t *b) { |
<> | 144:ef7eb2e8f9f7 | 132 | if (*a > *b) { |
<> | 144:ef7eb2e8f9f7 | 133 | uint32_t t = *a; |
<> | 144:ef7eb2e8f9f7 | 134 | *a = *b; |
<> | 144:ef7eb2e8f9f7 | 135 | *b = t; |
<> | 144:ef7eb2e8f9f7 | 136 | } |
<> | 144:ef7eb2e8f9f7 | 137 | } |
<> | 144:ef7eb2e8f9f7 | 138 | |
<> | 144:ef7eb2e8f9f7 | 139 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 140 | uint32_t value; |
<> | 144:ef7eb2e8f9f7 | 141 | #if ANALOGIN_MEDIAN_FILTER |
<> | 144:ef7eb2e8f9f7 | 142 | uint32_t v1 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 143 | uint32_t v2 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 144 | uint32_t v3 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 145 | order(&v1, &v2); |
<> | 144:ef7eb2e8f9f7 | 146 | order(&v2, &v3); |
<> | 144:ef7eb2e8f9f7 | 147 | order(&v1, &v2); |
<> | 144:ef7eb2e8f9f7 | 148 | value = v2; |
<> | 144:ef7eb2e8f9f7 | 149 | #else |
<> | 144:ef7eb2e8f9f7 | 150 | value = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 151 | #endif |
<> | 144:ef7eb2e8f9f7 | 152 | return value; |
<> | 144:ef7eb2e8f9f7 | 153 | } |
<> | 144:ef7eb2e8f9f7 | 154 | |
<> | 144:ef7eb2e8f9f7 | 155 | uint16_t analogin_read_u16(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 156 | uint32_t value = adc_read_u32(obj); |
<> | 144:ef7eb2e8f9f7 | 157 | |
<> | 144:ef7eb2e8f9f7 | 158 | return (value << 6) | ((value >> 4) & 0x003F); // 10 bit |
<> | 144:ef7eb2e8f9f7 | 159 | } |
<> | 144:ef7eb2e8f9f7 | 160 | |
<> | 144:ef7eb2e8f9f7 | 161 | float analogin_read(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 162 | uint32_t value = adc_read_u32(obj); |
<> | 144:ef7eb2e8f9f7 | 163 | return (float)value * (1.0f / (float)ADC_RANGE); |
<> | 144:ef7eb2e8f9f7 | 164 | } |