mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c
Dependents: Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay
Fork of mbed-dev by
Diff: targets/TARGET_STM/TARGET_STM32L0/analogin_api.c
- Revision:
- 171:89b338f31ef1
- Parent:
- 169:e3b6fe271b81
- Child:
- 172:7d866c31b3c5
--- a/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c Thu Aug 03 13:13:39 2017 +0100 +++ b/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c Wed Aug 16 18:27:13 2017 +0100 @@ -36,14 +36,12 @@ #include "mbed_error.h" #include "PeripheralPins.h" -ADC_HandleTypeDef AdcHandle; - int adc_inited = 0; void analogin_init(analogin_t *obj, PinName pin) { uint32_t function = (uint32_t)NC; - obj->adc = (ADCName)NC; + obj->handle.Instance = (ADC_TypeDef *)NC; // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) // are described in PinNames.h and PeripheralPins.c @@ -51,18 +49,18 @@ if (pin < 0xF0) { // Normal channels // Get the peripheral name from the pin and assign it to the object - obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); + obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC); // Get the functions (adc channel) from the pin and assign it to the object function = pinmap_function(pin, PinMap_ADC); // Configure GPIO pinmap_pinout(pin, PinMap_ADC); } else { // Internal channels - obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC_Internal); + obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal); function = pinmap_function(pin, PinMap_ADC_Internal); // No GPIO configuration for internal channels } - MBED_ASSERT(obj->adc != (ADCName)NC); + MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); MBED_ASSERT(function != (uint32_t)NC); obj->channel = STM_PIN_CHANNEL(function); @@ -74,37 +72,36 @@ if (adc_inited == 0) { adc_inited = 1; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - + obj->handle.State = HAL_ADC_STATE_RESET; // Enable ADC clock __ADC1_CLK_ENABLE(); // Configure ADC - AdcHandle.Init.OversamplingMode = DISABLE; - AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; - AdcHandle.Init.Resolution = ADC_RESOLUTION12b; - AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; - AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; - AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; - AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN; - AdcHandle.Init.LowPowerAutoWait = ENABLE; - AdcHandle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz - AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; + obj->handle.Init.OversamplingMode = DISABLE; + obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; + obj->handle.Init.Resolution = ADC_RESOLUTION12b; + obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; + obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; + obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; + obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; + obj->handle.Init.LowPowerAutoWait = ENABLE; + obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz + obj->handle.Init.LowPowerAutoPowerOff = DISABLE; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC"); } // Calibration - HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED); + HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED); - __HAL_ADC_ENABLE(&AdcHandle); + __HAL_ADC_ENABLE(&obj->handle); } } @@ -112,8 +109,6 @@ { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; switch (obj->channel) { @@ -181,13 +176,13 @@ } ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected. - HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); + HAL_ADC_ConfigChannel(&obj->handle, &sConfig); - HAL_ADC_Start(&AdcHandle); // Start conversion + HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value - if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) { - return (HAL_ADC_GetValue(&AdcHandle)); + if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { + return (HAL_ADC_GetValue(&obj->handle)); } else { return 0; }