Fix strange bug when usb cannot write.

In this case, the buffer is kept until the next success call.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2022-09-29 23:11:49 +02:00
parent d0fe447416
commit 8f14db677e
2 changed files with 23 additions and 15 deletions

View File

@@ -82,31 +82,38 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
return 0; return 0;
} }
void hid_write_offset(uint16_t size, uint16_t offset) { uint32_t hid_write_offset(uint16_t size, uint16_t offset) {
if (*usb_get_tx() != 0x81) if (*usb_get_tx() != 0x81)
DEBUG_PAYLOAD(usb_get_tx()+offset, size); DEBUG_PAYLOAD(usb_get_tx()+offset, size);
usb_write_offset(size, offset); return usb_write_offset(size, offset);
} }
void hid_write(uint16_t size) { uint32_t hid_write(uint16_t size) {
hid_write_offset(size, 0); return hid_write_offset(size, 0);
} }
uint16_t send_buffer_size = 0; uint16_t send_buffer_size = 0;
bool last_write_result = false;
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len) { void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len) {
uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4] + 1; uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4] + 1;
if (send_buffer_size > 0) { if (send_buffer_size > 0) {
ctap_resp->cid = ctap_req->cid; if (last_write_result == true) {
ctap_resp->cont.seq = seq; ctap_resp->cid = ctap_req->cid;
hid_write_offset(64, (uint8_t *)ctap_resp - (usb_get_tx())); ctap_resp->cont.seq = seq;
send_buffer_size -= MIN(64 - 5, send_buffer_size); }
ctap_resp = (CTAPHID_FRAME *)((uint8_t *)ctap_resp + 64 - 5); if (hid_write_offset(64, (uint8_t *)ctap_resp - (usb_get_tx())) > 0) {
send_buffer_size -= MIN(64 - 5, send_buffer_size);
ctap_resp = (CTAPHID_FRAME *)((uint8_t *)ctap_resp + 64 - 5);
}
} }
} }
int driver_write(const uint8_t *buffer, size_t buffer_size) { int driver_write(const uint8_t *buffer, size_t buffer_size) {
tud_hid_report(0, buffer, buffer_size); last_write_result = tud_hid_report(0, buffer, buffer_size);
printf("result %d\n", last_write_result);
if (last_write_result == false)
return 0;
return MIN(64, buffer_size); return MIN(64, buffer_size);
} }
@@ -359,9 +366,10 @@ void driver_exec_finished_cont(size_t size_next, size_t offset) {
ctap_resp->init.cmd = last_cmd; ctap_resp->init.cmd = last_cmd;
ctap_resp->init.bcnth = size_next >> 8; ctap_resp->init.bcnth = size_next >> 8;
ctap_resp->init.bcntl = size_next & 0xff; ctap_resp->init.bcntl = size_next & 0xff;
hid_write_offset(64, offset);
ctap_resp = (CTAPHID_FRAME *)((uint8_t *)ctap_resp + 64 - 5);
send_buffer_size = size_next; send_buffer_size = size_next;
send_buffer_size -= MIN(64-7, send_buffer_size); if (hid_write_offset(64, offset) > 0) {
ctap_resp = (CTAPHID_FRAME *)((uint8_t *)ctap_resp + 64 - 5);
send_buffer_size -= MIN(64-7, send_buffer_size);
}
} }

View File

@@ -54,7 +54,7 @@ uint32_t usb_write_offset(uint16_t len, uint16_t offset) {
w = driver_write(tx_buffer+offset, MIN(len, pkt_max)); w = driver_write(tx_buffer+offset, MIN(len, pkt_max));
w_len -= w; w_len -= w;
tx_r_offset += w; tx_r_offset += w;
return MIN(w_len, pkt_max); return w;
} }
size_t usb_rx(const uint8_t *buffer, size_t len) { size_t usb_rx(const uint8_t *buffer, size_t len) {