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