mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

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.

Committer:
mbed_official
Date:
Sat Apr 05 17:45:06 2014 +0100
Revision:
151:7a600087bf72
Parent:
149:1fb5f62b92bd
Child:
227:7bd0639b8911
Synchronized with git revision 6a7b119b46ef99976a5e26f3ff0f8e8af916a24a

Full URL: https://github.com/mbedmicro/mbed/commit/6a7b119b46ef99976a5e26f3ff0f8e8af916a24a/

K64F - ADC mbed HAL - channels definition correction

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 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 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 151:7a600087bf72 66 if (clkdiv == 4) {
mbed_official 151:7a600087bf72 67 clkdiv = 0x7; //Set max div
mbed_official 146:f64d43ff0c18 68 }
mbed_official 151:7a600087bf72 69 /* adc is enabled/triggered when reading. */
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 151:7a600087bf72 75 adc_hal_configure_hw_trigger(instance, false); /* sw trigger */
mbed_official 151:7a600087bf72 76 adc_hal_configure_hw_average(instance, true);
mbed_official 151:7a600087bf72 77 adc_hal_set_hw_average_mode(instance, kAdcHwAverageCount4);
mbed_official 151:7a600087bf72 78 adc_hal_set_group_mux(instance, kAdcChannelMuxB); /* only B channels are avail */
mbed_official 146:f64d43ff0c18 79
mbed_official 151:7a600087bf72 80 pinmap_pinout(pin, PinMap_ADC);
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 151:7a600087bf72 85 /* sw trigger (SC1A) */
mbed_official 151:7a600087bf72 86 adc_hal_enable(instance, 0, (adc_channel_mode_t)(obj->adc & 0xF), false);
mbed_official 151:7a600087bf72 87 while (!adc_hal_is_conversion_completed(instance, 0));
mbed_official 151:7a600087bf72 88 return adc_hal_get_conversion_value(instance, 0);
mbed_official 146:f64d43ff0c18 89 }
mbed_official 146:f64d43ff0c18 90
mbed_official 146:f64d43ff0c18 91 float analogin_read(analogin_t *obj) {
mbed_official 146:f64d43ff0c18 92 uint16_t value = analogin_read_u16(obj);
mbed_official 146:f64d43ff0c18 93 return (float)value * (1.0f / (float)0xFFFF);
mbed_official 146:f64d43ff0c18 94 }
mbed_official 146:f64d43ff0c18 95