-#endif
- // Not wait once timeouted.
- // Because sendchar() is called so many times, waiting each call causes big lag.
- static bool timeouted = false;
-
- // prevents Console_Task() from running during sendchar() runs.
- // or char will be lost. These two function is mutually exclusive.
- CONSOLE_FLUSH_SET(false);
-
- if (USB_DeviceState != DEVICE_STATE_Configured)
- return -1;
-
- uint8_t ep = Endpoint_GetCurrentEndpoint();
- Endpoint_SelectEndpoint(CONSOLE_IN_EPNUM);
- if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
- goto ERROR_EXIT;
- }
-
- if (timeouted && !Endpoint_IsReadWriteAllowed()) {
- goto ERROR_EXIT;
- }
-
- timeouted = false;
-
- uint8_t timeout = SEND_TIMEOUT;
- while (!Endpoint_IsReadWriteAllowed()) {
- if (USB_DeviceState != DEVICE_STATE_Configured) {
- goto ERROR_EXIT;
- }
- if (Endpoint_IsStalled()) {
- goto ERROR_EXIT;
- }
- if (!(timeout--)) {
- timeouted = true;
- goto ERROR_EXIT;
- }
- _delay_ms(1);
- }