Print raw USB Data to CDC
This commit is contained in:
@@ -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)
|
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})
|
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}
|
||||||
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define CONFIG_H
|
#define CONFIG_H
|
||||||
|
|
||||||
#define USB_DP 16 // USB D+ pin on GP16
|
#define USB_DP 16 // USB D+ pin on GP16
|
||||||
|
#define USB_EN 18
|
||||||
|
|
||||||
#define MAC_MOUSE_X2
|
#define MAC_MOUSE_X2
|
||||||
#define MAC_MOUSE_X1
|
#define MAC_MOUSE_X1
|
||||||
@@ -10,9 +11,9 @@
|
|||||||
#define MAC_MOUSE_SW
|
#define MAC_MOUSE_SW
|
||||||
#define MAC_MOUSE_5V
|
#define MAC_MOUSE_5V
|
||||||
|
|
||||||
#define MAC_KB_CLOCK 3
|
#define MAC_KB_CLOCK 26
|
||||||
#define MAC_KB_DATA 2
|
#define MAC_KB_DATA 27
|
||||||
#define MAC_KB_5V
|
#define MAC_KB_5V 28
|
||||||
|
|
||||||
#define NULL_TRANSITION 0x7b
|
#define NULL_TRANSITION 0x7b
|
||||||
#define CAPS_LOCK 0x73
|
#define CAPS_LOCK 0x73
|
||||||
|
|||||||
117
src/hirsute.c
117
src/hirsute.c
@@ -36,6 +36,7 @@ void sendNextKey();
|
|||||||
void sendKey(uint8_t key);
|
void sendKey(uint8_t key);
|
||||||
|
|
||||||
queue_t key_queue;
|
queue_t key_queue;
|
||||||
|
static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII };
|
||||||
|
|
||||||
void core_one() {
|
void core_one() {
|
||||||
sleep_ms(10);
|
sleep_ms(10);
|
||||||
@@ -43,6 +44,7 @@ void core_one() {
|
|||||||
pio_config.pin_dp = USB_DP;
|
pio_config.pin_dp = USB_DP;
|
||||||
tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_config);
|
tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_config);
|
||||||
tuh_init(1);
|
tuh_init(1);
|
||||||
|
gpio_put(USB_EN, 1); // Enable USB
|
||||||
while (true) {
|
while (true) {
|
||||||
tuh_task();
|
tuh_task();
|
||||||
}
|
}
|
||||||
@@ -59,6 +61,8 @@ int main() {
|
|||||||
// Initialize USB Host Core
|
// Initialize USB Host Core
|
||||||
multicore_reset_core1();
|
multicore_reset_core1();
|
||||||
multicore_launch_core1(core_one);
|
multicore_launch_core1(core_one);
|
||||||
|
gpio_init(USB_EN);
|
||||||
|
gpio_set_dir(USB_EN, GPIO_OUT);\
|
||||||
|
|
||||||
// Initialize USB Device Core for debugging
|
// Initialize USB Device Core for debugging
|
||||||
tud_init(0);
|
tud_init(0);
|
||||||
@@ -73,24 +77,24 @@ int main() {
|
|||||||
while (true){
|
while (true){
|
||||||
tud_task();
|
tud_task();
|
||||||
tud_cdc_write_flush();
|
tud_cdc_write_flush();
|
||||||
switch(readCommand()) {
|
// switch(readCommand()) {
|
||||||
case 0x10:
|
// case 0x10:
|
||||||
// Key Requested
|
// // Key Requested
|
||||||
inquiry();
|
// inquiry();
|
||||||
break;
|
// break;
|
||||||
case 0x14:
|
// case 0x14:
|
||||||
// Send Key
|
// // Send Key
|
||||||
sendNextKey();
|
// sendNextKey();
|
||||||
break;
|
// break;
|
||||||
case 0x16:
|
// case 0x16:
|
||||||
// Requests Model Number
|
// // Requests Model Number
|
||||||
sendByte(0x0b);
|
// sendByte(0x0b);
|
||||||
break;
|
// break;
|
||||||
case 0x36:
|
// case 0x36:
|
||||||
// Test?
|
// // Test?
|
||||||
sendByte(0x7d);
|
// sendByte(0x7d);
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
return 0;
|
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 tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len){
|
||||||
(void) len;
|
(void) len;
|
||||||
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
|
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:
|
case HID_ITF_PROTOCOL_KEYBOARD:
|
||||||
// process_kbd_report(dev_addr, (hid_keyboard_report_t const*) report );
|
// 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;
|
break;
|
||||||
|
|
||||||
case HID_ITF_PROTOCOL_MOUSE:
|
case HID_ITF_PROTOCOL_MOUSE:
|
||||||
|
|||||||
@@ -36,8 +36,8 @@
|
|||||||
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
||||||
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
|
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
|
||||||
|
|
||||||
#define USB_VID 0x2E8A // Raspberry Pi Vendor ID
|
#define USB_VID 0x05AC
|
||||||
#define USB_BCD 0x05AC
|
#define USB_BCD 0x0200
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Device Descriptors
|
// Device Descriptors
|
||||||
@@ -117,11 +117,11 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const* string_desc_arr [] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"Cray", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"Macintosh USB Keyboard Adapter", // 2: Product
|
||||||
"123456789012", // 3: Serials, should use chip ID
|
"DF641C05474E0A29", // 3: Serials, should use chip ID
|
||||||
"TinyUSB CDC", // 4: CDC Interface
|
"TinyUSB CDC", // 4: CDC Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32];
|
||||||
|
|||||||
Reference in New Issue
Block a user