]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/commitdiff
Better hack for EP1 RX
authorMartin Meloun <meloumar@cmp.felk.cvut.cz>
Tue, 13 Aug 2013 14:19:24 +0000 (16:19 +0200)
committerMartin Meloun <meloumar@cmp.felk.cvut.cz>
Tue, 13 Aug 2013 14:20:18 +0000 (16:20 +0200)
Clean solution is more complicated (also mess in sysless) and this will do for now.

sw/app/rocon/appl_usb.c

index bf56995e2e9d18df872dd69ca7b8b3930d69eb9a..90b8865d8387175049c842a004f9620f288f78ca 100644 (file)
@@ -277,6 +277,28 @@ int usb_app_init(void)
   return 0;
 }
 
+int usb_check_ep1()
+{
+  if (usb_device.ep_events & MASK_EP1RX)
+  {
+    usb_device.ep_events &= ~MASK_EP1RX;
+    //TODO: Use some field in the structure, probably flags
+    ep1_rx_ready = 1;
+  }
+
+  /* Respond if there is something to send and RX is ready */
+  if (ep1_rx_ready && ep1_rx_index != 0)
+  {
+    usb_udev_write_endpoint(&eps[0], ep1_rx_buff, ep1_rx_index);
+    ep1_rx_index = 0;
+    ep1_rx_ready = 0;
+    CLR_OUT_PIN(LED_PORT, LED2_BIT);
+    return 1;
+  }
+
+  return 0;
+}
+
 int usb_app_poll(void)
 {
   int active = usb_active;
@@ -285,7 +307,7 @@ int usb_app_poll(void)
   usb_check_events(&usb_device);
   usb_control_response(&usb_device);
 
-  /* Check if host sent us something */
+  /* Check TX endpoint */
   if (usb_device.ep_events & MASK_EP1TX)
   {
     ep1_tx_chars = usb_udev_read_endpoint(&eps[1], ep1_tx_buff, USB_MAX_PACKET);
@@ -295,21 +317,8 @@ int usb_app_poll(void)
     usb_active = 1;
   }
 
-  if (usb_device.ep_events & MASK_EP1RX)
-  {
-    usb_device.ep_events &= ~MASK_EP1RX;
-    ep1_rx_ready = 1;
-  }
-
-  /* Respond if there is something to send and RX is ready */
-  if (ep1_rx_ready && ep1_rx_index != 0)
-  {
-    usb_udev_write_endpoint(&eps[0], ep1_rx_buff, ep1_rx_index);
-    ep1_rx_index = 0;
-    ep1_rx_ready = 0;
-    CLR_OUT_PIN(LED_PORT, LED2_BIT);
-    usb_active = 1;
-  }
+  /* Check RX endpoint */
+  usb_active |= usb_check_ep1();
 
   return active;
 }
@@ -334,11 +343,13 @@ int cmd_io_putc_usbcon(struct cmd_io *cmd_io, int ch)
 {
   if (ep1_rx_index >= USB_MAX_PACKET)
   {
-    /* Poll USB and return -1
-     * FIXME: Ugly and hackish. Should be in ISR of course.
-     */
-    usb_app_poll();
-    return -1;
+    /* Check EP1 status and return -1 if unavailable */
+    usb_check_events(&usb_device);
+    usb_check_ep1();
+
+    /* Check again if it wasn't emptied */
+    if (ep1_rx_index >= USB_MAX_PACKET)
+      return -1;
   }
 
   ep1_rx_buff[ep1_rx_index++] = (unsigned char)ch;