2025-12-03
2:44 AM
- last edited on
2025-12-03
6:06 AM
by
Lina_DABASINSKA
Hi Support team,
I am working on STM32H563 Nucleo project and trying to integrate internal temperature sensor.
I enabled internal temperature sensor channel available at ADC1, but getting crash while executing below conversion code:
TS_CAL1 = *TS_CAL1_ADDR;
PFA project for reference.
Please help in resolving the issue.
Thanks.
Solved! Go to Solution.
2025-12-05 3:00 AM - edited 2025-12-05 3:05 AM
Hello,
@Subhashzimetrics wrote:
Hi @Andrew Neil ,
Since this post is for same STM32H563 mcu, so attached project (H5_Internal_Temp.zip) should have worked for us straight. But we are getting Hard fault to access this OTP area.
Did you read my previous post:
PS: Unfortunately there is an issue regarding the attachments these days. Infinite "Virus scanning in progress" .. reported here.
2025-12-03 2:56 AM - edited 2025-12-03 2:56 AM
Welcome to the forum.
Please see How to write your question to maximize your chances to find a solution for best results.
@Subhashzimetrics wrote:getting crash while executing below conversion code:
TS_CAL1 = *TS_CAL1_ADDR;
What, exactly, do you mean by "getting crash" ?
If it's a Hard Fault, see Tips on debugging Cortex-M Hard Faults.
BTW: Although it doesn't really matter for just a single line like that, please see How to insert source code for putting source code in a post.
2025-12-03 5:34 AM - edited 2025-12-03 5:39 AM
Hello,
I suspect you have an issue as described in this knowledge base article: How to avoid a HardFault when ICACHE is enabled on the STM32H5 series
PS: your project hasn't been uploaded.
2025-12-03 5:56 AM - edited 2025-12-03 6:06 AM
@mƎALLEm wrote:PS: your project hasn't been uploaded.
It seems to be stuck on "virus scan in progress..." - I've raised this in the Feedback Forum.
2025-12-03 6:02 AM
@Andrew Neil wrote:
It was like that when I made my 1st reply - which is 3 hours ago now.
I think I've also seen other posts like that today...
Exact and I reported that internally.
2025-12-05 2:44 AM
Hi @Andrew Neil ,
Sorry for delayed reply from my end.
We followed project attached in below related post on:
Since this post is for same STM32H563 mcu, so attached project (H5_Internal_Temp.zip) should have worked for us straight. But we are getting Hard fault to access this OTP area.
Though we are able to read ADC channel data from internal temperature sensor, we dont' know how to convert that data to actual temperature value.
Pls help to resolve the issue.
attaching my project again
thanks,
Subhash
2025-12-05 2:56 AM
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#define TS_CAL1_ADDR ((__IO uint16_t*)((uint32_t)0x08FFF814))
#define TS_CAL2_ADDR ((__IO uint16_t*)((uint32_t)0x08FFF818))
#define TS_CAL1_TEMP 30.0f
#define TS_CAL2_TEMP 130.0f
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
DTS_HandleTypeDef hdts;
/* USER CODE BEGIN PV */
float slope;
uint16_t TS_CAL1, TS_CAL2;
float temp_sens;
int32_t temp_dts;
HAL_StatusTypeDef st;
void bsp_cpu_temp_init(void);
uint8_t bsp_get_cpu_temp(float* temp);
HAL_StatusTypeDef bsp_get_cpu_temp_dts(int32_t *temp);
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_ADC1_Init(void);
static void MX_ICACHE_Init(void);
static void MX_DTS_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t adc_val;
uint32_t Get_CPU_Temperature(void)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
adc_val = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
return adc_val;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t x;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_ADC1_Init();
MX_ICACHE_Init();
MX_DTS_Init();
/* USER CODE BEGIN 2 */
bsp_cpu_temp_init();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
bsp_get_cpu_temp(&temp_sens);
bsp_get_cpu_temp_dts(&temp_dts);
HAL_Delay(100);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_CSI;
RCC_OscInitStruct.CSIState = RCC_CSI_ON;
RCC_OscInitStruct.CSICalibrationValue = RCC_CSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLL1_SOURCE_CSI;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 250;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_0;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1_VCORANGE_WIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
/** Configure the programming delay
*/
__HAL_FLASH_SET_PROGRAM_DELAY(FLASH_PROGRAMMING_DELAY_2);
}
/**
* @brief ADC1 Initialization Function
* @PAram None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.SamplingMode = ADC_SAMPLING_MODE_NORMAL;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
/**
* @brief DTS Initialization Function
* @PAram None
* @retval None
*/
static void MX_DTS_Init(void)
{
/* USER CODE BEGIN DTS_Init 0 */
/* USER CODE END DTS_Init 0 */
/* USER CODE BEGIN DTS_Init 1 */
/* USER CODE END DTS_Init 1 */
hdts.Instance = DTS;
hdts.Init.QuickMeasure = DTS_QUICKMEAS_DISABLE;
hdts.Init.RefClock = DTS_REFCLKSEL_PCLK;
hdts.Init.TriggerInput = DTS_TRIGGER_HW_NONE;
hdts.Init.SamplingTime = DTS_SMP_TIME_1_CYCLE;
hdts.Init.Divider = 0;
hdts.Init.HighThreshold = 0x0;
hdts.Init.LowThreshold = 0x0;
if (HAL_DTS_Init(&hdts) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN DTS_Init 2 */
/* USER CODE END DTS_Init 2 */
}
/**
* @brief ICACHE Initialization Function
* @PAram None
* @retval None
*/
static void MX_ICACHE_Init(void)
{
/* USER CODE BEGIN ICACHE_Init 0 */
/* USER CODE END ICACHE_Init 0 */
/* USER CODE BEGIN ICACHE_Init 1 */
/* USER CODE END ICACHE_Init 1 */
/** Enable instruction cache (default 2-ways set associative cache)
*/
if (HAL_ICACHE_Enable() != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ICACHE_Init 2 */
/* USER CODE END ICACHE_Init 2 */
}
/* USER CODE BEGIN 4 */
void bsp_cpu_temp_init(void)
{
HAL_StatusTypeDef status = HAL_OK;
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
TS_CAL1 = *TS_CAL1_ADDR;
TS_CAL2 = *TS_CAL2_ADDR;
slope = (TS_CAL2_TEMP - TS_CAL1_TEMP) / (TS_CAL2 - TS_CAL1);
}
uint8_t bsp_get_cpu_temp(float* temp)
{
uint32_t adc_val = 0;
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)
{
adc_val = HAL_ADC_GetValue(&hadc1);
*temp = (float)(slope * ((int32_t)adc_val - (int32_t)TS_CAL1) + TS_CAL1_TEMP);
}
else
{
return 1;
}
return 0;
}
HAL_StatusTypeDef bsp_get_cpu_temp_dts(int32_t *temp)
{
HAL_StatusTypeDef status = HAL_OK;
int32_t temperature = 0;
status = HAL_DTS_Start(&hdts);
if(status != HAL_OK)
return status;
status = HAL_DTS_GetTemperature(&hdts, &temperature);
if(status != HAL_OK)
return status;
status = HAL_DTS_Stop(&hdts);
if(status != HAL_OK)
return status;
*temp = temperature;
return status;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @PAram file: pointer to the source file name
* @PAram line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
2025-12-05 3:00 AM - edited 2025-12-05 3:05 AM
Hello,
@Subhashzimetrics wrote:
Hi @Andrew Neil ,
Since this post is for same STM32H563 mcu, so attached project (H5_Internal_Temp.zip) should have worked for us straight. But we are getting Hard fault to access this OTP area.
Did you read my previous post:
PS: Unfortunately there is an issue regarding the attachments these days. Infinite "Virus scanning in progress" .. reported here.
2025-12-07 10:18 PM
Hi,
Thanks for your support. i am able to get temperature after mpu config. it is not calibrate
Please help to calibrate the temperature.
2025-12-08 12:09 AM
@Subhashzimetrics wrote:
Hi,
it is not calibrate
Please help to calibrate the temperature.
That's a new question. Please ask that question separately.
Thank you for your understanding