mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
186:707f6e361f3e
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 150:02e0a0aed4ec 1 /*******************************************************************************
<> 150:02e0a0aed4ec 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 150:02e0a0aed4ec 3 *
<> 150:02e0a0aed4ec 4 * Permission is hereby granted, free of charge, to any person obtaining a
<> 150:02e0a0aed4ec 5 * copy of this software and associated documentation files (the "Software"),
<> 150:02e0a0aed4ec 6 * to deal in the Software without restriction, including without limitation
<> 150:02e0a0aed4ec 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 150:02e0a0aed4ec 8 * and/or sell copies of the Software, and to permit persons to whom the
<> 150:02e0a0aed4ec 9 * Software is furnished to do so, subject to the following conditions:
<> 150:02e0a0aed4ec 10 *
<> 150:02e0a0aed4ec 11 * The above copyright notice and this permission notice shall be included
<> 150:02e0a0aed4ec 12 * in all copies or substantial portions of the Software.
<> 150:02e0a0aed4ec 13 *
<> 150:02e0a0aed4ec 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 150:02e0a0aed4ec 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 150:02e0a0aed4ec 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 150:02e0a0aed4ec 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 150:02e0a0aed4ec 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 150:02e0a0aed4ec 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 150:02e0a0aed4ec 20 * OTHER DEALINGS IN THE SOFTWARE.
<> 150:02e0a0aed4ec 21 *
<> 150:02e0a0aed4ec 22 * Except as contained in this notice, the name of Maxim Integrated
<> 150:02e0a0aed4ec 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 150:02e0a0aed4ec 24 * Products, Inc. Branding Policy.
<> 150:02e0a0aed4ec 25 *
<> 150:02e0a0aed4ec 26 * The mere transfer of this software does not imply any licenses
<> 150:02e0a0aed4ec 27 * of trade secrets, proprietary technology, copyrights, patents,
<> 150:02e0a0aed4ec 28 * trademarks, maskwork rights, or any other form of intellectual
<> 150:02e0a0aed4ec 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 150:02e0a0aed4ec 30 * ownership rights.
<> 150:02e0a0aed4ec 31 *******************************************************************************
<> 150:02e0a0aed4ec 32 */
<> 150:02e0a0aed4ec 33 #include "mbed_assert.h"
<> 150:02e0a0aed4ec 34 #include "analogin_api.h"
<> 150:02e0a0aed4ec 35 #include "adc.h"
<> 150:02e0a0aed4ec 36 #include "pinmap.h"
<> 150:02e0a0aed4ec 37 #include "PeripheralPins.h"
<> 150:02e0a0aed4ec 38
<> 150:02e0a0aed4ec 39 #define ADC_FULL_SCALE 0x3FFU
<> 150:02e0a0aed4ec 40 #define INT_FULL_SCALE 0xFFFFU
<> 150:02e0a0aed4ec 41 #define FLOAT_FULL_SCALE 1.0f
<> 150:02e0a0aed4ec 42
<> 150:02e0a0aed4ec 43 static int initialized = 0;
<> 150:02e0a0aed4ec 44
<> 150:02e0a0aed4ec 45 //******************************************************************************
<> 150:02e0a0aed4ec 46 void analogin_init(analogin_t *obj, PinName pin)
<> 150:02e0a0aed4ec 47 {
<> 150:02e0a0aed4ec 48 // Make sure pin is an analog pin we can use for ADC
<> 150:02e0a0aed4ec 49 MBED_ASSERT((ADCName)pinmap_peripheral(pin, PinMap_ADC) != (ADCName)NC);
<> 150:02e0a0aed4ec 50
<> 150:02e0a0aed4ec 51 // Set the object pointer and channel encoding
<> 150:02e0a0aed4ec 52 obj->adc = MXC_ADC;
Anna Bridge 186:707f6e361f3e 53 obj->channel = (mxc_adc_chsel_t)pinmap_find_function(pin, PinMap_ADC);
<> 150:02e0a0aed4ec 54
<> 150:02e0a0aed4ec 55 if (!initialized) {
<> 150:02e0a0aed4ec 56 MBED_ASSERT(ADC_Init() == E_NO_ERROR);
<> 150:02e0a0aed4ec 57 initialized = 1;
<> 150:02e0a0aed4ec 58 }
<> 150:02e0a0aed4ec 59 }
<> 150:02e0a0aed4ec 60
<> 150:02e0a0aed4ec 61 //******************************************************************************
<> 150:02e0a0aed4ec 62 float analogin_read(analogin_t *obj)
<> 150:02e0a0aed4ec 63 {
<> 150:02e0a0aed4ec 64 uint16_t tmp;
<> 150:02e0a0aed4ec 65 float result;
<> 150:02e0a0aed4ec 66
<> 150:02e0a0aed4ec 67 // Start conversion with no input scaling and no input buffer bypass
AnnaBridge 182:a56a73fd2a6f 68 ADC_StartConvert(obj->channel, 1, 0);
<> 150:02e0a0aed4ec 69
<> 150:02e0a0aed4ec 70 if (ADC_GetData(&tmp) == E_OVERFLOW) {
<> 150:02e0a0aed4ec 71 result = FLOAT_FULL_SCALE;
<> 150:02e0a0aed4ec 72 } else {
<> 150:02e0a0aed4ec 73 result = (float)tmp * (FLOAT_FULL_SCALE / (float)ADC_FULL_SCALE);
<> 150:02e0a0aed4ec 74 }
<> 150:02e0a0aed4ec 75
<> 150:02e0a0aed4ec 76 return result;
<> 150:02e0a0aed4ec 77 }
<> 150:02e0a0aed4ec 78
<> 150:02e0a0aed4ec 79 //******************************************************************************
<> 150:02e0a0aed4ec 80 uint16_t analogin_read_u16(analogin_t *obj)
<> 150:02e0a0aed4ec 81 {
<> 150:02e0a0aed4ec 82 uint16_t tmp;
<> 150:02e0a0aed4ec 83 uint16_t result;
<> 150:02e0a0aed4ec 84
<> 150:02e0a0aed4ec 85 // Start conversion with no input scaling and no input buffer bypass
AnnaBridge 182:a56a73fd2a6f 86 ADC_StartConvert(obj->channel, 1, 0);
<> 150:02e0a0aed4ec 87
<> 150:02e0a0aed4ec 88 if (ADC_GetData(&tmp) == E_OVERFLOW) {
<> 150:02e0a0aed4ec 89 result = INT_FULL_SCALE;
<> 150:02e0a0aed4ec 90 } else {
<> 150:02e0a0aed4ec 91 result = ((tmp << 6) & 0xFFC0) | ((tmp >> 4) & 0x003F);
<> 150:02e0a0aed4ec 92 }
<> 150:02e0a0aed4ec 93
<> 150:02e0a0aed4ec 94 return result;
<> 150:02e0a0aed4ec 95 }