summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorhathach <[email protected]>2022-04-29 22:24:36 +0700
committerhathach <[email protected]>2022-04-29 22:24:36 +0700
commit0b30a10ab3c2f1fdcf4fae8f530c80bf5cffaf96 (patch)
treed7457b6e9e2b78c0c47b67eedb742ae997508407 /examples
parent608577e05c9f2e2e8831d8819090ed376d08284b (diff)
configure hid_to_cdc example to build with rp2040 with pio usb as host
Diffstat (limited to 'examples')
-rw-r--r--examples/host/hid_to_cdc/CMakeLists.txt3
-rw-r--r--examples/host/hid_to_cdc/only.txt1
-rw-r--r--examples/host/hid_to_cdc/src/main.c199
-rw-r--r--examples/host/hid_to_cdc/src/tusb_config.h12
4 files changed, 96 insertions, 119 deletions
diff --git a/examples/host/hid_to_cdc/CMakeLists.txt b/examples/host/hid_to_cdc/CMakeLists.txt
index abc4d91da..ff9f58c4c 100644
--- a/examples/host/hid_to_cdc/CMakeLists.txt
+++ b/examples/host/hid_to_cdc/CMakeLists.txt
@@ -25,4 +25,5 @@ target_include_directories(${PROJECT} PUBLIC
# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
-family_configure_device_example(${PROJECT}) \ No newline at end of file
+family_configure_device_example(${PROJECT})
+family_configure_host_example(${PROJECT})
diff --git a/examples/host/hid_to_cdc/only.txt b/examples/host/hid_to_cdc/only.txt
index 78d94e3ce..6ee8e3fde 100644
--- a/examples/host/hid_to_cdc/only.txt
+++ b/examples/host/hid_to_cdc/only.txt
@@ -1,2 +1,3 @@
board:mimxrt1060_evk
board:mimxrt1064_evk
+mcu:RP2040
diff --git a/examples/host/hid_to_cdc/src/main.c b/examples/host/hid_to_cdc/src/main.c
index cdf5d264e..f0e78887b 100644
--- a/examples/host/hid_to_cdc/src/main.c
+++ b/examples/host/hid_to_cdc/src/main.c
@@ -73,12 +73,14 @@ enum {
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
void led_blinking_task(void);
-void cdc_task(void);
/*------------- MAIN -------------*/
int main(void)
{
board_init();
+
+ printf("TinyUSB Host HID <-> Device CDC Example\r\n");
+
tusb_init();
while (1)
@@ -86,15 +88,13 @@ int main(void)
tud_task(); // tinyusb device task
tuh_task(); // tinyusb host task
led_blinking_task();
-
- cdc_task();
}
return 0;
}
//--------------------------------------------------------------------+
-// Device callbacks
+// Device CDC
//--------------------------------------------------------------------+
// Invoked when device is mounted
@@ -124,8 +124,20 @@ void tud_resume_cb(void)
blink_interval_ms = BLINK_MOUNTED;
}
+// Invoked when CDC interface received data from host
+void tud_cdc_rx_cb(uint8_t itf)
+{
+ (void) itf;
+
+ char buf[64];
+ uint32_t count = tud_cdc_read(buf, sizeof(buf));
+
+ // TODO control LED on keyboard of host stack
+ (void) count;
+}
+
//--------------------------------------------------------------------+
-// Host callbacks
+// Host HID
//--------------------------------------------------------------------+
// Invoked when device with hid interface is mounted
@@ -137,169 +149,138 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
{
(void)desc_report;
(void)desc_len;
+
+ // Interface protocol (hid_interface_protocol_enum_t)
+ const char* protocol_str[] = { "None", "Keyboard", "Mouse" };
+ uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
+
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
- printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
- printf("VID = %04x, PID = %04x\r\n", vid, pid);
+ printf("[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]);
- // Receive any report and treat it like a keyboard.
+ // Receive report from boot keyboard & mouse only
// tuh_hid_report_received_cb() will be invoked when report is available
- if ( !tuh_hid_receive_report(dev_addr, instance) )
+ if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD || itf_protocol == HID_ITF_PROTOCOL_MOUSE)
{
- printf("Error: cannot request to receive report\r\n");
+ if ( !tuh_hid_receive_report(dev_addr, instance) )
+ {
+ printf("Error: cannot request report\r\n");
+ }
}
}
// Invoked when device with hid interface is un-mounted
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{
- printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
+ printf("[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance);
}
// keycodes from last report to check if key is holding or newly pressed
uint8_t last_keycodes[6] = {0};
// look up new key in previous keys
-static inline bool key_in_last_report(const uint8_t key_arr[6], uint8_t keycode)
+static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8_t keycode)
{
for(uint8_t i=0; i<6; i++)
{
- if (key_arr[i] == keycode) return true;
+ if (report->keycode[i] == keycode) return true;
}
return false;
}
-// Invoked when received report from device via interrupt endpoint
-void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
-{
- if (len != 8)
- {
- char ch_num;
-
- tud_cdc_write_str("incorrect report len: ");
-
- if ( len > 10 )
- {
- ch_num = '0' + (len / 10);
- tud_cdc_write(&ch_num, 1);
- len = len % 10;
- }
- ch_num = '0' + len;
- tud_cdc_write(&ch_num, 1);
-
- tud_cdc_write_str("\r\n");
- tud_cdc_write_flush();
-
- // Don't request a new report for a wrong sized endpoint.
- return;
- }
-
- uint8_t const modifiers = report[0];
+// convert hid keycode to ascii and print via usb device CDC (ignore non-printable)
+static void process_kbd_report(hid_keyboard_report_t const *report)
+{
+ static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released
bool flush = false;
- for (int i = 2; i < 8; i++)
+ for(uint8_t i=0; i<6; i++)
{
- uint8_t keycode = report[i];
-
- if (keycode)
+ uint8_t keycode = report->keycode[i];
+ if ( keycode )
{
- if ( key_in_last_report(last_keycodes, keycode) )
+ if ( find_key_in_report(&prev_report, keycode) )
{
// exist in previous report means the current key is holding
- // do nothing
}else
{
// not existed in previous report means the current key is pressed
- // Only print keycodes 0 - 128.
- if (keycode < 128)
- {
- // remap the key code for Colemak layout so @tannewt can type.
- #ifdef KEYBOARD_COLEMAK
- uint8_t colemak_key_code = colemak[keycode];
- if (colemak_key_code != 0) keycode = colemak_key_code;
- #endif
- bool const is_shift = modifiers & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
- char c = keycode2ascii[keycode][is_shift ? 1 : 0];
- if (c)
- {
- if (c == '\n') tud_cdc_write("\r", 1);
- tud_cdc_write(&c, 1);
- flush = true;
- }
+ // remap the key code for Colemak layout
+ #ifdef KEYBOARD_COLEMAK
+ uint8_t colemak_key_code = colemak[keycode];
+ if (colemak_key_code != 0) keycode = colemak_key_code;
+ #endif
+
+ bool const is_shift = report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
+ uint8_t ch = keycode2ascii[keycode][is_shift ? 1 : 0];
+
+ if (ch)
+ {
+ if (ch == '\n') tud_cdc_write("\r", 1);
+ tud_cdc_write(&ch, 1);
+ flush = true;
}
}
}
+ // TODO example skips key released
}
if (flush) tud_cdc_write_flush();
- // save current report
- memcpy(last_keycodes, report+2, 6);
-
- // continue to request to receive report
- if ( !tuh_hid_receive_report(dev_addr, instance) )
- {
- printf("Error: cannot request to receive report\r\n");
- }
+ prev_report = *report;
}
+// send mouse report to usb device CDC
+static void process_mouse_report(hid_mouse_report_t const * report)
+{
+ static hid_mouse_report_t prev_report = { 0 };
+ char tempbuf[32];
+ int count;
-//--------------------------------------------------------------------+
-// USB CDC
-//--------------------------------------------------------------------+
-void cdc_task(void)
-{
- // connected() check for DTR bit
- // Most but not all terminal client set this when making connection
- // if ( tud_cdc_connected() )
- {
- // connected and there are data available
- if ( tud_cdc_available() )
- {
- // read datas
- char buf[64];
- uint32_t count = tud_cdc_read(buf, sizeof(buf));
- (void) count;
+ //------------- button state -------------//
+ //uint8_t button_changed_mask = report->buttons ^ prev_report.buttons;
+ char l = report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-';
+ char m = report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-';
+ char r = report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-';
- // Echo back
- // Note: Skip echo by commenting out write() and write_flush()
- // for throughput test e.g
- // $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
- tud_cdc_write(buf, count);
- tud_cdc_write_flush();
- }
- }
+ count = sprintf(tempbuf, " %c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel);
+
+ tud_cdc_write(tempbuf, count);
+ tud_cdc_write_flush();
}
-// Invoked when cdc when line state changed e.g connected/disconnected
-void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
+// Invoked when received report from device via interrupt endpoint
+void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
{
- (void) itf;
- (void) rts;
+ uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
- // TODO set some indicator
- if ( dtr )
+ switch(itf_protocol)
{
- // Terminal connected
- }else
- {
- // Terminal disconnected
+ case HID_ITF_PROTOCOL_KEYBOARD:
+ process_kbd_report( (hid_keyboard_report_t const*) report );
+ break;
+
+ case HID_ITF_PROTOCOL_MOUSE:
+ process_mouse_report( (hid_mouse_report_t const*) report );
+ break;
+
+ default: break;
}
-}
-// Invoked when CDC interface received data from host
-void tud_cdc_rx_cb(uint8_t itf)
-{
- (void) itf;
+ // continue to request to receive report
+ if ( !tuh_hid_receive_report(dev_addr, instance) )
+ {
+ printf("Error: cannot request report\r\n");
+ }
}
//--------------------------------------------------------------------+
-// BLINKING TASK
+// Blinking Task
//--------------------------------------------------------------------+
void led_blinking_task(void)
{
diff --git a/examples/host/hid_to_cdc/src/tusb_config.h b/examples/host/hid_to_cdc/src/tusb_config.h
index 9a35ffc30..0fa4899a9 100644
--- a/examples/host/hid_to_cdc/src/tusb_config.h
+++ b/examples/host/hid_to_cdc/src/tusb_config.h
@@ -49,6 +49,9 @@
#define BOARD_HOST_RHPORT_NUM 1
#endif
+// Use raspberry pio-usb for host
+#define CFG_TUH_RPI_PIO_USB 1
+
// RHPort max operational speed can defined by board.mk
// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed
#ifndef BOARD_DEVICE_RHPORT_SPEED
@@ -124,10 +127,6 @@
//------------- CLASS -------------//
#define CFG_TUD_CDC 1
-#define CFG_TUD_MSC 0
-#define CFG_TUD_HID 0
-#define CFG_TUD_MIDI 0
-#define CFG_TUD_VENDOR 0
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
@@ -144,14 +143,9 @@
#define CFG_TUH_ENUMERATION_BUFSIZE 256
#define CFG_TUH_HUB 1
-#define CFG_TUH_CDC 0
-#define CFG_TUH_MSC 0
-#define CFG_TUH_VENDOR 0
-
// max device support (excluding hub device)
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports
-//------------- HID -------------//
#define CFG_TUH_HID 4
#define CFG_TUH_HID_EPIN_BUFSIZE 64
#define CFG_TUH_HID_EPOUT_BUFSIZE 64