mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
296:ec1b66a3d094
test with CLOCK_SETUP = 0

Who changed what in which revision?

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