summaryrefslogtreecommitdiff
path: root/examples/host/cdc_msc_hid/src
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2020-09-04 10:46:48 +0700
committerGitHub <[email protected]>2020-09-04 10:46:48 +0700
commited7a0de3abd12b7fefaeb2fc97607ae7d73d5419 (patch)
tree15e3453593465f5879a75fee50e2dbb765d7c6a9 /examples/host/cdc_msc_hid/src
parentf10b8145afb6d7b78fbf31b525951b4137e1d774 (diff)
parenta8e538efe7d11291e32e016fad5910ebaf9eac43 (diff)
Merge pull request #505 from hathach/update-host
Update host
Diffstat (limited to 'examples/host/cdc_msc_hid/src')
-rw-r--r--examples/host/cdc_msc_hid/src/main.c101
-rw-r--r--examples/host/cdc_msc_hid/src/msc_app.c36
-rw-r--r--examples/host/cdc_msc_hid/src/tusb_config.h27
3 files changed, 109 insertions, 55 deletions
diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c
index 0fbbd4534..4200cc5e0 100644
--- a/examples/host/cdc_msc_hid/src/main.c
+++ b/examples/host/cdc_msc_hid/src/main.c
@@ -58,7 +58,7 @@ int main(void)
cdc_task();
#endif
-#if CFG_TUD_HID
+#if CFG_TUH_HID_KEYBOARD || CFG_TUH_HID_MOUSE
hid_task();
#endif
}
@@ -75,7 +75,7 @@ CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
void tuh_mount_cb(uint8_t dev_addr)
{
// application set-up
- printf("\na CDC device (address %d) is mounted\n", dev_addr);
+ printf("A device with address %d is mounted\r\n", dev_addr);
tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer
}
@@ -83,7 +83,7 @@ void tuh_mount_cb(uint8_t dev_addr)
void tuh_umount_cb(uint8_t dev_addr)
{
// application tear-down
- printf("\na CDC device (address %d) is unmounted \n", dev_addr);
+ printf("A device with address %d is unmounted \r\n", dev_addr);
}
// invoked ISR context
@@ -110,27 +110,83 @@ void cdc_task(void)
// USB HID
//--------------------------------------------------------------------+
#if CFG_TUH_HID_KEYBOARD
-void hid_task(void)
+
+uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII };
+
+// look up new key in previous keys
+static inline bool find_key_in_report(hid_keyboard_report_t const *p_report, uint8_t keycode)
{
+ for(uint8_t i=0; i<6; i++)
+ {
+ if (p_report->keycode[i] == keycode) return true;
+ }
+
+ return false;
+}
+
+static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report)
+{
+ static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released
+
+ //------------- example code ignore control (non-printable) key affects -------------//
+ for(uint8_t i=0; i<6; i++)
+ {
+ if ( p_new_report->keycode[i] )
+ {
+ if ( find_key_in_report(&prev_report, p_new_report->keycode[i]) )
+ {
+ // exist in previous report means the current key is holding
+ }else
+ {
+ // not existed in previous report means the current key is pressed
+ bool const is_shift = p_new_report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
+ uint8_t ch = keycode2ascii[p_new_report->keycode[i]][is_shift ? 1 : 0];
+ putchar(ch);
+ if ( ch == '\r' ) putchar('\n'); // added new line for enter key
+
+ fflush(stdout); // flush right away, else nanolib will wait for newline
+ }
+ }
+ // TODO example skips key released
+ }
+
+ prev_report = *p_new_report;
+}
+CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;
+
+void hid_task(void)
+{
+ uint8_t const addr = 1;
+ if ( tuh_hid_keyboard_is_mounted(addr) )
+ {
+ if ( !tuh_hid_keyboard_is_busy(addr) )
+ {
+ process_kbd_report(&usb_keyboard_report);
+ tuh_hid_keyboard_get_report(addr, &usb_keyboard_report);
+ }
+ }
}
void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr)
{
// application set-up
- printf("\na Keyboard device (address %d) is mounted\n", dev_addr);
+ printf("A Keyboard device (address %d) is mounted\r\n", dev_addr);
+
+ tuh_hid_keyboard_get_report(dev_addr, &usb_keyboard_report);
}
void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr)
{
// application tear-down
- printf("\na Keyboard device (address %d) is unmounted\n", dev_addr);
+ printf("A Keyboard device (address %d) is unmounted\r\n", dev_addr);
}
// invoked ISR context
void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event)
{
-
+ (void) dev_addr;
+ (void) event;
}
#endif
@@ -139,18 +195,20 @@ void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event)
void tuh_hid_mouse_mounted_cb(uint8_t dev_addr)
{
// application set-up
- printf("\na Mouse device (address %d) is mounted\n", dev_addr);
+ printf("A Mouse device (address %d) is mounted\r\n", dev_addr);
}
void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr)
{
// application tear-down
- printf("\na Mouse device (address %d) is unmounted\n", dev_addr);
+ printf("A Mouse device (address %d) is unmounted\r\n", dev_addr);
}
// invoked ISR context
void tuh_hid_mouse_isr(uint8_t dev_addr, xfer_result_t event)
{
+ (void) dev_addr;
+ (void) event;
}
#endif
@@ -187,16 +245,23 @@ void print_greeting(void)
[OPT_OS_FREERTOS] = "FreeRTOS",
};
- printf("\n--------------------------------------------------------------------\n");
- printf("- Host example\n");
- printf("- if you find any bugs or get any questions, feel free to file an\n");
- printf("- issue at https://github.com/hathach/tinyusb\n");
- printf("--------------------------------------------------------------------\n\n");
+ printf("--------------------------------------------------------------------\r\n");
+ printf("- Host example\r\n");
+ printf("- if you find any bugs or get any questions, feel free to file an\r\n");
+ printf("- issue at https://github.com/hathach/tinyusb\r\n");
+ printf("--------------------------------------------------------------------\r\n\r\n");
+
+ printf("This Host demo is configured to support:\r\n");
+ printf(" - RTOS = %s\r\n", rtos_name[CFG_TUSB_OS]);
+
+#if CFG_TUH_CDC
+ printf(" - Communication Device Class\r\n");
+#endif
+
+#if CFG_TUH_MSC
+ printf(" - Mass Storage\r\n");
+#endif
- printf("This Host demo is configured to support:");
- printf(" - RTOS = %s\n", rtos_name[CFG_TUSB_OS]);
-// if (CFG_TUH_CDC ) puts(" - Communication Device Class");
-// if (CFG_TUH_MSC ) puts(" - Mass Storage");
// if (CFG_TUH_HID_KEYBOARD ) puts(" - HID Keyboard");
// if (CFG_TUH_HID_MOUSE ) puts(" - HID Mouse");
}
diff --git a/examples/host/cdc_msc_hid/src/msc_app.c b/examples/host/cdc_msc_hid/src/msc_app.c
index 20ac88744..62bd961c1 100644
--- a/examples/host/cdc_msc_hid/src/msc_app.c
+++ b/examples/host/cdc_msc_hid/src/msc_app.c
@@ -35,23 +35,24 @@
//------------- IMPLEMENTATION -------------//
void tuh_msc_mounted_cb(uint8_t dev_addr)
{
- puts("\na MassStorage device is mounted");
+ printf("A MassStorage device is mounted\r\n");
-// //------------- Disk Information -------------//
-// // SCSI VendorID[8] & ProductID[16] from Inquiry Command
-// uint8_t const* p_vendor = tuh_msc_get_vendor_name(dev_addr);
-// uint8_t const* p_product = tuh_msc_get_product_name(dev_addr);
-//
-// for(uint8_t i=0; i<8; i++) putchar(p_vendor[i]);
-//
-// putchar(' ');
-// for(uint8_t i=0; i<16; i++) putchar(p_product[i]);
-// putchar('\n');
-//
-// uint32_t last_lba, block_size;
-// tuh_msc_get_capacity(dev_addr, &last_lba, &block_size);
-// printf("Disk Size: %d MB\n", (last_lba+1)/ ((1024*1024)/block_size) );
-// printf("LBA 0-0x%X Block Size: %d\n", last_lba, block_size);
+ //------------- Disk Information -------------//
+ // SCSI VendorID[8] & ProductID[16] from Inquiry Command
+ uint8_t const* p_vendor = tuh_msc_get_vendor_name(dev_addr);
+ uint8_t const* p_product = tuh_msc_get_product_name(dev_addr);
+
+ for(uint8_t i=0; i<8; i++) putchar(p_vendor[i]);
+
+ putchar(' ');
+ for(uint8_t i=0; i<16; i++) putchar(p_product[i]);
+ putchar('\n');
+
+ uint32_t last_lba = 0;
+ uint32_t block_size = 0;
+ tuh_msc_get_capacity(dev_addr, &last_lba, &block_size);
+ printf("Disk Size: %ld MB\r\n", (last_lba+1)/ ((1024*1024)/block_size) );
+ printf("LBA 0-0x%lX Block Size: %ld\r\n", last_lba, block_size);
//
// //------------- file system (only 1 LUN support) -------------//
// uint8_t phy_disk = dev_addr-1;
@@ -81,7 +82,8 @@ void tuh_msc_mounted_cb(uint8_t dev_addr)
void tuh_msc_unmounted_cb(uint8_t dev_addr)
{
- puts("\na MassStorage device is unmounted");
+ (void) dev_addr;
+ printf("A MassStorage device is unmounted\r\n");
// uint8_t phy_disk = dev_addr-1;
//
diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h
index aa259dafd..6604623b1 100644
--- a/examples/host/cdc_msc_hid/src/tusb_config.h
+++ b/examples/host/cdc_msc_hid/src/tusb_config.h
@@ -40,9 +40,9 @@
#endif
#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
-#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
+ #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED)
#else
-#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
+ #define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
#endif
#define CFG_TUSB_OS OPT_OS_NONE
@@ -69,29 +69,16 @@
// CONFIGURATION
//--------------------------------------------------------------------
-#define CFG_TUH_HUB 1
+#define CFG_TUH_HUB 0
#define CFG_TUH_CDC 1
-#define CFG_TUH_HID_KEYBOARD 0
-#define CFG_TUH_HID_MOUSE 0
+#define CFG_TUH_HID_KEYBOARD 1
+#define CFG_TUH_HID_MOUSE 1
#define CFG_TUSB_HOST_HID_GENERIC 0 // (not yet supported)
-#define CFG_TUH_MSC 0
+#define CFG_TUH_MSC 1
+#define CFG_TUH_VENDOR 0
#define CFG_TUSB_HOST_DEVICE_MAX (CFG_TUH_HUB ? 5 : 1) // normal hub has 4 ports
-//------------- CLASS -------------//
-#define CFG_TUD_CDC 0
-#define CFG_TUD_MSC 0
-#define CFG_TUD_HID 0
-#define CFG_TUD_VENDOR 0
-
-// CDC FIFO size of TX and RX
-#define CFG_TUD_CDC_RX_BUFSIZE 64
-#define CFG_TUD_CDC_TX_BUFSIZE 64
-
-// MSC Buffer size of Device Mass storage
-#define CFG_TUD_MSC_BUFSIZE 512
-
-
#ifdef __cplusplus
}
#endif