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:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
324:406fd2029f23
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

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

ignore target files not supported by the yotta module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 324:406fd2029f23 1 /* mbed Microcontroller Library
mbed_official 324:406fd2029f23 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 324:406fd2029f23 3 *
mbed_official 324:406fd2029f23 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 324:406fd2029f23 5 * you may not use this file except in compliance with the License.
mbed_official 324:406fd2029f23 6 * You may obtain a copy of the License at
mbed_official 324:406fd2029f23 7 *
mbed_official 324:406fd2029f23 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 324:406fd2029f23 9 *
mbed_official 324:406fd2029f23 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 324:406fd2029f23 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 324:406fd2029f23 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 324:406fd2029f23 13 * See the License for the specific language governing permissions and
mbed_official 324:406fd2029f23 14 * limitations under the License.
mbed_official 324:406fd2029f23 15 */
mbed_official 324:406fd2029f23 16 #include "mbed_assert.h"
mbed_official 324:406fd2029f23 17 #include "analogin_api.h"
mbed_official 324:406fd2029f23 18
mbed_official 324:406fd2029f23 19 #if DEVICE_ANALOGIN
mbed_official 324:406fd2029f23 20
mbed_official 324:406fd2029f23 21 #include "cmsis.h"
mbed_official 324:406fd2029f23 22 #include "pinmap.h"
mbed_official 324:406fd2029f23 23 #include "PeripheralNames.h"
mbed_official 324:406fd2029f23 24 #include "fsl_adc_hal.h"
mbed_official 324:406fd2029f23 25 #include "fsl_clock_manager.h"
mbed_official 324:406fd2029f23 26 #include "PeripheralPins.h"
mbed_official 324:406fd2029f23 27 #include "fsl_device_registers.h"
mbed_official 324:406fd2029f23 28
mbed_official 324:406fd2029f23 29 #define MAX_FADC 6000000
mbed_official 324:406fd2029f23 30
mbed_official 324:406fd2029f23 31 void analogin_init(analogin_t *obj, PinName pin) {
mbed_official 324:406fd2029f23 32 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 324:406fd2029f23 33 MBED_ASSERT(obj->adc != (ADCName)NC);
mbed_official 324:406fd2029f23 34
mbed_official 324:406fd2029f23 35 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 324:406fd2029f23 36 uint32_t adc_addrs[] = ADC_BASE_ADDRS;
mbed_official 324:406fd2029f23 37
mbed_official 324:406fd2029f23 38 CLOCK_SYS_EnableAdcClock(instance);
mbed_official 324:406fd2029f23 39
mbed_official 324:406fd2029f23 40 uint32_t bus_clock;
mbed_official 324:406fd2029f23 41 CLOCK_SYS_GetFreq(kBusClock, &bus_clock);
mbed_official 324:406fd2029f23 42 uint32_t clkdiv;
mbed_official 324:406fd2029f23 43 for (clkdiv = 0; clkdiv < 4; clkdiv++) {
mbed_official 324:406fd2029f23 44 if ((bus_clock >> clkdiv) <= MAX_FADC)
mbed_official 324:406fd2029f23 45 break;
mbed_official 324:406fd2029f23 46 }
mbed_official 324:406fd2029f23 47 if (clkdiv == 4) {
mbed_official 324:406fd2029f23 48 clkdiv = 0x3; //Set max div
mbed_official 324:406fd2029f23 49 }
mbed_official 324:406fd2029f23 50 /* adc is enabled/triggered when reading. */
mbed_official 324:406fd2029f23 51 ADC_HAL_Init(adc_addrs[instance]);
mbed_official 324:406fd2029f23 52 ADC_HAL_SetClkSrcMode(adc_addrs[instance], kAdcClkSrcOfBusClk);
mbed_official 324:406fd2029f23 53 ADC_HAL_SetClkDividerMode(adc_addrs[instance], (adc_clk_divider_mode_t)(clkdiv & 0x3));
mbed_official 324:406fd2029f23 54 ADC_HAL_SetRefVoltSrcMode(adc_addrs[instance], kAdcRefVoltSrcOfVref);
mbed_official 324:406fd2029f23 55 ADC_HAL_SetResolutionMode(adc_addrs[instance], kAdcResolutionBitOfSingleEndAs16);
mbed_official 324:406fd2029f23 56 ADC_HAL_SetContinuousConvCmd(adc_addrs[instance], false);
mbed_official 324:406fd2029f23 57 ADC_HAL_SetHwTriggerCmd(adc_addrs[instance], false); /* sw trigger */
mbed_official 324:406fd2029f23 58 ADC_HAL_SetHwAverageCmd(adc_addrs[instance], true);
mbed_official 324:406fd2029f23 59 ADC_HAL_SetHwAverageMode(adc_addrs[instance], kAdcHwAverageCountOf4);
mbed_official 324:406fd2029f23 60 ADC_HAL_SetChnMuxMode(adc_addrs[instance], kAdcChnMuxOfB); /* only B channels are avail */
mbed_official 324:406fd2029f23 61
mbed_official 324:406fd2029f23 62 pinmap_pinout(pin, PinMap_ADC);
mbed_official 324:406fd2029f23 63 }
mbed_official 324:406fd2029f23 64
mbed_official 324:406fd2029f23 65 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 324:406fd2029f23 66 uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
mbed_official 324:406fd2029f23 67 uint32_t adc_addrs[] = ADC_BASE_ADDRS;
mbed_official 324:406fd2029f23 68 /* sw trigger (SC1A) */
mbed_official 324:406fd2029f23 69 ADC_HAL_ConfigChn(adc_addrs[instance], 0, false, false, obj->adc & 0xF);
mbed_official 324:406fd2029f23 70 while (!ADC_HAL_GetChnConvCompletedCmd(adc_addrs[instance], 0));
mbed_official 324:406fd2029f23 71 return ADC_HAL_GetChnConvValueRAW(adc_addrs[instance], 0);
mbed_official 324:406fd2029f23 72 }
mbed_official 324:406fd2029f23 73
mbed_official 324:406fd2029f23 74 float analogin_read(analogin_t *obj) {
mbed_official 324:406fd2029f23 75 uint16_t value = analogin_read_u16(obj);
mbed_official 324:406fd2029f23 76 return (float)value * (1.0f / (float)0xFFFF);
mbed_official 324:406fd2029f23 77 }
mbed_official 324:406fd2029f23 78
mbed_official 324:406fd2029f23 79 #endif