cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411 uart asynch

eran yas
Associate
Posted on April 02, 2017 at 08:19

Hi, 

I am following STM32F411RE-Nucleo example for UART_HyperTerminal_IT.

My first TX_IT works great. The problem is in the second call to 

HAL_UART_Transmit_IT()

 which fails on 

if(huart->gState == HAL_UART_STATE_READY)

So I looked on the code to see where huart->gState is set to HAL_UART_STATE_READY. I saw that it is called in 

UART_EndTransmit_IT()

The problem is that this API is not being called. So what I did is setting 

huart->gState = HAL_UART_STATE_READY

in 

USART_CharTransmitComplete_Callback

is this the right way? Am I missing anything here?

Thanks,

#asynchronous #uart
1 REPLY 1
Guenael Cadier
ST Employee
Posted on April 03, 2017 at 12:19

HI,

My answer to question would be No : you should not have to update UART Handle state in application code.

UART Handle state is expected to be set automatically to READY as soon as TX transfer will be completed.

Here is expected behavior following a HAL_UART_Transmit_IT() call :

- TXE interrupt is enabled. Transfer is started in HAL_UART_Transmit_IT, by writing 1st data to sent in TDR.

- On each TXE interrupt, next byte to send is also written in TDR (done in IRQ_Handler).

- For last data, when TXE interrupt is raised, TXE interrupt is disabled, and TC interrupt is enabled (for ensuirng that data has been really sent).

- When TC interrupt is raised, transfer is considered as completed. This is managed inside UART_EndTransmit_IT(), which will call HAL_UART_TxCpltCallback (). (no link with USART_CharTransmitComplete_Callback())

This HAL_UART_TxCpltCallback callback is defined as empty in provided HAL code, but as weak, could be replaced/defined in your application side. This callback should be used on application side, to determine when transfer initiated by calling HAL_UART_Transmit_IT() could be considered as comple.

Coming back to your question :

- you mentioned your first TX sequence is ok. So, I guess you see sent data properly received on HyperTerminal side and that your UART interrupt is propely enabled and linked to correct handler.

- if second call to HAL_UART_Transmit_IT() is exiting due to wrong status of UART handle, maybe it is due to 1st transfer not completed (still ongoing).

Could you check that the Tx complete callback HAL_UART_TxCpltCallback() (in UART_HyperTerminal_IT example, this callback will for example, switch on a Led), is called prior you requests 2nd transfer ?

Basically, while 1st transfer is ongoing, no new transfer could be called. You have to wait 1st transfer is completed, before starting 2nd one. This point could be identified with HAL_UART_TxCpltCallback() execution.

Regards

Guenael