diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h index e81d727759..45a37ac57d 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/objects.h index cc273c9206..d0f3565f66 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/objects.h index cc273c9206..d0f3565f66 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/objects.h index e83cd368dc..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/objects.h index cc273c9206..d0f3565f66 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h index e83cd368dc..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h index e83cd368dc..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c index 50335b936f..8c4f34c810 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/analogin_api.c @@ -36,15 +36,13 @@ #include "PeripheralPins.h" #include "mbed_error.h" -ADC_HandleTypeDef AdcHandle; - int adc_inited = 0; void analogin_init(analogin_t *obj, PinName pin) { // 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); - + obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); + MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); + // Get the functions (adc channel) from the pin and assign it to the object uint32_t function = pinmap_function(pin, PinMap_ADC); MBED_ASSERT(function != (uint32_t)NC); @@ -67,25 +65,25 @@ void analogin_init(analogin_t *obj, PinName pin) { __ADC1_CLK_ENABLE(); // Configure ADC - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; - AdcHandle.Init.Resolution = ADC_RESOLUTION12b; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; - AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; - AdcHandle.Init.LowPowerAutoWait = DISABLE; - AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + obj->handle.State = HAL_ADC_STATE_RESET; + obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; + obj->handle.Init.Resolution = ADC_RESOLUTION12b; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; + obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; + obj->handle.Init.LowPowerAutoWait = DISABLE; + obj->handle.Init.LowPowerAutoPowerOff = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC"); } // Run the ADC calibration - if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK) { + if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) { error("Cannot Start ADC_Calibration"); } } @@ -94,8 +92,6 @@ void analogin_init(analogin_t *obj, PinName pin) { static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; #if defined (TARGET_STM32F091RC) @@ -169,15 +165,15 @@ static inline uint16_t adc_read(analogin_t *obj) { } // Clear all channels as it is not done in HAL_ADC_ConfigChannel() - AdcHandle.Instance->CHSELR = 0; + obj->handle.Instance->CHSELR = 0; - 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; } diff --git a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h index c0e1bc1dd4..3d11acebd0 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h @@ -111,6 +111,12 @@ struct i2c_s { #endif }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #include "gpio_object.h" #if DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/objects.h b/targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/objects.h index ff3e69e73f..24fa9d1f17 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/objects.h b/targets/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/objects.h index c6411ece64..493408b24f 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/objects.h b/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/objects.h index ff3e69e73f..24fa9d1f17 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F1/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F1/analogin_api.c index 68d72d6251..490410a1a0 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F1/analogin_api.c @@ -35,8 +35,6 @@ #include "pinmap.h" #include "PeripheralPins.h" -ADC_HandleTypeDef AdcHandle; - int adc_inited = 0; void analogin_init(analogin_t *obj, PinName pin) @@ -44,9 +42,9 @@ void analogin_init(analogin_t *obj, PinName pin) RCC_PeriphCLKInitTypeDef PeriphClkInit; // 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); - + obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); + MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); + // Get the functions (adc channel) from the pin and assign it to the object uint32_t function = pinmap_function(pin, PinMap_ADC); MBED_ASSERT(function != (uint32_t)NC); @@ -79,15 +77,15 @@ void analogin_init(analogin_t *obj, PinName pin) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); // Configure ADC - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.ScanConvMode = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfConversion = 1; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfDiscConversion = 0; - AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; - HAL_ADC_Init(&AdcHandle); + obj->handle.State = HAL_ADC_STATE_RESET; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ScanConvMode = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.NbrOfConversion = 1; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.NbrOfDiscConversion = 0; + obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; + HAL_ADC_Init(&obj->handle); } } @@ -95,8 +93,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5; @@ -160,13 +156,13 @@ static inline uint16_t adc_read(analogin_t *obj) return 0; } - 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; } diff --git a/targets/TARGET_STM/TARGET_STM32F1/common_objects.h b/targets/TARGET_STM/TARGET_STM32F1/common_objects.h index 31b80fe17c..12487fcf0b 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F1/common_objects.h @@ -110,6 +110,12 @@ struct i2c_s { #endif }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #include "gpio_object.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F2/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F2/analogin_api.c index ba154c94f1..19c82c2036 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F2/analogin_api.c @@ -36,12 +36,9 @@ #include "mbed_error.h" #include "PeripheralPins.h" -ADC_HandleTypeDef AdcHandle; - void analogin_init(analogin_t *obj, PinName pin) { uint32_t function = (uint32_t)NC; - obj->adc = (ADCName)NC; #if defined(ADC1) static int adc1_inited = 0; @@ -58,18 +55,18 @@ void analogin_init(analogin_t *obj, PinName pin) if ((pin < 0xF0) || (pin >= 0x100)) { // 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); @@ -80,41 +77,41 @@ void analogin_init(analogin_t *obj, PinName pin) // Check if ADC is already initialized // Enable ADC clock #if defined(ADC1) - if ((obj->adc == ADC_1) && adc1_inited) return; - if (obj->adc == ADC_1) { + if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return; + if ((ADCName)obj->handle.Instance == ADC_1) { __ADC1_CLK_ENABLE(); adc1_inited = 1; } #endif #if defined(ADC2) - if ((obj->adc == ADC_2) && adc2_inited) return; - if (obj->adc == ADC_2) { + if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return; + if ((ADCName)obj->handle.Instance == ADC_2) { __ADC2_CLK_ENABLE(); adc2_inited = 1; } #endif #if defined(ADC3) - if ((obj->adc == ADC_3) && adc3_inited) return; - if (obj->adc == ADC_3) { + if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return; + if ((ADCName)obj->handle.Instance == ADC_3) { __ADC3_CLK_ENABLE(); adc3_inited = 1; } #endif // Configure ADC - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; - AdcHandle.Init.Resolution = ADC_RESOLUTION12b; - AdcHandle.Init.ScanConvMode = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfDiscConversion = 0; - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.NbrOfConversion = 1; - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.EOCSelection = DISABLE; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + obj->handle.State = HAL_ADC_STATE_RESET; + obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; + obj->handle.Init.Resolution = ADC_RESOLUTION12b; + obj->handle.Init.ScanConvMode = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.NbrOfDiscConversion = 0; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.NbrOfConversion = 1; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.EOCSelection = DISABLE; + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC\n"); } } @@ -123,8 +120,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; @@ -192,13 +187,13 @@ static inline uint16_t adc_read(analogin_t *obj) return 0; } - 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; } diff --git a/targets/TARGET_STM/TARGET_STM32F2/objects.h b/targets/TARGET_STM/TARGET_STM32F2/objects.h index b2824cacd2..c2489cb851 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F2/objects.h @@ -55,7 +55,7 @@ struct port_s { }; struct analogin_s { - ADCName adc; + ADC_HandleTypeDef handle; PinName pin; uint8_t channel; }; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h index e83cd368dc..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h index e83cd368dc..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h index 1706a1a120..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h @@ -54,13 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; - DAC_HandleTypeDef handle; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h index e83cd368dc..8022de0cb7 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h index de612becfb..90b0f732c1 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #if defined (DEVICE_CAN) struct can_s { CANName can; diff --git a/targets/TARGET_STM/TARGET_STM32F3/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F3/analogin_api.c index 92d3e244b9..8e8e234fe0 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F3/analogin_api.c @@ -36,7 +36,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -ADC_HandleTypeDef AdcHandle; void analogin_init(analogin_t *obj, PinName pin) { @@ -54,8 +53,8 @@ void analogin_init(analogin_t *obj, PinName pin) #endif // 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); + obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); + MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_ADC); @@ -74,56 +73,52 @@ void analogin_init(analogin_t *obj, PinName pin) // Check if ADC is already initialized // Enable ADC clock #if defined(ADC1) - if ((obj->adc == ADC_1) && adc1_inited) return; - if (obj->adc == ADC_1) { - AdcHandle.State = HAL_ADC_STATE_RESET; + if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return; + if ((ADCName)obj->handle.Instance == ADC_1) { __ADC1_CLK_ENABLE(); adc1_inited = 1; } #endif #if defined(ADC2) - if ((obj->adc == ADC_2) && adc2_inited) return; - if (obj->adc == ADC_2) { - AdcHandle.State = HAL_ADC_STATE_RESET; + if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return; + if ((ADCName)obj->handle.Instance == ADC_2) { __ADC2_CLK_ENABLE(); adc2_inited = 1; } #endif #if defined(ADC3) - if ((obj->adc == ADC_3) && adc3_inited) return; - if (obj->adc == ADC_3) { - AdcHandle.State = HAL_ADC_STATE_RESET; + if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return; + if ((ADCName)obj->handle.Instance == ADC_3) { __ADC34_CLK_ENABLE(); adc3_inited = 1; } #endif #if defined(ADC4) - if ((obj->adc == ADC_4) && adc4_inited) return; - if (obj->adc == ADC_4) { - AdcHandle.State = HAL_ADC_STATE_RESET; + if (((ADCName)obj->handle.Instance == ADC_4) && adc4_inited) return; + if ((ADCName)obj->handle.Instance == ADC_4) { __ADC34_CLK_ENABLE(); adc4_inited = 1; } #endif // 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 = EOC_SINGLE_CONV; - 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; + obj->handle.State = HAL_ADC_STATE_RESET; + obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; + obj->handle.Init.Resolution = ADC_RESOLUTION12b; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ScanConvMode = DISABLE; + obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; + obj->handle.Init.LowPowerAutoWait = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.NbrOfConversion = 1; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.NbrOfDiscConversion = 0; + obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC"); } } @@ -132,8 +127,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5; @@ -200,13 +193,13 @@ static inline uint16_t adc_read(analogin_t *obj) return 0; } - 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; } diff --git a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h index 1e1d3c1263..8db726ed14 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h @@ -118,6 +118,12 @@ struct dac_s { DAC_HandleTypeDef handle; }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #include "gpio_object.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/objects.h index d423c5ebef..16467b7c87 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h index d423c5ebef..16467b7c87 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F411RE/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F411RE/objects.h index d423c5ebef..16467b7c87 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F411RE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F411RE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xC/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xC/objects.h index d423c5ebef..16467b7c87 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xC/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xE/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xE/objects.h index d423c5ebef..16467b7c87 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F401xE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h index 48f9b79b01..d0f283f022 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h index 1766e2b570..bea7fef547 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/objects.h index d423c5ebef..16467b7c87 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F412xG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F412xG/objects.h index aa714d6118..9ab04158e0 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F412xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F412xG/objects.h @@ -40,12 +40,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h index f8625bb942..ceee07a594 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h @@ -40,12 +40,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h index 7229a50a28..a7e928ec3a 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h index d92fa15901..e1b80b20e4 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h index 7229a50a28..a7e928ec3a 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h index f3fde7a621..a2e2ad0f34 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h index 50f0cfc362..0de614ff89 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F4/analogin_api.c index ee62a48d97..867d1e87fb 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/analogin_api.c @@ -36,12 +36,9 @@ #include "mbed_error.h" #include "PeripheralPins.h" -ADC_HandleTypeDef AdcHandle; - void analogin_init(analogin_t *obj, PinName pin) { uint32_t function = (uint32_t)NC; - obj->adc = (ADCName)NC; #if defined(ADC1) static int adc1_inited = 0; @@ -58,18 +55,18 @@ void analogin_init(analogin_t *obj, PinName pin) 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); @@ -80,42 +77,42 @@ void analogin_init(analogin_t *obj, PinName pin) // Check if ADC is already initialized // Enable ADC clock #if defined(ADC1) - if ((obj->adc == ADC_1) && adc1_inited) return; - if (obj->adc == ADC_1) { + if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return; + if ((ADCName)obj->handle.Instance == ADC_1) { __HAL_RCC_ADC1_CLK_ENABLE(); adc1_inited = 1; } #endif #if defined(ADC2) - if ((obj->adc == ADC_2) && adc2_inited) return; - if (obj->adc == ADC_2) { + if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return; + if ((ADCName)obj->handle.Instance == ADC_2) { __HAL_RCC_ADC2_CLK_ENABLE(); adc2_inited = 1; } #endif #if defined(ADC3) - if ((obj->adc == ADC_3) && adc3_inited) return; - if (obj->adc == ADC_3) { + if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return; + if ((ADCName)obj->handle.Instance == ADC_3) { __HAL_RCC_ADC3_CLK_ENABLE(); adc3_inited = 1; } #endif // Configure ADC - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; - AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; - AdcHandle.Init.ScanConvMode = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfDiscConversion = 0; - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.NbrOfConversion = 1; - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.EOCSelection = DISABLE; + obj->handle.State = HAL_ADC_STATE_RESET; + obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; + obj->handle.Init.Resolution = ADC_RESOLUTION_12B; + obj->handle.Init.ScanConvMode = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.NbrOfDiscConversion = 0; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.NbrOfConversion = 1; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.EOCSelection = DISABLE; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC\n"); } } @@ -124,8 +121,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; @@ -202,13 +197,13 @@ static inline uint16_t adc_read(analogin_t *obj) // so VBAT readings are returned in place of temperature. ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE); - 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 (uint16_t)HAL_ADC_GetValue(&AdcHandle); + if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { + return (uint16_t)HAL_ADC_GetValue(&obj->handle); } else { return 0; } diff --git a/targets/TARGET_STM/TARGET_STM32F4/common_objects.h b/targets/TARGET_STM/TARGET_STM32F4/common_objects.h index f294fa2595..d5d8a6c184 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/common_objects.h @@ -109,11 +109,19 @@ struct i2c_s { uint8_t available_events; #endif }; + #if DEVICE_FLASH struct flash_s { uint32_t dummy; }; #endif + +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #define GPIO_IP_WITHOUT_BRR #include "gpio_object.h" diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h index e43e5aea05..52b8e159eb 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h index e43e5aea05..52b8e159eb 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h index e43e5aea05..52b8e159eb 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h index e43e5aea05..52b8e159eb 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/analogin_api.c b/targets/TARGET_STM/TARGET_STM32F7/analogin_api.c index 4751affe0b..431ddb846a 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32F7/analogin_api.c @@ -36,12 +36,9 @@ #include "PeripheralPins.h" #include "mbed_error.h" -ADC_HandleTypeDef AdcHandle; - void analogin_init(analogin_t *obj, PinName pin) { uint32_t function = (uint32_t)NC; - obj->adc = (ADCName)NC; #if defined(ADC1) static int adc1_inited = 0; @@ -58,18 +55,17 @@ void analogin_init(analogin_t *obj, PinName pin) 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); - // Get the functions (adc channel) from the pin and assign it to the object + obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); 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); @@ -80,43 +76,43 @@ void analogin_init(analogin_t *obj, PinName pin) // Check if ADC is already initialized // Enable ADC clock #if defined(ADC1) - if ((obj->adc == ADC_1) && adc1_inited) return; - if (obj->adc == ADC_1) { + if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return; + if ((ADCName)obj->handle.Instance == ADC_1) { __HAL_RCC_ADC1_CLK_ENABLE(); adc1_inited = 1; } #endif #if defined(ADC2) - if ((obj->adc == ADC_2) && adc2_inited) return; - if (obj->adc == ADC_2) { + if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return; + if ((ADCName)obj->handle.Instance == ADC_2) { __HAL_RCC_ADC2_CLK_ENABLE(); adc2_inited = 1; } #endif #if defined(ADC3) - if ((obj->adc == ADC_3) && adc3_inited) return; - if (obj->adc == ADC_3) { + if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return; + if ((ADCName)obj->handle.Instance == ADC_3) { __HAL_RCC_ADC3_CLK_ENABLE(); adc3_inited = 1; } #endif // Configure ADC - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; - AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; - AdcHandle.Init.ScanConvMode = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; - AdcHandle.Init.DiscontinuousConvMode = DISABLE; - AdcHandle.Init.NbrOfDiscConversion = 0; - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.NbrOfConversion = 1; - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.EOCSelection = DISABLE; + obj->handle.State = HAL_ADC_STATE_RESET; + obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; + obj->handle.Init.Resolution = ADC_RESOLUTION_12B; + obj->handle.Init.ScanConvMode = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; + obj->handle.Init.DiscontinuousConvMode = DISABLE; + obj->handle.Init.NbrOfDiscConversion = 0; + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.NbrOfConversion = 1; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.EOCSelection = DISABLE; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC"); } } @@ -125,8 +121,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; @@ -194,16 +188,16 @@ static inline uint16_t adc_read(analogin_t *obj) return 0; } - if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { + if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) { error("Cannot configure ADC channel"); } - HAL_ADC_Start(&AdcHandle); // Start conversion + HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value - HAL_ADC_PollForConversion(&AdcHandle, 10); - if (HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_EOC_REG) { - return (HAL_ADC_GetValue(&AdcHandle)); + HAL_ADC_PollForConversion(&obj->handle, 10); + if (HAL_ADC_GetState(&obj->handle) & HAL_ADC_STATE_EOC_REG) { + return (HAL_ADC_GetValue(&obj->handle)); } else { return 0; } diff --git a/targets/TARGET_STM/TARGET_STM32F7/common_objects.h b/targets/TARGET_STM/TARGET_STM32F7/common_objects.h index cf5cad0028..ddcccb01af 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/common_objects.h @@ -111,6 +111,12 @@ struct i2c_s { #endif }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #define GPIO_IP_WITHOUT_BRR #include "gpio_object.h" diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h index 27c980be5d..ece5f1679f 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h index 9728243d34..e68c4eb619 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L011K4/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L011K4/objects.h index cc273c9206..d0f3565f66 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L011K4/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L011K4/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L031K6/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L031K6/objects.h index cc273c9206..d0f3565f66 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L031K6/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L031K6/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h index cc273c9206..d0f3565f66 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h index 27c980be5d..ece5f1679f 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c b/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c index ac813ef1ea..c53447efa3 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32L0/analogin_api.c @@ -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 @@ void analogin_init(analogin_t *obj, PinName pin) 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 @@ void analogin_init(analogin_t *obj, PinName pin) 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 @@ static inline uint16_t adc_read(analogin_t *obj) { 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 @@ static inline uint16_t adc_read(analogin_t *obj) } 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; } diff --git a/targets/TARGET_STM/TARGET_STM32L0/common_objects.h b/targets/TARGET_STM/TARGET_STM32L0/common_objects.h index 7233c0871e..31306f48e4 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/common_objects.h @@ -116,6 +116,12 @@ struct flash_s { uint32_t dummy; }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #include "gpio_object.h" #if DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h index 69653d026a..6751a6a3c2 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #define GPIO_IP_WITHOUT_BRR #include "common_objects.h" diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h index 4ef06de688..5ddc595468 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h index 4ef06de688..5ddc595468 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h index 69653d026a..6751a6a3c2 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - #define GPIO_IP_WITHOUT_BRR #include "common_objects.h" diff --git a/targets/TARGET_STM/TARGET_STM32L1/analogin_api.c b/targets/TARGET_STM/TARGET_STM32L1/analogin_api.c index 9ae9b74d50..c716ba8da5 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/analogin_api.c @@ -36,8 +36,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -ADC_HandleTypeDef AdcHandle; - int adc_inited = 0; void analogin_init(analogin_t *obj, PinName pin) @@ -45,8 +43,8 @@ void analogin_init(analogin_t *obj, PinName pin) RCC_OscInitTypeDef RCC_OscInitStruct; // 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); + obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); + MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_ADC); @@ -73,29 +71,28 @@ void analogin_init(analogin_t *obj, PinName pin) RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; HAL_RCC_OscConfig(&RCC_OscInitStruct); - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - + obj->handle.State = HAL_ADC_STATE_RESET; // Enable ADC clock __ADC1_CLK_ENABLE(); // Configure ADC - AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4; - AdcHandle.Init.Resolution = ADC_RESOLUTION12b; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) - AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example). - AdcHandle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software. - AdcHandle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling). - AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A; - AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig - AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled - AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled - AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled - AdcHandle.Init.ExternalTrigConv = 0; // Not used - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4; + obj->handle.Init.Resolution = ADC_RESOLUTION12b; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) + obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example). + obj->handle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software. + obj->handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling). + obj->handle.Init.ChannelsBank = ADC_CHANNELS_BANK_A; + obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig + obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled + obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled + obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled + obj->handle.Init.ExternalTrigConv = 0; // Not used + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.DMAContinuousRequests = DISABLE; - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC"); } } @@ -105,8 +102,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel switch (obj->channel) { case 0: @@ -222,13 +217,13 @@ static inline uint16_t adc_read(analogin_t *obj) sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES; - 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; } diff --git a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h index 844e0f5b25..93a1dde240 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h @@ -122,6 +122,12 @@ struct dac_s { DAC_HandleTypeDef handle; }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #include "gpio_object.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h index 0b8ee2687a..a7e928ec3a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h index 6f41dbeb74..a0dee029b4 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h index 0b8ee2687a..a7e928ec3a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h index 0b8ee2687a..a7e928ec3a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h @@ -54,12 +54,6 @@ struct port_s { __IO uint32_t *reg_out; }; -struct analogin_s { - ADCName adc; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/analogin_api.c b/targets/TARGET_STM/TARGET_STM32L4/analogin_api.c index 94ccdb0f06..064e21cc99 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/analogin_api.c +++ b/targets/TARGET_STM/TARGET_STM32L4/analogin_api.c @@ -36,14 +36,11 @@ #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; // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) // are described in PinNames.h and PeripheralPins.c @@ -51,18 +48,18 @@ void analogin_init(analogin_t *obj, PinName pin) if ((pin < 0xF0) || (pin >= 0x100)) { // 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); @@ -78,26 +75,25 @@ void analogin_init(analogin_t *obj, PinName pin) __HAL_RCC_ADC_CLK_ENABLE(); __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - + obj->handle.State = HAL_ADC_STATE_RESET; // Configure ADC - AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock - AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; - AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) - AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example). - AdcHandle.Init.LowPowerAutoWait = DISABLE; - AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig - AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled - AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled - AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled - AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; - AdcHandle.Init.DMAContinuousRequests = DISABLE; - AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun - AdcHandle.Init.OversamplingMode = DISABLE; // No oversampling + obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock + obj->handle.Init.Resolution = ADC_RESOLUTION_12B; + obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; + obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) + obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example). + obj->handle.Init.LowPowerAutoWait = DISABLE; + obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig + obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled + obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled + obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled + obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event + obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + obj->handle.Init.DMAContinuousRequests = DISABLE; + obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun + obj->handle.Init.OversamplingMode = DISABLE; // No oversampling - if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { + if (HAL_ADC_Init(&obj->handle) != HAL_OK) { error("Cannot initialize ADC\n"); } } @@ -107,8 +103,6 @@ static inline uint16_t adc_read(analogin_t *obj) { ADC_ChannelConfTypeDef sConfig = {0}; - AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); - // Configure ADC channel switch (obj->channel) { case 0: @@ -178,13 +172,13 @@ static inline uint16_t adc_read(analogin_t *obj) sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; - 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; } diff --git a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h index d5d9d2707b..e9e10f3069 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h @@ -116,6 +116,12 @@ struct flash_s { uint32_t dummy; }; +struct analogin_s { + ADC_HandleTypeDef handle; + PinName pin; + uint8_t channel; +}; + #include "gpio_object.h" struct dac_s {