From 14a33312f5a3bef7186b535a383f155a8ee2d128 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Wed, 21 Mar 2018 15:14:47 +0100 Subject: [PATCH] STM32F4 ADC internal channels update --- .../TARGET_STM32F4/analogin_device.c | 7 +- .../TARGET_STM32F4/device/stm32f4xx_hal_adc.h | 3 + .../TARGET_STM32F4/device/stm32f4xx_ll_adc.h | 70 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c index 54d266e4c1..e18fd9f29d 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c @@ -161,16 +161,15 @@ uint16_t adc_read(analogin_t *obj) break; case 16: sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; + sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Minimum ADC sampling time when reading the temperature is 10us break; case 17: sConfig.Channel = ADC_CHANNEL_VREFINT; - /* From experiment, measurement needs max sampling time to be valid */ - sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; + sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Minimum ADC sampling time when reading the internal reference voltage is 10us break; case 18: sConfig.Channel = ADC_CHANNEL_VBAT; - /* From experiment, measurement needs max sampling time to be valid */ - sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; + sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Minimum ADC sampling time when reading the VBAT is 5us break; default: return 0; diff --git a/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_adc.h b/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_adc.h index 5b80503859..b62752e355 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_adc.h +++ b/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_adc.h @@ -44,6 +44,9 @@ /* Includes ------------------------------------------------------------------*/ #include "stm32f4xx_hal_def.h" +/* Include low level driver */ +#include "stm32f4xx_ll_adc.h" + /** @addtogroup STM32F4xx_HAL_Driver * @{ */ diff --git a/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_ll_adc.h b/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_ll_adc.h index e28b75b646..16536f0f38 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_ll_adc.h +++ b/targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_ll_adc.h @@ -269,6 +269,18 @@ extern "C" { /* ADC registers bits positions */ #define ADC_CR1_RES_BITOFFSET_POS (24U) /* Value equivalent to POSITION_VAL(ADC_CR1_RES) */ #define ADC_TR_HT_BITOFFSET_POS (16U) /* Value equivalent to POSITION_VAL(ADC_TR_HT) */ + +/* ADC internal channels related definitions */ +/* Internal voltage reference VrefInt */ +#define VREFINT_CAL_ADDR ((uint16_t*) (0x1FFF7A2AU)) /* Internal voltage reference, address of parameter VREFINT_CAL: VrefInt ADC raw data acquired at temperature 30 DegC (tolerance: +-5 DegC), Vref+ = 3.3 V (tolerance: +-10 mV). */ +#define VREFINT_CAL_VREF ( 3300U) /* Analog voltage reference (Vref+) value with which temperature sensor has been calibrated in production (tolerance: +-10 mV) (unit: mV). */ +/* Temperature sensor */ +#define TEMPSENSOR_CAL1_ADDR ((uint16_t*) (0x1FFF7A2CU)) /* Internal temperature sensor, address of parameter TS_CAL1: On STM32F4, temperature sensor ADC raw data acquired at temperature 30 DegC (tolerance: +-5 DegC), Vref+ = 3.3 V (tolerance: +-10 mV). */ +#define TEMPSENSOR_CAL2_ADDR ((uint16_t*) (0x1FFF7A2EU)) /* Internal temperature sensor, address of parameter TS_CAL2: On STM32F4, temperature sensor ADC raw data acquired at temperature 110 DegC (tolerance: +-5 DegC), Vref+ = 3.3 V (tolerance: +-10 mV). */ +#define TEMPSENSOR_CAL1_TEMP (( int32_t) 30) /* Internal temperature sensor, temperature at which temperature sensor has been calibrated in production for data into TEMPSENSOR_CAL1_ADDR (tolerance: +-5 DegC) (unit: DegC). */ +#define TEMPSENSOR_CAL2_TEMP (( int32_t) 110) /* Internal temperature sensor, temperature at which temperature sensor has been calibrated in production for data into TEMPSENSOR_CAL2_ADDR (tolerance: +-5 DegC) (unit: DegC). */ +#define TEMPSENSOR_CAL_VREFANALOG ( 3300U) /* Analog voltage reference (Vref+) voltage with which temperature sensor has been calibrated in production (+-10 mV) (unit: mV). */ + /** * @} */ @@ -1654,6 +1666,64 @@ typedef struct / __LL_ADC_DIGITAL_SCALE(__ADC_RESOLUTION__) \ ) +/** + * @brief Helper macro to calculate the temperature (unit: degree Celsius) + * from ADC conversion data of internal temperature sensor. + * @note Computation is using temperature sensor calibration values + * stored in system memory for each device during production. + * @note Calculation formula: + * Temperature = ((TS_ADC_DATA - TS_CAL1) + * * (TS_CAL2_TEMP - TS_CAL1_TEMP)) + * / (TS_CAL2 - TS_CAL1) + TS_CAL1_TEMP + * with TS_ADC_DATA = temperature sensor raw data measured by ADC + * Avg_Slope = (TS_CAL2 - TS_CAL1) + * / (TS_CAL2_TEMP - TS_CAL1_TEMP) + * TS_CAL1 = equivalent TS_ADC_DATA at temperature + * TEMP_DEGC_CAL1 (calibrated in factory) + * TS_CAL2 = equivalent TS_ADC_DATA at temperature + * TEMP_DEGC_CAL2 (calibrated in factory) + * Caution: Calculation relevancy under reserve that calibration + * parameters are correct (address and data). + * To calculate temperature using temperature sensor + * datasheet typical values (generic values less, therefore + * less accurate than calibrated values), + * use helper macro @ref __LL_ADC_CALC_TEMPERATURE_TYP_PARAMS(). + * @note As calculation input, the analog reference voltage (Vref+) must be + * defined as it impacts the ADC LSB equivalent voltage. + * @note Analog reference voltage (Vref+) must be either known from + * user board environment or can be calculated using ADC measurement + * and ADC helper macro @ref __LL_ADC_CALC_VREFANALOG_VOLTAGE(). + * @note On this STM32 serie, calibration data of temperature sensor + * corresponds to a resolution of 12 bits, + * this is the recommended ADC resolution to convert voltage of + * temperature sensor. + * Otherwise, this macro performs the processing to scale + * ADC conversion data to 12 bits. + * @param __VREFANALOG_VOLTAGE__ Analog reference voltage (unit: mV) + * @param __TEMPSENSOR_ADC_DATA__ ADC conversion data of internal + * temperature sensor (unit: digital value). + * @param __ADC_RESOLUTION__ ADC resolution at which internal temperature + * sensor voltage has been measured. + * This parameter can be one of the following values: + * @arg @ref LL_ADC_RESOLUTION_12B + * @arg @ref LL_ADC_RESOLUTION_10B + * @arg @ref LL_ADC_RESOLUTION_8B + * @arg @ref LL_ADC_RESOLUTION_6B + * @retval Temperature (unit: degree Celsius) + */ +#define __LL_ADC_CALC_TEMPERATURE(__VREFANALOG_VOLTAGE__,\ + __TEMPSENSOR_ADC_DATA__,\ + __ADC_RESOLUTION__) \ + (((( ((int32_t)((__LL_ADC_CONVERT_DATA_RESOLUTION((__TEMPSENSOR_ADC_DATA__), \ + (__ADC_RESOLUTION__), \ + LL_ADC_RESOLUTION_12B) \ + * (__VREFANALOG_VOLTAGE__)) \ + / TEMPSENSOR_CAL_VREFANALOG) \ + - (int32_t) *TEMPSENSOR_CAL1_ADDR) \ + ) * (int32_t)(TEMPSENSOR_CAL2_TEMP - TEMPSENSOR_CAL1_TEMP) \ + ) / (int32_t)((int32_t)*TEMPSENSOR_CAL2_ADDR - (int32_t)*TEMPSENSOR_CAL1_ADDR) \ + ) + TEMPSENSOR_CAL1_TEMP \ + ) /** * @brief Helper macro to calculate the temperature (unit: degree Celsius)