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:
Tue Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
414:4ec4c5b614b0
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

Who changed what in which revision?

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