Ver Fonte

cdc_vcom: fix the bug that use ringbuffer pool as usb packet buffer

This commit set the buffer for packet to CDC_MaxPacketSize which is a
reasonable value for it. However, maybe we should make
CDC_{RX,TX}_BUFSIZE configurable as well.
Grissiom há 12 anos atrás
pai
commit
778c4239b1
1 ficheiros alterados com 36 adições e 17 exclusões
  1. 36 17
      components/drivers/usb/usbdevice/class/cdc_vcom.c

+ 36 - 17
components/drivers/usb/usbdevice/class/cdc_vcom.c

@@ -20,14 +20,20 @@
 
 #ifdef RT_USB_DEVICE_CDC
 
-#define CDC_RX_BUFSIZE          64
-#define CDC_TX_BUFSIZE          2048
-static rt_uint8_t rx_pool[CDC_RX_BUFSIZE];
-static rt_uint8_t tx_pool[CDC_TX_BUFSIZE];
+#define CDC_RX_BUFSIZE          2048
+#define CDC_TX_BUFSIZE          1024
+static rt_uint8_t rx_rbp[CDC_RX_BUFSIZE];
+static rt_uint8_t tx_rbp[CDC_TX_BUFSIZE];
 static struct rt_ringbuffer rx_ringbuffer;
 static struct rt_ringbuffer tx_ringbuffer;
-static struct rt_serial_device vcom_serial;
 static struct serial_ringbuffer vcom_int_rx;
+
+static struct rt_serial_device vcom_serial;
+
+#define CDC_MaxPacketSize 64
+static rt_uint8_t rx_buf[CDC_MaxPacketSize];
+static rt_uint8_t tx_buf[CDC_MaxPacketSize];
+
 static rt_bool_t vcom_connected = RT_FALSE;
 
 static struct udevice_descriptor dev_desc =
@@ -38,7 +44,7 @@ static struct udevice_descriptor dev_desc =
     USB_CLASS_CDC,              //bDeviceClass;
     0x00,                       //bDeviceSubClass;
     0x00,                       //bDeviceProtocol;
-    0x40,                       //bMaxPacketSize0;
+    CDC_MaxPacketSize,          //bMaxPacketSize0;
     _VENDOR_ID,                 //idVendor;
     _PRODUCT_ID,                //idProduct;
     USB_BCD_DEVICE,             //bcdDevice;
@@ -160,7 +166,8 @@ static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
     eps = (cdc_eps_t)cls->eps;
     mps = eps->ep_in->ep_desc->wMaxPacketSize;
     size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
-    if(size == 0) return RT_EOK;
+    if(size == 0)
+        return RT_EOK;
 
     length = size > mps ? mps : size;
 
@@ -192,6 +199,7 @@ static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
     eps = (cdc_eps_t)cls->eps;
     /* receive data from USB VCOM */
     level = rt_hw_interrupt_disable();
+
     rt_ringbuffer_put(&rx_ringbuffer, eps->ep_out->buffer, size);
     rt_hw_interrupt_enable(level);
 
@@ -337,8 +345,8 @@ static rt_err_t _class_run(udevice_t device, uclass_t cls)
     RT_DEBUG_LOG(RT_DEBUG_USB, ("cdc class run\n"));
     eps = (cdc_eps_t)cls->eps;
 
-    eps->ep_in->buffer=tx_pool;
-    eps->ep_out->buffer=rx_pool;
+    eps->ep_in->buffer = tx_buf;
+    eps->ep_out->buffer = rx_buf;
 
     dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
                 eps->ep_out->ep_desc->wMaxPacketSize);
@@ -373,21 +381,22 @@ static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls)
 {
     rt_uint32_t level;
     rt_size_t size;
-    static rt_uint32_t frame_count = 0;
+    /*static rt_uint32_t frame_count = 0;*/
     cdc_eps_t eps;
 
     if(vcom_connected != RT_TRUE) return -RT_ERROR;
 
     eps = (cdc_eps_t)cls->eps;
-    if (frame_count ++ == 5)
+    /*if (frame_count ++ == 5)*/
     {
         rt_size_t mps = eps->ep_in->ep_desc->wMaxPacketSize;
 
         /* reset the frame counter */
-        frame_count = 0;
+        /*frame_count = 0;*/
 
         size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
-        if(size == 0) return -RT_EFULL;
+        if(size == 0)
+            return -RT_EFULL;
 
         size = size > mps ? mps : size;
 
@@ -406,6 +415,7 @@ static struct uclass_ops ops =
 {
     _class_run,
     _class_stop,
+    /*RT_NULL,*/
     _class_sof_handler,
 };
 
@@ -534,8 +544,17 @@ static rt_err_t _vcom_control(struct rt_serial_device *serial,
 static int _vcom_putc(struct rt_serial_device *serial, char c)
 {
     rt_uint32_t level;
+    int cnt = 0;
+
+    if (vcom_connected != RT_TRUE)
+        return 0;
 
-    if (vcom_connected != RT_TRUE) return 0;
+    while (RT_RINGBUFFER_EMPTY(&tx_ringbuffer) == 0)
+    {
+        rt_kprintf("wait for %d\n", cnt++);
+        if (vcom_connected != RT_TRUE)
+            return 0;
+    }
 
     level = rt_hw_interrupt_disable();
     if (RT_RINGBUFFER_EMPTY(&tx_ringbuffer))
@@ -579,8 +598,8 @@ void rt_usb_vcom_init(void)
     struct serial_configure config;
 
     /* initialize ring buffer */
-    rt_ringbuffer_init(&rx_ringbuffer, rx_pool, CDC_RX_BUFSIZE);
-    rt_ringbuffer_init(&tx_ringbuffer, tx_pool, CDC_TX_BUFSIZE);
+    rt_ringbuffer_init(&rx_ringbuffer, rx_rbp, CDC_RX_BUFSIZE);
+    rt_ringbuffer_init(&tx_ringbuffer, tx_rbp, CDC_TX_BUFSIZE);
 
     config.baud_rate = BAUD_RATE_115200;
     config.bit_order = BIT_ORDER_LSB;
@@ -595,7 +614,7 @@ void rt_usb_vcom_init(void)
 
     /* register vcom device */
     rt_hw_serial_register(&vcom_serial, "vcom",
-                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
+                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
                           RT_NULL);
 }