adc.c 7.54 KB
Newer Older
schneider's avatar
schneider committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* *********************************************************************************************************
 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *
 * $Date: 2019-01-31 19:49:54 +0000 (Thu, 31 Jan 2019) $
 * $Revision: 40668 $
 *
 ********************************************************************************************************** */

/* **** Includes **** */
#include <stdint.h>
#include <stdio.h>
#include "mxc_config.h"
#include "mxc_errors.h"
#include "mxc_sys.h"
#include "adc_regs.h"
#include "adc.h"

/* **** MACROS **** */
#define CLEAR   0x00
// Mask for all Interrupt Flag Fields
#define ADC_IF_MASK (MXC_F_ADC_INTR_DONE_IF     | MXC_F_ADC_INTR_REF_READY_IF | \
                     MXC_F_ADC_INTR_HI_LIMIT_IF | MXC_F_ADC_INTR_LO_LIMIT_IF  | \
                     MXC_F_ADC_INTR_OVERFLOW_IF )

// Mask for all Interrupt Enable Fields
#define ADC_IE_MASK (MXC_F_ADC_INTR_DONE_IE     | MXC_F_ADC_INTR_REF_READY_IE | \
                     MXC_F_ADC_INTR_HI_LIMIT_IE | MXC_F_ADC_INTR_LO_LIMIT_IE  | \
                     MXC_F_ADC_INTR_OVERFLOW_IE )

/* **** Functions **** */
// ************************************ Function to Read Interrupt Flag **************************************
uint32_t ADC_GetFlags(void)
{
    return (MXC_ADC->intr & ADC_IF_MASK);
}

// ************************************ Function to Clear Interrupt ******************************************
void ADC_ClearFlags(uint32_t mask)
{
    MXC_ADC->intr |= (mask & ADC_IF_MASK);
}

// ************************************ Function to Read ADC Status ******************************************
uint32_t ADC_GetStatus(void)
{
    return (MXC_ADC->status);
}

// ************************************ Function to Enable Interrupt *****************************************
void ADC_EnableINT(uint32_t mask)
{
    MXC_ADC->intr = ((MXC_ADC->intr | mask) & ADC_IE_MASK);
}

// ************************************ Function to Disable Interrupt ****************************************
void ADC_DisableINT(uint32_t mask)
{
    MXC_ADC->intr = ((MXC_ADC->intr & ~mask) & ADC_IE_MASK);
}

// ************************************ Function to Configure ADC ********************************************
int ADC_Init(unsigned divisor, const sys_cfg_adc_t* sys_cfg)
{
    int err;
    int clk;
    if (divisor > 0xf || divisor < 2) {
        return E_BAD_PARAM;
    } else {
        clk = PeripheralClock/divisor;
        if (clk > MXC_ADC_MAX_CLOCK) {
            return E_BAD_PARAM;
        }
    }
    if ((err = SYS_ADC_Init(sys_cfg)) != E_NO_ERROR) {
        return err;
    }
    
    // Wipe previous configuration
    MXC_ADC->ctrl = 0;
    //enable clock
    MXC_ADC->ctrl |= MXC_F_ADC_CTRL_CLK_EN;
    //clear interrupts
    MXC_ADC->intr = MXC_ADC->intr;
    //clear clock divisor
    MXC_GCR->pckdiv &= (~MXC_F_GCR_PCKDIV_ADCFRQ);
    //set clock divisor
    MXC_GCR->pckdiv |= (divisor << MXC_F_GCR_PCKDIV_ADCFRQ_POS);
    // Enable done interrupt
    MXC_ADC->intr = MXC_F_ADC_INTR_DONE_IE;
    // Power up the ADC
    MXC_ADC->ctrl |= MXC_F_ADC_CTRL_PWR;
    MXC_ADC->ctrl |= MXC_F_ADC_CTRL_REFBUF_PWR;
Martin Ling's avatar
Martin Ling committed
121
    MXC_ADC->ctrl |= MXC_F_ADC_CTRL_CHGPUMP_PWR;
schneider's avatar
schneider committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    
    while ((MXC_ADC->intr & MXC_F_ADC_INTR_REF_READY_IF)>>MXC_F_ADC_INTR_REF_READY_IF_POS);
    ADC_ClearFlags(MXC_F_ADC_INTR_REF_READY_IF);
    
    return E_NO_ERROR;
}

