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 May 22 10:45:46 2015 +0100
Revision:
548:1abac31e188e
Parent:
543:9dba91c44009
Child:
627:4fa1328d9c60
Synchronized with git revision 88d158e43b54f97c5e94da305ea9a096889cc81b

Full URL: https://github.com/mbedmicro/mbed/commit/88d158e43b54f97c5e94da305ea9a096889cc81b/

Silicon Labs - Cosmetic: apply mbed coding style to HAL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 525:c320967f86b9 1 /* mbed Microcontroller Library
mbed_official 525:c320967f86b9 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 525:c320967f86b9 3 *
mbed_official 525:c320967f86b9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 525:c320967f86b9 5 * you may not use this file except in compliance with the License.
mbed_official 525:c320967f86b9 6 * You may obtain a copy of the License at
mbed_official 525:c320967f86b9 7 *
mbed_official 525:c320967f86b9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 525:c320967f86b9 9 *
mbed_official 525:c320967f86b9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 525:c320967f86b9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 525:c320967f86b9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 525:c320967f86b9 13 * See the License for the specific language governing permissions and
mbed_official 525:c320967f86b9 14 * limitations under the License.
mbed_official 525:c320967f86b9 15 */
mbed_official 525:c320967f86b9 16
mbed_official 525:c320967f86b9 17 #include "device.h"
mbed_official 525:c320967f86b9 18 #if DEVICE_ANALOGIN
mbed_official 525:c320967f86b9 19
mbed_official 525:c320967f86b9 20 #include "mbed_assert.h"
mbed_official 525:c320967f86b9 21 #include "analogin_api.h"
mbed_official 525:c320967f86b9 22
mbed_official 525:c320967f86b9 23 #include "pinmap.h"
mbed_official 525:c320967f86b9 24 #include "pinmap_function.h"
mbed_official 525:c320967f86b9 25 #include "PeripheralPins.h"
mbed_official 525:c320967f86b9 26
mbed_official 525:c320967f86b9 27 #include "em_adc.h"
mbed_official 525:c320967f86b9 28 #include "em_cmu.h"
mbed_official 525:c320967f86b9 29
mbed_official 525:c320967f86b9 30 uint8_t analogin_get_index(analogin_t *obj)
mbed_official 525:c320967f86b9 31 {
mbed_official 525:c320967f86b9 32 return 0; //only one module availalbe
mbed_official 525:c320967f86b9 33 }
mbed_official 525:c320967f86b9 34
mbed_official 525:c320967f86b9 35 void analogin_preinit(analogin_t *obj, PinName pin)
mbed_official 525:c320967f86b9 36 {
mbed_official 525:c320967f86b9 37 obj->adc = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
mbed_official 525:c320967f86b9 38 MBED_ASSERT((int) obj->adc != NC);
mbed_official 525:c320967f86b9 39
mbed_official 525:c320967f86b9 40 obj->channel = pin_location(pin, PinMap_ADC);
mbed_official 525:c320967f86b9 41 MBED_ASSERT((int) obj->channel != NC);
mbed_official 525:c320967f86b9 42 }
mbed_official 525:c320967f86b9 43
mbed_official 525:c320967f86b9 44 void analogin_init(analogin_t *obj, PinName pin)
mbed_official 525:c320967f86b9 45 {
mbed_official 543:9dba91c44009 46 static uint8_t adc_initialized = 0;
mbed_official 525:c320967f86b9 47
mbed_official 543:9dba91c44009 48 /* Init structure */
mbed_official 543:9dba91c44009 49 analogin_preinit(obj, pin);
mbed_official 525:c320967f86b9 50
mbed_official 543:9dba91c44009 51 /* Only initialize the ADC once */
mbed_official 543:9dba91c44009 52 if (!adc_initialized) {
mbed_official 543:9dba91c44009 53 /* Turn on the clock */
mbed_official 543:9dba91c44009 54 CMU_ClockEnable(cmuClock_ADC0, true);
mbed_official 548:1abac31e188e 55
mbed_official 543:9dba91c44009 56 /* Init with default settings */
mbed_official 543:9dba91c44009 57 ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
mbed_official 543:9dba91c44009 58 init.prescale = 4;
mbed_official 543:9dba91c44009 59 ADC_Init(obj->adc, &init);
mbed_official 548:1abac31e188e 60
mbed_official 543:9dba91c44009 61 /* Init for single conversion use */
mbed_official 543:9dba91c44009 62 ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT;
mbed_official 525:c320967f86b9 63
mbed_official 543:9dba91c44009 64 /* Measure input channel with Vdd reference. */
mbed_official 543:9dba91c44009 65 singleInit.reference = adcRefVDD;
mbed_official 543:9dba91c44009 66 singleInit.resolution = adcRes12Bit;
mbed_official 543:9dba91c44009 67 singleInit.acqTime = adcAcqTime32;
mbed_official 525:c320967f86b9 68
mbed_official 543:9dba91c44009 69 ADC_InitSingle(obj->adc, &singleInit);
mbed_official 548:1abac31e188e 70
mbed_official 543:9dba91c44009 71 adc_initialized = 1;
mbed_official 543:9dba91c44009 72 }
mbed_official 525:c320967f86b9 73 }
mbed_official 525:c320967f86b9 74
mbed_official 525:c320967f86b9 75 void analogin_enable(analogin_t *obj, uint8_t enable)
mbed_official 525:c320967f86b9 76 {
mbed_official 525:c320967f86b9 77 //not avail for EFM32
mbed_official 525:c320967f86b9 78 }
mbed_official 525:c320967f86b9 79
mbed_official 525:c320967f86b9 80 void analogin_enable_pins(analogin_t *obj, uint8_t enable)
mbed_official 525:c320967f86b9 81 {
mbed_official 525:c320967f86b9 82 //not avail for EFM32
mbed_official 525:c320967f86b9 83 }
mbed_official 525:c320967f86b9 84
mbed_official 525:c320967f86b9 85 void analogin_enable_interrupt(analogin_t *obj, uint32_t address, uint8_t enable)
mbed_official 525:c320967f86b9 86 {
mbed_official 525:c320967f86b9 87 NVIC_SetVector(ADC0_IRQn, address);
mbed_official 525:c320967f86b9 88 if (enable) {
mbed_official 525:c320967f86b9 89 // enable end of conversion interrupt
mbed_official 525:c320967f86b9 90 ADC_IntEnable(obj->adc, ADC_IEN_SCAN);
mbed_official 525:c320967f86b9 91 ADC_IntSet(obj->adc, ADC_IF_SCAN);
mbed_official 525:c320967f86b9 92 NVIC_EnableIRQ(ADC0_IRQn);
mbed_official 525:c320967f86b9 93 } else {
mbed_official 525:c320967f86b9 94 ADC_IntDisable(obj->adc, ADC_IEN_SCAN);
mbed_official 525:c320967f86b9 95 ADC_IntClear(obj->adc, ADC_IF_SCAN);
mbed_official 525:c320967f86b9 96 NVIC_DisableIRQ(ADC0_IRQn);
mbed_official 525:c320967f86b9 97 }
mbed_official 525:c320967f86b9 98 }
mbed_official 525:c320967f86b9 99
mbed_official 525:c320967f86b9 100 uint16_t analogin_read_u16(analogin_t *obj)
mbed_official 525:c320967f86b9 101 {
mbed_official 525:c320967f86b9 102 ADC_TypeDef *adc = obj->adc;
mbed_official 525:c320967f86b9 103 uint16_t sample = 0;
mbed_official 525:c320967f86b9 104
mbed_official 525:c320967f86b9 105 //Make sure a single conversion is not in progress
mbed_official 525:c320967f86b9 106 adc->CMD = ADC_CMD_SINGLESTOP;
mbed_official 525:c320967f86b9 107
mbed_official 525:c320967f86b9 108 // Make sure we are checking the correct channel
mbed_official 525:c320967f86b9 109 adc->SINGLECTRL = (adc->SINGLECTRL & ~_ADC_SINGLECTRL_INPUTSEL_MASK) | obj->channel;
mbed_official 525:c320967f86b9 110
mbed_official 525:c320967f86b9 111 ADC_Start(adc, adcStartSingle);
mbed_official 525:c320967f86b9 112
mbed_official 525:c320967f86b9 113 /* Wait while conversion is active */
mbed_official 525:c320967f86b9 114 while (adc->STATUS & ADC_STATUS_SINGLEACT);
mbed_official 525:c320967f86b9 115
mbed_official 525:c320967f86b9 116 /* Get ADC result */
mbed_official 525:c320967f86b9 117 sample = ADC_DataSingleGet(adc);
mbed_official 525:c320967f86b9 118
mbed_official 525:c320967f86b9 119 /* The ADC has 12 bit resolution. We shift in 4 0s */
mbed_official 525:c320967f86b9 120 /* from the right to make it a 16 bit number as expected */
mbed_official 525:c320967f86b9 121 return sample << 4;
mbed_official 525:c320967f86b9 122 }
mbed_official 525:c320967f86b9 123
mbed_official 525:c320967f86b9 124 float analogin_read(analogin_t *obj)
mbed_official 525:c320967f86b9 125 {
mbed_official 525:c320967f86b9 126 /* Convert from a uint16 to a float between 0 and 1 by division by 0xFFFF */
mbed_official 525:c320967f86b9 127 return analogin_read_u16(obj) / (float) 0xFFFF;
mbed_official 525:c320967f86b9 128 }
mbed_official 525:c320967f86b9 129
mbed_official 525:c320967f86b9 130 #endif