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;
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);
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;
}
{
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;