|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2014, STMicroelectronics |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 14 | + * may be used to endorse or promote products derived from this software |
| 15 | + * without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | +#include "analogin_api.h" |
| 29 | +#include "wait_api.h" |
| 30 | + |
| 31 | +#if DEVICE_ANALOGIN |
| 32 | + |
| 33 | +#include "cmsis.h" |
| 34 | +#include "pinmap.h" |
| 35 | +#include "error.h" |
| 36 | + |
| 37 | +static const PinMap PinMap_ADC[] = { |
| 38 | + {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0 |
| 39 | + {PA_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1 |
| 40 | + {PA_4, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN4 |
| 41 | + {PB_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN8 |
| 42 | + {PC_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN11 |
| 43 | + {PC_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN10 |
| 44 | + {NC, NC, 0} |
| 45 | +}; |
| 46 | + |
| 47 | +int adc_inited = 0; |
| 48 | + |
| 49 | +void analogin_init(analogin_t *obj, PinName pin) { |
| 50 | + |
| 51 | + ADC_TypeDef *adc; |
| 52 | + ADC_InitTypeDef ADC_InitStructure; |
| 53 | + |
| 54 | + // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object |
| 55 | + obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
| 56 | + |
| 57 | + if (obj->adc == (ADCName)NC) { |
| 58 | + error("ADC pin mapping failed"); |
| 59 | + } |
| 60 | + |
| 61 | + // Configure GPIO |
| 62 | + pinmap_pinout(pin, PinMap_ADC); |
| 63 | + |
| 64 | + // Save pin number for the read function |
| 65 | + obj->pin = pin; |
| 66 | + |
| 67 | + // The ADC initialization is done once |
| 68 | + if (adc_inited == 0) { |
| 69 | + adc_inited = 1; |
| 70 | + |
| 71 | + // Get ADC registers structure address |
| 72 | + adc = (ADC_TypeDef *)(obj->adc); |
| 73 | + |
| 74 | + // Enable ADC clock |
| 75 | + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); |
| 76 | + |
| 77 | + // Configure ADC |
| 78 | + ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; |
| 79 | + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; |
| 80 | + ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; |
| 81 | + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; |
| 82 | + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; |
| 83 | + ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; |
| 84 | + ADC_Init(adc, &ADC_InitStructure); |
| 85 | + |
| 86 | + // Calibrate ADC |
| 87 | + ADC_GetCalibrationFactor(adc); |
| 88 | + |
| 89 | + // Enable ADC |
| 90 | + ADC_Cmd(adc, ENABLE); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +static inline uint16_t adc_read(analogin_t *obj) { |
| 95 | + // Get ADC registers structure address |
| 96 | + ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc); |
| 97 | + |
| 98 | + adc->CHSELR = 0; // Clear all channels first |
| 99 | + |
| 100 | + // Configure ADC channel |
| 101 | + switch (obj->pin) { |
| 102 | + case PA_0: |
| 103 | + ADC_ChannelConfig(adc, ADC_Channel_0, ADC_SampleTime_7_5Cycles); |
| 104 | + break; |
| 105 | + case PA_1: |
| 106 | + ADC_ChannelConfig(adc, ADC_Channel_1, ADC_SampleTime_7_5Cycles); |
| 107 | + break; |
| 108 | + case PA_4: |
| 109 | + ADC_ChannelConfig(adc, ADC_Channel_4, ADC_SampleTime_7_5Cycles); |
| 110 | + break; |
| 111 | + case PB_0: |
| 112 | + ADC_ChannelConfig(adc, ADC_Channel_8, ADC_SampleTime_7_5Cycles); |
| 113 | + break; |
| 114 | + case PC_1: |
| 115 | + ADC_ChannelConfig(adc, ADC_Channel_11, ADC_SampleTime_7_5Cycles); |
| 116 | + break; |
| 117 | + case PC_0: |
| 118 | + ADC_ChannelConfig(adc, ADC_Channel_10, ADC_SampleTime_7_5Cycles); |
| 119 | + break; |
| 120 | + default: |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + while(!ADC_GetFlagStatus(adc, ADC_FLAG_ADRDY)); // Wait ADC ready |
| 125 | + |
| 126 | + ADC_StartOfConversion(adc); // Start conversion |
| 127 | + |
| 128 | + while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion |
| 129 | + |
| 130 | + return(ADC_GetConversionValue(adc)); // Get conversion value |
| 131 | +} |
| 132 | + |
| 133 | +uint16_t analogin_read_u16(analogin_t *obj) { |
| 134 | + return(adc_read(obj)); |
| 135 | +} |
| 136 | + |
| 137 | +float analogin_read(analogin_t *obj) { |
| 138 | + uint16_t value = adc_read(obj); |
| 139 | + return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
| 140 | +} |
| 141 | + |
| 142 | +#endif |
0 commit comments