mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew 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 * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18 #include "mbed_assert.h"
elessair 0:f269e3021894 19 #include "analogin_api.h"
elessair 0:f269e3021894 20 #include "cmsis.h"
elessair 0:f269e3021894 21 #include "pinmap.h"
elessair 0:f269e3021894 22 #include "mbed_error.h"
elessair 0:f269e3021894 23 #include "gpio_api.h"
elessair 0:f269e3021894 24
elessair 0:f269e3021894 25 #define ANALOGIN_MEDIAN_FILTER 1
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 {P4_3, ADC0_0, 0},
elessair 0:f269e3021894 33 {P4_1, ADC0_1, 0},
elessair 0:f269e3021894 34 {PF_8, ADC0_2, 0},
elessair 0:f269e3021894 35 {P7_5, ADC0_3, 0},
elessair 0:f269e3021894 36 {P7_4, ADC0_4, 0},
elessair 0:f269e3021894 37 {PF_10, ADC0_5, 0},
elessair 0:f269e3021894 38 {PB_6, ADC0_6, 0},
elessair 0:f269e3021894 39 {PC_3, ADC1_0, 0},
elessair 0:f269e3021894 40 {PC_0, ADC1_1, 0},
elessair 0:f269e3021894 41 {PF_9, ADC1_2, 0},
elessair 0:f269e3021894 42 {PF_6, ADC1_3, 0},
elessair 0:f269e3021894 43 {PF_5, ADC1_4, 0},
elessair 0:f269e3021894 44 {PF_11, ADC1_5, 0},
elessair 0:f269e3021894 45 {P7_7, ADC1_6, 0},
elessair 0:f269e3021894 46 {PF_7, ADC1_7, 0},
elessair 0:f269e3021894 47 {adc0_0, ADC_pin0_0, 0},
elessair 0:f269e3021894 48 {adc0_1, ADC_pin0_1, 0},
elessair 0:f269e3021894 49 {adc0_2, ADC_pin0_2, 0},
elessair 0:f269e3021894 50 {adc0_3, ADC_pin0_3, 0},
elessair 0:f269e3021894 51 {adc0_4, ADC_pin0_4, 0},
elessair 0:f269e3021894 52 {adc0_5, ADC_pin0_5, 0},
elessair 0:f269e3021894 53 {adc0_6, ADC_pin0_6, 0},
elessair 0:f269e3021894 54 {adc0_7, ADC_pin0_7, 0},
elessair 0:f269e3021894 55 {adc1_0, ADC_pin1_0, 0},
elessair 0:f269e3021894 56 {adc1_1, ADC_pin1_1, 0},
elessair 0:f269e3021894 57 {adc1_2, ADC_pin1_2, 0},
elessair 0:f269e3021894 58 {adc1_3, ADC_pin1_3, 0},
elessair 0:f269e3021894 59 {adc1_4, ADC_pin1_4, 0},
elessair 0:f269e3021894 60 {adc1_5, ADC_pin1_5, 0},
elessair 0:f269e3021894 61 {adc1_6, ADC_pin1_6, 0},
elessair 0:f269e3021894 62 {adc1_7, ADC_pin1_7, 0},
elessair 0:f269e3021894 63 {NC, NC, 0 }
elessair 0:f269e3021894 64 };
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 void analogin_init(analogin_t *obj, PinName pin) {
elessair 0:f269e3021894 67 ADCName name;
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 name = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
elessair 0:f269e3021894 70 MBED_ASSERT(obj->adc != (LPC_ADC_T *)NC);
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 // Set ADC number
elessair 0:f269e3021894 73 if(name < ADC1_0) {
elessair 0:f269e3021894 74 obj->num = 0;
elessair 0:f269e3021894 75 } else if(name < ADC_pin0_0 && name > ADC0_6) {
elessair 0:f269e3021894 76 obj->num = 1;
elessair 0:f269e3021894 77 } else if(name < ADC_pin1_1 && name > ADC1_7) {
elessair 0:f269e3021894 78 obj->num = 0;
elessair 0:f269e3021894 79 } else if(name > ADC_pin0_7) {
elessair 0:f269e3021894 80 obj->num = 1;
elessair 0:f269e3021894 81 }
elessair 0:f269e3021894 82
elessair 0:f269e3021894 83 //ADC register and channel
elessair 0:f269e3021894 84 obj->ch = name % (ADC0_7 + 1);
elessair 0:f269e3021894 85 obj->adc = (LPC_ADC_T *) (obj->num > 0) ? LPC_ADC1 : LPC_ADC0;
elessair 0:f269e3021894 86
elessair 0:f269e3021894 87 // Reset pin function to GPIO if it is a GPIO pin. for adc only pins it is not necessary
elessair 0:f269e3021894 88 if(name < ADC_pin0_0) {
elessair 0:f269e3021894 89 gpio_set(pin);
elessair 0:f269e3021894 90 // Select ADC on analog function select register in SCU
elessair 0:f269e3021894 91 LPC_SCU->ENAIO[obj->num] |= (1 << obj->ch);
elessair 0:f269e3021894 92 } else {
elessair 0:f269e3021894 93 LPC_SCU->ENAIO[obj->num] &= ~(1 << obj->ch);
elessair 0:f269e3021894 94 }
elessair 0:f269e3021894 95
elessair 0:f269e3021894 96 // Calculate minimum clock divider
elessair 0:f269e3021894 97 // clkdiv = divider - 1
elessair 0:f269e3021894 98 uint32_t PCLK = SystemCoreClock;
elessair 0:f269e3021894 99 uint32_t adcRate = 400000;
elessair 0:f269e3021894 100 uint32_t clkdiv = div_round_up(PCLK, adcRate) - 1;
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 // Set the generic software-controlled ADC settings
elessair 0:f269e3021894 103 obj->adc->CR = (0 << 0) // SEL: 0 = no channels selected
elessair 0:f269e3021894 104 | (clkdiv << 8) // CLKDIV:
elessair 0:f269e3021894 105 | (0 << 16) // BURST: 0 = software control
elessair 0:f269e3021894 106 | (1 << 21) // PDN: 1 = operational
elessair 0:f269e3021894 107 | (0 << 24) // START: 0 = no start
elessair 0:f269e3021894 108 | (0 << 27); // EDGE: not applicable
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 static inline uint32_t adc_read(analogin_t *obj) {
elessair 0:f269e3021894 112 uint32_t temp;
elessair 0:f269e3021894 113 uint8_t channel = obj->ch;
elessair 0:f269e3021894 114 LPC_ADC_T *pADC = obj->adc;
elessair 0:f269e3021894 115
elessair 0:f269e3021894 116 // Select the appropriate channel and start conversion
elessair 0:f269e3021894 117 pADC->CR |= ADC_CR_CH_SEL(channel);
elessair 0:f269e3021894 118 temp = pADC->CR & ~ADC_CR_START_MASK;
elessair 0:f269e3021894 119 pADC->CR = temp | (ADC_CR_START_MODE_SEL(ADC_START_NOW));
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 // Wait for DONE bit and read data
elessair 0:f269e3021894 122 while (!(pADC->STAT & ADC_CR_CH_SEL(channel)));
elessair 0:f269e3021894 123 temp = pADC->DR[channel];
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 // Deselect channel and return result
elessair 0:f269e3021894 126 pADC->CR &= ~ADC_CR_START_MASK;
elessair 0:f269e3021894 127 pADC->CR &= ~ADC_CR_CH_SEL(channel);
elessair 0:f269e3021894 128 return ADC_DR_RESULT(temp);
elessair 0:f269e3021894 129 }
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 static inline void order(uint32_t *a, uint32_t *b) {
elessair 0:f269e3021894 132 if (*a > *b) {
elessair 0:f269e3021894 133 uint32_t t = *a;
elessair 0:f269e3021894 134 *a = *b;
elessair 0:f269e3021894 135 *b = t;
elessair 0:f269e3021894 136 }
elessair 0:f269e3021894 137 }
elessair 0:f269e3021894 138
elessair 0:f269e3021894 139 static inline uint32_t adc_read_u32(analogin_t *obj) {
elessair 0:f269e3021894 140 uint32_t value;
elessair 0:f269e3021894 141 #if ANALOGIN_MEDIAN_FILTER
elessair 0:f269e3021894 142 uint32_t v1 = adc_read(obj);
elessair 0:f269e3021894 143 uint32_t v2 = adc_read(obj);
elessair 0:f269e3021894 144 uint32_t v3 = adc_read(obj);
elessair 0:f269e3021894 145 order(&v1, &v2);
elessair 0:f269e3021894 146 order(&v2, &v3);
elessair 0:f269e3021894 147 order(&v1, &v2);
elessair 0:f269e3021894 148 value = v2;
elessair 0:f269e3021894 149 #else
elessair 0:f269e3021894 150 value = adc_read(obj);
elessair 0:f269e3021894 151 #endif
elessair 0:f269e3021894 152 return value;
elessair 0:f269e3021894 153 }
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 uint16_t analogin_read_u16(analogin_t *obj) {
elessair 0:f269e3021894 156 uint32_t value = adc_read_u32(obj);
elessair 0:f269e3021894 157
elessair 0:f269e3021894 158 return (value << 6) | ((value >> 4) & 0x003F); // 10 bit
elessair 0:f269e3021894 159 }
elessair 0:f269e3021894 160
elessair 0:f269e3021894 161 float analogin_read(analogin_t *obj) {
elessair 0:f269e3021894 162 uint32_t value = adc_read_u32(obj);
elessair 0:f269e3021894 163 return (float)value * (1.0f / (float)ADC_RANGE);
elessair 0:f269e3021894 164 }