mbed library sources
Fork of mbed-src by
Revision 444:d80869345ae6, committed 2015-01-14
- Comitter:
- mbed_official
- Date:
- Wed Jan 14 09:30:07 2015 +0000
- Parent:
- 443:ed48b4122bfb
- Child:
- 445:9a3ffe6cfa19
- Commit message:
- Synchronized with git revision 36015c68a8b82eac56ee82cd293af82250ab9dc3
Full URL: https://github.com/mbedmicro/mbed/commit/36015c68a8b82eac56ee82cd293af82250ab9dc3/
NUCLEO_F303RE - Correct ADC initialization
Changed in this revision
targets/hal/TARGET_STM/TARGET_NUCLEO_F303RE/analogin_api.c | Show annotated file Show diff for this revision Revisions of this file |
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F303RE/analogin_api.c Mon Jan 12 08:15:07 2015 +0000 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F303RE/analogin_api.c Wed Jan 14 09:30:07 2015 +0000 @@ -37,13 +37,13 @@ ADC_HandleTypeDef AdcHandle; -int adc1_inited = 0; -int adc2_inited = 0; -int adc3_inited = 0; -int adc4_inited = 0; - void analogin_init(analogin_t *obj, PinName pin) { + static int adc1_inited = 0; + static int adc2_inited = 0; + static int adc3_inited = 0; + static int adc4_inited = 0; + // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); MBED_ASSERT(obj->adc != (ADCName)NC); @@ -54,42 +54,49 @@ // Save pin number for the read function obj->pin = pin; - // The ADC initialization is done only once for each ADC - if ((adc1_inited == 0) || (adc2_inited == 0) || (adc3_inited == 0) || (adc4_inited == 0)) { - if (obj->adc == ADC_1) { - __ADC12_CLK_ENABLE(); - adc1_inited = 1; - } - if (obj->adc == ADC_2) { - __ADC12_CLK_ENABLE(); - adc2_inited = 1; - } - if (obj->adc == ADC_3) { - __ADC34_CLK_ENABLE(); - adc3_inited = 1; - } - if (obj->adc == ADC_4) { - __ADC34_CLK_ENABLE(); - adc4_inited = 1; - } - // Configure ADC - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; - AdcHandle.Init.Resolution = ADC_RESOLUTION12b; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.ScanConvMode = DISABLE; - AdcHandle.Init.EOCSelection = DISABLE; - AdcHandle.Init.LowPowerAutoWait = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfConversion = 1; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfDiscConversion = 0; - AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN; - HAL_ADC_Init(&AdcHandle); + // Check if ADC is already initialized + if ((obj->adc == ADC_1) && adc1_inited) return; + if ((obj->adc == ADC_2) && adc2_inited) return; + if ((obj->adc == ADC_3) && adc3_inited) return; + if ((obj->adc == ADC_4) && adc4_inited) return; + + if (obj->adc == ADC_1) { + __ADC12_CLK_ENABLE(); + adc1_inited = 1; + } + + if (obj->adc == ADC_2) { + __ADC12_CLK_ENABLE(); + adc2_inited = 1; + } + + if (obj->adc == ADC_3) { + __ADC34_CLK_ENABLE(); + adc3_inited = 1; } + + if (obj->adc == ADC_4) { + __ADC34_CLK_ENABLE(); + adc4_inited = 1; + } + + // Configure ADC + AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); + AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; + AdcHandle.Init.Resolution = ADC_RESOLUTION12b; + AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + AdcHandle.Init.ScanConvMode = DISABLE; + AdcHandle.Init.EOCSelection = DISABLE; + AdcHandle.Init.LowPowerAutoWait = DISABLE; + AdcHandle.Init.ContinuousConvMode = DISABLE; + AdcHandle.Init.NbrOfConversion = 1; + AdcHandle.Init.DiscontinuousConvMode = DISABLE; + AdcHandle.Init.NbrOfDiscConversion = 0; + AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; + AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + AdcHandle.Init.DMAContinuousRequests = DISABLE; + AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN; + HAL_ADC_Init(&AdcHandle); } static inline uint16_t adc_read(analogin_t *obj)