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