Mouse code for the MacroRat
mbed-dev/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2015, STMicroelectronics |
sahilmgandhi | 18:6a4db94011d3 | 3 | * All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 4 | * |
sahilmgandhi | 18:6a4db94011d3 | 5 | * Redistribution and use in source and binary forms, with or without |
sahilmgandhi | 18:6a4db94011d3 | 6 | * modification, are permitted provided that the following conditions are met: |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
sahilmgandhi | 18:6a4db94011d3 | 9 | * this list of conditions and the following disclaimer. |
sahilmgandhi | 18:6a4db94011d3 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
sahilmgandhi | 18:6a4db94011d3 | 11 | * this list of conditions and the following disclaimer in the documentation |
sahilmgandhi | 18:6a4db94011d3 | 12 | * and/or other materials provided with the distribution. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
sahilmgandhi | 18:6a4db94011d3 | 14 | * may be used to endorse or promote products derived from this software |
sahilmgandhi | 18:6a4db94011d3 | 15 | * without specific prior written permission. |
sahilmgandhi | 18:6a4db94011d3 | 16 | * |
sahilmgandhi | 18:6a4db94011d3 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
sahilmgandhi | 18:6a4db94011d3 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
sahilmgandhi | 18:6a4db94011d3 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
sahilmgandhi | 18:6a4db94011d3 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
sahilmgandhi | 18:6a4db94011d3 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
sahilmgandhi | 18:6a4db94011d3 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
sahilmgandhi | 18:6a4db94011d3 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
sahilmgandhi | 18:6a4db94011d3 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
sahilmgandhi | 18:6a4db94011d3 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
sahilmgandhi | 18:6a4db94011d3 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sahilmgandhi | 18:6a4db94011d3 | 27 | */ |
sahilmgandhi | 18:6a4db94011d3 | 28 | #include "mbed_assert.h" |
sahilmgandhi | 18:6a4db94011d3 | 29 | #include "analogin_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | #if DEVICE_ANALOGIN |
sahilmgandhi | 18:6a4db94011d3 | 32 | |
sahilmgandhi | 18:6a4db94011d3 | 33 | #include "mbed_wait_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 34 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 35 | #include "pinmap.h" |
sahilmgandhi | 18:6a4db94011d3 | 36 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 37 | #include "PeripheralPins.h" |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | ADC_HandleTypeDef AdcHandle; |
sahilmgandhi | 18:6a4db94011d3 | 40 | |
sahilmgandhi | 18:6a4db94011d3 | 41 | int adc_inited = 0; |
sahilmgandhi | 18:6a4db94011d3 | 42 | |
sahilmgandhi | 18:6a4db94011d3 | 43 | void analogin_init(analogin_t *obj, PinName pin) |
sahilmgandhi | 18:6a4db94011d3 | 44 | { |
sahilmgandhi | 18:6a4db94011d3 | 45 | // Get the peripheral name from the pin and assign it to the object |
sahilmgandhi | 18:6a4db94011d3 | 46 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
sahilmgandhi | 18:6a4db94011d3 | 47 | MBED_ASSERT(obj->adc != (ADCName)NC); |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | // Get the pin function and assign the used channel to the object |
sahilmgandhi | 18:6a4db94011d3 | 50 | uint32_t function = pinmap_function(pin, PinMap_ADC); |
sahilmgandhi | 18:6a4db94011d3 | 51 | MBED_ASSERT(function != (uint32_t)NC); |
sahilmgandhi | 18:6a4db94011d3 | 52 | obj->channel = STM_PIN_CHANNEL(function); |
sahilmgandhi | 18:6a4db94011d3 | 53 | |
sahilmgandhi | 18:6a4db94011d3 | 54 | // Configure GPIO excepted for internal channels (Temperature, Vref, Vbat, ...) |
sahilmgandhi | 18:6a4db94011d3 | 55 | // ADC Internal Channels "pins" are described in PinNames.h and must have a value >= 0xF0 |
sahilmgandhi | 18:6a4db94011d3 | 56 | if (pin < 0xF0) { |
sahilmgandhi | 18:6a4db94011d3 | 57 | pinmap_pinout(pin, PinMap_ADC); |
sahilmgandhi | 18:6a4db94011d3 | 58 | } |
sahilmgandhi | 18:6a4db94011d3 | 59 | |
sahilmgandhi | 18:6a4db94011d3 | 60 | // Save pin number for the read function |
sahilmgandhi | 18:6a4db94011d3 | 61 | obj->pin = pin; |
sahilmgandhi | 18:6a4db94011d3 | 62 | |
sahilmgandhi | 18:6a4db94011d3 | 63 | // The ADC initialization is done once |
sahilmgandhi | 18:6a4db94011d3 | 64 | if (adc_inited == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 65 | adc_inited = 1; |
sahilmgandhi | 18:6a4db94011d3 | 66 | |
sahilmgandhi | 18:6a4db94011d3 | 67 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
sahilmgandhi | 18:6a4db94011d3 | 68 | |
sahilmgandhi | 18:6a4db94011d3 | 69 | // Enable ADC clock |
sahilmgandhi | 18:6a4db94011d3 | 70 | __ADC1_CLK_ENABLE(); |
sahilmgandhi | 18:6a4db94011d3 | 71 | |
sahilmgandhi | 18:6a4db94011d3 | 72 | // Configure ADC |
sahilmgandhi | 18:6a4db94011d3 | 73 | AdcHandle.Init.OversamplingMode = DISABLE; |
sahilmgandhi | 18:6a4db94011d3 | 74 | AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; |
sahilmgandhi | 18:6a4db94011d3 | 75 | AdcHandle.Init.Resolution = ADC_RESOLUTION12b; |
sahilmgandhi | 18:6a4db94011d3 | 76 | AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_41CYCLES_5; |
sahilmgandhi | 18:6a4db94011d3 | 77 | AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; |
sahilmgandhi | 18:6a4db94011d3 | 78 | AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
sahilmgandhi | 18:6a4db94011d3 | 79 | AdcHandle.Init.ContinuousConvMode = DISABLE; |
sahilmgandhi | 18:6a4db94011d3 | 80 | AdcHandle.Init.DiscontinuousConvMode = DISABLE; |
sahilmgandhi | 18:6a4db94011d3 | 81 | AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; |
sahilmgandhi | 18:6a4db94011d3 | 82 | AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here |
sahilmgandhi | 18:6a4db94011d3 | 83 | AdcHandle.Init.DMAContinuousRequests = DISABLE; |
sahilmgandhi | 18:6a4db94011d3 | 84 | AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; |
sahilmgandhi | 18:6a4db94011d3 | 85 | AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN; |
sahilmgandhi | 18:6a4db94011d3 | 86 | AdcHandle.Init.LowPowerAutoWait = ENABLE; |
sahilmgandhi | 18:6a4db94011d3 | 87 | AdcHandle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz |
sahilmgandhi | 18:6a4db94011d3 | 88 | AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; |
sahilmgandhi | 18:6a4db94011d3 | 89 | |
sahilmgandhi | 18:6a4db94011d3 | 90 | if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { |
sahilmgandhi | 18:6a4db94011d3 | 91 | error("Cannot initialize ADC"); |
sahilmgandhi | 18:6a4db94011d3 | 92 | } |
sahilmgandhi | 18:6a4db94011d3 | 93 | |
sahilmgandhi | 18:6a4db94011d3 | 94 | // Calibration |
sahilmgandhi | 18:6a4db94011d3 | 95 | HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED); |
sahilmgandhi | 18:6a4db94011d3 | 96 | |
sahilmgandhi | 18:6a4db94011d3 | 97 | __HAL_ADC_ENABLE(&AdcHandle); |
sahilmgandhi | 18:6a4db94011d3 | 98 | } |
sahilmgandhi | 18:6a4db94011d3 | 99 | } |
sahilmgandhi | 18:6a4db94011d3 | 100 | |
sahilmgandhi | 18:6a4db94011d3 | 101 | static inline uint16_t adc_read(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 102 | { |
sahilmgandhi | 18:6a4db94011d3 | 103 | ADC_ChannelConfTypeDef sConfig = {0}; |
sahilmgandhi | 18:6a4db94011d3 | 104 | |
sahilmgandhi | 18:6a4db94011d3 | 105 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
sahilmgandhi | 18:6a4db94011d3 | 106 | |
sahilmgandhi | 18:6a4db94011d3 | 107 | // Configure ADC channel |
sahilmgandhi | 18:6a4db94011d3 | 108 | sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; |
sahilmgandhi | 18:6a4db94011d3 | 109 | switch (obj->channel) { |
sahilmgandhi | 18:6a4db94011d3 | 110 | case 0: |
sahilmgandhi | 18:6a4db94011d3 | 111 | sConfig.Channel = ADC_CHANNEL_0; |
sahilmgandhi | 18:6a4db94011d3 | 112 | break; |
sahilmgandhi | 18:6a4db94011d3 | 113 | case 1: |
sahilmgandhi | 18:6a4db94011d3 | 114 | sConfig.Channel = ADC_CHANNEL_1; |
sahilmgandhi | 18:6a4db94011d3 | 115 | break; |
sahilmgandhi | 18:6a4db94011d3 | 116 | case 2: |
sahilmgandhi | 18:6a4db94011d3 | 117 | sConfig.Channel = ADC_CHANNEL_2; |
sahilmgandhi | 18:6a4db94011d3 | 118 | break; |
sahilmgandhi | 18:6a4db94011d3 | 119 | case 3: |
sahilmgandhi | 18:6a4db94011d3 | 120 | sConfig.Channel = ADC_CHANNEL_3; |
sahilmgandhi | 18:6a4db94011d3 | 121 | break; |
sahilmgandhi | 18:6a4db94011d3 | 122 | case 4: |
sahilmgandhi | 18:6a4db94011d3 | 123 | sConfig.Channel = ADC_CHANNEL_4; |
sahilmgandhi | 18:6a4db94011d3 | 124 | break; |
sahilmgandhi | 18:6a4db94011d3 | 125 | case 5: |
sahilmgandhi | 18:6a4db94011d3 | 126 | sConfig.Channel = ADC_CHANNEL_5; |
sahilmgandhi | 18:6a4db94011d3 | 127 | break; |
sahilmgandhi | 18:6a4db94011d3 | 128 | case 6: |
sahilmgandhi | 18:6a4db94011d3 | 129 | sConfig.Channel = ADC_CHANNEL_6; |
sahilmgandhi | 18:6a4db94011d3 | 130 | break; |
sahilmgandhi | 18:6a4db94011d3 | 131 | case 7: |
sahilmgandhi | 18:6a4db94011d3 | 132 | sConfig.Channel = ADC_CHANNEL_7; |
sahilmgandhi | 18:6a4db94011d3 | 133 | break; |
sahilmgandhi | 18:6a4db94011d3 | 134 | case 8: |
sahilmgandhi | 18:6a4db94011d3 | 135 | sConfig.Channel = ADC_CHANNEL_8; |
sahilmgandhi | 18:6a4db94011d3 | 136 | break; |
sahilmgandhi | 18:6a4db94011d3 | 137 | case 9: |
sahilmgandhi | 18:6a4db94011d3 | 138 | sConfig.Channel = ADC_CHANNEL_9; |
sahilmgandhi | 18:6a4db94011d3 | 139 | break; |
sahilmgandhi | 18:6a4db94011d3 | 140 | case 10: |
sahilmgandhi | 18:6a4db94011d3 | 141 | sConfig.Channel = ADC_CHANNEL_10; |
sahilmgandhi | 18:6a4db94011d3 | 142 | break; |
sahilmgandhi | 18:6a4db94011d3 | 143 | case 11: |
sahilmgandhi | 18:6a4db94011d3 | 144 | sConfig.Channel = ADC_CHANNEL_11; |
sahilmgandhi | 18:6a4db94011d3 | 145 | break; |
sahilmgandhi | 18:6a4db94011d3 | 146 | case 12: |
sahilmgandhi | 18:6a4db94011d3 | 147 | sConfig.Channel = ADC_CHANNEL_12; |
sahilmgandhi | 18:6a4db94011d3 | 148 | break; |
sahilmgandhi | 18:6a4db94011d3 | 149 | case 13: |
sahilmgandhi | 18:6a4db94011d3 | 150 | sConfig.Channel = ADC_CHANNEL_13; |
sahilmgandhi | 18:6a4db94011d3 | 151 | break; |
sahilmgandhi | 18:6a4db94011d3 | 152 | case 14: |
sahilmgandhi | 18:6a4db94011d3 | 153 | sConfig.Channel = ADC_CHANNEL_14; |
sahilmgandhi | 18:6a4db94011d3 | 154 | break; |
sahilmgandhi | 18:6a4db94011d3 | 155 | case 15: |
sahilmgandhi | 18:6a4db94011d3 | 156 | sConfig.Channel = ADC_CHANNEL_15; |
sahilmgandhi | 18:6a4db94011d3 | 157 | break; |
sahilmgandhi | 18:6a4db94011d3 | 158 | #ifdef ADC_CHANNEL_VLCD |
sahilmgandhi | 18:6a4db94011d3 | 159 | case 16: |
sahilmgandhi | 18:6a4db94011d3 | 160 | sConfig.Channel = ADC_CHANNEL_VLCD; |
sahilmgandhi | 18:6a4db94011d3 | 161 | break; |
sahilmgandhi | 18:6a4db94011d3 | 162 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 163 | case 17: |
sahilmgandhi | 18:6a4db94011d3 | 164 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
sahilmgandhi | 18:6a4db94011d3 | 165 | break; |
sahilmgandhi | 18:6a4db94011d3 | 166 | case 18: |
sahilmgandhi | 18:6a4db94011d3 | 167 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
sahilmgandhi | 18:6a4db94011d3 | 168 | break; |
sahilmgandhi | 18:6a4db94011d3 | 169 | default: |
sahilmgandhi | 18:6a4db94011d3 | 170 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 171 | } |
sahilmgandhi | 18:6a4db94011d3 | 172 | |
sahilmgandhi | 18:6a4db94011d3 | 173 | ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected. |
sahilmgandhi | 18:6a4db94011d3 | 174 | HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); |
sahilmgandhi | 18:6a4db94011d3 | 175 | |
sahilmgandhi | 18:6a4db94011d3 | 176 | HAL_ADC_Start(&AdcHandle); // Start conversion |
sahilmgandhi | 18:6a4db94011d3 | 177 | |
sahilmgandhi | 18:6a4db94011d3 | 178 | // Wait end of conversion and get value |
sahilmgandhi | 18:6a4db94011d3 | 179 | if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) { |
sahilmgandhi | 18:6a4db94011d3 | 180 | return (HAL_ADC_GetValue(&AdcHandle)); |
sahilmgandhi | 18:6a4db94011d3 | 181 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 182 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 183 | } |
sahilmgandhi | 18:6a4db94011d3 | 184 | } |
sahilmgandhi | 18:6a4db94011d3 | 185 | |
sahilmgandhi | 18:6a4db94011d3 | 186 | uint16_t analogin_read_u16(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 187 | { |
sahilmgandhi | 18:6a4db94011d3 | 188 | uint16_t value = adc_read(obj); |
sahilmgandhi | 18:6a4db94011d3 | 189 | // 12-bit to 16-bit conversion |
sahilmgandhi | 18:6a4db94011d3 | 190 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
sahilmgandhi | 18:6a4db94011d3 | 191 | return value; |
sahilmgandhi | 18:6a4db94011d3 | 192 | } |
sahilmgandhi | 18:6a4db94011d3 | 193 | |
sahilmgandhi | 18:6a4db94011d3 | 194 | float analogin_read(analogin_t *obj) |
sahilmgandhi | 18:6a4db94011d3 | 195 | { |
sahilmgandhi | 18:6a4db94011d3 | 196 | uint16_t value = adc_read(obj); |
sahilmgandhi | 18:6a4db94011d3 | 197 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
sahilmgandhi | 18:6a4db94011d3 | 198 | } |
sahilmgandhi | 18:6a4db94011d3 | 199 | |
sahilmgandhi | 18:6a4db94011d3 | 200 | #endif |