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