Skip to content

Commit

Permalink
lufa: Fix console flush qmk#223
Browse files Browse the repository at this point in the history
Old console sent unneeded empty data every one milli sencond.
After this fix console flushes endpoint data bank every 50ms only when needed.
  • Loading branch information
tmk committed May 22, 2015
1 parent 5b46031 commit fdce0c9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,24 @@ void EVENT_USB_Device_WakeUp()
#endif
}

#ifdef CONSOLE_ENABLE
static bool console_flush = false;
#define CONSOLE_FLUSH_SET(b) do { \
uint8_t sreg = SREG; cli(); console_flush = b; SREG = sreg; \
} while (0)

// called every 1ms
void EVENT_USB_Device_StartOfFrame(void)
{
static uint8_t count;
if (++count % 50) return;
count = 0;

if (!console_flush) return;
Console_Task();
console_flush = false;
}
#endif

/** Event handler for the USB_ConfigurationChanged event.
* This is fired when the host sets the current configuration of the USB device after enumeration.
Expand Down Expand Up @@ -491,6 +505,10 @@ int8_t sendchar(uint8_t c)
// 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;

Expand Down Expand Up @@ -524,8 +542,12 @@ int8_t sendchar(uint8_t c)
Endpoint_Write_8(c);

// send when bank is full
if (!Endpoint_IsReadWriteAllowed())
if (!Endpoint_IsReadWriteAllowed()) {
while (!(Endpoint_IsINReady()));
Endpoint_ClearIN();
} else {
CONSOLE_FLUSH_SET(true);
}

Endpoint_SelectEndpoint(ep);
return 0;
Expand Down

0 comments on commit fdce0c9

Please sign in to comment.