summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2023-02-05 15:16:40 +0800
committersakimisu <[email protected]>2023-02-06 21:08:58 +0800
commite37d5d3866822c30d2405608bc12c8e13b6b6ee0 (patch)
tree5ed58752ccd66158e270feac9705a36bbc106ca9
parent1330b98ad8b570c51059ede98c9f68c6b1d8cbea (diff)
split host demo into standalone thread
-rw-r--r--demo/usb_host.c314
1 files changed, 186 insertions, 128 deletions
diff --git a/demo/usb_host.c b/demo/usb_host.c
index 9ae79845..0abc4c22 100644
--- a/demo/usb_host.c
+++ b/demo/usb_host.c
@@ -3,7 +3,16 @@
#include "usbh_hid.h"
#include "usbh_msc.h"
#include "usbh_video.h"
+#include "usbh_audio.h"
+#define TEST_USBH_CDC_ACM 1
+#define TEST_USBH_HID 1
+#define TEST_USBH_MSC 1
+#define TEST_USBH_MSC_FATFS 0
+#define TEST_USBH_AUDIO 0
+#define TEST_USBH_VIDEO 0
+
+#if TEST_USBH_CDC_ACM
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_buffer[512];
struct usbh_urb cdc_bulkin_urb;
@@ -22,52 +31,119 @@ void usbh_cdc_acm_callback(void *arg, int nbytes)
USB_LOG_RAW("nbytes:%d\r\n", nbytes);
}
-int cdc_acm_test(void)
+static void usbh_cdc_acm_thread(void *argument)
{
int ret;
+ struct usbh_cdc_acm *cdc_acm_class;
- struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)usbh_find_class_instance("/dev/ttyACM0");
+ while (1) {
+ // clang-format off
+find_class:
+ // clang-format on
+ cdc_acm_class = (struct usbh_cdc_acm *)usbh_find_class_instance("/dev/ttyACM0");
+ if (cdc_acm_class == NULL) {
+ USB_LOG_RAW("do not find /dev/ttyACM0\r\n");
+ usb_osal_msleep(1000);
+ continue;
+ }
+ memset(cdc_buffer, 0, 512);
- if (cdc_acm_class == NULL) {
- USB_LOG_RAW("do not find /dev/ttyACM0\r\n");
- return -1;
- }
+ 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", 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]);
+ }
+ }
- memset(cdc_buffer, 0, 512);
+ USB_LOG_RAW("\r\n");
+ const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
- 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", 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]);
+ memcpy(cdc_buffer, data1, 8);
+ 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", cdc_bulkout_urb.actual_length);
}
- }
- USB_LOG_RAW("\r\n");
- const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
+ 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 {
+ }
- memcpy(cdc_buffer, data1, 8);
- 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", cdc_bulkout_urb.actual_length);
+ while (1) {
+ cdc_acm_class = (struct usbh_cdc_acm *)usbh_find_class_instance("/dev/ttyACM0");
+ if (cdc_acm_class == NULL) {
+ goto find_class;
+ }
+ usb_osal_msleep(1000);
+ }
}
+}
+#endif
- 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 {
+#if TEST_USBH_HID
+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;
+
+ if (nbytes > 0) {
+ for (size_t i = 0; i < nbytes; i++) {
+ USB_LOG_RAW("0x%02x ", hid_buffer[i]);
+ }
}
- return ret;
+ USB_LOG_RAW("nbytes:%d\r\n", nbytes);
+ usbh_submit_urb(&hid_intin_urb);
}
-#if 0
+
+static void usbh_hid_thread(void *argument)
+{
+ int ret;
+ struct usbh_hid *hid_class;
+
+ while (1) {
+ // clang-format off
+find_class:
+ // clang-format on
+ hid_class = (struct usbh_hid *)usbh_find_class_instance("/dev/input0");
+ if (hid_class == NULL) {
+ USB_LOG_RAW("do not find /dev/input0\r\n");
+ usb_osal_msleep(1500);
+ continue;
+ }
+ 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);
+ if (ret < 0) {
+ usb_osal_msleep(1500);
+ goto find_class;
+ }
+
+ while (1) {
+ hid_class = (struct usbh_hid *)usbh_find_class_instance("/dev/input0");
+ if (hid_class == NULL) {
+ goto find_class;
+ }
+ usb_osal_msleep(1500);
+ }
+ }
+}
+#endif
+
+#if TEST_USBH_MSC
+
+#if TEST_USBH_MSC_FATFS
#include "ff.h"
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[25 * 100];
@@ -133,128 +209,110 @@ unmount:
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t partition_table[512];
-int msc_test(void)
+static void usbh_msc_thread(void *argument)
{
int ret;
- struct usbh_msc *msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
- if (msc_class == NULL) {
- USB_LOG_RAW("do not find /dev/sda\r\n");
- return -1;
- }
+ struct usbh_msc *msc_class;
+
+ while (1) {
+ // clang-format off
+find_class:
+ // clang-format on
+ msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
+ if (msc_class == NULL) {
+ USB_LOG_RAW("do not find /dev/sda\r\n");
+ usb_osal_msleep(2000);
+ continue;
+ }
+
#if 1
- /* get the partition table */
- ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
- if (ret < 0) {
- USB_LOG_RAW("scsi_read10 error,ret:%d\r\n", ret);
- return ret;
- }
- for (uint32_t i = 0; i < 512; i++) {
- if (i % 16 == 0) {
- USB_LOG_RAW("\r\n");
+ /* get the partition table */
+ ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
+ if (ret < 0) {
+ USB_LOG_RAW("scsi_read10 error,ret:%d\r\n", ret);
+ usb_osal_msleep(2000);
+ goto find_class;
}
- USB_LOG_RAW("%02x ", partition_table[i]);
- }
- USB_LOG_RAW("\r\n");
+ for (uint32_t i = 0; i < 512; i++) {
+ if (i % 16 == 0) {
+ USB_LOG_RAW("\r\n");
+ }
+ USB_LOG_RAW("%02x ", partition_table[i]);
+ }
+ USB_LOG_RAW("\r\n");
#endif
-#if 0
- usb_msc_fatfs_test();
+#if TEST_USBH_MSC_FATFS
+ usb_msc_fatfs_test();
#endif
- return ret;
-}
-
-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;
-
- if (nbytes > 0) {
- for (size_t i = 0; i < nbytes; i++) {
- USB_LOG_RAW("0x%02x ", hid_buffer[i]);
+ while (1) {
+ msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
+ if (msc_class == NULL) {
+ goto find_class;
+ }
+ usb_osal_msleep(2000);
}
}
-
- USB_LOG_RAW("nbytes:%d\r\n", nbytes);
- usbh_submit_urb(&hid_intin_urb);
}
+#endif
-int hid_test(void)
+void usbh_cdc_acm_run(struct usbh_cdc_acm *cdc_acm_class)
{
- int ret;
- struct usbh_hid *hid_class = (struct usbh_hid *)usbh_find_class_instance("/dev/input0");
- if (hid_class == NULL) {
- USB_LOG_RAW("do not find /dev/input0\r\n");
- return -1;
- }
+}
- 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;
+void usbh_cdc_acm_stop(struct usbh_cdc_acm *cdc_acm_class)
+{
}
-#if 0
-USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t video_buffer[10 * 1024]; /* just for reference , use ram larger than 4M */
+void usbh_hid_run(struct usbh_hid *hid_class)
+{
+}
-#define VIDEO_ISO_PACKETS 512
+void usbh_hid_stop(struct usbh_hid *hid_class)
+{
+}
-int video_test(void)
+void usbh_msc_run(struct usbh_msc *msc_class)
{
- 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;
- }
+}
- usbh_videostreaming_set_cur_commit(video_class, 1, 1, 160 * 120 * 2, 512); /* select resolution from list ,just for reference now */
+void usbh_msc_stop(struct usbh_msc *msc_class)
+{
+}
- usbh_video_open(video_class, 7); /* select ep mps from altsettings ,just for reference now */
- usb_osal_msleep(100);
+void usbh_audio_run(struct usbh_audio *audio_class)
+{
+}
- 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) {
- }
- }
+void usbh_audio_stop(struct usbh_audio *audio_class)
+{
+}
- 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;
+void usbh_video_run(struct usbh_video *video_class)
+{
}
-#endif
-static void usbh_class_test_thread(void *argument)
+
+void usbh_video_stop(struct usbh_video *video_class)
{
- while (1) {
- printf("helloworld\r\n");
- usb_osal_msleep(1000);
- cdc_acm_test();
- msc_test();
- hid_test();
- //video_test();
- }
}
void usbh_class_test(void)
{
- usb_osal_thread_create("usbh_test", 4096, CONFIG_USBHOST_PSC_PRIO + 1, usbh_class_test_thread, NULL);
+#if TEST_USBH_CDC_ACM
+ usb_osal_thread_create("usbh_cdc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_acm_thread, NULL);
+#endif
+#if TEST_USBH_HID
+ usb_osal_thread_create("usbh_hid", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_hid_thread, NULL);
+#endif
+#if TEST_USBH_MSC
+ usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, NULL);
+#endif
+#if TEST_USBH_AUDIO
+#error "if you want to use iso, please contact with me"
+ usb_osal_thread_create("usbh_audio", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_audio_thread, NULL);
+#endif
+#if TEST_USBH_VIDEO
+#error "if you want to use iso, please contact with me"
+ usb_osal_thread_create("usbh_video", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_video_thread, NULL);
+#endif
} \ No newline at end of file