Skip to content

Commit 74a3158

Browse files
committed
don't block when the OS isn't listening
1 parent 2a4e3b1 commit 74a3158

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

firmware/common/usb_serial.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ static void endpoint_setup(usbd_device *dev,
266266

267267
/* just read the packet 'beyond' the buffer, copy the wraparound */
268268
static uint8_t rx_buf[USB_RX_BUFSIZE+PACKET_SIZE_FULL_SPEED];
269+
269270
static size_t rx_start, rx_len;
270271

272+
/* not re-entrant-safe, assumes polling */
271273
static void serial_rx_cb(usbd_device *dev, uint8_t ep)
272274
{
273275
size_t rx_end = rx_start+rx_len;
@@ -326,7 +328,10 @@ int usb_serial_getchar(void)
326328
void usb_serial_putchar(int c)
327329
{
328330
uint8_t buf = c;
329-
while ( usbd_ep_write_packet(device, UART_DEVICE_TO_HOST_ENDPOINT, &buf, 1) == 0 );
331+
while ( usbd_ep_write_packet(device, UART_DEVICE_TO_HOST_ENDPOINT, &buf, 1) == 0 )
332+
{
333+
usbd_poll(device);
334+
}
330335
}
331336

332337
size_t usb_serial_write_noblock(const uint8_t *buf, size_t len)

firmware/usbserial/main.c

+22-15
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
#include "ws2812_new.h"
3838
#include "keypad.h"
39-
#include "ani.h"
4039
#include "usb_serial.h"
40+
#include "ani.h"
4141

4242
static void enable_sys_tick(uint32_t ticks)
4343
{
@@ -49,11 +49,7 @@ static void enable_sys_tick(uint32_t ticks)
4949
volatile uint32_t tick=0;
5050
void SysTick_Handler(void)
5151
{
52-
if ( (tick & 3) == 0)
53-
ws2812_write();
54-
55-
keypad_poll();
56-
52+
ws2812_write();
5753
tick+=1;
5854
}
5955

@@ -70,10 +66,10 @@ static void init(void)
7066
set_animation(OFF);
7167

7268
usb_serial_init();
73-
enable_sys_tick(F_SYS_TICK_CLK/4000);
69+
enable_sys_tick(F_SYS_TICK_CLK/1600);
7470
}
7571

76-
static const uint16_t keymap[N_KEYS] = KEY_MAPPING;
72+
static const uint8_t keymap[N_KEYS] = KEY_MAPPING;
7773

7874
static volatile uint8_t key_event[N_KEYS];
7975

@@ -96,24 +92,35 @@ int main(void)
9692

9793
for(;;)
9894
{
99-
f = ws2812_get_frame();
95+
keypad_poll();
10096
usb_serial_poll();
97+
f = ws2812_get_frame();
10198

102-
if (f != NULL && (tick-t_last > 16) )
99+
if (f != NULL && (tick-t_last > 4) )
103100
{
104101
prepare_next_frame(f);
105102
ws2812_swap_frame();
106-
t_last += 16;
103+
t_last += 4;
107104
}
108105

109-
int key;
106+
int key, n_written, off=0;
107+
uint8_t outbuf[N_KEYS];
108+
uint8_t ixbuf[N_KEYS];
109+
110110
for (key=0; key<N_KEYS; key++)
111-
{
112111
if (key_event[key])
113112
{
114-
usb_serial_putchar(keymap[key]);
115-
key_event[key] = 0;
113+
ixbuf[off] = key;
114+
outbuf[off] = keymap[key];
115+
off += 1;
116116
}
117+
118+
if (off > 0)
119+
{
120+
n_written = usb_serial_write_noblock(outbuf, off);
121+
122+
for (off=0; off<n_written; off++)
123+
key_event[ixbuf[off]] = 0;
117124
}
118125

119126
switch (usb_serial_getchar())

0 commit comments

Comments
 (0)