mbed library sources

Fork of mbed-src by mbed official

Committer:
todotani
Date:
Fri Sep 05 14:20:33 2014 +0000
Revision:
308:59cc78a25982
Parent:
298:7557d401dbc3
BLE_Health_Thermometer for mbed HRM1017 with BLE library 0.1.0

Who changed what in which revision?

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