mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-18 20:38:10 +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:
commit
939e9e8ea2
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user