mbed SDK library sources
Fork of mbed-src by
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.
targets/hal/TARGET_Freescale/TARGET_K20D5M/analogin_api.c@68:41613245dfd7, 2013-12-26 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Dec 26 13:00:06 2013 +0000
- Revision:
- 68:41613245dfd7
- Child:
- 73:299c67215126
Synchronized with git revision fba199a9c4445231b0f38e1e113c118182635546
Full URL: https://github.com/mbedmicro/mbed/commit/fba199a9c4445231b0f38e1e113c118182635546/
target K20D5M
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 68:41613245dfd7 | 1 | /* mbed Microcontroller Library |
mbed_official | 68:41613245dfd7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 68:41613245dfd7 | 3 | * |
mbed_official | 68:41613245dfd7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 68:41613245dfd7 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 68:41613245dfd7 | 6 | * You may obtain a copy of the License at |
mbed_official | 68:41613245dfd7 | 7 | * |
mbed_official | 68:41613245dfd7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 68:41613245dfd7 | 9 | * |
mbed_official | 68:41613245dfd7 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 68:41613245dfd7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 68:41613245dfd7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 68:41613245dfd7 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 68:41613245dfd7 | 14 | * limitations under the License. |
mbed_official | 68:41613245dfd7 | 15 | */ |
mbed_official | 68:41613245dfd7 | 16 | #include "analogin_api.h" |
mbed_official | 68:41613245dfd7 | 17 | |
mbed_official | 68:41613245dfd7 | 18 | #include "cmsis.h" |
mbed_official | 68:41613245dfd7 | 19 | #include "pinmap.h" |
mbed_official | 68:41613245dfd7 | 20 | #include "error.h" |
mbed_official | 68:41613245dfd7 | 21 | |
mbed_official | 68:41613245dfd7 | 22 | static const PinMap PinMap_ADC[] = { |
mbed_official | 68:41613245dfd7 | 23 | {PTC2, ADC0_SE4b, 0}, |
mbed_official | 68:41613245dfd7 | 24 | {PTD1, ADC0_SE5b, 0}, |
mbed_official | 68:41613245dfd7 | 25 | {PTD5, ADC0_SE6b, 0}, |
mbed_official | 68:41613245dfd7 | 26 | {PTD6, ADC0_SE7b, 0}, |
mbed_official | 68:41613245dfd7 | 27 | {PTB0, ADC0_SE8, 0}, |
mbed_official | 68:41613245dfd7 | 28 | {PTB1, ADC0_SE9, 0}, |
mbed_official | 68:41613245dfd7 | 29 | {PTB2, ADC0_SE12, 0}, |
mbed_official | 68:41613245dfd7 | 30 | {PTB3, ADC0_SE13, 0}, |
mbed_official | 68:41613245dfd7 | 31 | {PTC0, ADC0_SE14, 0}, |
mbed_official | 68:41613245dfd7 | 32 | {PTC1, ADC0_SE15, 0}, |
mbed_official | 68:41613245dfd7 | 33 | {NC, NC, 0} |
mbed_official | 68:41613245dfd7 | 34 | }; |
mbed_official | 68:41613245dfd7 | 35 | |
mbed_official | 68:41613245dfd7 | 36 | void analogin_init(analogin_t *obj, PinName pin) { |
mbed_official | 68:41613245dfd7 | 37 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 68:41613245dfd7 | 38 | if (obj->adc == (ADCName)NC) |
mbed_official | 68:41613245dfd7 | 39 | error("ADC pin mapping failed"); |
mbed_official | 68:41613245dfd7 | 40 | |
mbed_official | 68:41613245dfd7 | 41 | SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; |
mbed_official | 68:41613245dfd7 | 42 | |
mbed_official | 68:41613245dfd7 | 43 | uint32_t port = (uint32_t)pin >> PORT_SHIFT; |
mbed_official | 68:41613245dfd7 | 44 | SIM->SCGC5 |= 1 << (SIM_SCGC5_PORTA_SHIFT + port); |
mbed_official | 68:41613245dfd7 | 45 | |
mbed_official | 68:41613245dfd7 | 46 | ADC0->SC1[1] = ADC_SC1_ADCH(obj->adc); |
mbed_official | 68:41613245dfd7 | 47 | |
mbed_official | 68:41613245dfd7 | 48 | ADC0->CFG1 = ADC_CFG1_ADLPC_MASK // Low-Power Configuration |
mbed_official | 68:41613245dfd7 | 49 | | ADC_CFG1_ADIV(3) // Clock Divide Select: (Input Clock)/8 |
mbed_official | 68:41613245dfd7 | 50 | | ADC_CFG1_ADLSMP_MASK // Long Sample Time |
mbed_official | 68:41613245dfd7 | 51 | | ADC_CFG1_MODE(3) // (16)bits Resolution |
mbed_official | 68:41613245dfd7 | 52 | | ADC_CFG1_ADICLK(1); // Input Clock: (Bus Clock)/2 |
mbed_official | 68:41613245dfd7 | 53 | |
mbed_official | 68:41613245dfd7 | 54 | ADC0->CFG2 = ADC_CFG2_MUXSEL_MASK // ADxxb or ADxxa channels |
mbed_official | 68:41613245dfd7 | 55 | | ADC_CFG2_ADACKEN_MASK // Asynchronous Clock Output Enable |
mbed_official | 68:41613245dfd7 | 56 | | ADC_CFG2_ADHSC_MASK // High-Speed Configuration |
mbed_official | 68:41613245dfd7 | 57 | | ADC_CFG2_ADLSTS(0); // Long Sample Time Select |
mbed_official | 68:41613245dfd7 | 58 | |
mbed_official | 68:41613245dfd7 | 59 | ADC0->SC2 = ADC_SC2_REFSEL(0); // Default Voltage Reference |
mbed_official | 68:41613245dfd7 | 60 | |
mbed_official | 68:41613245dfd7 | 61 | ADC0->SC3 = ADC_SC3_AVGE_MASK // Hardware Average Enable |
mbed_official | 68:41613245dfd7 | 62 | | ADC_SC3_AVGS(0); // 4 Samples Averaged |
mbed_official | 68:41613245dfd7 | 63 | |
mbed_official | 68:41613245dfd7 | 64 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 68:41613245dfd7 | 65 | } |
mbed_official | 68:41613245dfd7 | 66 | |
mbed_official | 68:41613245dfd7 | 67 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 68:41613245dfd7 | 68 | // start conversion |
mbed_official | 68:41613245dfd7 | 69 | ADC0->SC1[0] = ADC_SC1_ADCH(obj->adc); |
mbed_official | 68:41613245dfd7 | 70 | |
mbed_official | 68:41613245dfd7 | 71 | // Wait Conversion Complete |
mbed_official | 68:41613245dfd7 | 72 | while ((ADC0->SC1[0] & ADC_SC1_COCO_MASK) != ADC_SC1_COCO_MASK); |
mbed_official | 68:41613245dfd7 | 73 | |
mbed_official | 68:41613245dfd7 | 74 | return (uint16_t)ADC0->R[0]; |
mbed_official | 68:41613245dfd7 | 75 | } |
mbed_official | 68:41613245dfd7 | 76 | |
mbed_official | 68:41613245dfd7 | 77 | float analogin_read(analogin_t *obj) { |
mbed_official | 68:41613245dfd7 | 78 | uint16_t value = analogin_read_u16(obj); |
mbed_official | 68:41613245dfd7 | 79 | return (float)value * (1.0f / (float)0xFFFF); |
mbed_official | 68:41613245dfd7 | 80 | } |
mbed_official | 68:41613245dfd7 | 81 |