mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jun 11 16:00:09 2014 +0100
Revision:
227:7bd0639b8911
Parent:
151:7a600087bf72
Child:
265:9632ea190e16
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af

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

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 146:f64d43ff0c18 19 #include "cmsis.h"
mbed_official 146:f64d43ff0c18 20 #include "pinmap.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 151:7a600087bf72 30 {PTC9, ADC1_SE5b, 0},
mbed_official 146:f64d43ff0c18 31 {PTD1, ADC0_SE5b, 0},
mbed_official 151:7a600087bf72 32 {PTC10, ADC1_SE6b, 0},
mbed_official 146:f64d43ff0c18 33 {PTD5, ADC0_SE6b, 0},
mbed_official 151:7a600087bf72 34 {PTC11, ADC1_SE7b, 0},
mbed_official 151:7a600087bf72 35 {PTD6, 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 {PTA17, ADC1_SE17, 0},
mbed_official 151:7a600087bf72 45 //{PTE24, ADC0_SE17, 0}, //I2C pull up
mbed_official 151:7a600087bf72 46 //{PTE25, ADC0_SE18, 0}, //I2C pull up
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 227:7bd0639b8911 52 MBED_ASSERT(obj->adc != (ADCName)NC);
mbed_official 227:7bd0639b8911 53
mbed_official 146:f64d43ff0c18 54 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 146:f64d43ff0c18 55
mbed_official 146:f64d43ff0c18 56 clock_manager_set_gate(kClockModuleADC, instance, true);
mbed_official 146:f64d43ff0c18 57
mbed_official 146:f64d43ff0c18 58 uint32_t bus_clock;
mbed_official 146:f64d43ff0c18 59 clock_manager_get_frequency(kBusClock, &bus_clock);
mbed_official 146:f64d43ff0c18 60 uint32_t clkdiv;
mbed_official 146:f64d43ff0c18 61 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
mbed_official 146:f64d43ff0c18 62 if ((bus_clock >> clkdiv) <= MAX_FADC)
mbed_official 146:f64d43ff0c18 63 break;
mbed_official 146:f64d43ff0c18 64 }
mbed_official 151:7a600087bf72 65 if (clkdiv == 4) {
mbed_official 151:7a600087bf72 66 clkdiv = 0x7; //Set max div
mbed_official 146:f64d43ff0c18 67 }
mbed_official 151:7a600087bf72 68 /* adc is enabled/triggered when reading. */
mbed_official 146:f64d43ff0c18 69 adc_hal_set_clock_source_mode(instance, (adc_clock_source_mode_t)(clkdiv >> 2));
mbed_official 146:f64d43ff0c18 70 adc_hal_set_clock_divider_mode(instance, (adc_clock_divider_mode_t)(clkdiv & 0x3));
mbed_official 146:f64d43ff0c18 71 adc_hal_set_reference_voltage_mode(instance, kAdcVoltageVref);
mbed_official 146:f64d43ff0c18 72 adc_hal_set_resolution_mode(instance, kAdcSingleDiff16);
mbed_official 146:f64d43ff0c18 73 adc_hal_configure_continuous_conversion(instance, false);
mbed_official 151:7a600087bf72 74 adc_hal_configure_hw_trigger(instance, false); /* sw trigger */
mbed_official 151:7a600087bf72 75 adc_hal_configure_hw_average(instance, true);
mbed_official 151:7a600087bf72 76 adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4);
mbed_official 151:7a600087bf72 77 adc_hal_set_group_mux(instance, kAdcChannelMuxB); /* only B channels are avail */
mbed_official 146:f64d43ff0c18 78
mbed_official 151:7a600087bf72 79 pinmap_pinout(pin, PinMap_ADC);
mbed_official 146:f64d43ff0c18 80 }
mbed_official 146:f64d43ff0c18 81
mbed_official 146:f64d43ff0c18 82 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 83 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 151:7a600087bf72 84 /* sw trigger (SC1A) */
mbed_official 151:7a600087bf72 85 adc_hal_enable(instance, 0, (adc_channel_mode_t)(obj->adc & 0xF), false);
mbed_official 151:7a600087bf72 86 while (!adc_hal_is_conversion_completed(instance, 0));
mbed_official 151:7a600087bf72 87 return adc_hal_get_conversion_value(instance, 0);
mbed_official 146:f64d43ff0c18 88 }
mbed_official 146:f64d43ff0c18 89
mbed_official 146:f64d43ff0c18 90 float analogin_read(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 91 uint16_t value = analogin_read_u16(obj);
mbed_official 146:f64d43ff0c18 92 return (float)value * (1.0f / (float)0xFFFF);
mbed_official 146:f64d43ff0c18 93 }
mbed_official 146:f64d43ff0c18 94