mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
511:532f83b66a7f
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/

ignore target files not supported by the yotta module

Who changed what in which revision?

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