Skip to content

Commit 538c192

Browse files
committed
lufa: Fix console flush #223
Old console sent unneeded empty data every one milli sencond. After this fix console flushes endpoint data bank every 50ms only when needed.
1 parent 87628c9 commit 538c192

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

protocol/lufa/lufa.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,24 @@ void EVENT_USB_Device_WakeUp()
197197
#endif
198198
}
199199

200+
#ifdef CONSOLE_ENABLE
201+
static bool console_flush = false;
202+
#define CONSOLE_FLUSH_SET(b) do { \
203+
uint8_t sreg = SREG; cli(); console_flush = b; SREG = sreg; \
204+
} while (0)
205+
206+
// called every 1ms
200207
void EVENT_USB_Device_StartOfFrame(void)
201208
{
209+
static uint8_t count;
210+
if (++count % 50) return;
211+
count = 0;
212+
213+
if (!console_flush) return;
202214
Console_Task();
215+
console_flush = false;
203216
}
217+
#endif
204218

205219
/** Event handler for the USB_ConfigurationChanged event.
206220
* This is fired when the host sets the current configuration of the USB device after enumeration.
@@ -491,6 +505,10 @@ int8_t sendchar(uint8_t c)
491505
// Because sendchar() is called so many times, waiting each call causes big lag.
492506
static bool timeouted = false;
493507

508+
// prevents Console_Task() from running during sendchar() runs.
509+
// or char will be lost. These two function is mutually exclusive.
510+
CONSOLE_FLUSH_SET(false);
511+
494512
if (USB_DeviceState != DEVICE_STATE_Configured)
495513
return -1;
496514

@@ -524,8 +542,12 @@ int8_t sendchar(uint8_t c)
524542
Endpoint_Write_8(c);
525543

526544
// send when bank is full
527-
if (!Endpoint_IsReadWriteAllowed())
545+
if (!Endpoint_IsReadWriteAllowed()) {
546+
while (!(Endpoint_IsINReady()));
528547
Endpoint_ClearIN();
548+
} else {
549+
CONSOLE_FLUSH_SET(true);
550+
}
529551

530552
Endpoint_SelectEndpoint(ep);
531553
return 0;

0 commit comments

Comments
 (0)