Hello,
I began experimenting with an ATtiny1616, writing some simple UART routines. It seems something's not right with the TXC interrupt flag.
If I understand the datasheet correctly, the TX module has a Data Buffer (TXDATA) which transfers its contents to a Shift Register, from which the bits are sent to the IO pin.
Now, there are two TX flags:
DREIF - set to 1 when the Data Buffer is empty (meaning we can write a byte to the buffer, even if the Shift Register is still working). This works just fine as far as I can see.
TXCIF - Set to 1 when the shift Register is done, and no further data is pending in the Data Buffer, i.e. when the IO pin returns to the idle state.
It is also stated for TXCIF that "This flag is automatically cleared when the transmit complete interrupt vector is executed." [emphasis mine] However, if I write the ISR like so-
ISR (USART0_TXC_vect) { // This is a status variable of my own _UARTTXBusy = 0; }
It garbles up the rest of the transmission (my test program sent bytes sequentially). I put another command in the ISR to toggle an output pin and it toggles like crazy all the time, except for a few microseconds following a write to TXDATA. The baudrate doens't effect this - 9600, 115200, whatever.
However, if I ignore the datasheet and clear the flag manually (by writing 1 to it):
ISR (USART0_TXC_vect) { // Datasheet error? TXCIF has to be cleared manually? USART0_STATUS |= USART_TXCIF_bm; _UARTTXBusy = 0; }
Suddenly it behaves as expected, raising just one interrupt event when the transmission is completed.
Of course the global interrupts and TX are enabled, and so are TXCIE and, while transmitting, DREIE.
So... 1) why is TXCIF set and causing interrupts when the UASRT is still sending? 2) Why doesn't it get cleared in the ISR as the datasheet says?
[ Edit: I tried to debug it live or with the Simulator in AS7, and it seems TXCIF isn't cleared automatically, but I'm not sure I'm using the debug tools correctly; for instance, they never go into ISR code unless I "Run to cursor" explicitly (edit^2: found the solution for debugging with ISRs - https://www.avrfreaks.net/forum/... :-) but doesn't help my issue ) ]