- #define KDBG_WRITE_CHAR(c) do { UCR |= BV(TXEN); UDR = (c); } while(0)
- #define KDBG_MASK_IRQ(old) do { (old) = UCR & BV(TXCIE); cbi(UCR, TXCIE); } while(0)
- #define KDBG_RESTORE_IRQ(old) do { UCR |= (old); } while(0)
+ #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
+ /*
+ * BUG: before sending a new character the TXC flag is cleared to allow
+ * KDBG_WAIT_TXDONE() to work properly, but, if KDBG_WRITE_CHAR() is called
+ * after the RXC flag is set by hardware, a new TXC could be generated
+ * after we clear it and before the new character is put in UDR. In this
+ * case if a 485 is used the transceiver will be put in RX mode while
+ * transmitting the last char.
+ */
+ #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
+
+ #define KDBG_MASK_IRQ(old) do { \
+ SER_UART0_485_TX; \
+ (old) = UCR; \
+ UCR |= BV(TXEN); \
+ UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
+ } while(0)
+
+ #define KDBG_RESTORE_IRQ(old) do { \
+ KDBG_WAIT_TXDONE(); \
+ SER_UART0_485_RX; \
+ UCR = (old); \
+ } while(0)
+