- #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)
+
+ /*
+ * We must clear the TXC flag before sending a new character to allow
+ * KDBG_WAIT_TXDONE() to work properly.
+ *
+ * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
+ * a new TXC could be generated after we've cleared it and before the new
+ * character is written to UDR. On a 485 bus, the transceiver will be put
+ * in RX mode while still transmitting the last char.
+ */
+ #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
+
+ #define KDBG_MASK_IRQ(old) do { \
+ (old) = UCR; \
+ UCR |= BV(TXEN); \
+ UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
+ KDBG_UART0_BUS_TX; \
+ } while(0)
+
+ #define KDBG_RESTORE_IRQ(old) do { \
+ KDBG_WAIT_TXDONE(); \
+ KDBG_UART0_BUS_RX; \
+ UCR = (old); \
+ } while(0)
+
+ typedef uint8_t kdbg_irqsave_t;
+