Skip to content

Commit cb579cb

Browse files
committed
STM32F4 ADC internal channels update
1 parent db73ed0 commit cb579cb

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

targets/TARGET_STM/TARGET_STM32F4/analogin_device.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,15 @@ uint16_t adc_read(analogin_t *obj)
161161
break;
162162
case 16:
163163
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
164+
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Minimum ADC sampling time when reading the temperature is 10us
164165
break;
165166
case 17:
166167
sConfig.Channel = ADC_CHANNEL_VREFINT;
167-
/* From experiment, measurement needs max sampling time to be valid */
168-
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
168+
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Minimum ADC sampling time when reading the internal reference voltage is 10us
169169
break;
170170
case 18:
171171
sConfig.Channel = ADC_CHANNEL_VBAT;
172-
/* From experiment, measurement needs max sampling time to be valid */
173-
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
172+
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; // Minimum ADC sampling time when reading the VBAT is 5us
174173
break;
175174
default:
176175
return 0;

targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_adc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
/* Includes ------------------------------------------------------------------*/
4545
#include "stm32f4xx_hal_def.h"
4646

47+
/* Include low level driver */
48+
#include "stm32f4xx_ll_adc.h"
49+
4750
/** @addtogroup STM32F4xx_HAL_Driver
4851
* @{
4952
*/

targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_ll_adc.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,18 @@ extern "C" {
269269
/* ADC registers bits positions */
270270
#define ADC_CR1_RES_BITOFFSET_POS (24U) /* Value equivalent to POSITION_VAL(ADC_CR1_RES) */
271271
#define ADC_TR_HT_BITOFFSET_POS (16U) /* Value equivalent to POSITION_VAL(ADC_TR_HT) */
272+
273+
/* ADC internal channels related definitions */
274+
/* Internal voltage reference VrefInt */
275+
#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). */
276+
#define VREFINT_CAL_VREF ( 3300U) /* Analog voltage reference (Vref+) value with which temperature sensor has been calibrated in production (tolerance: +-10 mV) (unit: mV). */
277+
/* Temperature sensor */
278+
#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). */
279+
#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). */
280+
#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). */
281+
#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). */
282+
#define TEMPSENSOR_CAL_VREFANALOG ( 3300U) /* Analog voltage reference (Vref+) voltage with which temperature sensor has been calibrated in production (+-10 mV) (unit: mV). */
283+
272284
/**
273285
* @}
274286
*/
@@ -1654,6 +1666,64 @@ typedef struct
16541666
/ __LL_ADC_DIGITAL_SCALE(__ADC_RESOLUTION__) \
16551667
)
16561668

1669+
/**
1670+
* @brief Helper macro to calculate the temperature (unit: degree Celsius)
1671+
* from ADC conversion data of internal temperature sensor.
1672+
* @note Computation is using temperature sensor calibration values
1673+
* stored in system memory for each device during production.
1674+
* @note Calculation formula:
1675+
* Temperature = ((TS_ADC_DATA - TS_CAL1)
1676+
* * (TS_CAL2_TEMP - TS_CAL1_TEMP))
1677+
* / (TS_CAL2 - TS_CAL1) + TS_CAL1_TEMP
1678+
* with TS_ADC_DATA = temperature sensor raw data measured by ADC
1679+
* Avg_Slope = (TS_CAL2 - TS_CAL1)
1680+
* / (TS_CAL2_TEMP - TS_CAL1_TEMP)
1681+
* TS_CAL1 = equivalent TS_ADC_DATA at temperature
1682+
* TEMP_DEGC_CAL1 (calibrated in factory)
1683+
* TS_CAL2 = equivalent TS_ADC_DATA at temperature
1684+
* TEMP_DEGC_CAL2 (calibrated in factory)
1685+
* Caution: Calculation relevancy under reserve that calibration
1686+
* parameters are correct (address and data).
1687+
* To calculate temperature using temperature sensor
1688+
* datasheet typical values (generic values less, therefore
1689+
* less accurate than calibrated values),
1690+
* use helper macro @ref __LL_ADC_CALC_TEMPERATURE_TYP_PARAMS().
1691+
* @note As calculation input, the analog reference voltage (Vref+) must be
1692+
* defined as it impacts the ADC LSB equivalent voltage.
1693+
* @note Analog reference voltage (Vref+) must be either known from
1694+
* user board environment or can be calculated using ADC measurement
1695+
* and ADC helper macro @ref __LL_ADC_CALC_VREFANALOG_VOLTAGE().
1696+
* @note On this STM32 serie, calibration data of temperature sensor
1697+
* corresponds to a resolution of 12 bits,
1698+
* this is the recommended ADC resolution to convert voltage of
1699+
* temperature sensor.
1700+
* Otherwise, this macro performs the processing to scale
1701+
* ADC conversion data to 12 bits.
1702+
* @param __VREFANALOG_VOLTAGE__ Analog reference voltage (unit: mV)
1703+
* @param __TEMPSENSOR_ADC_DATA__ ADC conversion data of internal
1704+
* temperature sensor (unit: digital value).
1705+
* @param __ADC_RESOLUTION__ ADC resolution at which internal temperature
1706+
* sensor voltage has been measured.
1707+
* This parameter can be one of the following values:
1708+
* @arg @ref LL_ADC_RESOLUTION_12B
1709+
* @arg @ref LL_ADC_RESOLUTION_10B
1710+
* @arg @ref LL_ADC_RESOLUTION_8B
1711+
* @arg @ref LL_ADC_RESOLUTION_6B
1712+
* @retval Temperature (unit: degree Celsius)
1713+
*/
1714+
#define __LL_ADC_CALC_TEMPERATURE(__VREFANALOG_VOLTAGE__,\
1715+
__TEMPSENSOR_ADC_DATA__,\
1716+
__ADC_RESOLUTION__) \
1717+
(((( ((int32_t)((__LL_ADC_CONVERT_DATA_RESOLUTION((__TEMPSENSOR_ADC_DATA__), \
1718+
(__ADC_RESOLUTION__), \
1719+
LL_ADC_RESOLUTION_12B) \
1720+
* (__VREFANALOG_VOLTAGE__)) \
1721+
/ TEMPSENSOR_CAL_VREFANALOG) \
1722+
- (int32_t) *TEMPSENSOR_CAL1_ADDR) \
1723+
) * (int32_t)(TEMPSENSOR_CAL2_TEMP - TEMPSENSOR_CAL1_TEMP) \
1724+
) / (int32_t)((int32_t)*TEMPSENSOR_CAL2_ADDR - (int32_t)*TEMPSENSOR_CAL1_ADDR) \
1725+
) + TEMPSENSOR_CAL1_TEMP \
1726+
)
16571727

16581728
/**
16591729
* @brief Helper macro to calculate the temperature (unit: degree Celsius)

0 commit comments

Comments
 (0)