2023-05-17 05:44 AM
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?
2023-05-17 06:02 AM
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.
2023-05-17 06:13 AM
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.
2023-05-17 01:28 PM
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);
}
2023-05-17 01:45 PM
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.
2023-05-18 02:14 AM
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
2023-05-18 07:47 AM
Is there a question to this?
2023-05-18 08:10 AM
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.