test
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC176X/analogin_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_NXP/TARGET_LPC176X/analogin_api.c@144:ef7eb2e8f9f7
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 17 | #include "analogin_api.h" |
<> | 144:ef7eb2e8f9f7 | 18 | |
<> | 144:ef7eb2e8f9f7 | 19 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 21 | |
<> | 144:ef7eb2e8f9f7 | 22 | #define ANALOGIN_MEDIAN_FILTER 1 |
<> | 144:ef7eb2e8f9f7 | 23 | |
<> | 144:ef7eb2e8f9f7 | 24 | #define ADC_10BIT_RANGE 0x3FF |
<> | 144:ef7eb2e8f9f7 | 25 | #define ADC_12BIT_RANGE 0xFFF |
<> | 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 | {P0_23, ADC0_0, 1}, |
<> | 144:ef7eb2e8f9f7 | 33 | {P0_24, ADC0_1, 1}, |
<> | 144:ef7eb2e8f9f7 | 34 | {P0_25, ADC0_2, 1}, |
<> | 144:ef7eb2e8f9f7 | 35 | {P0_26, ADC0_3, 1}, |
<> | 144:ef7eb2e8f9f7 | 36 | {P1_30, ADC0_4, 3}, |
<> | 144:ef7eb2e8f9f7 | 37 | {P1_31, ADC0_5, 3}, |
<> | 144:ef7eb2e8f9f7 | 38 | {P0_2, ADC0_7, 2}, |
<> | 144:ef7eb2e8f9f7 | 39 | {P0_3, ADC0_6, 2}, |
<> | 144:ef7eb2e8f9f7 | 40 | {NC, NC, 0} |
<> | 144:ef7eb2e8f9f7 | 41 | }; |
<> | 144:ef7eb2e8f9f7 | 42 | |
<> | 144:ef7eb2e8f9f7 | 43 | #define ADC_RANGE ADC_12BIT_RANGE |
<> | 144:ef7eb2e8f9f7 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | void analogin_init(analogin_t *obj, PinName pin) { |
<> | 144:ef7eb2e8f9f7 | 46 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 47 | MBED_ASSERT(obj->adc != (ADCName)NC); |
<> | 144:ef7eb2e8f9f7 | 48 | |
<> | 144:ef7eb2e8f9f7 | 49 | // ensure power is turned on |
<> | 144:ef7eb2e8f9f7 | 50 | LPC_SC->PCONP |= (1 << 12); |
<> | 144:ef7eb2e8f9f7 | 51 | |
<> | 144:ef7eb2e8f9f7 | 52 | // set PCLK of ADC to /1 |
<> | 144:ef7eb2e8f9f7 | 53 | LPC_SC->PCLKSEL0 &= ~(0x3 << 24); |
<> | 144:ef7eb2e8f9f7 | 54 | LPC_SC->PCLKSEL0 |= (0x1 << 24); |
<> | 144:ef7eb2e8f9f7 | 55 | uint32_t PCLK = SystemCoreClock; |
<> | 144:ef7eb2e8f9f7 | 56 | |
<> | 144:ef7eb2e8f9f7 | 57 | // calculate minimum clock divider |
<> | 144:ef7eb2e8f9f7 | 58 | // clkdiv = divider - 1 |
<> | 144:ef7eb2e8f9f7 | 59 | uint32_t MAX_ADC_CLK = 13000000; |
<> | 144:ef7eb2e8f9f7 | 60 | uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1; |
<> | 144:ef7eb2e8f9f7 | 61 | |
<> | 144:ef7eb2e8f9f7 | 62 | // Set the generic software-controlled ADC settings |
<> | 144:ef7eb2e8f9f7 | 63 | LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected |
<> | 144:ef7eb2e8f9f7 | 64 | | (clkdiv << 8) // CLKDIV: PCLK max ~= 25MHz, /25 to give safe 1MHz at fastest |
<> | 144:ef7eb2e8f9f7 | 65 | | (0 << 16) // BURST: 0 = software control |
<> | 144:ef7eb2e8f9f7 | 66 | | (0 << 17) // CLKS: not applicable |
<> | 144:ef7eb2e8f9f7 | 67 | | (1 << 21) // PDN: 1 = operational |
<> | 144:ef7eb2e8f9f7 | 68 | | (0 << 24) // START: 0 = no start |
<> | 144:ef7eb2e8f9f7 | 69 | | (0 << 27); // EDGE: not applicable |
<> | 144:ef7eb2e8f9f7 | 70 | |
<> | 144:ef7eb2e8f9f7 | 71 | pinmap_pinout(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | static inline uint32_t adc_read(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 75 | // Select the appropriate channel and start conversion |
<> | 144:ef7eb2e8f9f7 | 76 | LPC_ADC->ADCR &= ~0xFF; |
<> | 144:ef7eb2e8f9f7 | 77 | LPC_ADC->ADCR |= 1 << (int)obj->adc; |
<> | 144:ef7eb2e8f9f7 | 78 | LPC_ADC->ADCR |= 1 << 24; |
<> | 144:ef7eb2e8f9f7 | 79 | |
<> | 144:ef7eb2e8f9f7 | 80 | // Repeatedly get the sample data until DONE bit |
<> | 144:ef7eb2e8f9f7 | 81 | unsigned int data; |
<> | 144:ef7eb2e8f9f7 | 82 | do { |
<> | 144:ef7eb2e8f9f7 | 83 | data = LPC_ADC->ADGDR; |
<> | 144:ef7eb2e8f9f7 | 84 | } while ((data & ((unsigned int)1 << 31)) == 0); |
<> | 144:ef7eb2e8f9f7 | 85 | |
<> | 144:ef7eb2e8f9f7 | 86 | // Stop conversion |
<> | 144:ef7eb2e8f9f7 | 87 | LPC_ADC->ADCR &= ~(1 << 24); |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | return (data >> 4) & ADC_RANGE; // 12 bit |
<> | 144:ef7eb2e8f9f7 | 90 | } |
<> | 144:ef7eb2e8f9f7 | 91 | |
<> | 144:ef7eb2e8f9f7 | 92 | static inline void order(uint32_t *a, uint32_t *b) { |
<> | 144:ef7eb2e8f9f7 | 93 | if (*a > *b) { |
<> | 144:ef7eb2e8f9f7 | 94 | uint32_t t = *a; |
<> | 144:ef7eb2e8f9f7 | 95 | *a = *b; |
<> | 144:ef7eb2e8f9f7 | 96 | *b = t; |
<> | 144:ef7eb2e8f9f7 | 97 | } |
<> | 144:ef7eb2e8f9f7 | 98 | } |
<> | 144:ef7eb2e8f9f7 | 99 | |
<> | 144:ef7eb2e8f9f7 | 100 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 101 | uint32_t value; |
<> | 144:ef7eb2e8f9f7 | 102 | #if ANALOGIN_MEDIAN_FILTER |
<> | 144:ef7eb2e8f9f7 | 103 | uint32_t v1 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 104 | uint32_t v2 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 105 | uint32_t v3 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 106 | order(&v1, &v2); |
<> | 144:ef7eb2e8f9f7 | 107 | order(&v2, &v3); |
<> | 144:ef7eb2e8f9f7 | 108 | order(&v1, &v2); |
<> | 144:ef7eb2e8f9f7 | 109 | value = v2; |
<> | 144:ef7eb2e8f9f7 | 110 | #else |
<> | 144:ef7eb2e8f9f7 | 111 | value = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 112 | #endif |
<> | 144:ef7eb2e8f9f7 | 113 | return value; |
<> | 144:ef7eb2e8f9f7 | 114 | } |
<> | 144:ef7eb2e8f9f7 | 115 | |
<> | 144:ef7eb2e8f9f7 | 116 | uint16_t analogin_read_u16(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 117 | uint32_t value = adc_read_u32(obj); |
<> | 144:ef7eb2e8f9f7 | 118 | |
<> | 144:ef7eb2e8f9f7 | 119 | return (value << 4) | ((value >> 8) & 0x000F); // 12 bit |
<> | 144:ef7eb2e8f9f7 | 120 | } |
<> | 144:ef7eb2e8f9f7 | 121 | |
<> | 144:ef7eb2e8f9f7 | 122 | float analogin_read(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 123 | uint32_t value = adc_read_u32(obj); |
<> | 144:ef7eb2e8f9f7 | 124 | return (float)value * (1.0f / (float)ADC_RANGE); |
<> | 144:ef7eb2e8f9f7 | 125 | } |