1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-19 04:48:12 +00:00

Merge pull request #873 from trezor/andrewkozlik/vcp-fix

core: Prevent data loss when writing to USB VCP.
This commit is contained in:
Pavol Rusnak 2020-02-25 13:43:11 +01:00 committed by GitHub
commit 939e9e8ea2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -33,7 +33,8 @@ int mp_hal_stdin_rx_chr(void) {
void mp_hal_stdout_tx_strn(const char *str, size_t len) {
if (vcp_iface_num >= 0) {
int r = usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, 0);
int r =
usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, 500);
(void)r;
}
}

View File

@ -297,7 +297,7 @@ int usb_vcp_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {
b->write++;
}
return len;
return i;
}
int usb_vcp_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len,
@ -315,13 +315,19 @@ int usb_vcp_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len,
int usb_vcp_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len,
int timeout) {
uint32_t start = HAL_GetTick();
uint32_t i = 0;
while (i < len) {
while (sectrue != usb_vcp_can_write(iface_num)) {
if (timeout >= 0 && HAL_GetTick() - start >= timeout) {
return 0; // Timeout
return i; // Timeout
}
__WFI(); // Enter sleep mode, waiting for interrupt
}
return usb_vcp_write(iface_num, buf, len);
int ret = usb_vcp_write(iface_num, buf + i, len - i);
if (ret < 0) return ret;
i += ret;
}
return i;
}
static void usb_vcp_class_init(USBD_HandleTypeDef *dev, usb_vcp_state_t *state,