I'm trying to get UART working on a STM32F0 part and can't figure out the interrupts. According to the reference manual
Bit 7 TXE: Transmit data register empty
This bit is set by hardware when the content of the USARTx_TDR register has been
transferred into the shift register. It is cleared by a write to the USARTx_TDR register.
An interrupt is generated if the TXEIE bit =1 in the USARTx_CR1 register.
However, I can never see the TXE flag clear. Here is what I have in my IRQ Handler:
if(USART_GetITStatus(EVAL_COM1, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
if (TxBuffer[TxCount] != '\0')
{
USART_SendData(EVAL_COM1, TxBuffer[TxCount++]);
}
//else{
// USART_ITConfig(EVAL_COM1, USART_IT_TXE, DISABLE);
//}
}
Unless I uncomment the else statement, the send interrupt keeps triggering endlessly even after I stop sending bytes to the TDR.
Is there a way to stop the interrupts without disabling them? Or do I need to re-enable every time I want to send something?
Best Answer
When
USARTx_TDR
register is emptyUSART_ISR_TXE
flag is set to 1. WhenUSARTx_CR1_TXEIE
(TXE
Interrupt Enable flag) is enabled andTXE=1
it will pend interrupt.So you code sample with disabling interrupt after completed transmission is good solution.
When starting transmission periodically just write first byte (it will clear
TXE
) and enable interrupt.Or if possible use DMA - a bit more code, but after you calculate
strlen
and configure it, it is rather fire and forget ;)