configure hid_to_cdc example to build with rp2040 with pio usb as host

This commit is contained in:
hathach 2022-04-29 22:24:36 +07:00
parent 608577e05c
commit 0b30a10ab3
7 changed files with 148 additions and 141 deletions

View File

@ -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})
family_configure_device_example(${PROJECT})
family_configure_host_example(${PROJECT})

View File

@ -1,2 +1,3 @@
board:mimxrt1060_evk
board:mimxrt1064_evk
mcu:RP2040

View File

@ -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)
// 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)
{
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];
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);
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;
//------------- 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' : '-';
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 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)
{
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
switch(itf_protocol)
{
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;
}
// continue to request to receive report
if ( !tuh_hid_receive_report(dev_addr, instance) )
{
printf("Error: cannot request to receive report\r\n");
printf("Error: cannot request report\r\n");
}
}
//--------------------------------------------------------------------+
// 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;
// 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();
}
}
}
// Invoked when cdc when line state changed e.g connected/disconnected
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
{
(void) itf;
(void) rts;
// TODO set some indicator
if ( dtr )
{
// Terminal connected
}else
{
// Terminal disconnected
}
}
// Invoked when CDC interface received data from host
void tud_cdc_rx_cb(uint8_t itf)
{
(void) itf;
}
//--------------------------------------------------------------------+
// BLINKING TASK
// Blinking Task
//--------------------------------------------------------------------+
void led_blinking_task(void)
{

View File

@ -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

View File

@ -23,23 +23,20 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
set(PICO_TINYUSB_PATH ${TOP})
endif()
#------------------------------------
# Base config for both device and host; wrapped by SDK's tinyusb_common
#------------------------------------
add_library(tinyusb_common_base INTERFACE)
target_sources(tinyusb_common_base INTERFACE
${TOP}/src/tusb.c
${TOP}/src/common/tusb_fifo.c
${TOP}/lib/Pico-PIO-USB/pio_usb.c
${TOP}/lib/Pico-PIO-USB/pio_usb_host.c
${TOP}/lib/Pico-PIO-USB/pio_usb_device.c
${TOP}/lib/Pico-PIO-USB/usb_crc.c
)
target_include_directories(tinyusb_common_base INTERFACE
${TOP}/src
${TOP}/src/common
${TOP}/hw
${TOP}/lib/Pico-PIO-USB
)
target_link_libraries(tinyusb_common_base INTERFACE
@ -47,10 +44,6 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
hardware_irq
hardware_resets
pico_sync
# for usb-pio
hardware_dma
hardware_pio
pico_multicore
)
set(TINYUSB_DEBUG_LEVEL 0)
@ -65,7 +58,35 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
CFG_TUSB_DEBUG=${TINYUSB_DEBUG_LEVEL}
)
#------------------------------------
# PIO USB for both host and device
#------------------------------------
add_library(tinyusb_pio_usb_base INTERFACE)
target_sources(tinyusb_pio_usb_base INTERFACE
${TOP}/lib/Pico-PIO-USB/pio_usb.c
${TOP}/lib/Pico-PIO-USB/pio_usb_host.c
${TOP}/lib/Pico-PIO-USB/pio_usb_device.c
${TOP}/lib/Pico-PIO-USB/usb_crc.c
)
target_include_directories(tinyusb_pio_usb_base INTERFACE
${TOP}/lib/Pico-PIO-USB
)
target_link_libraries(tinyusb_pio_usb_base INTERFACE
hardware_dma
hardware_pio
pico_multicore
)
target_compile_definitions(tinyusb_pio_usb_base INTERFACE
PIO_USB_USE_TINYUSB
)
#------------------------------------
# Base config for device mode; wrapped by SDK's tinyusb_device
#------------------------------------
add_library(tinyusb_device_base INTERFACE)
target_sources(tinyusb_device_base INTERFACE
${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c
@ -87,7 +108,9 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
${TOP}/src/class/video/video_device.c
)
#------------------------------------
# Base config for host mode; wrapped by SDK's tinyusb_host
#------------------------------------
add_library(tinyusb_host_base INTERFACE)
target_sources(tinyusb_host_base INTERFACE
${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c
@ -105,7 +128,10 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
target_compile_definitions(tinyusb_host_base INTERFACE
RP2040_USB_HOST_MODE=1
)
#------------------------------------
# BSP & Additions
#------------------------------------
add_library(tinyusb_bsp INTERFACE)
target_sources(tinyusb_bsp INTERFACE
${TOP}/hw/bsp/rp2040/family.c
@ -139,6 +165,10 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
)
endif()
#------------------------------------
# Functions
#------------------------------------
function(family_configure_target TARGET)
pico_add_extra_outputs(${TARGET})
pico_enable_stdio_uart(${TARGET} 1)
@ -147,12 +177,12 @@ if (NOT TARGET _rp2040_family_inclusion_marker)
function(family_configure_device_example TARGET)
family_configure_target(${TARGET})
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device)
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device tinyusb_pio_usb_base)
endfunction()
function(family_configure_host_example TARGET)
family_configure_target(${TARGET})
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host)
target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host tinyusb_pio_usb_base)
endfunction()
function(family_initialize_project PROJECT DIR)

@ -1 +1 @@
Subproject commit 7e147ad44dbd5038590449418b26fb867024db6c
Subproject commit 1ab409f13bd888b8f1f7c9bf3bd7269d1ccc1c79

View File

@ -630,7 +630,7 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result
if (XFER_RESULT_SUCCESS != result)
{
TU_LOG2("[%u:%u] Control %s\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED");
TU_LOG1("[%u:%u] Control %s\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED");
// terminate transfer if any stage failed
_xfer_complete(dev_addr, result);