+ static bool hid_listen_ready = false;
+ if (!hid_listen_ready) {
+ if (timer_read32() < 2500)
+ return false;
+ hid_listen_ready = true;
+ }
+ return true;
+}
+
+static bool console_putc(uint8_t c)
+{
+ // return immediately if called while interrupt
+ if (!(SREG & (1<<SREG_I)))
+ goto EXIT;
+
+ if (USB_DeviceState != DEVICE_STATE_Configured && !ringbuf_is_full(&sendbuf))
+ goto EXIT;
+
+ if (!console_is_ready() && !ringbuf_is_full(&sendbuf))
+ goto EXIT;
+
+ /* Data lost considerations:
+ * 1. When buffer is full at early satage of startup, we will have to start sending
+ * before console_is_ready() returns true. Data can be lost even if sending data
+ * seems to be successful on USB. hid_listen on host is not ready perhaps?
+ * Sometime first few packets are lost when buffer is full at startup.
+ * 2. When buffer is full and USB pipe is not ready, new coming data will be lost.
+ * 3. console_task() cannot send data in buffer while main loop is blocked.
+ */
+ /* retry timeout */
+ const uint8_t CONSOLE_TIMOUT = 5; // 1 is too small, 2 seems to be enough for Linux
+ static uint8_t timeout = CONSOLE_TIMOUT;
+ uint16_t prev = timer_read();
+ bool done = false;