2023-02-23 10:57 PM
I am using UART to transmit and receive data. for transmitting I am using polling method function HAL_UART_Transmit(&hart1, buf, 8,1000) and for receiving i am using HAL_UART_Receive_IT(&hart, Rx_buf,8);
receiving is working fine but after transmitting any data the receive callback is getting fired and after that the receiving interrupts are not getting fired even though I am transmitting data from another device.
Solved! Go to Solution.
2023-02-28 12:10 AM
So it looks like there's a connection between your Tx and Rx pins on sender side.
If you look at RX pin signal with a logic analyzer, what do you see when you are sending your 8 bytes ?
Either bytes are really echoed by distant part, or it is a hw issue on your sender device.
What happens if you disconnect anything from the sender device ? Is your Rx callback still executed ?
2023-02-24 01:39 AM
Hello @Anwar.786 and welcome to ST Community,
You may not have enabled the UART receive interrupt in your initialization code. Make sure you have enabled the receive interrupt before attempting to send and receive data.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2023-02-24 02:49 AM
Hi @Sarra.S I already enabled the USART interrupts in STM32CUBE MX, do I also have to force enable the receive interrupt individually?
2023-02-24 08:00 AM
You should always post your relevant code so we can see what you're doing. But from what I'm reading you're saying that you get at least one receive interrupt but after that it doesn't interrupt again.
Sounds like you are not calling HAL_UART_Receive_IT again. Everytime HAL_UART_RxCpltCallback is called, the Receive interrupt is disabled. You have to re-enable it again each time.
See the code that I've posted here to get the understanding of enabling interrupts again and also checking for HAL status to be sure HAL_UART_Receive_IT returns HAL_OK.
2023-02-27 06:53 AM
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, RX_Buf, 8);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(i==5 || i==10){
Uart_status=HAL_UART_Transmit(&huart1, TX_Buf, 8, 1000);
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
Uart_status=HAL_UART_Receive_IT(&huart1, RX_Buf, 8);
i++;
}
The only problem i am facing is once the transmission is completed the Rx callback is getting executed which is not supposed to happen.
can you explain why it is happing?
2023-02-27 06:54 AM
@Karl Yamashita please look into above reply, I forgot to tag you there.
2023-02-27 08:13 AM
Hello @Anwar.786
What is the content of RX_Buf buffer at the moment when your Rx Complete callback is executed ? (if callback is executed, I guess that at least 8 datas have been detected on rx line)
Could it correspond to some echo of the transmitted data ?
Regards
2023-02-27 08:20 AM
2023-02-27 08:24 PM
Sanity check : Did you select the right part number in the compiler settings ?
2023-02-27 10:51 PM
After transmission is completed, it is Rx callback is executed and the content of the RX_Buf is same as the TX_Buf.
Yes, it corresponds to echo of the data transmitted.