mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
320:be04b2b1e3f2
test with CLOCK_SETUP = 0

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 227:7bd0639b8911 16 #include "mbed_assert.h"
mbed_official 146:f64d43ff0c18 17 #include "analogin_api.h"
mbed_official 146:f64d43ff0c18 18
mbed_official 267:8673334f2cbe 19 #if DEVICE_ANALOGIN
mbed_official 267:8673334f2cbe 20
mbed_official 146:f64d43ff0c18 21 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 22 #include "pinmap.h"
mbed_official 146:f64d43ff0c18 23 #include "PeripheralNames.h"
mbed_official 146:f64d43ff0c18 24 #include "fsl_adc_hal.h"
mbed_official 146:f64d43ff0c18 25 #include "fsl_clock_manager.h"
mbed_official 265:9632ea190e16 26 #include "PeripheralPins.h"
mbed_official 146:f64d43ff0c18 27
mbed_official 146:f64d43ff0c18 28 #define MAX_FADC 6000000
mbed_official 146:f64d43ff0c18 29
mbed_official 146:f64d43ff0c18 30 void analogin_init(analogin_t *obj, PinName pin) {
mbed_official 146:f64d43ff0c18 31 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 227:7bd0639b8911 32 MBED_ASSERT(obj->adc != (ADCName)NC);
mbed_official 227:7bd0639b8911 33
mbed_official 146:f64d43ff0c18 34 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 146:f64d43ff0c18 35
mbed_official 146:f64d43ff0c18 36 clock_manager_set_gate(kClockModuleADC, instance, true);
mbed_official 146:f64d43ff0c18 37
mbed_official 146:f64d43ff0c18 38 uint32_t bus_clock;
mbed_official 146:f64d43ff0c18 39 clock_manager_get_frequency(kBusClock, &bus_clock);
mbed_official 146:f64d43ff0c18 40 uint32_t clkdiv;
mbed_official 146:f64d43ff0c18 41 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
mbed_official 146:f64d43ff0c18 42 if ((bus_clock >> clkdiv) <= MAX_FADC)
mbed_official 146:f64d43ff0c18 43 break;
mbed_official 146:f64d43ff0c18 44 }
mbed_official 151:7a600087bf72 45 if (clkdiv == 4) {
mbed_official 151:7a600087bf72 46 clkdiv = 0x7; //Set max div
mbed_official 146:f64d43ff0c18 47 }
mbed_official 151:7a600087bf72 48 /* adc is enabled/triggered when reading. */
mbed_official 146:f64d43ff0c18 49 adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2));
mbed_official 146:f64d43ff0c18 50 adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3));
mbed_official 146:f64d43ff0c18 51 adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref);
mbed_official 146:f64d43ff0c18 52 adc_hal_set_resolution_mode(instance, kAdcSingleDiff16);
mbed_official 146:f64d43ff0c18 53 adc_hal_configure_continuous_conversion(instance, false);
mbed_official 151:7a600087bf72 54 adc_hal_configure_hw_trigger(instance, false); /* sw trigger */
mbed_official 151:7a600087bf72 55 adc_hal_configure_hw_average(instance, true);
mbed_official 151:7a600087bf72 56 adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4);
mbed_official 151:7a600087bf72 57 adc_hal_set_group_mux(instance, kAdcChannelMuxB); /* only B channels are avail */
mbed_official 146:f64d43ff0c18 58
mbed_official 151:7a600087bf72 59 pinmap_pinout(pin, PinMap_ADC);
mbed_official 146:f64d43ff0c18 60 }
mbed_official 146:f64d43ff0c18 61
mbed_official 146:f64d43ff0c18 62 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 63 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 151:7a600087bf72 64 /* sw trigger (SC1A) */
mbed_official 151:7a600087bf72 65 adc_hal_enable(instance, 0, (adc_channel_mode_t)(obj->adc & 0xF), false);
mbed_official 151:7a600087bf72 66 while (!adc_hal_is_conversion_completed(instance, 0));
mbed_official 151:7a600087bf72 67 return adc_hal_get_conversion_value(instance, 0);
mbed_official 146:f64d43ff0c18 68 }
mbed_official 146:f64d43ff0c18 69
mbed_official 146:f64d43ff0c18 70 float analogin_read(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 71 uint16_t value = analogin_read_u16(obj);
mbed_official 146:f64d43ff0c18 72 return (float)value * (1.0f / (float)0xFFFF);
mbed_official 146:f64d43ff0c18 73 }
mbed_official 146:f64d43ff0c18 74
mbed_official 267:8673334f2cbe 75 #endif