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:
Thu Mar 12 14:30:49 2015 +0000
Revision:
489:119543c9f674
Parent:
414:4ec4c5b614b0
Synchronized with git revision 051854181516992fb498d51f9ee6e70cbad9e083

Full URL: https://github.com/mbedmicro/mbed/commit/051854181516992fb498d51f9ee6e70cbad9e083/

Fix ksdk mcu HAL - stopbit

Who changed what in which revision?

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