Make _write wait for CDC transmission to finish

This commit is contained in:
Unknown
2017-11-08 01:30:13 -05:00
parent 81e461b6cf
commit e8b7deac5a
+15 -1
View File
@@ -5,7 +5,9 @@
******************************************************************************
*/
#include <sys/unistd.h>
#include <sys/unistd.h>
#include <cmsis_os.h>
#include <freertos_vars.h>
#include <usbd_cdc_if.h>
#include <usart.h>
#include <commands.h>
@@ -19,6 +21,8 @@
#define UART_TX_BUFFER_SIZE 64
static uint8_t uart_tx_buf[UART_TX_BUFFER_SIZE];
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern USBD_HandleTypeDef hUsbDeviceFS;
int _write(int file, char *data, int len) {
//number of bytes written
@@ -49,5 +53,15 @@ int _write(int file, char *data, int len) {
} break;
}
// Wait for transmission to complete
USBD_CDC_HandleTypeDef* hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
while (hcdc->TxState != 0) {
osSemaphoreWait(sem_usb_irq, 0);
// We have a new incoming USB transmission: handle it
HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
// Let the irq (OTG_FS_IRQHandler) fire again.
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
}
return written;
}