5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
jacobjohnson
Date:
Mon Feb 27 17:45:05 2017 +0000
Revision:
1:f30bdcd2b33b
Parent:
0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c.  This will need to be changed later, and accessed from the main level, but for now this allows the  adc to read a value from 0 to 3.7V, instead of just up to 1V.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-onsemi 0:098463de4c5d 1 /**
group-onsemi 0:098463de4c5d 2 ******************************************************************************
group-onsemi 0:098463de4c5d 3 * @file adc_sar.c
group-onsemi 0:098463de4c5d 4 * @brief Implementation of a SAR ADC driver
group-onsemi 0:098463de4c5d 5 * @internal
group-onsemi 0:098463de4c5d 6 * @author ON Semiconductor
group-onsemi 0:098463de4c5d 7 * $Rev:
group-onsemi 0:098463de4c5d 8 * $Date:
group-onsemi 0:098463de4c5d 9 ******************************************************************************
jacobjohnson 1:f30bdcd2b33b 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a �ON Semiconductor�).
group-onsemi 0:098463de4c5d 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
group-onsemi 0:098463de4c5d 12 * under limited terms and conditions. The terms and conditions pertaining to the software
group-onsemi 0:098463de4c5d 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
jacobjohnson 1:f30bdcd2b33b 14 * (�ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software�) and
group-onsemi 0:098463de4c5d 15 * if applicable the software license agreement. Do not use this software and/or
group-onsemi 0:098463de4c5d 16 * documentation unless you have carefully read and you agree to the limited terms and
group-onsemi 0:098463de4c5d 17 * conditions. By using this software and/or documentation, you agree to the limited
group-onsemi 0:098463de4c5d 18 * terms and conditions.
group-onsemi 0:098463de4c5d 19 *
group-onsemi 0:098463de4c5d 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
group-onsemi 0:098463de4c5d 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
group-onsemi 0:098463de4c5d 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
group-onsemi 0:098463de4c5d 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
group-onsemi 0:098463de4c5d 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
group-onsemi 0:098463de4c5d 25 * @endinternal
group-onsemi 0:098463de4c5d 26 *
group-onsemi 0:098463de4c5d 27 * @ingroup adc_sar
group-onsemi 0:098463de4c5d 28 *
group-onsemi 0:098463de4c5d 29 * @details
group-onsemi 0:098463de4c5d 30 * <p>
group-onsemi 0:098463de4c5d 31 * </p>
group-onsemi 0:098463de4c5d 32 *
group-onsemi 0:098463de4c5d 33 */
group-onsemi 0:098463de4c5d 34
group-onsemi 0:098463de4c5d 35 #include "device.h"
group-onsemi 0:098463de4c5d 36 #include "analogin_api.h"
group-onsemi 0:098463de4c5d 37 #include "PeripheralPins.h"
group-onsemi 0:098463de4c5d 38 #include "mbed_assert.h"
group-onsemi 0:098463de4c5d 39 #include "clock.h"
group-onsemi 0:098463de4c5d 40 #include "adc_sar.h"
group-onsemi 0:098463de4c5d 41
group-onsemi 0:098463de4c5d 42 #if DEVICE_ANALOGIN
group-onsemi 0:098463de4c5d 43
group-onsemi 0:098463de4c5d 44 /**
group-onsemi 0:098463de4c5d 45 * \defgroup hal_analogin Analogin hal functions
group-onsemi 0:098463de4c5d 46 * @{
group-onsemi 0:098463de4c5d 47 */
group-onsemi 0:098463de4c5d 48
group-onsemi 0:098463de4c5d 49 /** Initialize the analogin peripheral
group-onsemi 0:098463de4c5d 50 *
group-onsemi 0:098463de4c5d 51 * Configures the pin used by analogin.
group-onsemi 0:098463de4c5d 52 * @param obj The analogin object to initialize
group-onsemi 0:098463de4c5d 53 * @param pin The analogin pin name
group-onsemi 0:098463de4c5d 54 */
group-onsemi 0:098463de4c5d 55 void analogin_init(analogin_t *obj, PinName pin)
group-onsemi 0:098463de4c5d 56 {
group-onsemi 0:098463de4c5d 57 CLOCK_ENABLE(CLOCK_ADC);
group-onsemi 0:098463de4c5d 58 ADCName adc;
group-onsemi 0:098463de4c5d 59 uint8_t adc_pin = 0;
group-onsemi 0:098463de4c5d 60
group-onsemi 0:098463de4c5d 61 adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
group-onsemi 0:098463de4c5d 62 MBED_ASSERT(adc != (ADCName)NC);
group-onsemi 0:098463de4c5d 63
group-onsemi 0:098463de4c5d 64 obj->adcReg = (AdcReg_pt)adc;
group-onsemi 0:098463de4c5d 65 obj->pin = pin;
group-onsemi 0:098463de4c5d 66 obj->pinFlag = 1;
group-onsemi 0:098463de4c5d 67 obj->ADC_Offset_Value = TRIMREG->ADC_OFFSET_TRIM;
group-onsemi 0:098463de4c5d 68
group-onsemi 0:098463de4c5d 69 switch (pin) {
group-onsemi 0:098463de4c5d 70 case A0:
group-onsemi 0:098463de4c5d 71 adc_pin=0;
group-onsemi 0:098463de4c5d 72 break;
group-onsemi 0:098463de4c5d 73 case A1:
group-onsemi 0:098463de4c5d 74 adc_pin = 1;
group-onsemi 0:098463de4c5d 75 break;
group-onsemi 0:098463de4c5d 76 case A2:
group-onsemi 0:098463de4c5d 77 adc_pin = 2;
group-onsemi 0:098463de4c5d 78 break;
group-onsemi 0:098463de4c5d 79 case A3:
group-onsemi 0:098463de4c5d 80 adc_pin = 3;
group-onsemi 0:098463de4c5d 81 break;
group-onsemi 0:098463de4c5d 82 default:
group-onsemi 0:098463de4c5d 83 break;
group-onsemi 0:098463de4c5d 84 }
group-onsemi 0:098463de4c5d 85
group-onsemi 0:098463de4c5d 86 /* If no config parameters are passed on; assume default value */
group-onsemi 0:098463de4c5d 87 if (obj->adcConf == Null) {
group-onsemi 0:098463de4c5d 88 /* Single sample, absolute conversion, scale = 1 */
group-onsemi 0:098463de4c5d 89 obj->adcReg->CONTROL.WORD = ((0 << ADC_CONTROL_MODE_BIT_POS) |
group-onsemi 0:098463de4c5d 90 (1 << ADC_CONTROL_MEASTYPE_BIT_POS) |
jacobjohnson 1:f30bdcd2b33b 91 (7 << ADC_CONTROL_INPUTSCALE_BIT_POS) |
group-onsemi 0:098463de4c5d 92 (((uint8_t)adc_pin) << ADC_CONTROL_MEAS_CH_BIT_POS));
group-onsemi 0:098463de4c5d 93
group-onsemi 0:098463de4c5d 94 /* Prescaler enabled; set to 7 */
group-onsemi 0:098463de4c5d 95 obj->adcReg->PRESCALE.WORD = ((True << ADC_PRESCALE_EN_BIT_POS) |
group-onsemi 0:098463de4c5d 96 (7 << ADC_PRESCALE_VAL_BIT_POS)); /* ADC clock = 32MHz/(Prescale + 1) */
group-onsemi 0:098463de4c5d 97
group-onsemi 0:098463de4c5d 98 obj->adcReg->DELAY.WORD = ((0x34 << ADC_DELAY_SAMPLE_RATE_BIT_POS) | /** 25 uS Sets the sample rate in units of PCLKperiod * (Prescale + 1). */
group-onsemi 0:098463de4c5d 99 (0x05 << ADC_DELAY_WARMUP_BIT_POS) | /** 12.5 uS Sets the measure time in units of PCLKperiod * (Prescale + 1). */
group-onsemi 0:098463de4c5d 100 (0x1A << ADC_DELAY_SAMPLE_TIME_BIT_POS)); /** 2.5 uS Sets the warm-up time in units of PCLKperiod * (Prescale + 1). */
group-onsemi 0:098463de4c5d 101
group-onsemi 0:098463de4c5d 102 obj->adcReg->IR = 0; /** No interrupt generated */
group-onsemi 0:098463de4c5d 103 } else {
group-onsemi 0:098463de4c5d 104 obj->adcConf->convCh = adc_pin;
group-onsemi 0:098463de4c5d 105
group-onsemi 0:098463de4c5d 106 /* ADC register settings */
group-onsemi 0:098463de4c5d 107 if((obj->adcConf->measurementType) == ADC_RELATIVE_MEAS) {
group-onsemi 0:098463de4c5d 108 obj->adcReg->CONTROL.WORD = ((obj->adcConf->mode << ADC_CONTROL_MODE_BIT_POS) |
group-onsemi 0:098463de4c5d 109 (obj->adcConf->measurementType << ADC_CONTROL_MEASTYPE_BIT_POS) |
group-onsemi 0:098463de4c5d 110 (obj->adcConf->inputScale << ADC_CONTROL_INPUTSCALE_BIT_POS) |
group-onsemi 0:098463de4c5d 111 (obj->adcConf->convCh << ADC_CONTROL_MEAS_CH_BIT_POS) |
group-onsemi 0:098463de4c5d 112 (obj->adcConf->referenceCh << ADC_CONTROL_REF_CH_BIT_POS));
group-onsemi 0:098463de4c5d 113 } else {
group-onsemi 0:098463de4c5d 114 obj->adcReg->CONTROL.WORD = ((obj->adcConf->mode << ADC_CONTROL_MODE_BIT_POS) |
group-onsemi 0:098463de4c5d 115 (obj->adcConf->measurementType << ADC_CONTROL_MEASTYPE_BIT_POS) |
group-onsemi 0:098463de4c5d 116 (obj->adcConf->inputScale << ADC_CONTROL_INPUTSCALE_BIT_POS) |
group-onsemi 0:098463de4c5d 117 (obj->adcConf->convCh << ADC_CONTROL_MEAS_CH_BIT_POS));
group-onsemi 0:098463de4c5d 118 }
group-onsemi 0:098463de4c5d 119
group-onsemi 0:098463de4c5d 120 obj->adcReg->PRESCALE.WORD = ((True << ADC_PRESCALE_EN_BIT_POS) |
group-onsemi 0:098463de4c5d 121 (obj->adcConf->PrescaleVal << ADC_PRESCALE_VAL_BIT_POS)); /* ADC clock = 32MHz/(Prescale + 1) */
group-onsemi 0:098463de4c5d 122
group-onsemi 0:098463de4c5d 123 obj->adcReg->DELAY.WORD = ((obj->adcConf->samplingRate << ADC_DELAY_SAMPLE_RATE_BIT_POS) | /** 25 uS Sets the sample rate in units of PCLKperiod * (Prescale + 1). */
group-onsemi 0:098463de4c5d 124 (obj->adcConf->WarmUpTime << ADC_DELAY_WARMUP_BIT_POS) | /** 12.5 uS Sets the measure time in units of PCLKperiod * (Prescale + 1). */
group-onsemi 0:098463de4c5d 125 (obj->adcConf->samplingTime << ADC_DELAY_SAMPLE_TIME_BIT_POS)); /** 2.5 uS Sets the warm-up time in units of PCLKperiod * (Prescale + 1). */
group-onsemi 0:098463de4c5d 126
group-onsemi 0:098463de4c5d 127 obj->adcReg->IR = obj->adcConf->interruptConfig; /** Interrupt setting */
group-onsemi 0:098463de4c5d 128 }
group-onsemi 0:098463de4c5d 129
group-onsemi 0:098463de4c5d 130 /* Enable interrupt */
group-onsemi 0:098463de4c5d 131 NVIC_ClearPendingIRQ(Adc_IRQn);
group-onsemi 0:098463de4c5d 132 NVIC_EnableIRQ(Adc_IRQn);
group-onsemi 0:098463de4c5d 133 }
group-onsemi 0:098463de4c5d 134
group-onsemi 0:098463de4c5d 135 /** Read the input voltage, represented as a float in the range [0.0, 1.0]
group-onsemi 0:098463de4c5d 136 *
group-onsemi 0:098463de4c5d 137 * @param obj The analogin object
group-onsemi 0:098463de4c5d 138 * @return A floating value representing the current input voltage
group-onsemi 0:098463de4c5d 139 */
group-onsemi 0:098463de4c5d 140 float analogin_read(analogin_t *obj)
group-onsemi 0:098463de4c5d 141 {
group-onsemi 0:098463de4c5d 142 float retVal = 0.0;
group-onsemi 0:098463de4c5d 143 uint16_t value = analogin_read_u16(obj);
group-onsemi 0:098463de4c5d 144 retVal = (float)value * (1.0f / (float)0x03FF);
group-onsemi 0:098463de4c5d 145 return(retVal);
group-onsemi 0:098463de4c5d 146 }
group-onsemi 0:098463de4c5d 147
group-onsemi 0:098463de4c5d 148 /** Read the value from analogin pin, represented as an unsigned 16bit value
group-onsemi 0:098463de4c5d 149 *
group-onsemi 0:098463de4c5d 150 * @param obj The analogin object
group-onsemi 0:098463de4c5d 151 * @return An unsigned 16bit value representing the current input voltage
group-onsemi 0:098463de4c5d 152 */
group-onsemi 0:098463de4c5d 153 uint16_t analogin_read_u16(analogin_t *obj)
group-onsemi 0:098463de4c5d 154 {
group-onsemi 0:098463de4c5d 155 uint16_t adcData = 0;
group-onsemi 0:098463de4c5d 156 uint8_t adc_pin = 0;
group-onsemi 0:098463de4c5d 157
group-onsemi 0:098463de4c5d 158 CLOCK_ENABLE(CLOCK_ADC);
group-onsemi 0:098463de4c5d 159
group-onsemi 0:098463de4c5d 160 if (obj->pinFlag) {
group-onsemi 0:098463de4c5d 161 switch (obj->pin) {
group-onsemi 0:098463de4c5d 162 case A0:
group-onsemi 0:098463de4c5d 163 adc_pin=0;
group-onsemi 0:098463de4c5d 164 break;
group-onsemi 0:098463de4c5d 165 case A1:
group-onsemi 0:098463de4c5d 166 adc_pin = 1;
group-onsemi 0:098463de4c5d 167 break;
group-onsemi 0:098463de4c5d 168 case A2:
group-onsemi 0:098463de4c5d 169 adc_pin = 2;
group-onsemi 0:098463de4c5d 170 break;
group-onsemi 0:098463de4c5d 171 case A3:
group-onsemi 0:098463de4c5d 172 adc_pin = 3;
group-onsemi 0:098463de4c5d 173 break;
group-onsemi 0:098463de4c5d 174 default:
group-onsemi 0:098463de4c5d 175 break;
group-onsemi 0:098463de4c5d 176 }
group-onsemi 0:098463de4c5d 177
group-onsemi 0:098463de4c5d 178 /* Re initialize the pin configured for ADC read */
group-onsemi 0:098463de4c5d 179 obj->adcReg->CONTROL.BITS.CONV_CH = adc_pin;
group-onsemi 0:098463de4c5d 180 }
group-onsemi 0:098463de4c5d 181
group-onsemi 0:098463de4c5d 182 obj->adcReg->CONTROL.BITS.START_CONV=1; /* Start The Conversion */
group-onsemi 0:098463de4c5d 183
group-onsemi 0:098463de4c5d 184 while((uint32_t)(obj->adcReg->STATUS)!=(uint32_t)1) {
group-onsemi 0:098463de4c5d 185 }
group-onsemi 0:098463de4c5d 186 adcData =(uint16_t)(obj->adcReg->DATA);
group-onsemi 0:098463de4c5d 187
group-onsemi 0:098463de4c5d 188 /* Offset the ADC data with trim value */
group-onsemi 0:098463de4c5d 189 if (obj->ADC_Offset_Value != 0xFFFFFFFF) {
group-onsemi 0:098463de4c5d 190
group-onsemi 0:098463de4c5d 191 if(adcData >= obj->ADC_Offset_Value) {
group-onsemi 0:098463de4c5d 192 adcData -= obj->ADC_Offset_Value;
group-onsemi 0:098463de4c5d 193 }
group-onsemi 0:098463de4c5d 194 }
group-onsemi 0:098463de4c5d 195
group-onsemi 0:098463de4c5d 196 return(adcData);
group-onsemi 0:098463de4c5d 197 }
group-onsemi 0:098463de4c5d 198
group-onsemi 0:098463de4c5d 199 #endif // DEVICE_ANALOGIN
group-onsemi 0:098463de4c5d 200