t

Fork of mbed-dev by mbed official

Committer:
amithy
Date:
Thu Nov 09 22:14:37 2017 +0000
Revision:
178:c26431f84b0d
Parent:
150:02e0a0aed4ec
test export

Who changed what in which revision?

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