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