|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "mbed_assert.h" |
| 17 | +#include "analogin_api.h" |
| 18 | + |
| 19 | +#if DEVICE_ANALOGIN && defined(NXP_LPADC) |
| 20 | + |
| 21 | +#include "cmsis.h" |
| 22 | +#include "pinmap.h" |
| 23 | +#include "gpio_api.h" |
| 24 | +#include "PeripheralNames.h" |
| 25 | +#include "fsl_lpadc.h" |
| 26 | +#include "fsl_power.h" |
| 27 | +#include "PeripheralPins.h" |
| 28 | + |
| 29 | +/* Array of ADC peripheral base address. */ |
| 30 | +static ADC_Type *const adc_addrs[] = ADC_BASE_PTRS; |
| 31 | +extern void ADC_ClockPower_Configuration(void); |
| 32 | + |
| 33 | +#define LPADC_USER_CMDID 1U /* CMD1 */ |
| 34 | + |
| 35 | +void analogin_init(analogin_t *obj, PinName pin) |
| 36 | +{ |
| 37 | + gpio_t gpio; |
| 38 | + |
| 39 | + obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
| 40 | + MBED_ASSERT(obj->adc != (ADCName)NC); |
| 41 | + |
| 42 | + uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT; |
| 43 | + lpadc_config_t adc_config; |
| 44 | + uint32_t reg; |
| 45 | + uint32_t pin_number = pin & 0x1F; |
| 46 | + uint8_t port_number = pin / 32; |
| 47 | + |
| 48 | + ADC_ClockPower_Configuration(); |
| 49 | + |
| 50 | + LPADC_GetDefaultConfig(&adc_config); |
| 51 | + adc_config.enableAnalogPreliminary = true; |
| 52 | +#if defined(LPADC_VREF_SOURCE) |
| 53 | + adc_config.referenceVoltageSource = LPADC_VREF_SOURCE; |
| 54 | +#endif |
| 55 | +#if defined(FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS) && FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS |
| 56 | + adc_config.conversionAverageMode = kLPADC_ConversionAverage128; |
| 57 | +#endif /* FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS */ |
| 58 | + LPADC_Init(adc_addrs[instance], &adc_config); |
| 59 | + |
| 60 | +#if defined(FSL_FEATURE_LPADC_HAS_CTRL_CALOFS) && FSL_FEATURE_LPADC_HAS_CTRL_CALOFS |
| 61 | +#if defined(FSL_FEATURE_LPADC_HAS_OFSTRIM) && FSL_FEATURE_LPADC_HAS_OFSTRIM |
| 62 | + /* Request offset calibration. */ |
| 63 | + if (true == LPADC_DO_OFFSET_CALIBRATION) { |
| 64 | + LPADC_DoOffsetCalibration(adc_addrs[instance]); |
| 65 | + } else { |
| 66 | + LPADC_SetOffsetValue(adc_addrs[instance], LPADC_OFFSET_VALUE_A, LPADC_OFFSET_VALUE_B); |
| 67 | + } |
| 68 | +#endif /* FSL_FEATURE_LPADC_HAS_OFSTRIM */ |
| 69 | + /* Request gain calibration. */ |
| 70 | + LPADC_DoAutoCalibration(adc_addrs[instance]); |
| 71 | +#endif /* FSL_FEATURE_LPADC_HAS_CTRL_CALOFS */ |
| 72 | + |
| 73 | +#if (defined(FSL_FEATURE_LPADC_HAS_CFG_CALOFS) && FSL_FEATURE_LPADC_HAS_CFG_CALOFS) |
| 74 | + /* Do auto calibration. */ |
| 75 | + LPADC_DoAutoCalibration(adc_addrs[instance]); |
| 76 | +#endif /* FSL_FEATURE_LPADC_HAS_CFG_CALOFS */ |
| 77 | + |
| 78 | + pinmap_pinout(pin, PinMap_ADC); |
| 79 | + |
| 80 | + reg = IOCON->PIO[port_number][pin_number]; |
| 81 | + /* Clear the DIGIMODE bit */ |
| 82 | + reg &= ~IOCON_PIO_DIGIMODE_MASK; |
| 83 | + /* For pins PIO0_9, PIO0_11, PIO0_12, PIO0_15, PIO0_18, PIO0_31, PIO1_0 and |
| 84 | + PIO1_9, leave ASW bit at '0' in the related IOCON register. */ |
| 85 | + if (((port_number == 0) && ((pin_number == 9) || (pin_number == 11) || (pin_number == 12) || |
| 86 | + (pin_number == 15) || (pin_number == 18) || (pin_number == 31))) || |
| 87 | + ((port_number == 1) && ((pin_number == 0) || (pin_number == 9)))) { |
| 88 | + /* Disable Analog Switch Input control */ |
| 89 | + reg &= ~IOCON_PIO_ASW_MASK; |
| 90 | + } else { |
| 91 | + /* Enable Analog Switch Input control */ |
| 92 | + reg |= IOCON_PIO_ASW_MASK; |
| 93 | + } |
| 94 | + IOCON->PIO[port_number][pin_number] = reg; |
| 95 | + |
| 96 | + /* Need to ensure the pin is in input mode */ |
| 97 | + gpio_init(&gpio, pin); |
| 98 | + gpio_dir(&gpio, PIN_INPUT); |
| 99 | +} |
| 100 | + |
| 101 | +uint16_t analogin_read_u16(analogin_t *obj) |
| 102 | +{ |
| 103 | + uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT; |
| 104 | + uint32_t channel = obj->adc & 0xF; |
| 105 | + lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct; |
| 106 | + lpadc_conv_command_config_t mLpadcCommandConfigStruct; |
| 107 | + lpadc_conv_result_t mLpadcResultConfigStruct; |
| 108 | + |
| 109 | + memset(&mLpadcTriggerConfigStruct, 0, sizeof(mLpadcTriggerConfigStruct)); |
| 110 | + memset(&mLpadcCommandConfigStruct, 0, sizeof(mLpadcCommandConfigStruct)); |
| 111 | + memset(&mLpadcResultConfigStruct, 0, sizeof(mLpadcResultConfigStruct)); |
| 112 | + |
| 113 | + /* Set conversion CMD configuration. */ |
| 114 | + LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct); |
| 115 | + mLpadcCommandConfigStruct.channelNumber = channel; |
| 116 | + LPADC_SetConvCommandConfig(adc_addrs[instance], LPADC_USER_CMDID, &mLpadcCommandConfigStruct); |
| 117 | + |
| 118 | + /* Set trigger configuration. */ |
| 119 | + LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct); |
| 120 | + mLpadcTriggerConfigStruct.targetCommandId = LPADC_USER_CMDID; |
| 121 | + mLpadcTriggerConfigStruct.enableHardwareTrigger = false; |
| 122 | + LPADC_SetConvTriggerConfig(adc_addrs[instance], 0U, &mLpadcTriggerConfigStruct); /* Configurate the trigger0. */ |
| 123 | + |
| 124 | + LPADC_DoSoftwareTrigger(adc_addrs[instance], 1U); /* 1U is trigger0 mask. */ |
| 125 | + |
| 126 | +#if (defined(FSL_FEATURE_LPADC_FIFO_COUNT) && (FSL_FEATURE_LPADC_FIFO_COUNT == 2U)) |
| 127 | + while (!LPADC_GetConvResult(adc_addrs[instance], &mLpadcResultConfigStruct, 0U)) { |
| 128 | + } |
| 129 | +#else |
| 130 | + while (!LPADC_GetConvResult(adc_addrs[instance], &mLpadcResultConfigStruct)) { |
| 131 | + } |
| 132 | +#endif /* FSL_FEATURE_LPADC_FIFO_COUNT */ |
| 133 | + |
| 134 | + return ((mLpadcResultConfigStruct.convValue) >> 3U); |
| 135 | +} |
| 136 | + |
| 137 | +float analogin_read(analogin_t *obj) |
| 138 | +{ |
| 139 | + uint16_t value = analogin_read_u16(obj); |
| 140 | + return (float)value * (1.0f / (float)0xFFFF); |
| 141 | +} |
| 142 | + |
| 143 | +const PinMap *analogin_pinmap() |
| 144 | +{ |
| 145 | + return PinMap_ADC; |
| 146 | +} |
| 147 | + |
| 148 | +#endif |
0 commit comments