cancel
Showing results for 
Search instead for 
Did you mean: 

About Application on "\Repository\STM32Cube_FW_L5_V1.4.0\Projects\STM32L562E-DK\Applications\TFM".

HChen.10
Associate

I want to implement USART1 to the non-secure project of TFM_Appli in STM32L562 TF-M firmware package. I have add some code in secure project of TFM_Appli such as following:

enum tfm_plat_err_t tfm_spm_hal_nvic_interrupt_target_state_cfg(void)
{
  /*    return nvic_interrupt_target_state_cfg();*/
	HAL_GTZC_TZSC_ConfigPeriphAttributes(GTZC_PERIPH_USART1, GTZC_TZSC_PERIPH_NSEC);
	HAL_GPIO_ConfigPinAttributes(GPIOB, GPIO_PIN_6|GPIO_PIN_5, GPIO_PIN_NSEC);
 
  return TFM_PLAT_ERR_SUCCESS;
}
 
enum tfm_plat_err_t tfm_spm_hal_nvic_interrupt_enable(void)
{
  /*    return nvic_interrupt_enable();*/
	HAL_GTZC_TZIC_EnableIT(GTZC_PERIPH_USART1);
  return TFM_PLAT_ERR_SUCCESS;
}

And add some code in non-secure project of TFM_Appli such as following:

void MX_USART1_UART_Init(void)
{
 
  /* USER CODE BEGIN USART1_Init 0 */
 
  /* USER CODE END USART1_Init 0 */
 
  /* USER CODE BEGIN USART1_Init 1 */
 
  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
 
  if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
 
  /* USER CODE BEGIN USART1_Init 2 */
  /* USER CODE END USART1_Init 2 */
}
 
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(huart->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */
 
  /* USER CODE END USART1_MspInit 0 */
 
  /** Initializes the peripherals clock
  */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
    PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }
 
    /* Peripheral clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
 
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PB6     ------> USART1_TX
    PB7     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = TMC2209_MCU_Rx_Pin|TMC2209_MCU_Tx_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  /* USER CODE BEGIN USART1_MspInit 1 */
    HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  /* USER CODE END USART1_MspInit 1 */
  }
}

Then I add the following code in non-secure project of TFM_Appli main loop:

int main(int argc, char **argv)
 
/*int main(void) */
 
{
 
 /* set example to const : this const changes in binary without rebuild */
 
 pUserAppId = (uint8_t *)&UserAppId;
 
 
 
 /* STM32L5xx HAL library initialization:
 
 - Systick timer is configured by default as source of time base, but user
 
 can eventually implement his proper time base source (a general purpose
 
 timer for example or other time source), keeping in mind that Time base
 
 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
 
 handled in milliseconds basis.
 
 - Set NVIC Group Priority to 3
 
 - Low Level Initialization
 
 */
 HAL_Init();
 periph_Init();
 
 HAL_UART_Transmit(&huart1, (uint8_t *)"Initial test\r\n", sizeof("Initial test\r\n"), 0xFFFF);
 while(HAL_UART_Receive_IT(&huart1, &RxBuffer, 1) != HAL_OK);
 
 while (1U)
 {
 }
}
  
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART1)
	{
		while(HAL_UART_Receive_IT(&huart1, &RxBuffer, 1) != HAL_OK);
	}
}
 
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART1)
	{
		while(HAL_UART_Receive_IT(&huart1, &RxBuffer, 1) != HAL_OK);
	}
}

But The Interrupt do not work correctly which mean after the USART1 received a byte of data, the interrupt didn't be triggered. But if I use the

if(HAL_UART_Receive(&huart1,&RxBuffer,1,0xFFFF) == HAL_OK)
{
 }

The Data is received correctly.

So, my question is "What things do I miss leading the interrupt be triggered incorrectly?"

Thanks a lot!!

1 REPLY 1
Jocelyn RICARD
ST Employee

Hello @HChen.10​ ,

I think you need to set UART interrupt as non secure.

You can use the NVIC_SetTargetState for this

Best regards

Jocelyn