Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
targets/TARGET_Silicon_Labs/TARGET_EFM32/analogin_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/analogin_api.c@144:ef7eb2e8f9f7
- Child:
- 176:447f873cad2f
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /***************************************************************************//** |
<> | 144:ef7eb2e8f9f7 | 2 | * @file analogin_api.c |
<> | 144:ef7eb2e8f9f7 | 3 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 4 | * @section License |
<> | 144:ef7eb2e8f9f7 | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
<> | 144:ef7eb2e8f9f7 | 6 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * SPDX-License-Identifier: Apache-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
<> | 144:ef7eb2e8f9f7 | 11 | * not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 12 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 13 | * |
<> | 144:ef7eb2e8f9f7 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 15 | * |
<> | 144:ef7eb2e8f9f7 | 16 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
<> | 144:ef7eb2e8f9f7 | 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 19 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 20 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 21 | * |
<> | 144:ef7eb2e8f9f7 | 22 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 23 | |
<> | 144:ef7eb2e8f9f7 | 24 | #include "device.h" |
<> | 144:ef7eb2e8f9f7 | 25 | #if DEVICE_ANALOGIN |
<> | 144:ef7eb2e8f9f7 | 26 | |
<> | 144:ef7eb2e8f9f7 | 27 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 28 | #include "analogin_api.h" |
<> | 144:ef7eb2e8f9f7 | 29 | |
<> | 144:ef7eb2e8f9f7 | 30 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 31 | #include "pinmap_function.h" |
<> | 144:ef7eb2e8f9f7 | 32 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | #include "em_adc.h" |
<> | 144:ef7eb2e8f9f7 | 35 | #include "em_cmu.h" |
<> | 144:ef7eb2e8f9f7 | 36 | |
<> | 144:ef7eb2e8f9f7 | 37 | void analogin_init(analogin_t *obj, PinName pin) |
<> | 144:ef7eb2e8f9f7 | 38 | { |
<> | 144:ef7eb2e8f9f7 | 39 | static uint8_t adc_initialized = 0; |
<> | 144:ef7eb2e8f9f7 | 40 | |
<> | 144:ef7eb2e8f9f7 | 41 | /* Init structure */ |
<> | 144:ef7eb2e8f9f7 | 42 | obj->adc = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 43 | MBED_ASSERT((int) obj->adc != NC); |
<> | 144:ef7eb2e8f9f7 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | obj->channel = pin_location(pin, PinMap_ADC); |
<> | 144:ef7eb2e8f9f7 | 46 | MBED_ASSERT((int) obj->channel != NC); |
<> | 144:ef7eb2e8f9f7 | 47 | |
<> | 144:ef7eb2e8f9f7 | 48 | /* Only initialize the ADC once */ |
<> | 144:ef7eb2e8f9f7 | 49 | if (!adc_initialized) { |
<> | 144:ef7eb2e8f9f7 | 50 | /* Turn on the clock */ |
<> | 144:ef7eb2e8f9f7 | 51 | CMU_ClockEnable(cmuClock_ADC0, true); |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | /* Init with default settings */ |
<> | 144:ef7eb2e8f9f7 | 54 | ADC_Init_TypeDef init = ADC_INIT_DEFAULT; |
<> | 144:ef7eb2e8f9f7 | 55 | init.prescale = 4; |
<> | 144:ef7eb2e8f9f7 | 56 | ADC_Init(obj->adc, &init); |
<> | 144:ef7eb2e8f9f7 | 57 | |
<> | 144:ef7eb2e8f9f7 | 58 | /* Init for single conversion use */ |
<> | 144:ef7eb2e8f9f7 | 59 | ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT; |
<> | 144:ef7eb2e8f9f7 | 60 | |
<> | 144:ef7eb2e8f9f7 | 61 | /* Measure input channel with Vdd reference. */ |
<> | 144:ef7eb2e8f9f7 | 62 | singleInit.reference = adcRefVDD; |
<> | 144:ef7eb2e8f9f7 | 63 | singleInit.resolution = adcRes12Bit; |
<> | 144:ef7eb2e8f9f7 | 64 | singleInit.acqTime = adcAcqTime32; |
<> | 144:ef7eb2e8f9f7 | 65 | |
<> | 144:ef7eb2e8f9f7 | 66 | ADC_InitSingle(obj->adc, &singleInit); |
<> | 144:ef7eb2e8f9f7 | 67 | |
<> | 144:ef7eb2e8f9f7 | 68 | adc_initialized = 1; |
<> | 144:ef7eb2e8f9f7 | 69 | } |
<> | 144:ef7eb2e8f9f7 | 70 | } |
<> | 144:ef7eb2e8f9f7 | 71 | |
<> | 144:ef7eb2e8f9f7 | 72 | uint16_t analogin_read_u16(analogin_t *obj) |
<> | 144:ef7eb2e8f9f7 | 73 | { |
<> | 144:ef7eb2e8f9f7 | 74 | ADC_TypeDef *adc = obj->adc; |
<> | 144:ef7eb2e8f9f7 | 75 | uint16_t sample = 0; |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | //Make sure a single conversion is not in progress |
<> | 144:ef7eb2e8f9f7 | 78 | adc->CMD = ADC_CMD_SINGLESTOP; |
<> | 144:ef7eb2e8f9f7 | 79 | |
<> | 144:ef7eb2e8f9f7 | 80 | // Make sure we are checking the correct channel |
<> | 144:ef7eb2e8f9f7 | 81 | #if defined _ADC_SINGLECTRL_INPUTSEL_MASK |
<> | 144:ef7eb2e8f9f7 | 82 | adc->SINGLECTRL = (adc->SINGLECTRL & ~_ADC_SINGLECTRL_INPUTSEL_MASK) | obj->channel; |
<> | 144:ef7eb2e8f9f7 | 83 | #elif _ADC_SINGLECTRL_POSSEL_MASK |
<> | 144:ef7eb2e8f9f7 | 84 | adc->SINGLECTRL = (adc->SINGLECTRL & ~_ADC_SINGLECTRL_POSSEL_MASK) | obj->channel << _ADC_SINGLECTRL_POSSEL_SHIFT; |
<> | 144:ef7eb2e8f9f7 | 85 | #endif |
<> | 144:ef7eb2e8f9f7 | 86 | |
<> | 144:ef7eb2e8f9f7 | 87 | ADC_Start(adc, adcStartSingle); |
<> | 144:ef7eb2e8f9f7 | 88 | |
<> | 144:ef7eb2e8f9f7 | 89 | /* Wait while conversion is active */ |
<> | 144:ef7eb2e8f9f7 | 90 | while (adc->STATUS & ADC_STATUS_SINGLEACT); |
<> | 144:ef7eb2e8f9f7 | 91 | |
<> | 144:ef7eb2e8f9f7 | 92 | /* Get ADC result */ |
<> | 144:ef7eb2e8f9f7 | 93 | sample = ADC_DataSingleGet(adc); |
<> | 144:ef7eb2e8f9f7 | 94 | |
<> | 144:ef7eb2e8f9f7 | 95 | /* The ADC has 12 bit resolution. We shift in 4 0s */ |
<> | 144:ef7eb2e8f9f7 | 96 | /* from the right to make it a 16 bit number as expected */ |
<> | 144:ef7eb2e8f9f7 | 97 | return sample << 4; |
<> | 144:ef7eb2e8f9f7 | 98 | } |
<> | 144:ef7eb2e8f9f7 | 99 | |
<> | 144:ef7eb2e8f9f7 | 100 | float analogin_read(analogin_t *obj) |
<> | 144:ef7eb2e8f9f7 | 101 | { |
<> | 144:ef7eb2e8f9f7 | 102 | /* Convert from a uint16 to a float between 0 and 1 by division by 0xFFFF */ |
<> | 144:ef7eb2e8f9f7 | 103 | return analogin_read_u16(obj) / (float) 0xFFFF; |
<> | 144:ef7eb2e8f9f7 | 104 | } |
<> | 144:ef7eb2e8f9f7 | 105 | |
<> | 144:ef7eb2e8f9f7 | 106 | #endif |