Skip to main content
Explorer
May 17, 2023
Question

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

  • May 17, 2023
  • 3 replies
  • 5114 views

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)

measure(tx1_buffer);

else if(huart->Instance == USART2)

measure(tx2_buffer_buffer);

}

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?

    This topic has been closed for replies.

    3 replies

    Graduate II
    May 17, 2023

    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.

    MÇETİ.1Author
    Explorer
    May 17, 2023

    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.

    Graduate II
    May 17, 2023

    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.

    Graduate II
    May 17, 2023

    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)
    	measure(tx1_buffer);
    	else if(huart->Instance == huart6.Instance)
    	measure(tx2_buffer_buffer);
    }

    MÇETİ.1Author
    Explorer
    May 18, 2023

    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));
    		memcpy(&tx1_buffer,&rx1_buffer,buff);
    		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)
     memset(&tx2_buffer,0,sizeof(tx2_buffer));
    		memcpy(&tx2_buffer, rx2_buffer, buff);
    		dataSend(tx2_buffer);
    		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()
    {
    while(1)
    {
     processReq(); ----> I call processReq here and all other things call each other 
    }}

    This is written for a rs485-uart module

    Graduate II
    May 18, 2023

    Is there a question to this?