Mbed for VNG board

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Nov 25 07:15:09 2014 +0000
Revision:
414:4ec4c5b614b0
Parent:
404:cbc7dfcb0ce9
Synchronized with git revision fbc74e874ac089c6cb05add312fb5422be628886

Full URL: https://github.com/mbedmicro/mbed/commit/fbc74e874ac089c6cb05add312fb5422be628886/

Targets: NUCLEOs - Add PeripheralPins files for all nucleo targets

Who changed what in which revision?

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