Lancaster University / mbed-src

Fork of mbed-src by mbed official

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:52:54 2016 +0100
Revision:
641:be9b2017785a
Parent:
544:1af5f1c39e80
Synchronized with git rev 1fb8ab4c
Author: James Devine
mbed-classic: BUGFIX for timer when using wait_ms from interrupt context

Previously if a user used wait[_ms,_us] in interrupt context the device would
hang indefinitely. This was due to incrementing overflowCount from
interrupt context only.

This meant that if a user used wait[_ms,_us] in an ISR with
the same or greater interrupt priority, it would result in an infinite
loop as the overflowCount variable would never be incremented, and
wait[_ms,_us] would never return.

This patch simply applies a better solution for the race condition
mentioned in the previous commit. It instead disables the timer1
interrupt and increments the overflowCount variable, preventing
the race condition whilst supporting wait[_ms,_us] in interrupt
context.

Who changed what in which revision?

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