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