mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32630/mxc/adc.c@157:ff67d9f36b67, 2017-02-02 (annotated)
- Committer:
- <>
- Date:
- Thu Feb 02 17:01:33 2017 +0000
- Revision:
- 157:ff67d9f36b67
This updates the lib to the mbed lib v135
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 157:ff67d9f36b67 | 1 | /** |
<> | 157:ff67d9f36b67 | 2 | * @file |
<> | 157:ff67d9f36b67 | 3 | * @brief This file contains the function implementations for the Analog to |
<> | 157:ff67d9f36b67 | 4 | * Digital Converter (ADC) peripheral module. |
<> | 157:ff67d9f36b67 | 5 | */ |
<> | 157:ff67d9f36b67 | 6 | |
<> | 157:ff67d9f36b67 | 7 | /* **************************************************************************** |
<> | 157:ff67d9f36b67 | 8 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 157:ff67d9f36b67 | 9 | * |
<> | 157:ff67d9f36b67 | 10 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 157:ff67d9f36b67 | 11 | * copy of this software and associated documentation files (the "Software"), |
<> | 157:ff67d9f36b67 | 12 | * to deal in the Software without restriction, including without limitation |
<> | 157:ff67d9f36b67 | 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 157:ff67d9f36b67 | 14 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 157:ff67d9f36b67 | 15 | * Software is furnished to do so, subject to the following conditions: |
<> | 157:ff67d9f36b67 | 16 | * |
<> | 157:ff67d9f36b67 | 17 | * The above copyright notice and this permission notice shall be included |
<> | 157:ff67d9f36b67 | 18 | * in all copies or substantial portions of the Software. |
<> | 157:ff67d9f36b67 | 19 | * |
<> | 157:ff67d9f36b67 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 157:ff67d9f36b67 | 21 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 157:ff67d9f36b67 | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 157:ff67d9f36b67 | 23 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 157:ff67d9f36b67 | 24 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 157:ff67d9f36b67 | 25 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 157:ff67d9f36b67 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 157:ff67d9f36b67 | 27 | * |
<> | 157:ff67d9f36b67 | 28 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 157:ff67d9f36b67 | 29 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 157:ff67d9f36b67 | 30 | * Products, Inc. Branding Policy. |
<> | 157:ff67d9f36b67 | 31 | * |
<> | 157:ff67d9f36b67 | 32 | * The mere transfer of this software does not imply any licenses |
<> | 157:ff67d9f36b67 | 33 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 157:ff67d9f36b67 | 34 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 157:ff67d9f36b67 | 35 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 157:ff67d9f36b67 | 36 | * ownership rights. |
<> | 157:ff67d9f36b67 | 37 | * |
<> | 157:ff67d9f36b67 | 38 | * $Date: 2016-09-09 12:50:50 -0500 (Fri, 09 Sep 2016) $ |
<> | 157:ff67d9f36b67 | 39 | * $Revision: 24349 $ |
<> | 157:ff67d9f36b67 | 40 | * |
<> | 157:ff67d9f36b67 | 41 | *************************************************************************** */ |
<> | 157:ff67d9f36b67 | 42 | |
<> | 157:ff67d9f36b67 | 43 | /** |
<> | 157:ff67d9f36b67 | 44 | * @ingroup adc |
<> | 157:ff67d9f36b67 | 45 | * @{ |
<> | 157:ff67d9f36b67 | 46 | */ |
<> | 157:ff67d9f36b67 | 47 | |
<> | 157:ff67d9f36b67 | 48 | /* **** Includes **** */ |
<> | 157:ff67d9f36b67 | 49 | #include "mxc_config.h" |
<> | 157:ff67d9f36b67 | 50 | #include "mxc_assert.h" |
<> | 157:ff67d9f36b67 | 51 | #include "mxc_sys.h" |
<> | 157:ff67d9f36b67 | 52 | #include "adc.h" |
<> | 157:ff67d9f36b67 | 53 | |
<> | 157:ff67d9f36b67 | 54 | /* **** Definitions **** */ |
<> | 157:ff67d9f36b67 | 55 | |
<> | 157:ff67d9f36b67 | 56 | /* **** Globals **** */ |
<> | 157:ff67d9f36b67 | 57 | |
<> | 157:ff67d9f36b67 | 58 | /* **** Functions **** */ |
<> | 157:ff67d9f36b67 | 59 | |
<> | 157:ff67d9f36b67 | 60 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 61 | int ADC_Init(void) |
<> | 157:ff67d9f36b67 | 62 | { |
<> | 157:ff67d9f36b67 | 63 | int err; |
<> | 157:ff67d9f36b67 | 64 | |
<> | 157:ff67d9f36b67 | 65 | if ((err = SYS_ADC_Init()) != E_NO_ERROR) { |
<> | 157:ff67d9f36b67 | 66 | return err; |
<> | 157:ff67d9f36b67 | 67 | } |
<> | 157:ff67d9f36b67 | 68 | |
<> | 157:ff67d9f36b67 | 69 | /* Wipe previous configuration */ |
<> | 157:ff67d9f36b67 | 70 | MXC_ADC->intr = 0; |
<> | 157:ff67d9f36b67 | 71 | |
<> | 157:ff67d9f36b67 | 72 | /* Clear all ADC interrupt flags (W1C) */ |
<> | 157:ff67d9f36b67 | 73 | MXC_ADC->intr = MXC_ADC->intr; |
<> | 157:ff67d9f36b67 | 74 | |
<> | 157:ff67d9f36b67 | 75 | /* Enable done interrupt */ |
<> | 157:ff67d9f36b67 | 76 | MXC_ADC->intr = MXC_F_ADC_INTR_ADC_DONE_IE; |
<> | 157:ff67d9f36b67 | 77 | |
<> | 157:ff67d9f36b67 | 78 | /* Power up the ADC */ |
<> | 157:ff67d9f36b67 | 79 | MXC_ADC->ctrl = (MXC_F_ADC_CTRL_ADC_PU | |
<> | 157:ff67d9f36b67 | 80 | MXC_F_ADC_CTRL_ADC_CLK_EN | |
<> | 157:ff67d9f36b67 | 81 | MXC_F_ADC_CTRL_BUF_PU | |
<> | 157:ff67d9f36b67 | 82 | MXC_F_ADC_CTRL_ADC_REFBUF_PU | |
<> | 157:ff67d9f36b67 | 83 | MXC_F_ADC_CTRL_ADC_CHGPUMP_PU); |
<> | 157:ff67d9f36b67 | 84 | |
<> | 157:ff67d9f36b67 | 85 | return E_NO_ERROR; |
<> | 157:ff67d9f36b67 | 86 | } |
<> | 157:ff67d9f36b67 | 87 | |
<> | 157:ff67d9f36b67 | 88 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 89 | void ADC_StartConvert(mxc_adc_chsel_t channel, unsigned int adc_scale, unsigned int bypass) |
<> | 157:ff67d9f36b67 | 90 | { |
<> | 157:ff67d9f36b67 | 91 | uint32_t ctrl_tmp; |
<> | 157:ff67d9f36b67 | 92 | |
<> | 157:ff67d9f36b67 | 93 | /* Clear the ADC done flag */ |
<> | 157:ff67d9f36b67 | 94 | ADC_ClearFlags(MXC_F_ADC_INTR_ADC_DONE_IF); |
<> | 157:ff67d9f36b67 | 95 | |
<> | 157:ff67d9f36b67 | 96 | /* Insert channel selection */ |
<> | 157:ff67d9f36b67 | 97 | ctrl_tmp = MXC_ADC->ctrl; |
<> | 157:ff67d9f36b67 | 98 | ctrl_tmp &= ~(MXC_F_ADC_CTRL_ADC_CHSEL); |
<> | 157:ff67d9f36b67 | 99 | ctrl_tmp |= ((channel << MXC_F_ADC_CTRL_ADC_CHSEL_POS) & MXC_F_ADC_CTRL_ADC_CHSEL); |
<> | 157:ff67d9f36b67 | 100 | |
<> | 157:ff67d9f36b67 | 101 | /* Clear channel configuration */ |
<> | 157:ff67d9f36b67 | 102 | ctrl_tmp &= ~(MXC_F_ADC_CTRL_ADC_REFSCL | MXC_F_ADC_CTRL_ADC_SCALE | MXC_F_ADC_CTRL_BUF_BYPASS); |
<> | 157:ff67d9f36b67 | 103 | |
<> | 157:ff67d9f36b67 | 104 | /* ADC reference scaling must be set for all channels but two*/ |
<> | 157:ff67d9f36b67 | 105 | if ((channel != ADC_CH_VDD18) && (channel != ADC_CH_VDD12)) { |
<> | 157:ff67d9f36b67 | 106 | ctrl_tmp |= MXC_F_ADC_CTRL_ADC_REFSCL; |
<> | 157:ff67d9f36b67 | 107 | } |
<> | 157:ff67d9f36b67 | 108 | |
<> | 157:ff67d9f36b67 | 109 | /* Finalize user-requested channel configuration */ |
<> | 157:ff67d9f36b67 | 110 | if (adc_scale || channel > ADC_CH_3) { |
<> | 157:ff67d9f36b67 | 111 | ctrl_tmp |= MXC_F_ADC_CTRL_ADC_SCALE; |
<> | 157:ff67d9f36b67 | 112 | } |
<> | 157:ff67d9f36b67 | 113 | if (bypass) { |
<> | 157:ff67d9f36b67 | 114 | ctrl_tmp |= MXC_F_ADC_CTRL_BUF_BYPASS; |
<> | 157:ff67d9f36b67 | 115 | } |
<> | 157:ff67d9f36b67 | 116 | |
<> | 157:ff67d9f36b67 | 117 | /* Write this configuration */ |
<> | 157:ff67d9f36b67 | 118 | MXC_ADC->ctrl = ctrl_tmp; |
<> | 157:ff67d9f36b67 | 119 | |
<> | 157:ff67d9f36b67 | 120 | /* Start conversion */ |
<> | 157:ff67d9f36b67 | 121 | MXC_ADC->ctrl |= MXC_F_ADC_CTRL_CPU_ADC_START; |
<> | 157:ff67d9f36b67 | 122 | |
<> | 157:ff67d9f36b67 | 123 | } |
<> | 157:ff67d9f36b67 | 124 | |
<> | 157:ff67d9f36b67 | 125 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 126 | int ADC_GetData(uint16_t *outdata) |
<> | 157:ff67d9f36b67 | 127 | { |
<> | 157:ff67d9f36b67 | 128 | /* See if a conversion is in process */ |
<> | 157:ff67d9f36b67 | 129 | if (MXC_ADC->status & MXC_F_ADC_STATUS_ADC_ACTIVE) { |
<> | 157:ff67d9f36b67 | 130 | /* Wait for conversion to complete */ |
<> | 157:ff67d9f36b67 | 131 | while ((MXC_ADC->intr & MXC_F_ADC_INTR_ADC_DONE_IF) == 0); |
<> | 157:ff67d9f36b67 | 132 | } |
<> | 157:ff67d9f36b67 | 133 | |
<> | 157:ff67d9f36b67 | 134 | /* Read 32-bit value and truncate to 16-bit for output depending on data align bit*/ |
<> | 157:ff67d9f36b67 | 135 | if((MXC_ADC->ctrl & MXC_F_ADC_CTRL_ADC_DATAALIGN) == 0) |
<> | 157:ff67d9f36b67 | 136 | *outdata = (uint16_t)(MXC_ADC->data); /* LSB justified */ |
<> | 157:ff67d9f36b67 | 137 | else |
<> | 157:ff67d9f36b67 | 138 | *outdata = (uint16_t)(MXC_ADC->data >> 6); /* MSB justified */ |
<> | 157:ff67d9f36b67 | 139 | |
<> | 157:ff67d9f36b67 | 140 | /* Check for overflow */ |
<> | 157:ff67d9f36b67 | 141 | if (MXC_ADC->status & MXC_F_ADC_STATUS_ADC_OVERFLOW) { |
<> | 157:ff67d9f36b67 | 142 | return E_OVERFLOW; |
<> | 157:ff67d9f36b67 | 143 | } |
<> | 157:ff67d9f36b67 | 144 | |
<> | 157:ff67d9f36b67 | 145 | return E_NO_ERROR; |
<> | 157:ff67d9f36b67 | 146 | } |
<> | 157:ff67d9f36b67 | 147 | |
<> | 157:ff67d9f36b67 | 148 | /* ************************************************************************* */ |
<> | 157:ff67d9f36b67 | 149 | int ADC_SetLimit(mxc_adc_limitsel_t unit, mxc_adc_chsel_t channel, |
<> | 157:ff67d9f36b67 | 150 | unsigned int low_enable, unsigned int low_limit, |
<> | 157:ff67d9f36b67 | 151 | unsigned int high_enable, unsigned int high_limit) |
<> | 157:ff67d9f36b67 | 152 | { |
<> | 157:ff67d9f36b67 | 153 | /* Check args */ |
<> | 157:ff67d9f36b67 | 154 | if ((unit >= ADC_LIMIT_MAX) || (channel >= ADC_CH_MAX)) |
<> | 157:ff67d9f36b67 | 155 | return E_BAD_PARAM; |
<> | 157:ff67d9f36b67 | 156 | |
<> | 157:ff67d9f36b67 | 157 | /* set channel using the limit */ |
<> | 157:ff67d9f36b67 | 158 | MXC_ADC->limit[unit] = ((channel << MXC_F_ADC_LIMIT0_CH_SEL_POS) & MXC_F_ADC_LIMIT0_CH_SEL); |
<> | 157:ff67d9f36b67 | 159 | |
<> | 157:ff67d9f36b67 | 160 | /* enable/disable the limit*/ |
<> | 157:ff67d9f36b67 | 161 | if (low_enable) { |
<> | 157:ff67d9f36b67 | 162 | MXC_ADC->limit[unit] |= MXC_F_ADC_LIMIT0_CH_LO_LIMIT_EN | |
<> | 157:ff67d9f36b67 | 163 | ((low_limit << MXC_F_ADC_LIMIT0_CH_LO_LIMIT_POS) & MXC_F_ADC_LIMIT0_CH_LO_LIMIT); |
<> | 157:ff67d9f36b67 | 164 | } |
<> | 157:ff67d9f36b67 | 165 | else{ |
<> | 157:ff67d9f36b67 | 166 | MXC_ADC->limit[unit] &= ~MXC_F_ADC_LIMIT0_CH_LO_LIMIT_EN; |
<> | 157:ff67d9f36b67 | 167 | } |
<> | 157:ff67d9f36b67 | 168 | |
<> | 157:ff67d9f36b67 | 169 | if (high_enable) { |
<> | 157:ff67d9f36b67 | 170 | MXC_ADC->limit[unit] |= MXC_F_ADC_LIMIT0_CH_HI_LIMIT_EN | |
<> | 157:ff67d9f36b67 | 171 | ((high_limit << MXC_F_ADC_LIMIT0_CH_HI_LIMIT_POS) & MXC_F_ADC_LIMIT0_CH_HI_LIMIT); |
<> | 157:ff67d9f36b67 | 172 | } |
<> | 157:ff67d9f36b67 | 173 | else{ |
<> | 157:ff67d9f36b67 | 174 | MXC_ADC->limit[unit] &= ~MXC_F_ADC_LIMIT0_CH_HI_LIMIT_EN; |
<> | 157:ff67d9f36b67 | 175 | } |
<> | 157:ff67d9f36b67 | 176 | |
<> | 157:ff67d9f36b67 | 177 | return E_NO_ERROR; |
<> | 157:ff67d9f36b67 | 178 | } |
<> | 157:ff67d9f36b67 | 179 | |
<> | 157:ff67d9f36b67 | 180 | /**@} end of group adc */ |