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