Forked.

Fork of mbed-dev by mbed official

Committer:
Dollyparton
Date:
Tue Dec 19 12:50:13 2017 +0000
Revision:
174:ed647f63e28d
Parent:
172:7d866c31b3c5
Added RAW socket.

Who changed what in which revision?

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