X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fcommon%2Fvirtual_com.c;h=804c91a95de332de0963865746ef18f236d79f19;hb=1364963a63e0f2ed2c46eebbb7afcd3997258c78;hp=faa3b67e0ac4c3a4171833e45d649c65e8363091;hpb=cf6fdbdca0e32b98074843803e5760cd6fa9fbde;p=gps-watch.git diff --git a/src/common/virtual_com.c b/src/common/virtual_com.c index faa3b67..804c91a 100644 --- a/src/common/virtual_com.c +++ b/src/common/virtual_com.c @@ -35,6 +35,7 @@ #include #include +#include #include "usb_device_config.h" #include "usb.h" @@ -53,6 +54,7 @@ #endif #include "ringbuf.h" +#include "buffer.h" /* Provided by users. */ extern void USB_DeviceClockInit(void); @@ -66,10 +68,13 @@ extern void USB_DeviceIsrEnable(void); ******************************************************************************/ static uint8_t cdc_rx_buf_space[1024]; +static uint8_t cdc_tx_buf_space[1024] alignas(4); static struct ringbuf cdc_rx_buf = RINGBUF_INIT (cdc_rx_buf_space, sizeof (cdc_rx_buf_space)); +struct buffer cdc_tx_buf; + /* Data structure of virtual com device */ usb_cdc_vcom_struct_t s_cdcVcom; @@ -100,7 +105,6 @@ static uint8_t s_currRecvBuf[DATA_BUFF_SIZE]; volatile static uint32_t s_recvSize = 0; volatile static uint32_t s_sendSize = 0; volatile static uint8_t s_sendComplete = 0; -volatile static uint8_t s_currRecvIndex = 0; static uint32_t s_usbBulkMaxPacketSize = FS_CDC_VCOM_BULK_OUT_PACKET_SIZE; /******************************************************************************* * Prototypes @@ -596,68 +600,27 @@ usb_status_t USB_DeviceConfigureEndpointStatus(usb_device_handle handle, uint8_t } } -/* See virtual_com.h for documentation of this function. */ -void USB_VcomWriteBlocking(usb_device_handle baseAddr, const uint8_t *buf, size_t count) +static ssize_t +flush_tx_buffer (void *user_data, void *vbuf, size_t bufsiz, size_t count) { - while ((s_cdcVcom.attach != 1) || (s_cdcVcom.startTransactions != 1)) - { - }; - USB_DeviceSendRequest((usb_device_handle)baseAddr, USB_CDC_VCOM_BULK_IN_ENDPOINT, (uint8_t *)buf, count); - while (!s_sendComplete) - { - }; - s_sendComplete = 0; -} + (void) bufsiz; -/* See virtual_com.h for documentation of this function. */ -status_t USB_VcomReadBlocking(usb_device_handle baseAddr, uint8_t *buf, size_t count) -{ - status_t error = kStatus_Success; - size_t bufIndex = 0U, bytesToReceive = 0U; - assert(count != 0U); + /* Don't write data unless link is open. */ + if (s_cdcVcom.attach != 1 || s_cdcVcom.startTransactions != 1) + return count; - /* Waiting for the USB ready. */ - while ((s_cdcVcom.attach != 1) || (s_cdcVcom.startTransactions != 1)) - { - }; + USB_DeviceSendRequest (user_data, USB_CDC_VCOM_BULK_IN_ENDPOINT, + vbuf, count); - do - { - /* If no receive request. */ - if (s_recvSize <= 0) - { - if (kStatus_USB_Success != - USB_DeviceRecvRequest(baseAddr, USB_CDC_VCOM_BULK_OUT_ENDPOINT, s_currRecvBuf, s_usbBulkMaxPacketSize)) - { - return kStatus_Fail; - } - s_currRecvIndex = 0; - } - /* Waiting for data received by virtual com. */ - while (s_recvSize <= 0) - { - }; + /* Wait for USB_DeviceCdcAcmBulkIn() to set s_sendComplete. */ + while (!s_sendComplete) + { + } - /* When receive request is error. */ - if (0xFFFFFFFFU == s_recvSize) - { - /* Waiting for the USB ready and transfer started. */ - while ((s_cdcVcom.attach != 1) || (s_cdcVcom.startTransactions != 1)) - { - }; - s_recvSize = 0; - } - else - { - bytesToReceive = MIN(count, s_recvSize); - memcpy((void *)&buf[bufIndex], s_currRecvBuf + s_currRecvIndex, bytesToReceive); - count -= bytesToReceive; - s_recvSize -= bytesToReceive; - bufIndex += bytesToReceive; - s_currRecvIndex += bytesToReceive; - } - } while (0U != count); - return error; + /* Reset for the next write. */ + s_sendComplete = 0; + + return count; } /* See virtual_com.h for documentation of this function. */ @@ -682,6 +645,10 @@ usb_device_handle USB_VcomInit(void) else { deviceHandle = s_cdcVcom.deviceHandle; + + buffer_init (&cdc_tx_buf, cdc_tx_buf_space, sizeof (cdc_tx_buf_space), + flush_tx_buffer, deviceHandle); + USB_DeviceIsrEnable(); USB_DeviceRun(s_cdcVcom.deviceHandle); }