helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Apr 20 11:31:35 2018 +0100
Revision:
186:9c2029bfadbe
Parent:
172:7d866c31b3c5
Update to latest version of mbed lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 172:7d866c31b3c5 1 /* mbed Microcontroller Library
AnnaBridge 172:7d866c31b3c5 2 * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved
AnnaBridge 172:7d866c31b3c5 3 *
AnnaBridge 172:7d866c31b3c5 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 172:7d866c31b3c5 5 * you may not use this file except in compliance with the License.
AnnaBridge 172:7d866c31b3c5 6 * You may obtain a copy of the License at
AnnaBridge 172:7d866c31b3c5 7 *
AnnaBridge 172:7d866c31b3c5 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 172:7d866c31b3c5 9 *
AnnaBridge 172:7d866c31b3c5 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 172:7d866c31b3c5 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 172:7d866c31b3c5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 172:7d866c31b3c5 13 * See the License for the specific language governing permissions and
AnnaBridge 172:7d866c31b3c5 14 * limitations under the License.
AnnaBridge 172:7d866c31b3c5 15 */
AnnaBridge 172:7d866c31b3c5 16 #include "analogin_api.h"
AnnaBridge 172:7d866c31b3c5 17 #include "PeripheralNames.h"
AnnaBridge 172:7d866c31b3c5 18 #include "pinmap.h"
AnnaBridge 172:7d866c31b3c5 19 #include "mbed_wait_api.h"
AnnaBridge 172:7d866c31b3c5 20
AnnaBridge 172:7d866c31b3c5 21 static uint8_t adc_reset_init = 0; // Is ADC Reset happened yet?
AnnaBridge 172:7d866c31b3c5 22 #define ADC_10BIT_RANGE 0x3FF
AnnaBridge 172:7d866c31b3c5 23
AnnaBridge 172:7d866c31b3c5 24 static const PinMap PinMap_ADC[] = {
AnnaBridge 172:7d866c31b3c5 25 {PA0, ADC_A0, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 26 {PA1, ADC_A1, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 27 {PA2, ADC_A2, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 28 {PA3, ADC_A3, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 29 {PA4, ADC_A4, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 30 {PA5, ADC_A5, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 31 {PA6, ADC_A6, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 32 {PA7, ADC_A7, PIN_DATA(0, 0)},
AnnaBridge 172:7d866c31b3c5 33 {NC, NC, 0}
AnnaBridge 172:7d866c31b3c5 34 };
AnnaBridge 172:7d866c31b3c5 35
AnnaBridge 172:7d866c31b3c5 36 void analogin_init(analogin_t *obj, PinName pin)
AnnaBridge 172:7d866c31b3c5 37 {
AnnaBridge 172:7d866c31b3c5 38 // Check that pin belong to ADC module
AnnaBridge 172:7d866c31b3c5 39 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 40 MBED_ASSERT(obj->adc != (ADCName)NC);
AnnaBridge 172:7d866c31b3c5 41
AnnaBridge 172:7d866c31b3c5 42 // enable clock supply to ADC
AnnaBridge 172:7d866c31b3c5 43 CG_SetFcPeriphA(CG_FC_PERIPH_ADC, ENABLE);
AnnaBridge 172:7d866c31b3c5 44 CG_SetADCClkSupply(ENABLE);
AnnaBridge 172:7d866c31b3c5 45 // Set pin function as ADC
AnnaBridge 172:7d866c31b3c5 46 pinmap_pinout(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 47 if (!adc_reset_init) {
AnnaBridge 172:7d866c31b3c5 48 // Software reset ADC
AnnaBridge 172:7d866c31b3c5 49 ADC_SWReset();
AnnaBridge 172:7d866c31b3c5 50 adc_reset_init = 1;
AnnaBridge 172:7d866c31b3c5 51 }
AnnaBridge 172:7d866c31b3c5 52 // Set sample hold time and pre-scale clock
AnnaBridge 172:7d866c31b3c5 53 ADC_SetClk(ADC_CONVERSION_81_CLOCK, ADC_FC_DIVIDE_LEVEL_8);
AnnaBridge 172:7d866c31b3c5 54 // Set input channel
AnnaBridge 172:7d866c31b3c5 55 ADC_SetInputChannel(obj->adc);
AnnaBridge 172:7d866c31b3c5 56 // Turn VREF on
AnnaBridge 172:7d866c31b3c5 57 ADC_SetVref(ENABLE);
AnnaBridge 172:7d866c31b3c5 58 // Use fixed-channel single conversion mode
AnnaBridge 172:7d866c31b3c5 59 ADC_SetRepeatMode(DISABLE);
AnnaBridge 172:7d866c31b3c5 60 ADC_SetScanMode(DISABLE);
AnnaBridge 172:7d866c31b3c5 61 }
AnnaBridge 172:7d866c31b3c5 62
AnnaBridge 172:7d866c31b3c5 63 uint16_t analogin_read_u16(analogin_t *obj)
AnnaBridge 172:7d866c31b3c5 64 {
AnnaBridge 172:7d866c31b3c5 65 ADC_ResultTypeDef ret;
AnnaBridge 172:7d866c31b3c5 66
AnnaBridge 172:7d866c31b3c5 67 // Assert that ADC channel is valid
AnnaBridge 172:7d866c31b3c5 68 MBED_ASSERT(obj->adc != (ADCName) NC);
AnnaBridge 172:7d866c31b3c5 69
AnnaBridge 172:7d866c31b3c5 70 // Set input channel
AnnaBridge 172:7d866c31b3c5 71 ADC_SetInputChannel(obj->adc);
AnnaBridge 172:7d866c31b3c5 72 // Enable Vref
AnnaBridge 172:7d866c31b3c5 73 ADC_SetVref(ENABLE);
AnnaBridge 172:7d866c31b3c5 74 // Wait at least 3us to ensure the voltage is stable
AnnaBridge 172:7d866c31b3c5 75 wait_us(10U);
AnnaBridge 172:7d866c31b3c5 76 // Start ADC conversion
AnnaBridge 172:7d866c31b3c5 77 ADC_Start();
AnnaBridge 172:7d866c31b3c5 78 // Wait until AD conversion complete
AnnaBridge 172:7d866c31b3c5 79 while (ADC_GetConvertState().Bit.NormalComplete != 1) {
AnnaBridge 172:7d866c31b3c5 80 // Do nothing
AnnaBridge 172:7d866c31b3c5 81 }
AnnaBridge 172:7d866c31b3c5 82 // Convert result
AnnaBridge 172:7d866c31b3c5 83 ret = ADC_GetConvertResult(obj->adc);
AnnaBridge 172:7d866c31b3c5 84 // Disable Vref to go into standby mode
AnnaBridge 172:7d866c31b3c5 85 ADC_SetVref(DISABLE);
AnnaBridge 172:7d866c31b3c5 86 return (uint16_t)ret.ADCResultValue;
AnnaBridge 172:7d866c31b3c5 87 }
AnnaBridge 172:7d866c31b3c5 88
AnnaBridge 172:7d866c31b3c5 89 float analogin_read(analogin_t *obj)
AnnaBridge 172:7d866c31b3c5 90 {
AnnaBridge 172:7d866c31b3c5 91 uint16_t value = analogin_read_u16(obj);
AnnaBridge 172:7d866c31b3c5 92 return (float)(value * (1.0f / (float)ADC_10BIT_RANGE));
AnnaBridge 172:7d866c31b3c5 93 }