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