Merge branch 'master' into ch32v307
This commit is contained in:
@@ -130,8 +130,11 @@ void usb_device_task(void* param)
|
||||
// RTOS forever loop
|
||||
while (1)
|
||||
{
|
||||
// tinyusb device task
|
||||
// put this thread to waiting state until there is new events
|
||||
tud_task();
|
||||
|
||||
// following code only run if tud_task() process at least 1 event
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +184,7 @@ void cdc_task(void* params)
|
||||
// if ( tud_cdc_connected() )
|
||||
{
|
||||
// There are data available
|
||||
if ( tud_cdc_available() )
|
||||
while ( tud_cdc_available() )
|
||||
{
|
||||
uint8_t buf[64];
|
||||
|
||||
@@ -194,12 +197,13 @@ void cdc_task(void* params)
|
||||
// for throughput test e.g
|
||||
// $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
|
||||
tud_cdc_write(buf, count);
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
|
||||
// For ESP32-S2 this delay is essential to allow idle how to run and reset wdt
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
// For ESP32-Sx this delay is essential to allow idle how to run and reset watchdog
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -39,6 +39,10 @@
|
||||
#error CFG_TUSB_MCU must be defined
|
||||
#endif
|
||||
|
||||
// Use raspberry pio-usb for device
|
||||
// #define CFG_TUD_RPI_PIO_USB 1
|
||||
// #define BOARD_DEVICE_RHPORT_NUM 1
|
||||
|
||||
// RHPort number used for device can be defined by board.mk, default to port 0
|
||||
#ifndef BOARD_DEVICE_RHPORT_NUM
|
||||
#define BOARD_DEVICE_RHPORT_NUM 0
|
||||
|
@@ -132,8 +132,10 @@ void usb_device_task(void* param)
|
||||
// RTOS forever loop
|
||||
while (1)
|
||||
{
|
||||
// tinyusb device task
|
||||
// put this thread to waiting state until there is new events
|
||||
tud_task();
|
||||
|
||||
// following code only run if tud_task() process at least 1 event
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -25,4 +25,6 @@ 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})
|
||||
family_configure_pico_pio_usb_example(${PROJECT})
|
@@ -1,2 +1,3 @@
|
||||
board:mimxrt1060_evk
|
||||
board:mimxrt1064_evk
|
||||
mcu:RP2040
|
@@ -23,10 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// This example runs both host and device concurrently. The USB host looks for
|
||||
// any HID device with reports that are 8 bytes long and then assumes they are
|
||||
// keyboard reports. It translates the keypresses of the reports to ASCII and
|
||||
// transmits it over CDC to the device's host.
|
||||
// This example runs both host and device concurrently. The USB host receive
|
||||
// reports from HID device and print it out over USB Device CDC interface.
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@@ -73,12 +71,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 +86,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 +122,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 +147,140 @@ 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);
|
||||
char tempbuf[256];
|
||||
int count = sprintf(tempbuf, "[%04x:%04x][%u] HID Interface%u, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]);
|
||||
|
||||
// Receive any report and treat it like a keyboard.
|
||||
tud_cdc_write(tempbuf, count);
|
||||
tud_cdc_write_flush();
|
||||
|
||||
// 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) )
|
||||
{
|
||||
tud_cdc_write_str("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);
|
||||
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();
|
||||
}
|
||||
|
||||
// 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(uint8_t dev_addr, 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];
|
||||
(void) dev_addr;
|
||||
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(uint8_t dev_addr, hid_mouse_report_t const * report)
|
||||
{
|
||||
//------------- 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' : '-';
|
||||
|
||||
char tempbuf[32];
|
||||
int count = sprintf(tempbuf, "[%u] %c%c%c %d %d %d\r\n", dev_addr, 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)
|
||||
{
|
||||
(void) len;
|
||||
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
|
||||
|
||||
switch(itf_protocol)
|
||||
{
|
||||
case HID_ITF_PROTOCOL_KEYBOARD:
|
||||
process_kbd_report(dev_addr, (hid_keyboard_report_t const*) report );
|
||||
break;
|
||||
|
||||
case HID_ITF_PROTOCOL_MOUSE:
|
||||
process_mouse_report(dev_addr, (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");
|
||||
tud_cdc_write_str("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)
|
||||
{
|
@@ -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
|
@@ -81,7 +81,6 @@ enum
|
||||
{
|
||||
ITF_NUM_CDC = 0,
|
||||
ITF_NUM_CDC_DATA,
|
||||
ITF_NUM_MSC,
|
||||
ITF_NUM_TOTAL
|
||||
};
|
||||
|
@@ -24,4 +24,7 @@ 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_host_example(${PROJECT})
|
||||
family_configure_host_example(${PROJECT})
|
||||
|
||||
# For rp2040, un-comment to enable pico-pio-usb
|
||||
# family_configure_pico_pio_usb_example(${PROJECT})
|
||||
|
@@ -26,4 +26,7 @@ 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_host_example(${PROJECT})
|
||||
family_configure_host_example(${PROJECT})
|
||||
|
||||
# For rp2040, un-comment to enable pico-pio-usb
|
||||
# family_configure_pico_pio_usb_example(${PROJECT})
|
||||
|
@@ -39,8 +39,15 @@
|
||||
#error CFG_TUSB_MCU must be defined
|
||||
#endif
|
||||
|
||||
// Use raspberry pio-usb for host
|
||||
// #define CFG_TUH_RPI_PIO_USB 1
|
||||
// #define CFG_TUH_RPI_PIO_USB 1
|
||||
|
||||
#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)
|
||||
#elif defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB
|
||||
// rp2040: port0 is native, port 1 for PIO-USB
|
||||
#define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST
|
||||
#else
|
||||
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST
|
||||
#endif
|
||||
|
@@ -25,4 +25,7 @@ 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_host_example(${PROJECT})
|
||||
family_configure_host_example(${PROJECT})
|
||||
|
||||
# For rp2040, un-comment to enable pico-pio-usb
|
||||
# family_configure_pico_pio_usb_example(${PROJECT})
|
||||
|
@@ -199,7 +199,7 @@ flash-xfel: $(BUILD)/$(PROJECT)-sunxi.bin
|
||||
PYOCD_OPTION ?=
|
||||
flash-pyocd: $(BUILD)/$(PROJECT).hex
|
||||
pyocd flash -t $(PYOCD_TARGET) $(PYOCD_OPTION) $<
|
||||
pyocd reset -t $(PYOCD_TARGET)
|
||||
#pyocd reset -t $(PYOCD_TARGET)
|
||||
|
||||
# Flash using openocd
|
||||
OPENOCD_OPTION ?=
|
||||
|
Reference in New Issue
Block a user