2026-03-30 2:26 PM
Hello everyone,
I'm working with the STM32L562E-DK board and the STM32Cube_FW_L5_V1.5.0 firmware. I want to integrate my own code (which reads a sensor MPU6050 via I2C) into the STM32CubeL5 TFM (Trusted Firmware-M) application, running it in the Non Secure environment. The base project I'm using is TFM_Appli, which comes in the package, and I've added my source code to the NonSecure section.
I created a separate project in STM32CubeMX using the board's I2C1, which utilizes pins PB6 (SCL) and PB7 (SDA). I then generated the code and copied the I2C communication initialization code into the "main.c" file of my "TFM_Appli_NonSecure" project. Specifically:
/* This is before "int main (void)"-----------------------*/
I2C_HandleTypeDef hi2c1;/*------------- I'm calling these function from "int main(void)" ------------*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x60514452;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}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 */
}I have also correctly included the I2C HAL drivers (stm32l5xx_hal_i2c.c, stm32l5xx_hal_i2c_ex.c) in my No-Secure project, and the compilation, generation, download and installation of the binary are successful.
However, at runtime, when calling:
/* The I2C address of the sensor is 0x68 but a 0 is added to the
right because the function reads the address of 1 byte (8 bits)*/
HAL_StatusTypeDef ret = HAL_I2C_IsDeviceReady(&hi2c1, 0x68 << 1, 1, 100);I consistently get ret = HAL_ERROR (1). I've verified that the sensor is correctly connected and works with the same address in other projects that don't use TFM. I've also verified that the I2C initialization (MX_I2C1_Init) runs beforehand and returns HAL_OK.
My questions are the nexts:
What is the correct (and recommended) way to make the I2C1 peripheral accessible from the Non-Secure world in a TFM project? Do I need to modify any specific files or add code?ç
I would greatly appreciate any guidance or links to documentation that addresses this specific issue. I've been stuck for several days and can't get my code to communicate with the sensor.
Thank you very much in advance.
2026-04-27 3:11 PM
Hello again. I'm still having this probem.
Best Regards