mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 24 14:45:08 2014 +0100
Revision:
237:f3da66175598
Child:
298:7557d401dbc3
Synchronized with git revision 8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7

Full URL: https://github.com/mbedmicro/mbed/commit/8ef659bca81f12dfc896b5a7af7c2abbd1a1b8b7/

[NUCLEO_F334R8] Add platform files

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 237:f3da66175598 36
mbed_official 237:f3da66175598 37 static const PinMap PinMap_ADC[] = {
mbed_official 237:f3da66175598 38 {PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN1 - ARDUINO
mbed_official 237:f3da66175598 39 {PA_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN2 - ARDUINO
mbed_official 237:f3da66175598 40 {PA_2, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN3
mbed_official 237:f3da66175598 41 {PA_3, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN4
mbed_official 237:f3da66175598 42 {PA_4, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN1 - ARDUINO
mbed_official 237:f3da66175598 43 {PA_5, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN2
mbed_official 237:f3da66175598 44 {PA_6, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN3
mbed_official 237:f3da66175598 45 {PA_7, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN4
mbed_official 237:f3da66175598 46
mbed_official 237:f3da66175598 47 {PB_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN11 - ARDUINO
mbed_official 237:f3da66175598 48 {PB_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN12
mbed_official 237:f3da66175598 49 {PB_2, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN12
mbed_official 237:f3da66175598 50 {PB_12, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN13
mbed_official 237:f3da66175598 51 {PB_13, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN13
mbed_official 237:f3da66175598 52 {PB_14, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN14
mbed_official 237:f3da66175598 53 {PB_15, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN15
mbed_official 237:f3da66175598 54
mbed_official 237:f3da66175598 55 {PC_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN6 - ARDUINO
mbed_official 237:f3da66175598 56 {PC_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN7 - ARDUINO
mbed_official 237:f3da66175598 57 {PC_2, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN8
mbed_official 237:f3da66175598 58 {PC_3, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN9
mbed_official 237:f3da66175598 59 {PC_4, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN5
mbed_official 237:f3da66175598 60 {PC_5, ADC_2, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC2_IN11
mbed_official 237:f3da66175598 61 {NC, NC, 0}
mbed_official 237:f3da66175598 62 };
mbed_official 237:f3da66175598 63
mbed_official 237:f3da66175598 64 ADC_HandleTypeDef AdcHandle;
mbed_official 237:f3da66175598 65
mbed_official 237:f3da66175598 66 int adc_inited = 0;
mbed_official 237:f3da66175598 67
mbed_official 237:f3da66175598 68 void analogin_init(analogin_t *obj, PinName pin)
mbed_official 237:f3da66175598 69 {
mbed_official 237:f3da66175598 70 // Get the peripheral name from the pin and assign it to the object
mbed_official 237:f3da66175598 71 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 237:f3da66175598 72 MBED_ASSERT(obj->adc != (ADCName)NC);
mbed_official 237:f3da66175598 73
mbed_official 237:f3da66175598 74 // Configure GPIO
mbed_official 237:f3da66175598 75 pinmap_pinout(pin, PinMap_ADC);
mbed_official 237:f3da66175598 76
mbed_official 237:f3da66175598 77 // Save pin number for the read function
mbed_official 237:f3da66175598 78 obj->pin = pin;
mbed_official 237:f3da66175598 79
mbed_official 237:f3da66175598 80 // The ADC initialization is done once
mbed_official 237:f3da66175598 81 if (adc_inited == 0) {
mbed_official 237:f3da66175598 82 adc_inited = 1;
mbed_official 237:f3da66175598 83
mbed_official 237:f3da66175598 84 // Enable ADC clock
mbed_official 237:f3da66175598 85 if (obj->adc == ADC_1) __ADC1_CLK_ENABLE();
mbed_official 237:f3da66175598 86 if (obj->adc == ADC_2) __ADC2_CLK_ENABLE();
mbed_official 237:f3da66175598 87
mbed_official 237:f3da66175598 88 // Configure ADC
mbed_official 237:f3da66175598 89 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
mbed_official 237:f3da66175598 90 AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
mbed_official 237:f3da66175598 91 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
mbed_official 237:f3da66175598 92 AdcHandle.Init.ScanConvMode = DISABLE;
mbed_official 237:f3da66175598 93 AdcHandle.Init.ContinuousConvMode = DISABLE;
mbed_official 237:f3da66175598 94 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
mbed_official 237:f3da66175598 95 AdcHandle.Init.NbrOfDiscConversion = 0;
mbed_official 237:f3da66175598 96 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
mbed_official 237:f3da66175598 97 AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
mbed_official 237:f3da66175598 98 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
mbed_official 237:f3da66175598 99 AdcHandle.Init.NbrOfConversion = 1;
mbed_official 237:f3da66175598 100 AdcHandle.Init.DMAContinuousRequests = DISABLE;
mbed_official 237:f3da66175598 101 AdcHandle.Init.EOCSelection = DISABLE;
mbed_official 237:f3da66175598 102 HAL_ADC_Init(&AdcHandle);
mbed_official 237:f3da66175598 103 }
mbed_official 237:f3da66175598 104 }
mbed_official 237:f3da66175598 105
mbed_official 237:f3da66175598 106 static inline uint16_t adc_read(analogin_t *obj)
mbed_official 237:f3da66175598 107 {
mbed_official 237:f3da66175598 108 ADC_ChannelConfTypeDef sConfig;
mbed_official 237:f3da66175598 109
mbed_official 237:f3da66175598 110 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
mbed_official 237:f3da66175598 111
mbed_official 237:f3da66175598 112 // Configure ADC channel
mbed_official 237:f3da66175598 113 sConfig.Rank = 1;
mbed_official 237:f3da66175598 114 sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES_5;
mbed_official 237:f3da66175598 115 sConfig.SingleDiff = ADC_SINGLE_ENDED;
mbed_official 237:f3da66175598 116 sConfig.OffsetNumber = ADC_OFFSET_NONE;
mbed_official 237:f3da66175598 117 sConfig.Offset = 0;
mbed_official 237:f3da66175598 118
mbed_official 237:f3da66175598 119 switch (obj->pin) {
mbed_official 237:f3da66175598 120 case PA_0:
mbed_official 237:f3da66175598 121 sConfig.Channel = ADC_CHANNEL_1;
mbed_official 237:f3da66175598 122 break;
mbed_official 237:f3da66175598 123 case PA_1:
mbed_official 237:f3da66175598 124 sConfig.Channel = ADC_CHANNEL_2;
mbed_official 237:f3da66175598 125 break;
mbed_official 237:f3da66175598 126 case PA_2:
mbed_official 237:f3da66175598 127 sConfig.Channel = ADC_CHANNEL_3;
mbed_official 237:f3da66175598 128 break;
mbed_official 237:f3da66175598 129 case PA_3:
mbed_official 237:f3da66175598 130 sConfig.Channel = ADC_CHANNEL_4;
mbed_official 237:f3da66175598 131 break;
mbed_official 237:f3da66175598 132 case PA_4:
mbed_official 237:f3da66175598 133 sConfig.Channel = ADC_CHANNEL_1;
mbed_official 237:f3da66175598 134 break;
mbed_official 237:f3da66175598 135 case PA_5:
mbed_official 237:f3da66175598 136 sConfig.Channel = ADC_CHANNEL_2;
mbed_official 237:f3da66175598 137 break;
mbed_official 237:f3da66175598 138 case PA_6:
mbed_official 237:f3da66175598 139 sConfig.Channel = ADC_CHANNEL_3;
mbed_official 237:f3da66175598 140 break;
mbed_official 237:f3da66175598 141 case PA_7:
mbed_official 237:f3da66175598 142 sConfig.Channel = ADC_CHANNEL_4;
mbed_official 237:f3da66175598 143 break;
mbed_official 237:f3da66175598 144 case PB_0:
mbed_official 237:f3da66175598 145 sConfig.Channel = ADC_CHANNEL_11;
mbed_official 237:f3da66175598 146 break;
mbed_official 237:f3da66175598 147 case PB_1:
mbed_official 237:f3da66175598 148 sConfig.Channel = ADC_CHANNEL_12;
mbed_official 237:f3da66175598 149 break;
mbed_official 237:f3da66175598 150 case PB_2:
mbed_official 237:f3da66175598 151 sConfig.Channel = ADC_CHANNEL_12;
mbed_official 237:f3da66175598 152 break;
mbed_official 237:f3da66175598 153 case PB_12:
mbed_official 237:f3da66175598 154 sConfig.Channel = ADC_CHANNEL_13;
mbed_official 237:f3da66175598 155 break;
mbed_official 237:f3da66175598 156 case PB_13:
mbed_official 237:f3da66175598 157 sConfig.Channel = ADC_CHANNEL_13;
mbed_official 237:f3da66175598 158 break;
mbed_official 237:f3da66175598 159 case PB_14:
mbed_official 237:f3da66175598 160 sConfig.Channel = ADC_CHANNEL_14;
mbed_official 237:f3da66175598 161 break;
mbed_official 237:f3da66175598 162 case PB_15:
mbed_official 237:f3da66175598 163 sConfig.Channel = ADC_CHANNEL_15;
mbed_official 237:f3da66175598 164 break;
mbed_official 237:f3da66175598 165 case PC_0:
mbed_official 237:f3da66175598 166 sConfig.Channel = ADC_CHANNEL_6;
mbed_official 237:f3da66175598 167 break;
mbed_official 237:f3da66175598 168 case PC_1:
mbed_official 237:f3da66175598 169 sConfig.Channel = ADC_CHANNEL_7;
mbed_official 237:f3da66175598 170 break;
mbed_official 237:f3da66175598 171 case PC_2:
mbed_official 237:f3da66175598 172 sConfig.Channel = ADC_CHANNEL_8;
mbed_official 237:f3da66175598 173 break;
mbed_official 237:f3da66175598 174 case PC_3:
mbed_official 237:f3da66175598 175 sConfig.Channel = ADC_CHANNEL_9;
mbed_official 237:f3da66175598 176 break;
mbed_official 237:f3da66175598 177 case PC_4:
mbed_official 237:f3da66175598 178 sConfig.Channel = ADC_CHANNEL_5;
mbed_official 237:f3da66175598 179 break;
mbed_official 237:f3da66175598 180 case PC_5:
mbed_official 237:f3da66175598 181 sConfig.Channel = ADC_CHANNEL_11;
mbed_official 237:f3da66175598 182 break;
mbed_official 237:f3da66175598 183 default:
mbed_official 237:f3da66175598 184 return 0;
mbed_official 237:f3da66175598 185 }
mbed_official 237:f3da66175598 186
mbed_official 237:f3da66175598 187 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
mbed_official 237:f3da66175598 188
mbed_official 237:f3da66175598 189 HAL_ADC_Start(&AdcHandle); // Start conversion
mbed_official 237:f3da66175598 190
mbed_official 237:f3da66175598 191 // Wait end of conversion and get value
mbed_official 237:f3da66175598 192 if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
mbed_official 237:f3da66175598 193 return (HAL_ADC_GetValue(&AdcHandle));
mbed_official 237:f3da66175598 194 } else {
mbed_official 237:f3da66175598 195 return 0;
mbed_official 237:f3da66175598 196 }
mbed_official 237:f3da66175598 197 }
mbed_official 237:f3da66175598 198
mbed_official 237:f3da66175598 199 uint16_t analogin_read_u16(analogin_t *obj)
mbed_official 237:f3da66175598 200 {
mbed_official 237:f3da66175598 201 return (adc_read(obj));
mbed_official 237:f3da66175598 202 }
mbed_official 237:f3da66175598 203
mbed_official 237:f3da66175598 204 float analogin_read(analogin_t *obj)
mbed_official 237:f3da66175598 205 {
mbed_official 237:f3da66175598 206 uint16_t value = adc_read(obj);
mbed_official 237:f3da66175598 207 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
mbed_official 237:f3da66175598 208 }
mbed_official 237:f3da66175598 209
mbed_official 237:f3da66175598 210 #endif