mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
285:31249416b6f9
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 15:4892fe388435 1 /* mbed Microcontroller Library
bogdanm 15:4892fe388435 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 15:4892fe388435 3 *
bogdanm 15:4892fe388435 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 15:4892fe388435 5 * you may not use this file except in compliance with the License.
bogdanm 15:4892fe388435 6 * You may obtain a copy of the License at
bogdanm 15:4892fe388435 7 *
bogdanm 15:4892fe388435 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 15:4892fe388435 9 *
bogdanm 15:4892fe388435 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 15:4892fe388435 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 15:4892fe388435 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 15:4892fe388435 13 * See the License for the specific language governing permissions and
bogdanm 15:4892fe388435 14 * limitations under the License.
bogdanm 15:4892fe388435 15 */
mbed_official 227:7bd0639b8911 16 #include "mbed_assert.h"
bogdanm 15:4892fe388435 17 #include "analogin_api.h"
bogdanm 15:4892fe388435 18 #include "cmsis.h"
bogdanm 15:4892fe388435 19 #include "pinmap.h"
mbed_official 285:31249416b6f9 20 #include "mbed_error.h"
bogdanm 15:4892fe388435 21
bogdanm 15:4892fe388435 22 #define ANALOGIN_MEDIAN_FILTER 1
bogdanm 15:4892fe388435 23
bogdanm 15:4892fe388435 24 #define ADC_10BIT_RANGE 0x3FF
bogdanm 15:4892fe388435 25 #define ADC_12BIT_RANGE 0xFFF
bogdanm 15:4892fe388435 26
bogdanm 15:4892fe388435 27 static inline int div_round_up(int x, int y) {
bogdanm 15:4892fe388435 28 return (x + (y - 1)) / y;
bogdanm 15:4892fe388435 29 }
bogdanm 15:4892fe388435 30
bogdanm 15:4892fe388435 31 static const PinMap PinMap_ADC[] = {
bogdanm 15:4892fe388435 32 {P0_23, ADC0_0, 0x01},
bogdanm 15:4892fe388435 33 {P0_24, ADC0_1, 0x01},
bogdanm 15:4892fe388435 34 {P0_25, ADC0_2, 0x01},
bogdanm 15:4892fe388435 35 {P0_26, ADC0_3, 0x01},
bogdanm 15:4892fe388435 36 {P1_30, ADC0_4, 0x03},
bogdanm 15:4892fe388435 37 {P1_31, ADC0_5, 0x03},
bogdanm 15:4892fe388435 38 {P0_12, ADC0_6, 0x03},
bogdanm 15:4892fe388435 39 {P0_13, ADC0_7, 0x03},
bogdanm 15:4892fe388435 40 {NC , NC , 0 }
bogdanm 15:4892fe388435 41 };
bogdanm 15:4892fe388435 42
bogdanm 15:4892fe388435 43 #define ADC_RANGE ADC_12BIT_RANGE
bogdanm 15:4892fe388435 44
bogdanm 15:4892fe388435 45 void analogin_init(analogin_t *obj, PinName pin) {
bogdanm 15:4892fe388435 46 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 227:7bd0639b8911 47 MBED_ASSERT(obj->adc != (ADCName)NC);
bogdanm 15:4892fe388435 48
bogdanm 15:4892fe388435 49 // ensure power is turned on
bogdanm 15:4892fe388435 50 LPC_SC->PCONP |= (1 << 12);
bogdanm 15:4892fe388435 51
bogdanm 15:4892fe388435 52 uint32_t PCLK = PeripheralClock;
bogdanm 15:4892fe388435 53
bogdanm 15:4892fe388435 54 // calculate minimum clock divider
bogdanm 15:4892fe388435 55 // clkdiv = divider - 1
bogdanm 15:4892fe388435 56 uint32_t MAX_ADC_CLK = 12400000;
bogdanm 15:4892fe388435 57 uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1;
bogdanm 15:4892fe388435 58
bogdanm 15:4892fe388435 59 // Set the generic software-controlled ADC settings
bogdanm 15:4892fe388435 60 LPC_ADC->CR = (0 << 0) // SEL: 0 = no channels selected
bogdanm 15:4892fe388435 61 | (clkdiv << 8) // CLKDIV:
bogdanm 15:4892fe388435 62 | (0 << 16) // BURST: 0 = software control
bogdanm 15:4892fe388435 63 | (1 << 21) // PDN: 1 = operational
bogdanm 15:4892fe388435 64 | (0 << 24) // START: 0 = no start
bogdanm 15:4892fe388435 65 | (0 << 27); // EDGE: not applicable
bogdanm 15:4892fe388435 66
bogdanm 15:4892fe388435 67 // must enable analog mode (ADMODE = 0)
bogdanm 15:4892fe388435 68 __IO uint32_t *reg = (__IO uint32_t*) (LPC_IOCON_BASE + 4 * pin);
bogdanm 15:4892fe388435 69 *reg &= ~(1 << 7);
bogdanm 15:4892fe388435 70
bogdanm 15:4892fe388435 71 pinmap_pinout(pin, PinMap_ADC);
bogdanm 15:4892fe388435 72 }
bogdanm 15:4892fe388435 73
bogdanm 15:4892fe388435 74 static inline uint32_t adc_read(analogin_t *obj) {
bogdanm 15:4892fe388435 75 // Select the appropriate channel and start conversion
bogdanm 15:4892fe388435 76 LPC_ADC->CR &= ~0xFF;
bogdanm 15:4892fe388435 77 LPC_ADC->CR |= 1 << (int)obj->adc;
bogdanm 15:4892fe388435 78 LPC_ADC->CR |= 1 << 24;
bogdanm 15:4892fe388435 79
bogdanm 15:4892fe388435 80 // Repeatedly get the sample data until DONE bit
bogdanm 15:4892fe388435 81 unsigned int data;
bogdanm 15:4892fe388435 82 do {
bogdanm 15:4892fe388435 83 data = LPC_ADC->GDR;
bogdanm 15:4892fe388435 84 } while ((data & ((unsigned int)1 << 31)) == 0);
bogdanm 15:4892fe388435 85
bogdanm 15:4892fe388435 86 // Stop conversion
bogdanm 15:4892fe388435 87 LPC_ADC->CR &= ~(1 << 24);
bogdanm 15:4892fe388435 88
bogdanm 15:4892fe388435 89 return (data >> 4) & ADC_RANGE; // 12 bit
bogdanm 15:4892fe388435 90 }
bogdanm 15:4892fe388435 91
bogdanm 15:4892fe388435 92 static inline void order(uint32_t *a, uint32_t *b) {
bogdanm 15:4892fe388435 93 if (*a > *b) {
bogdanm 15:4892fe388435 94 uint32_t t = *a;
bogdanm 15:4892fe388435 95 *a = *b;
bogdanm 15:4892fe388435 96 *b = t;
bogdanm 15:4892fe388435 97 }
bogdanm 15:4892fe388435 98 }
bogdanm 15:4892fe388435 99
bogdanm 15:4892fe388435 100 static inline uint32_t adc_read_u32(analogin_t *obj) {
bogdanm 15:4892fe388435 101 uint32_t value;
bogdanm 15:4892fe388435 102 #if ANALOGIN_MEDIAN_FILTER
bogdanm 15:4892fe388435 103 uint32_t v1 = adc_read(obj);
bogdanm 15:4892fe388435 104 uint32_t v2 = adc_read(obj);
bogdanm 15:4892fe388435 105 uint32_t v3 = adc_read(obj);
bogdanm 15:4892fe388435 106 order(&v1, &v2);
bogdanm 15:4892fe388435 107 order(&v2, &v3);
bogdanm 15:4892fe388435 108 order(&v1, &v2);
bogdanm 15:4892fe388435 109 value = v2;
bogdanm 15:4892fe388435 110 #else
bogdanm 15:4892fe388435 111 value = adc_read(obj);
bogdanm 15:4892fe388435 112 #endif
bogdanm 15:4892fe388435 113 return value;
bogdanm 15:4892fe388435 114 }
bogdanm 15:4892fe388435 115
bogdanm 15:4892fe388435 116 uint16_t analogin_read_u16(analogin_t *obj) {
bogdanm 15:4892fe388435 117 uint32_t value = adc_read_u32(obj);
bogdanm 15:4892fe388435 118
bogdanm 15:4892fe388435 119 return (value << 4) | ((value >> 8) & 0x000F); // 12 bit
bogdanm 15:4892fe388435 120 }
bogdanm 15:4892fe388435 121
bogdanm 15:4892fe388435 122 float analogin_read(analogin_t *obj) {
bogdanm 15:4892fe388435 123 uint32_t value = adc_read_u32(obj);
bogdanm 15:4892fe388435 124 return (float)value * (1.0f / (float)ADC_RANGE);
bogdanm 15:4892fe388435 125 }