Files
JoyD/ESP32/my_usb_project/main/my_usb_project.c
2026-04-03 09:43:49 +08:00

103 lines
2.7 KiB
C

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "tinyusb.h"
#include "tusb.h"
#include "tusb_config.h"
#include "rust_app_wrapper.h"
static const char *TAG = "USB";
#define TUSB_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_HID * TUD_HID_DESC_LEN)
#define USB_VID 0x30D8
#define USB_PID 0x50A6
#define USB_BCD 0x0100
static tusb_desc_device_t const desc_device =
{
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = USB_BCD,
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USB_VID,
.idProduct = USB_PID,
.bcdDevice = USB_BCD,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01
};
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(HID_ITF_PROTOCOL_KEYBOARD)),
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(HID_ITF_PROTOCOL_MOUSE))
};
uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance)
{
(void) instance;
return desc_hid_report;
}
static char const *hid_string_descriptor[5] = {
(char[]){0x09, 0x04},
"JoyD",
"Virtual Keyboard Mouse",
"123456",
"HID Interface",
};
static uint8_t const hid_configuration_descriptor[] = {
TUD_CONFIG_DESCRIPTOR(1, 2, 0, TUSB_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
TUD_HID_DESCRIPTOR(0, 4, false, sizeof(desc_hid_report), 0x81, 64, 1),
TUD_HID_DESCRIPTOR(2, 4, false, sizeof(desc_hid_report), 0x82, 64, 1),
};
void app_main(void)
{
ESP_LOGI(TAG, "USB 初始化");
const tinyusb_config_t tusb_cfg = {
.port = TINYUSB_PORT_FULL_SPEED_0,
.phy = {
.skip_setup = false,
.self_powered = false,
.vbus_monitor_io = 0,
},
.task = {
.size = 3500,
.priority = 14,
.xCoreID = 0,
},
.descriptor = {
.device = &desc_device,
.qualifier = NULL,
.string = hid_string_descriptor,
.string_count = sizeof(hid_string_descriptor) / sizeof(hid_string_descriptor[0]),
.full_speed_config = hid_configuration_descriptor,
.high_speed_config = NULL,
},
.event_cb = NULL,
.event_arg = NULL,
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_LOGI(TAG, "✅ USB HID 设备启动成功");
ESP_LOGI(TAG, "启动 Rust 应用...");
rust_app_wrapper_init();
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "USB 设备运行中...");
}
}