Showing results for 
Search instead for 
Did you mean: 

What should I use instead of huart->Instance == USART1?

Associate III

Hi everyone,

I write a function to start all other functions without any parameter like Start(); in while, but in this funciton I write different function like this

void Start()


if(huart->Instance == USART1)


else if(huart->Instance == USART2)



so I want to know in this function which UART is return to function, bcs It is a parameter of measure function. Can I use flags to specify which uart is? What would you recommend?


Pass parameters...

How does measure() know which UART to use? Does it use a UART?

Pass object/structures which are relevant to the sub-functions, and the things they call.

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

Please excuse me,

I understand that I have to use same parameters for start that I use in measure, that's right?

Measure function has huart and data parameters. It performs different operations depending on which uart the data comes from.

Karl Yamashita
Lead II

What is calling Start function and where in code are you calling the Start function? What does measure function do?

You have a pointer to huart but you are not passing the UART_HandleTypeDef as an argument so there is no way to compare huart to USART1 or USART2. You should've got a compiler error.

You need to pass structure like this code below so you can compare to an UART instance. Also use the instance instead of USART1 or USART2. If by mistake you didn't actually enable UART2, the compiler won't catch an error if you use either one of the defines but it will catch an error if it tries to compare to an instance that isn't instantiated.

void Start(UART_HandleTypeDef *huart)
	if(huart->Instance == huart3.Instance)
	else if(huart->Instance == huart6.Instance)

Like the compiler we can't infer things out of thin air.

Present this in a way that the interrelationships between the functions, uart and buffer are apparent.

You could make a huart structure that contained buffers, or pointers. That way you'd only need to pass huart, and the port instance, and buffer would be accessible from that. The less repetitive or duplicative data you can remove from the subroutine parameter passing the better.

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

Firstly thank you for your reply,

To be clear about what I want to do

void dataSend(UART_HandleTypeDef *huart) ----> there is a parameter 
    if(huart->Instance == huart1.Instance)
		memset(&tx1_buffer, 0 ,sizeof(tx1_buffer));
		HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rx2_buffer, buff);
		HAL_GPIO_WritePin(UART1_EN_GPIO_Port, UART1_EN_Pin, 1);
		HAL_UART_Transmit_DMA(&huart1, data, sizeof(data));
		HAL_GPIO_WritePin(UART1_EN_GPIO_Port, UART1_EN_Pin, 0);
		uartCh.rx_uart1 = 0;
    else if(huart->Instance == huart2.Instance)
		memcpy(&tx2_buffer, rx2_buffer, buff);
		HAL_GPIO_WritePin(UART2_EN_GPIO_Port, UART2_EN_Pin, 1);
		HAL_UART_Transmit_DMA(&huart2, data, sizeof(data));
		HAL_GPIO_WritePin(UART2_EN_GPIO_Port, UART2_EN_Pin, 0);
		uartCh.rx_uart2 = 0;
void processReq()-->there is no parameter 
    dataSend(&huart1); -----> to call this function I don't want to write a parameter in processReq()
int main()
    processReq(); ----> I call processReq here and all other things call each other 

This is written for a rs485-uart module

Is there a question to this?

Got a significant amount of duplicative code and global variables.

Would be more efficient and less cluttered if these things were in a structure.

If the pin, buffer and uartCh.rx_uartX lived in a unique instance for each uart the same code could use either, depending on the pointer furnished to the routine.

If you have to cut-n-paste large blocks of code, and change "1" references to "2" and remember to catch / fix everything, your approach is wrong. Step back and look at what's materially identical in functionality between code paths.

Random usage of dataSend(tx2_buffer); within same function

Use of a common/shared resource HAL_UART_Transmit_DMA(&huartX, data, sizeof(data));

Variable buff not passed but common to two code paths.

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