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 25 14:15:10 2015 +0100
Revision:
627:4fa1328d9c60
Parent:
548:1abac31e188e
Synchronized with git revision fe238a91ab7a4d1d72c4cab9da04967c619d54ad

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

Silicon Labs - Add support for low-power async Serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 627:4fa1328d9c60 1 /***************************************************************************//**
mbed_official 627:4fa1328d9c60 2 * @file analogin_api.c
mbed_official 627:4fa1328d9c60 3 *******************************************************************************
mbed_official 627:4fa1328d9c60 4 * @section License
mbed_official 627:4fa1328d9c60 5 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
mbed_official 627:4fa1328d9c60 6 *******************************************************************************
mbed_official 525:c320967f86b9 7 *
mbed_official 627:4fa1328d9c60 8 * Permission is granted to anyone to use this software for any purpose,
mbed_official 627:4fa1328d9c60 9 * including commercial applications, and to alter it and redistribute it
mbed_official 627:4fa1328d9c60 10 * freely, subject to the following restrictions:
mbed_official 525:c320967f86b9 11 *
mbed_official 627:4fa1328d9c60 12 * 1. The origin of this software must not be misrepresented; you must not
mbed_official 627:4fa1328d9c60 13 * claim that you wrote the original software.
mbed_official 627:4fa1328d9c60 14 * 2. Altered source versions must be plainly marked as such, and must not be
mbed_official 627:4fa1328d9c60 15 * misrepresented as being the original software.
mbed_official 627:4fa1328d9c60 16 * 3. This notice may not be removed or altered from any source distribution.
mbed_official 525:c320967f86b9 17 *
mbed_official 627:4fa1328d9c60 18 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
mbed_official 627:4fa1328d9c60 19 * obligation to support this Software. Silicon Labs is providing the
mbed_official 627:4fa1328d9c60 20 * Software "AS IS", with no express or implied warranties of any kind,
mbed_official 627:4fa1328d9c60 21 * including, but not limited to, any implied warranties of merchantability
mbed_official 627:4fa1328d9c60 22 * or fitness for any particular purpose or warranties against infringement
mbed_official 627:4fa1328d9c60 23 * of any proprietary rights of a third party.
mbed_official 627:4fa1328d9c60 24 *
mbed_official 627:4fa1328d9c60 25 * Silicon Labs will not be liable for any consequential, incidental, or
mbed_official 627:4fa1328d9c60 26 * special damages, or any other relief, or for any claim by any third party,
mbed_official 627:4fa1328d9c60 27 * arising from your use of this Software.
mbed_official 627:4fa1328d9c60 28 *
mbed_official 627:4fa1328d9c60 29 ******************************************************************************/
mbed_official 525:c320967f86b9 30
mbed_official 525:c320967f86b9 31 #include "device.h"
mbed_official 525:c320967f86b9 32 #if DEVICE_ANALOGIN
mbed_official 525:c320967f86b9 33
mbed_official 525:c320967f86b9 34 #include "mbed_assert.h"
mbed_official 525:c320967f86b9 35 #include "analogin_api.h"
mbed_official 525:c320967f86b9 36
mbed_official 525:c320967f86b9 37 #include "pinmap.h"
mbed_official 525:c320967f86b9 38 #include "pinmap_function.h"
mbed_official 525:c320967f86b9 39 #include "PeripheralPins.h"
mbed_official 525:c320967f86b9 40
mbed_official 525:c320967f86b9 41 #include "em_adc.h"
mbed_official 525:c320967f86b9 42 #include "em_cmu.h"
mbed_official 525:c320967f86b9 43
mbed_official 525:c320967f86b9 44 uint8_t analogin_get_index(analogin_t *obj)
mbed_official 525:c320967f86b9 45 {
mbed_official 525:c320967f86b9 46 return 0; //only one module availalbe
mbed_official 525:c320967f86b9 47 }
mbed_official 525:c320967f86b9 48
mbed_official 525:c320967f86b9 49 void analogin_preinit(analogin_t *obj, PinName pin)
mbed_official 525:c320967f86b9 50 {
mbed_official 525:c320967f86b9 51 obj->adc = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
mbed_official 525:c320967f86b9 52 MBED_ASSERT((int) obj->adc != NC);
mbed_official 525:c320967f86b9 53
mbed_official 525:c320967f86b9 54 obj->channel = pin_location(pin, PinMap_ADC);
mbed_official 525:c320967f86b9 55 MBED_ASSERT((int) obj->channel != NC);
mbed_official 525:c320967f86b9 56 }
mbed_official 525:c320967f86b9 57
mbed_official 525:c320967f86b9 58 void analogin_init(analogin_t *obj, PinName pin)
mbed_official 525:c320967f86b9 59 {
mbed_official 543:9dba91c44009 60 static uint8_t adc_initialized = 0;
mbed_official 525:c320967f86b9 61
mbed_official 543:9dba91c44009 62 /* Init structure */
mbed_official 543:9dba91c44009 63 analogin_preinit(obj, pin);
mbed_official 525:c320967f86b9 64
mbed_official 543:9dba91c44009 65 /* Only initialize the ADC once */
mbed_official 543:9dba91c44009 66 if (!adc_initialized) {
mbed_official 543:9dba91c44009 67 /* Turn on the clock */
mbed_official 543:9dba91c44009 68 CMU_ClockEnable(cmuClock_ADC0, true);
mbed_official 548:1abac31e188e 69
mbed_official 543:9dba91c44009 70 /* Init with default settings */
mbed_official 543:9dba91c44009 71 ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
mbed_official 543:9dba91c44009 72 init.prescale = 4;
mbed_official 543:9dba91c44009 73 ADC_Init(obj->adc, &init);
mbed_official 548:1abac31e188e 74
mbed_official 543:9dba91c44009 75 /* Init for single conversion use */
mbed_official 543:9dba91c44009 76 ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT;
mbed_official 525:c320967f86b9 77
mbed_official 543:9dba91c44009 78 /* Measure input channel with Vdd reference. */
mbed_official 543:9dba91c44009 79 singleInit.reference = adcRefVDD;
mbed_official 543:9dba91c44009 80 singleInit.resolution = adcRes12Bit;
mbed_official 543:9dba91c44009 81 singleInit.acqTime = adcAcqTime32;
mbed_official 525:c320967f86b9 82
mbed_official 543:9dba91c44009 83 ADC_InitSingle(obj->adc, &singleInit);
mbed_official 548:1abac31e188e 84
mbed_official 543:9dba91c44009 85 adc_initialized = 1;
mbed_official 543:9dba91c44009 86 }
mbed_official 525:c320967f86b9 87 }
mbed_official 525:c320967f86b9 88
mbed_official 525:c320967f86b9 89 void analogin_enable(analogin_t *obj, uint8_t enable)
mbed_official 525:c320967f86b9 90 {
mbed_official 525:c320967f86b9 91 //not avail for EFM32
mbed_official 525:c320967f86b9 92 }
mbed_official 525:c320967f86b9 93
mbed_official 525:c320967f86b9 94 void analogin_enable_pins(analogin_t *obj, uint8_t enable)
mbed_official 525:c320967f86b9 95 {
mbed_official 525:c320967f86b9 96 //not avail for EFM32
mbed_official 525:c320967f86b9 97 }
mbed_official 525:c320967f86b9 98
mbed_official 525:c320967f86b9 99 void analogin_enable_interrupt(analogin_t *obj, uint32_t address, uint8_t enable)
mbed_official 525:c320967f86b9 100 {
mbed_official 525:c320967f86b9 101 NVIC_SetVector(ADC0_IRQn, address);
mbed_official 525:c320967f86b9 102 if (enable) {
mbed_official 525:c320967f86b9 103 // enable end of conversion interrupt
mbed_official 525:c320967f86b9 104 ADC_IntEnable(obj->adc, ADC_IEN_SCAN);
mbed_official 525:c320967f86b9 105 ADC_IntSet(obj->adc, ADC_IF_SCAN);
mbed_official 525:c320967f86b9 106 NVIC_EnableIRQ(ADC0_IRQn);
mbed_official 525:c320967f86b9 107 } else {
mbed_official 525:c320967f86b9 108 ADC_IntDisable(obj->adc, ADC_IEN_SCAN);
mbed_official 525:c320967f86b9 109 ADC_IntClear(obj->adc, ADC_IF_SCAN);
mbed_official 525:c320967f86b9 110 NVIC_DisableIRQ(ADC0_IRQn);
mbed_official 525:c320967f86b9 111 }
mbed_official 525:c320967f86b9 112 }
mbed_official 525:c320967f86b9 113
mbed_official 525:c320967f86b9 114 uint16_t analogin_read_u16(analogin_t *obj)
mbed_official 525:c320967f86b9 115 {
mbed_official 525:c320967f86b9 116 ADC_TypeDef *adc = obj->adc;
mbed_official 525:c320967f86b9 117 uint16_t sample = 0;
mbed_official 525:c320967f86b9 118
mbed_official 525:c320967f86b9 119 //Make sure a single conversion is not in progress
mbed_official 525:c320967f86b9 120 adc->CMD = ADC_CMD_SINGLESTOP;
mbed_official 525:c320967f86b9 121
mbed_official 525:c320967f86b9 122 // Make sure we are checking the correct channel
mbed_official 525:c320967f86b9 123 adc->SINGLECTRL = (adc->SINGLECTRL & ~_ADC_SINGLECTRL_INPUTSEL_MASK) | obj->channel;
mbed_official 525:c320967f86b9 124
mbed_official 525:c320967f86b9 125 ADC_Start(adc, adcStartSingle);
mbed_official 525:c320967f86b9 126
mbed_official 525:c320967f86b9 127 /* Wait while conversion is active */
mbed_official 525:c320967f86b9 128 while (adc->STATUS & ADC_STATUS_SINGLEACT);
mbed_official 525:c320967f86b9 129
mbed_official 525:c320967f86b9 130 /* Get ADC result */
mbed_official 525:c320967f86b9 131 sample = ADC_DataSingleGet(adc);
mbed_official 525:c320967f86b9 132
mbed_official 525:c320967f86b9 133 /* The ADC has 12 bit resolution. We shift in 4 0s */
mbed_official 525:c320967f86b9 134 /* from the right to make it a 16 bit number as expected */
mbed_official 525:c320967f86b9 135 return sample << 4;
mbed_official 525:c320967f86b9 136 }
mbed_official 525:c320967f86b9 137
mbed_official 525:c320967f86b9 138 float analogin_read(analogin_t *obj)
mbed_official 525:c320967f86b9 139 {
mbed_official 525:c320967f86b9 140 /* Convert from a uint16 to a float between 0 and 1 by division by 0xFFFF */
mbed_official 525:c320967f86b9 141 return analogin_read_u16(obj) / (float) 0xFFFF;
mbed_official 525:c320967f86b9 142 }
mbed_official 525:c320967f86b9 143
mbed_official 525:c320967f86b9 144 #endif