mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
targets/TARGET_NXP/TARGET_LPC11UXX/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_LPC11UXX/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 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 19 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 20 | #include "mbed_error.h" |
<> | 144:ef7eb2e8f9f7 | 21 | #include "PeripheralPins.h" // For the Peripheral to Pin Definitions found in the individual Target's Platform |
<> | 144:ef7eb2e8f9f7 | 22 | |
<> | 144:ef7eb2e8f9f7 | 23 | #define ANALOGIN_MEDIAN_FILTER 1 |
<> | 144:ef7eb2e8f9f7 | 24 | |
<> | 144:ef7eb2e8f9f7 | 25 | #define ADC_10BIT_RANGE 0x3FF |
<> | 144:ef7eb2e8f9f7 | 26 | #define ADC_12BIT_RANGE 0xFFF |
<> | 144:ef7eb2e8f9f7 | 27 | |
<> | 144:ef7eb2e8f9f7 | 28 | static inline int div_round_up(int x, int y) { |
<> | 144:ef7eb2e8f9f7 | 29 | return (x + (y - 1)) / y; |
<> | 144:ef7eb2e8f9f7 | 30 | } |
<> | 144:ef7eb2e8f9f7 | 31 | |
<> | 144:ef7eb2e8f9f7 | 32 | #define LPC_IOCON0_BASE (LPC_IOCON_BASE) |
<> | 144:ef7eb2e8f9f7 | 33 | #define LPC_IOCON1_BASE (LPC_IOCON_BASE + 0x60) |
<> | 144:ef7eb2e8f9f7 | 34 | |
<> | 144:ef7eb2e8f9f7 | 35 | #define ADC_RANGE ADC_10BIT_RANGE |
<> | 144:ef7eb2e8f9f7 | 36 | |
<> | 144:ef7eb2e8f9f7 | 37 | void analogin_init(analogin_t *obj, PinName pin) { |
<> | 144:ef7eb2e8f9f7 | 38 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 39 | MBED_ASSERT(obj->adc != (ADCName)NC); |
<> | 144:ef7eb2e8f9f7 | 40 | |
<> | 144:ef7eb2e8f9f7 | 41 | // Power up ADC |
<> | 144:ef7eb2e8f9f7 | 42 | LPC_SYSCON->PDRUNCFG &= ~ (1 << 4); |
<> | 144:ef7eb2e8f9f7 | 43 | LPC_SYSCON->SYSAHBCLKCTRL |= ((uint32_t)1 << 13); |
<> | 144:ef7eb2e8f9f7 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | uint32_t pin_number = (uint32_t)pin; |
<> | 144:ef7eb2e8f9f7 | 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)); |
<> | 144:ef7eb2e8f9f7 | 47 | |
<> | 144:ef7eb2e8f9f7 | 48 | // set pin to ADC mode |
<> | 144:ef7eb2e8f9f7 | 49 | *reg &= ~(1 << 7); // set ADMODE = 0 (analog mode) |
<> | 144:ef7eb2e8f9f7 | 50 | |
<> | 144:ef7eb2e8f9f7 | 51 | uint32_t PCLK = SystemCoreClock; |
<> | 144:ef7eb2e8f9f7 | 52 | uint32_t MAX_ADC_CLK = 4500000; |
<> | 144:ef7eb2e8f9f7 | 53 | uint32_t clkdiv = div_round_up(PCLK, MAX_ADC_CLK) - 1; |
<> | 144:ef7eb2e8f9f7 | 54 | |
<> | 144:ef7eb2e8f9f7 | 55 | LPC_ADC->CR = (0 << 0) // no channels selected |
<> | 144:ef7eb2e8f9f7 | 56 | | (clkdiv << 8) // max of 4.5MHz |
<> | 144:ef7eb2e8f9f7 | 57 | | (0 << 16) // BURST = 0, software controlled |
<> | 144:ef7eb2e8f9f7 | 58 | | ( 0 << 17 ); // CLKS = 0, not applicable |
<> | 144:ef7eb2e8f9f7 | 59 | |
<> | 144:ef7eb2e8f9f7 | 60 | pinmap_pinout(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 61 | } |
<> | 144:ef7eb2e8f9f7 | 62 | |
<> | 144:ef7eb2e8f9f7 | 63 | static inline uint32_t adc_read(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 64 | // Select the appropriate channel and start conversion |
<> | 144:ef7eb2e8f9f7 | 65 | LPC_ADC->CR &= ~0xFF; |
<> | 144:ef7eb2e8f9f7 | 66 | LPC_ADC->CR |= 1 << (int)obj->adc; |
<> | 144:ef7eb2e8f9f7 | 67 | LPC_ADC->CR |= 1 << 24; |
<> | 144:ef7eb2e8f9f7 | 68 | |
<> | 144:ef7eb2e8f9f7 | 69 | // Repeatedly get the sample data until DONE bit |
<> | 144:ef7eb2e8f9f7 | 70 | unsigned int data; |
<> | 144:ef7eb2e8f9f7 | 71 | do { |
<> | 144:ef7eb2e8f9f7 | 72 | data = LPC_ADC->GDR; |
<> | 144:ef7eb2e8f9f7 | 73 | } while ((data & ((unsigned int)1 << 31)) == 0); |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | // Stop conversion |
<> | 144:ef7eb2e8f9f7 | 76 | LPC_ADC->CR &= ~(1 << 24); |
<> | 144:ef7eb2e8f9f7 | 77 | |
<> | 144:ef7eb2e8f9f7 | 78 | return (data >> 6) & ADC_RANGE; // 10 bit |
<> | 144:ef7eb2e8f9f7 | 79 | } |
<> | 144:ef7eb2e8f9f7 | 80 | |
<> | 144:ef7eb2e8f9f7 | 81 | static inline void order(uint32_t *a, uint32_t *b) { |
<> | 144:ef7eb2e8f9f7 | 82 | if (*a > *b) { |
<> | 144:ef7eb2e8f9f7 | 83 | uint32_t t = *a; |
<> | 144:ef7eb2e8f9f7 | 84 | *a = *b; |
<> | 144:ef7eb2e8f9f7 | 85 | *b = t; |
<> | 144:ef7eb2e8f9f7 | 86 | } |
<> | 144:ef7eb2e8f9f7 | 87 | } |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | static inline uint32_t adc_read_u32(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 90 | uint32_t value; |
<> | 144:ef7eb2e8f9f7 | 91 | #if ANALOGIN_MEDIAN_FILTER |
<> | 144:ef7eb2e8f9f7 | 92 | uint32_t v1 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 93 | uint32_t v2 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 94 | uint32_t v3 = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 95 | order(&v1, &v2); |
<> | 144:ef7eb2e8f9f7 | 96 | order(&v2, &v3); |
<> | 144:ef7eb2e8f9f7 | 97 | order(&v1, &v2); |
<> | 144:ef7eb2e8f9f7 | 98 | value = v2; |
<> | 144:ef7eb2e8f9f7 | 99 | #else |
<> | 144:ef7eb2e8f9f7 | 100 | value = adc_read(obj); |
<> | 144:ef7eb2e8f9f7 | 101 | #endif |
<> | 144:ef7eb2e8f9f7 | 102 | return value; |
<> | 144:ef7eb2e8f9f7 | 103 | } |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 144:ef7eb2e8f9f7 | 105 | uint16_t analogin_read_u16(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 106 | uint32_t value = adc_read_u32(obj); |
<> | 144:ef7eb2e8f9f7 | 107 | |
<> | 144:ef7eb2e8f9f7 | 108 | return (value << 6) | ((value >> 4) & 0x003F); // 10 bit |
<> | 144:ef7eb2e8f9f7 | 109 | } |
<> | 144:ef7eb2e8f9f7 | 110 | |
<> | 144:ef7eb2e8f9f7 | 111 | float analogin_read(analogin_t *obj) { |
<> | 144:ef7eb2e8f9f7 | 112 | uint32_t value = adc_read_u32(obj); |
<> | 144:ef7eb2e8f9f7 | 113 | return (float)value * (1.0f / (float)ADC_RANGE); |
<> | 144:ef7eb2e8f9f7 | 114 | } |