// ********************************* Function to start ADC Conversion ****************************************
void ADC_StartConvert(mxc_adc_chsel_t channel, unsigned int adc_scale, unsigned int ref_scale)
{
    uint32_t ctrl_tmp;
    
    // Clear the ADC done flag
    ADC_ClearFlags(MXC_F_ADC_INTR_DONE_IF);
    
    // Clear and insert channel selection
    ctrl_tmp = MXC_ADC->ctrl;
    ctrl_tmp &= ~(MXC_F_ADC_CTRL_CH_SEL | MXC_F_ADC_CTRL_REF_SCALE | MXC_F_ADC_CTRL_SCALE);
    ctrl_tmp |= ((channel << MXC_F_ADC_CTRL_CH_SEL_POS) & MXC_F_ADC_CTRL_CH_SEL);
    
    // Finalize user-requested channel configuration
    if (adc_scale || (channel > ADC_CH_3)) {
        ctrl_tmp |= MXC_F_ADC_CTRL_SCALE;
    }
    
    // Write this configuration
    MXC_ADC->ctrl = ctrl_tmp;
    
    // Start conversion
    MXC_ADC->ctrl |= MXC_F_ADC_CTRL_START;
}

// ************************************* Function to Read ADC Data *******************************************
int ADC_GetData(uint16_t *outdata)
{
    // See if a conversion is in process
    if (MXC_ADC->status & MXC_F_ADC_STATUS_ACTIVE) {
        // Wait for conversion to complete
        while ((MXC_ADC->intr & MXC_F_ADC_INTR_DONE_IF) == 0);
    }
    
    // Read 32-bit value and truncate to 16-bit for output depending on data align bit
    if ((MXC_ADC->ctrl & MXC_F_ADC_CTRL_DATA_ALIGN) == 0) {
        *outdata = (uint16_t)(MXC_ADC->data);    /* LSB justified */
    } else {
        *outdata = (uint16_t)(MXC_ADC->data >> 6);    /* MSB justified */
    }
    
    // Check for overflow
    if (MXC_ADC->status & MXC_F_ADC_STATUS_OVERFLOW) {
        return E_OVERFLOW;
    }
    
    return E_NO_ERROR;
}

// ************************************* Function to Set ADC Limit *******************************************
int ADC_SetLimit(mxc_adc_limitsel_t unit, mxc_adc_chsel_t channel, unsigned int low_enable,
                 unsigned int low_limit, unsigned int high_enable, unsigned int high_limit)
{
    // Check args
    if (unit >= ADC_LIMIT_MAX) {
        return E_BAD_PARAM;
    }
    
    // set channel using the limit
    MXC_ADC->limit[unit] = ((channel << MXC_F_ADC_LIMIT_CH_SEL_POS) & MXC_F_ADC_LIMIT_CH_SEL);
    
    // enable/disable the limit
    if (low_enable) {
        MXC_ADC->limit[unit] |= MXC_F_ADC_LIMIT_CH_LO_LIMIT_EN |
                                ((low_limit << MXC_F_ADC_LIMIT_CH_LO_LIMIT_POS) & MXC_F_ADC_LIMIT_CH_LO_LIMIT);
    } else {
        MXC_ADC->limit[unit] &= ~MXC_F_ADC_LIMIT_CH_LO_LIMIT_EN;
    }
    
    if (high_enable) {
        MXC_ADC->limit[unit] |= MXC_F_ADC_LIMIT_CH_HI_LIMIT_EN |
                                ((high_limit << MXC_F_ADC_LIMIT_CH_HI_LIMIT_POS) & MXC_F_ADC_LIMIT_CH_HI_LIMIT);
    } else {
        MXC_ADC->limit[unit] &= ~MXC_F_ADC_LIMIT_CH_HI_LIMIT_EN;
    }
    
    return E_NO_ERROR;
}