Erik - / mbed-src

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Apr 02 21:00:08 2014 +0100
Revision:
146:f64d43ff0c18
Synchronized with git revision d537c51d26da35e031d537f7fc90380fc74cb207

Full URL: https://github.com/mbedmicro/mbed/commit/d537c51d26da35e031d537f7fc90380fc74cb207/

target K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 146:f64d43ff0c18 1 /* mbed Microcontroller Library
mbed_official 146:f64d43ff0c18 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 146:f64d43ff0c18 3 *
mbed_official 146:f64d43ff0c18 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 146:f64d43ff0c18 5 * you may not use this file except in compliance with the License.
mbed_official 146:f64d43ff0c18 6 * You may obtain a copy of the License at
mbed_official 146:f64d43ff0c18 7 *
mbed_official 146:f64d43ff0c18 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 146:f64d43ff0c18 9 *
mbed_official 146:f64d43ff0c18 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 146:f64d43ff0c18 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 146:f64d43ff0c18 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 146:f64d43ff0c18 13 * See the License for the specific language governing permissions and
mbed_official 146:f64d43ff0c18 14 * limitations under the License.
mbed_official 146:f64d43ff0c18 15 */
mbed_official 146:f64d43ff0c18 16 #include "analogin_api.h"
mbed_official 146:f64d43ff0c18 17
mbed_official 146:f64d43ff0c18 18 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 19 #include "pinmap.h"
mbed_official 146:f64d43ff0c18 20 #include "error.h"
mbed_official 146:f64d43ff0c18 21 #include "PeripheralNames.h"
mbed_official 146:f64d43ff0c18 22 #include "fsl_adc_hal.h"
mbed_official 146:f64d43ff0c18 23 #include "fsl_clock_manager.h"
mbed_official 146:f64d43ff0c18 24
mbed_official 146:f64d43ff0c18 25 #define MAX_FADC 6000000
mbed_official 146:f64d43ff0c18 26
mbed_official 146:f64d43ff0c18 27 static const PinMap PinMap_ADC[] = {
mbed_official 146:f64d43ff0c18 28 {PTC2, ADC0_SE4b, 0},
mbed_official 146:f64d43ff0c18 29 {PTC8, ADC1_SE4b, 0},
mbed_official 146:f64d43ff0c18 30 {PTC9, ADC0_SE5b, 0},
mbed_official 146:f64d43ff0c18 31 {PTD1, ADC0_SE5b, 0},
mbed_official 146:f64d43ff0c18 32 {PTC10, ADC0_SE6b, 0},
mbed_official 146:f64d43ff0c18 33 {PTD5, ADC0_SE6b, 0},
mbed_official 146:f64d43ff0c18 34 {PTC11, ADC0_SE7b, 0},
mbed_official 146:f64d43ff0c18 35 {PTD7, ADC0_SE7b, 0},
mbed_official 146:f64d43ff0c18 36 {PTB0 , ADC0_SE8 , 0},
mbed_official 146:f64d43ff0c18 37 {PTB1 , ADC0_SE9 , 0},
mbed_official 146:f64d43ff0c18 38 {PTB2 , ADC0_SE12, 0},
mbed_official 146:f64d43ff0c18 39 {PTB3 , ADC0_SE13, 0},
mbed_official 146:f64d43ff0c18 40 {PTC0 , ADC0_SE14, 0},
mbed_official 146:f64d43ff0c18 41 {PTB10, ADC1_SE14, 0},
mbed_official 146:f64d43ff0c18 42 {PTB11, ADC1_SE15, 0},
mbed_official 146:f64d43ff0c18 43 {PTC1 , ADC0_SE15, 0},
mbed_official 146:f64d43ff0c18 44 {PTE24, ADC0_SE17, 0},
mbed_official 146:f64d43ff0c18 45 {PTA17, ADC1_SE17, 0},
mbed_official 146:f64d43ff0c18 46 {PTE25, ADC0_SE18, 0},
mbed_official 146:f64d43ff0c18 47 {NC , NC , 0}
mbed_official 146:f64d43ff0c18 48 };
mbed_official 146:f64d43ff0c18 49
mbed_official 146:f64d43ff0c18 50 void analogin_init(analogin_t *obj, PinName pin) {
mbed_official 146:f64d43ff0c18 51 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 146:f64d43ff0c18 52 if (obj->adc == (ADCName)NC) {
mbed_official 146:f64d43ff0c18 53 error("ADC pin mapping failed");
mbed_official 146:f64d43ff0c18 54 }
mbed_official 146:f64d43ff0c18 55 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 146:f64d43ff0c18 56
mbed_official 146:f64d43ff0c18 57 clock_manager_set_gate(kClockModuleADC, instance, true);
mbed_official 146:f64d43ff0c18 58
mbed_official 146:f64d43ff0c18 59 uint32_t bus_clock;
mbed_official 146:f64d43ff0c18 60 clock_manager_get_frequency(kBusClock, &bus_clock);
mbed_official 146:f64d43ff0c18 61 uint32_t clkdiv;
mbed_official 146:f64d43ff0c18 62 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
mbed_official 146:f64d43ff0c18 63 if ((bus_clock >> clkdiv) <= MAX_FADC)
mbed_official 146:f64d43ff0c18 64 break;
mbed_official 146:f64d43ff0c18 65 }
mbed_official 146:f64d43ff0c18 66 if (clkdiv == 4) {//Set max div
mbed_official 146:f64d43ff0c18 67 clkdiv = 0x7;
mbed_official 146:f64d43ff0c18 68 }
mbed_official 146:f64d43ff0c18 69
mbed_official 146:f64d43ff0c18 70 adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2));
mbed_official 146:f64d43ff0c18 71 adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3));
mbed_official 146:f64d43ff0c18 72 adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref);
mbed_official 146:f64d43ff0c18 73 adc_hal_set_resolution_mode(instance, kAdcSingleDiff16);
mbed_official 146:f64d43ff0c18 74 adc_hal_configure_continuous_conversion(instance, false);
mbed_official 146:f64d43ff0c18 75
mbed_official 146:f64d43ff0c18 76 adc_group_mux_mode_t mode = (obj->adc & (1 << ADC_B_CHANNEL_SHIFT)) ?
mbed_official 146:f64d43ff0c18 77 kAdcChannelMuxB : kAdcChannelMuxA;
mbed_official 146:f64d43ff0c18 78 adc_hal_disable(instance, mode);
mbed_official 146:f64d43ff0c18 79 adc_hal_set_group_mux(instance, mode);
mbed_official 146:f64d43ff0c18 80 adc_hal_enable(instance, mode, (adc_channel_mode_t)(obj->adc & 0xF), false);
mbed_official 146:f64d43ff0c18 81 }
mbed_official 146:f64d43ff0c18 82
mbed_official 146:f64d43ff0c18 83 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 84 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 146:f64d43ff0c18 85 adc_group_mux_mode_t mode = (obj->adc & (1 << ADC_B_CHANNEL_SHIFT)) ?
mbed_official 146:f64d43ff0c18 86 kAdcChannelMuxB : kAdcChannelMuxA;
mbed_official 146:f64d43ff0c18 87 adc_hal_enable(instance, mode, (adc_channel_mode_t)(obj->adc & 0xF), false);
mbed_official 146:f64d43ff0c18 88 while (!adc_hal_is_conversion_completed(instance, mode));
mbed_official 146:f64d43ff0c18 89 return adc_hal_get_conversion_value(instance, mode);
mbed_official 146:f64d43ff0c18 90 }
mbed_official 146:f64d43ff0c18 91
mbed_official 146:f64d43ff0c18 92 float analogin_read(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 93 uint16_t value = analogin_read_u16(obj);
mbed_official 146:f64d43ff0c18 94 return (float)value * (1.0f / (float)0xFFFF);
mbed_official 146:f64d43ff0c18 95 }
mbed_official 146:f64d43ff0c18 96