diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0fc76df..f4bd55d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,3 +35,12 @@ target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR}) target_link_libraries(${target_name} PRIVATE pico_stdlib pico_multicore pico_pio_usb tinyusb_device tinyusb_host tinyusb_board) pico_add_extra_outputs(${target_name}) + +add_custom_command( + COMMAND picotool load ${target_name}.uf2 && picotool reboot + COMMENT "Loading ${target_name}.uf2 to the board" + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + VERBATIM + POST_BUILD + TARGET ${target_name} +) diff --git a/src/config.h b/src/config.h index 4e92982..dee7a03 100644 --- a/src/config.h +++ b/src/config.h @@ -2,6 +2,7 @@ #define CONFIG_H #define USB_DP 16 // USB D+ pin on GP16 +#define USB_EN 18 #define MAC_MOUSE_X2 #define MAC_MOUSE_X1 @@ -10,9 +11,9 @@ #define MAC_MOUSE_SW #define MAC_MOUSE_5V -#define MAC_KB_CLOCK 3 -#define MAC_KB_DATA 2 -#define MAC_KB_5V +#define MAC_KB_CLOCK 26 +#define MAC_KB_DATA 27 +#define MAC_KB_5V 28 #define NULL_TRANSITION 0x7b #define CAPS_LOCK 0x73 diff --git a/src/hirsute.c b/src/hirsute.c index fbc8d9d..949fd36 100644 --- a/src/hirsute.c +++ b/src/hirsute.c @@ -36,6 +36,7 @@ void sendNextKey(); void sendKey(uint8_t key); queue_t key_queue; +static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; void core_one() { sleep_ms(10); @@ -43,6 +44,7 @@ void core_one() { pio_config.pin_dp = USB_DP; tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_config); tuh_init(1); + gpio_put(USB_EN, 1); // Enable USB while (true) { tuh_task(); } @@ -59,6 +61,8 @@ int main() { // Initialize USB Host Core multicore_reset_core1(); multicore_launch_core1(core_one); + gpio_init(USB_EN); + gpio_set_dir(USB_EN, GPIO_OUT);\ // Initialize USB Device Core for debugging tud_init(0); @@ -73,24 +77,24 @@ int main() { while (true){ tud_task(); tud_cdc_write_flush(); - switch(readCommand()) { - case 0x10: - // Key Requested - inquiry(); - break; - case 0x14: - // Send Key - sendNextKey(); - break; - case 0x16: - // Requests Model Number - sendByte(0x0b); - break; - case 0x36: - // Test? - sendByte(0x7d); - break; - } + // switch(readCommand()) { + // case 0x10: + // // Key Requested + // inquiry(); + // break; + // case 0x14: + // // Send Key + // sendNextKey(); + // break; + // case 0x16: + // // Requests Model Number + // sendByte(0x0b); + // break; + // case 0x36: + // // Test? + // sendByte(0x7d); + // break; + // } } return 0; } @@ -226,6 +230,64 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re } } +// Invoked when device with hid interface is un-mounted +void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) +{ + char tempbuf[256]; + int count = sprintf(tempbuf, "[%u] HID Interface%u is unmounted\r\n", dev_addr, instance); + tud_cdc_write(tempbuf, count); + tud_cdc_write_flush(); +} + +// look up new key in previous keys +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 (report->keycode[i] == keycode) return true; + } + + return false; +} + +// convert hid keycode to ascii and print via usb device CDC (ignore non-printable) +static void process_kbd_report(uint8_t dev_addr, hid_keyboard_report_t const *report) +{ + (void) dev_addr; + static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released + bool flush = false; + + for(uint8_t i=0; i<6; i++) + { + uint8_t keycode = report->keycode[i]; + if ( keycode ) + { + if ( find_key_in_report(&prev_report, keycode) ) + { + // 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 = 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(); + + prev_report = *report; +} + void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){ (void) len; uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); @@ -234,6 +296,25 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons { case HID_ITF_PROTOCOL_KEYBOARD: // process_kbd_report(dev_addr, (hid_keyboard_report_t const*) report ); + // == == ============++ + uint16_t vid, pid; + tuh_vid_pid_get(dev_addr, &vid, &pid); + char tempbuf[256]; + const hid_keyboard_report_t* kbd_report = (hid_keyboard_report_t const*) report; + const uint8_t* keycodes = kbd_report->keycode; + char *s = calloc(6 * 2 + 1, 1); + for(int i = 0; i < 6; ++i){ + char temp[3]; + sprintf(temp, "%02x", keycodes[i]); + s[2 * i] = temp[0]; + s[2 * i + 1] = temp[1]; + } + + // strcat(codeStr, "\r\n"); + int count = sprintf(tempbuf, "[%04x:%04x][%u] %02X %02X\r\n%s\r\n", vid, pid, dev_addr, kbd_report->modifier, kbd_report->reserved, s); + tud_cdc_write(tempbuf, count); + // tud_cdc_write(codeStr, strlen(codeStr)); + tud_cdc_write_flush(); break; case HID_ITF_PROTOCOL_MOUSE: diff --git a/src/usb_descriptors.c b/src/usb_descriptors.c index 14f06bc..be97ab7 100644 --- a/src/usb_descriptors.c +++ b/src/usb_descriptors.c @@ -36,8 +36,8 @@ #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) -#define USB_VID 0x2E8A // Raspberry Pi Vendor ID -#define USB_BCD 0x05AC +#define USB_VID 0x05AC +#define USB_BCD 0x0200 //--------------------------------------------------------------------+ // Device Descriptors @@ -117,11 +117,11 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) // array of pointer to string descriptors char const* string_desc_arr [] = { - (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) - "TinyUSB", // 1: Manufacturer - "TinyUSB Device", // 2: Product - "123456789012", // 3: Serials, should use chip ID - "TinyUSB CDC", // 4: CDC Interface + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "Cray", // 1: Manufacturer + "Macintosh USB Keyboard Adapter", // 2: Product + "DF641C05474E0A29", // 3: Serials, should use chip ID + "TinyUSB CDC", // 4: CDC Interface }; static uint16_t _desc_str[32];