Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that the RX interrupt is obviously used to save polling, but why the TX one too?

share|improve this question
3  
Same reason. You're often sending a long message a character at a time. – Brian Drummond 22 hours ago
1  
Code that needs to do very little else can block on the actual transmission. More efficient code can drain a software buffer in the interrupt. Yet more sophisticated code on hardware that supports it can set up a DMA engine to pump the transfer without distracting the CPU from more important work, though as serial baud rates are often fairly slow in MCU terms that may not always be important. – Chris Stratton 22 hours ago
3  
If you are asking about a UART TX interrupt on a particular type of device, then please post a link to its datasheet. – Nick Alexeev 22 hours ago
    
The interrupt that others have mentioned signals that there is buffer space available in the uart for more transmit data. The buffer may be one or more bytes in length depending on the uart hardware. There is often a different "transfer complete" interrupt that signals that all bits have been shifted out of the uart. This may be used used for other purposes such as switching a transceiver from transmit to receive. – Tut 21 hours ago
up vote 7 down vote accepted

The main goal of the TX interrupt (really an END OF TX) is to send the content of a buffer (multiple bytes) automatically. When implemented in a proper way:

  1. Enable the TX interrupt.
  2. The user code starts transmission by sending only the first byte in the buffer.
  3. At the end of TX (of the first byte), an interrupt will be generated.
  4. In the TX ISR (Interrupt Service Routine), the code must send the next byte in the buffer and update the buffer index.
  5. At the end of this transmission, a new interrupt occurs, and so on, until the entire content of buffer is sent "automatically".
  6. Disable the TX interrupt.

The exact behavior depends on the microcontroller. That is a general description.

share|improve this answer
3  
An easy simplification on many platforms is to have the user code merely be responsible for putting data in the buffer and enabling the interrupt; the ISR disables the interrupt if it fires when the buffer is dry. – supercat 21 hours ago

The TX interrupt is mainly for longer datagrams. You can initiate the transfer for a buffer of known length (bytecount). Now you can push your buffer pointer as often as there are bytes to send, when the TX interrupt occurs. This ensures the "as quick as possible" transfer of your buffer, without the need to poll any "TransferComplete"-Flag/Statusbit.

share|improve this answer

The TX interrupt fires when there is space in the transmit buffer.

For devices that don't have a transmit buffer (i.e. where you write one byte, which is transferred immediately), the interrupt is asserted when the transmit register can be written with the next byte.

For devices with a buffer, the interrupt is asserted at an implementation-defined time. For some, it is when the buffer is half empty, for some it is when transmission of the last byte has started and the buffer is completely empty.

share|improve this answer

Some UARTS have an internal buffer that is larger than one, the 16xxx series for one.

The procedure here was

  1. Set a transmit window mask, for example to 4 remaining.
  2. fill buffer positions until the UART said full or no more data need to be send
  3. do other stuff
  4. when only 4 buffer positions are left unsend, set TX interrupt
  5. wait for the interrupt to be serviced
  6. if more data needs to be send go to 2.

This decreases the CPU load by offloading some processing to the UART thus enabling slower CPU's to keep up and service other task instead of getting interrupted all the time.

share|improve this answer

Another use case is when you connect the UART to another communication interface like RS485. The controller has to release the bus driver as soon as the last bit has been shifted out of the TX buffer. This is easy to handle in the TX interrupt, but would be cumbersome to implement without, since you would have to wait an exact time after writing the last byte to the output buffer which would also vary with baud rate.

share|improve this answer
    
The RS485 usage works best if there's a transmit-complete interrupt available rather than just transmit-buffer-available. Especially on devices with big buffers, the only way to get an interrupt after the last useful byte is sent is to feed the UART unwanted additional bytes and hope one can disable the transmission before they go out. – supercat 21 hours ago
    
@supercat: You are indeed right to point out that this would require a Tx complete interrupt. But i think this still relates to the question. – Rev1.0 11 hours ago
    
It's often possible to get by with just a buffer-available interrupt, but the tx complete interrupt is often better; I had been thinking that it might be helpful to amplify why some UARTs have separate interrupts for buffer-ready and transmit complete. – supercat 4 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.