mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Nov 03 10:30:07 2014 +0000
Revision:
381:5460fc57b6e4
Synchronized with git revision 02478cd1f27fc7b9643486472635eb515b2bca81

Full URL: https://github.com/mbedmicro/mbed/commit/02478cd1f27fc7b9643486472635eb515b2bca81/

Target: LPC1549 - Fix serial interrupt issues (issue report #616)

Who changed what in which revision?

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