Przeglądaj źródła

remove bulk_transfer and intr_transfer

sakumisu 3 lat temu
rodzic
commit
ee026096b1
3 zmienionych plików z 67 dodań i 116 usunięć
  1. 0 38
      core/usbh_core.c
  2. 0 46
      core/usbh_core.h
  3. 67 32
      demo/usb_host.c

+ 0 - 38
core/usbh_core.c

@@ -698,8 +698,6 @@ int usbh_initialize(void)
     return 0;
 }
 
-/* usb host transfer wrapper */
-
 int usbh_control_transfer(usbh_pipe_t pipe, struct usb_setup_packet *setup, uint8_t *buffer)
 {
     struct usbh_urb *urb;
@@ -718,42 +716,6 @@ int usbh_control_transfer(usbh_pipe_t pipe, struct usb_setup_packet *setup, uint
     return ret;
 }
 
-int usbh_bulk_transfer(usbh_pipe_t pipe, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
-{
-    struct usbh_urb *urb;
-    int ret;
-
-    urb = usb_malloc(sizeof(struct usbh_urb));
-    memset(urb, 0, sizeof(struct usbh_urb));
-
-    usbh_bulk_urb_fill(urb, pipe, buffer, buflen, timeout, NULL, NULL);
-
-    ret = usbh_submit_urb(urb);
-    if (ret == 0) {
-        ret = urb->actual_length;
-    }
-    usb_free(urb);
-    return ret;
-}
-
-int usbh_int_transfer(usbh_pipe_t pipe, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
-{
-    struct usbh_urb *urb;
-    int ret;
-
-    urb = usb_malloc(sizeof(struct usbh_urb));
-    memset(urb, 0, sizeof(struct usbh_urb));
-
-    usbh_int_urb_fill(urb, pipe, buffer, buflen, timeout, NULL, NULL);
-
-    ret = usbh_submit_urb(urb);
-    if (ret == 0) {
-        ret = urb->actual_length;
-    }
-    usb_free(urb);
-    return ret;
-}
-
 int lsusb(int argc, char **argv)
 {
     usb_slist_t *i;

+ 0 - 46
core/usbh_core.h

@@ -165,8 +165,6 @@ struct usbh_hub {
 
 int usbh_hport_activate_epx(usbh_pipe_t *pipe, struct usbh_hubport *hport, struct usb_endpoint_descriptor *ep_desc);
 
-/* usb host transfer wrapper */
-
 /**
  * @brief Submit an control transfer to an endpoint.
  * This is a blocking method; this method will not return until the transfer has completed.
@@ -179,50 +177,6 @@ int usbh_hport_activate_epx(usbh_pipe_t *pipe, struct usbh_hubport *hport, struc
  */
 int usbh_control_transfer(usbh_pipe_t pipe, struct usb_setup_packet *setup, uint8_t *buffer);
 
-/**
- * @brief  Submit an bulk transfer to an endpoint.
- * This is a blocking method; this method will not return until the transfer has completed.
- *
- * @param pipe The IN or OUT endpoint pipe info.
- * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
- * @param buflen The length of the data to be sent or received.
- * @param timeout Timeout for transfer, unit is ms.
- * @return On success, a non-negative value is returned that indicates the number
- *   of bytes successfully transferred.  On a failure, a negated errno value
- *   is returned that indicates the nature of the failure:
- *
- *     -EAGAIN - If devices NAKs the transfer (or NYET or other error where
- *              it may be appropriate to restart the entire transaction).
- *     -EPERM  - If the endpoint stalls
- *     -EIO    - On a TX or data toggle error
- *     -EPIPE  - Overrun errors
- *     -ETIMEDOUT  - Sem wait timeout
- *
- */
-int usbh_bulk_transfer(usbh_pipe_t pipe, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
-
-/**
- * @brief  Submit an interrupt transfer to an endpoint.
- * This is a blocking method; this method will not return until the transfer has completed.
- *
- * @param pipe The IN or OUT endpoint pipe info.
- * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
- * @param buflen The length of the data to be sent or received.
- * @param timeout Timeout for transfer, unit is ms.
- * @return On success, a non-negative value is returned that indicates the number
- *   of bytes successfully transferred.  On a failure, a negated errno value
- *   is returned that indicates the nature of the failure:
- *
- *     -EAGAIN - If devices NAKs the transfer (or NYET or other error where
- *              it may be appropriate to restart the entire transaction).
- *     -EPERM  - If the endpoint stalls
- *     -EIO    - On a TX or data toggle error
- *     -EPIPE  - Overrun errors
- *     -ETIMEDOUT  - Sem wait timeout
- *
- */
-int usbh_int_transfer(usbh_pipe_t pipe, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
-
 int usbh_initialize(void);
 struct usbh_hubport *usbh_find_hubport(uint8_t dev_addr);
 void *usbh_find_class_instance(const char *devname);

+ 67 - 32
demo/usb_host.c

@@ -2,9 +2,13 @@
 #include "usbh_cdc_acm.h"
 #include "usbh_hid.h"
 #include "usbh_msc.h"
+#include "usbh_video.h"
 
 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_buffer[512];
 
+struct usbh_urb cdc_bulkin_urb;
+struct usbh_urb cdc_bulkout_urb;
+
 void usbh_cdc_acm_callback(void *arg, int nbytes)
 {
     //struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)arg;
@@ -21,8 +25,6 @@ void usbh_cdc_acm_callback(void *arg, int nbytes)
 int cdc_acm_test(void)
 {
     int ret;
-    usbh_pipe_t bulkin;
-    usbh_pipe_t bulkout;
 
     struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)usbh_find_class_instance("/dev/ttyACM0");
 
@@ -31,16 +33,15 @@ int cdc_acm_test(void)
         return -1;
     }
 
-    bulkin = cdc_acm_class->bulkin;
-    bulkout = cdc_acm_class->bulkout;
-
     memset(cdc_buffer, 0, 512);
-    ret = usbh_bulk_transfer(bulkin, cdc_buffer, 20, 3000);
+
+    usbh_bulk_urb_fill(&cdc_bulkin_urb, cdc_acm_class->bulkin, cdc_buffer, 64, 3000, NULL, NULL);
+    ret = usbh_submit_urb(&cdc_bulkin_urb);
     if (ret < 0) {
         USB_LOG_RAW("bulk in error,ret:%d\r\n", ret);
     } else {
-        USB_LOG_RAW("recv over:%d\r\n", ret);
-        for (size_t i = 0; i < ret; i++) {
+        USB_LOG_RAW("recv over:%d\r\n", cdc_bulkin_urb.actual_length);
+        for (size_t i = 0; i < cdc_bulkin_urb.actual_length; i++) {
             USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
         }
     }
@@ -49,28 +50,22 @@ int cdc_acm_test(void)
     const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
 
     memcpy(cdc_buffer, data1, 8);
-    ret = usbh_bulk_transfer(bulkout, cdc_buffer, 8, 3000);
+    usbh_bulk_urb_fill(&cdc_bulkout_urb, cdc_acm_class->bulkout, cdc_buffer, 8, 3000, NULL, NULL);
+    ret = usbh_submit_urb(&cdc_bulkout_urb);
     if (ret < 0) {
         USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
     } else {
-        USB_LOG_RAW("send over:%d\r\n", ret);
+        USB_LOG_RAW("send over:%d\r\n", cdc_bulkout_urb.actual_length);
     }
 
-#if 0
-    usbh_bulk_async_transfer(bulkin, cdc_buffer, 512, usbh_cdc_acm_callback, cdc_acm_class);
-#else
-    ret = usbh_bulk_transfer(bulkin, cdc_buffer, 512, 3000);
+    usbh_bulk_urb_fill(&cdc_bulkin_urb, cdc_acm_class->bulkin, cdc_buffer, 64, 3000, usbh_cdc_acm_callback, cdc_acm_class);
+    ret = usbh_submit_urb(&cdc_bulkin_urb);
     if (ret < 0) {
         USB_LOG_RAW("bulk in error,ret:%d\r\n", ret);
     } else {
-        USB_LOG_RAW("recv over:%d\r\n", ret);
-        for (size_t i = 0; i < ret; i++) {
-            USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
-        }
     }
-    USB_LOG_RAW("\r\n");
+
     return ret;
-#endif
 }
 #if 0
 #include "ff.h"
@@ -146,7 +141,7 @@ int msc_test(void)
         USB_LOG_RAW("do not find /dev/sda\r\n");
         return -1;
     }
-#if 0
+#if 1
     /* get the partition table */
     ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
     if (ret < 0) {
@@ -170,6 +165,8 @@ int msc_test(void)
 
 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t hid_buffer[128];
 
+struct usbh_urb hid_intin_urb;
+
 void usbh_hid_callback(void *arg, int nbytes)
 {
     //struct usbh_hid *hid_class = (struct usbh_hid *)arg;
@@ -181,6 +178,7 @@ void usbh_hid_callback(void *arg, int nbytes)
     }
 
     USB_LOG_RAW("nbytes:%d\r\n", nbytes);
+    usbh_submit_urb(&hid_intin_urb);
 }
 
 int hid_test(void)
@@ -191,19 +189,55 @@ int hid_test(void)
         USB_LOG_RAW("do not find /dev/input0\r\n");
         return -1;
     }
-#if 0
-    ret = usbh_intr_async_transfer(hid_class->intin, hid_buffer, 8, usbh_hid_callback, hid_class);
-    if (ret < 0) {
-        USB_LOG_RAW("intr asnyc in error,ret:%d\r\n", ret);
+
+    usbh_int_urb_fill(&hid_intin_urb, hid_class->intin, hid_buffer, 8, 0, usbh_hid_callback, hid_class);
+    ret = usbh_submit_urb(&hid_intin_urb);
+    return ret;
+}
+
+USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t video_buffer[10 * 1024]; /* just for reference , use ram larger than 4M */
+
+#define VIDEO_ISO_PACKETS 512
+
+int video_test(void)
+{
+    int ret;
+    struct usbh_video *video_class = (struct usbh_video *)usbh_find_class_instance("/dev/video0");
+    if (video_class == NULL) {
+        USB_LOG_RAW("do not find /dev/video0\r\n");
+        return -1;
     }
-#else
-    ret = usbh_int_transfer(hid_class->intin, hid_buffer, 8, 1000);
-    if (ret < 0) {
-        USB_LOG_RAW("intr in error,ret:%d\r\n", ret);
-        return ret;
+
+    usbh_videostreaming_set_cur_commit(video_class, 1, 1, 160 * 120 * 2, 512); /* select resolution from list ,just for reference now */
+
+    usbh_video_open(video_class, 7); /* select ep mps from altsettings ,just for reference now */
+    usb_osal_msleep(100);
+
+    struct usbh_urb *video_urb = usb_malloc(sizeof(struct usbh_urb) + sizeof(struct usbh_iso_frame_packet) * VIDEO_ISO_PACKETS);
+    if (video_urb == NULL) {
+        USB_LOG_ERR("No memory to alloc urb\r\n");
+        while (1) {
+        }
     }
-    USB_LOG_RAW("recv len:%d\r\n", ret);
-#endif
+
+    uint8_t *tmp_buf = video_buffer;
+    memset(video_urb, 0, sizeof(struct usbh_urb) + sizeof(struct usbh_iso_frame_packet) * VIDEO_ISO_PACKETS);
+    video_urb->pipe = video_class->isoin;
+    video_urb->num_of_iso_packets = VIDEO_ISO_PACKETS;
+    video_urb->timeout = 0xffffffff;
+    for (uint32_t i = 0; i < VIDEO_ISO_PACKETS; i++) {
+        video_urb->iso_packet[i].transfer_buffer = tmp_buf;
+        video_urb->iso_packet[i].transfer_buffer_length = video_class->isoin_mps;
+        //tmp_buf+=video_class->isoin_mps; /* enable this when use ram larger than 4M */
+    }
+    while (1) {
+        ret = usbh_submit_urb(video_urb);
+        if (ret < 0) {
+            USB_LOG_ERR("Fail to submit urb:%d\r\n", ret);
+            break;
+        }
+    }
+    usb_free(video_urb);
     return ret;
 }
 
@@ -223,6 +257,7 @@ static void usbh_class_test_thread(void *argument)
         cdc_acm_test();
         msc_test();
         hid_test();
+        video_test();
     }
 }