Skip to main content
Francesco_
Associate III
April 11, 2020
Question

How to pass UART_HandleTypeDef *huart trough the funciont?

  • April 11, 2020
  • 3 replies
  • 2402 views

I want pass UART_HandleTypeDef *my_huart at function HAL_UART_Transmit and and mange the response with interrupt. The code is show below but doesn't work. Can you help me?

//This is myfile.c

void myfunc(UART_HandleTypeDef *my_huart)

{

char command[] = "AT\r\n";

HAL_UART_Transmit(my_huart, (uint8_t *)command, sizeof(command)-1, HAL_MAX_DELAY);

}

//In the main

myfunc(&huart1);

//out of main and not call in the main

void USART1_IRQHandler(void)

{

 /* USER CODE BEGIN USART1_IRQn 0 */

 /* USER CODE END USART1_IRQn 0 */

 HAL_UART_IRQHandler(&huart1);

if(((USART1 -> SR) & (1<<5)) == (1<<5))

{

memset(buffer,0,sizeof(buffer));

HAL_UART_Receive(&huart1, (uint8_t *)buffer, sizeof(buffer)-1, HAL_MAX_DELAY);

HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer) , HAL_MAX_DELAY);

USART1 -> SR &= ~(1<<5);

}

}

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
April 11, 2020

Ok, don't call blocking functions in the IRQ handler. You shouldn't need to clear the SR, this will occur by actions you take with the DR

P​ointer passing in first function seems correct. I'd probably create a string send function and use strlen(), and then use that function rather than have duplicative code.

The IRQ Handler should call the HAL IRQ routine, which in turn calls your call back function.

Use the interrupt form HAL_UART_Transmit_IT() ​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Francesco_
Associate III
April 11, 2020

Hi @Community member ,

don't call blocking functions in the IRQ handler -> I don't have call HAL_UART_Receive(&huart1,..) and HAL_UART_Transmit(&huart2,..), how should I read the data? Do I have to read directly from the register?

These other points are not clear to me:

The IRQ Handler should call the HAL IRQ routine, which in turn calls your call back function.

Use the interrupt form HAL_UART_Transmit_IT()

Tesla DeLorean
Guru
April 11, 2020

You can't spend multiple bytes times within the IRQ, you must be brief and leave.

You should perhaps create your own buffers and service those in the IRQ Handler, or call back.

Try reviewing some UART IRQ examples and follow the anticipated flow​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..