USB cleanup and nkro loss issue fixed.

This commit is contained in:
whowechina 2023-10-13 23:12:43 +08:00
parent 38da1bd6f0
commit fdfb3975a3
6 changed files with 56 additions and 161 deletions

View File

@ -45,7 +45,7 @@ uint8_t button_num()
static uint16_t button_reading; static uint16_t button_reading;
/* If a switch flips, it freezes for a while */ /* If a switch flips, it freezes for a while */
#define DEBOUNCE_FREEZE_TIME_US 1000 #define DEBOUNCE_FREEZE_TIME_US 5000
void button_update() void button_update()
{ {
uint64_t now = time_us_64(); uint64_t now = time_us_64();

View File

@ -7,6 +7,8 @@
#include "pico/stdio.h" #include "pico/stdio.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "tusb.h"
#include "touch.h" #include "touch.h"
#include "config.h" #include "config.h"
#include "save.h" #include "save.h"
@ -316,6 +318,15 @@ static void handle_raw()
printf("\n"); printf("\n");
} }
static void handle_whoami()
{
const char *msg[] = {"\nThis is Command Line port.\n", "\nThis is Touch port.\n", "\nThis is LED port.\n"};
for (int i = 0; i < 3; i++) {
tud_cdc_n_write(i, msg[i], strlen(msg[i]));
tud_cdc_n_write_flush(i);
}
}
static void handle_save() static void handle_save()
{ {
save_request(true); save_request(true);
@ -337,6 +348,7 @@ void commands_init()
cli_register("sense", handle_sense, "Set sensitivity config."); cli_register("sense", handle_sense, "Set sensitivity config.");
cli_register("debounce", handle_debounce, "Set debounce config."); cli_register("debounce", handle_debounce, "Set debounce config.");
cli_register("raw", handle_raw, "Show key raw readings."); cli_register("raw", handle_raw, "Show key raw readings.");
cli_register("whoami", handle_whoami, "Identify each com port.");
cli_register("save", handle_save, "Save config to flash."); cli_register("save", handle_save, "Save config to flash.");
cli_register("factory", config_factory_reset, "Reset everything to default."); cli_register("factory", config_factory_reset, "Reset everything to default.");
} }

View File

@ -34,10 +34,7 @@
#include "io.h" #include "io.h"
struct __attribute__((packed)) { struct __attribute__((packed)) {
uint16_t buttons; // 16 buttons; see JoystickButtons_t for bit mapping uint16_t buttons;
uint8_t HAT; // HAT switch; one nibble w/ unused nibble
uint32_t axis; // slider touch data
uint8_t VendorSpec;
} hid_joy; } hid_joy;
struct __attribute__((packed)) { struct __attribute__((packed)) {
@ -48,16 +45,13 @@ struct __attribute__((packed)) {
void report_usb_hid() void report_usb_hid()
{ {
if (tud_hid_ready()) { if (tud_hid_ready()) {
hid_joy.HAT = 0;
hid_joy.VendorSpec = 0;
if (mai_cfg->hid.joy) { if (mai_cfg->hid.joy) {
hid_joy.buttons = button_read(); hid_joy.buttons = button_read();
tud_hid_n_report(0x00, REPORT_ID_JOYSTICK, &hid_joy, sizeof(hid_joy)); tud_hid_n_report(0, REPORT_ID_JOYSTICK, &hid_joy, sizeof(hid_joy));
} }
if (mai_cfg->hid.nkro && if (mai_cfg->hid.nkro) {
(memcmp(&hid_nkro, &sent_hid_nkro, sizeof(hid_nkro)) != 0)) {
sent_hid_nkro = hid_nkro; sent_hid_nkro = hid_nkro;
tud_hid_n_report(0x02, 0, &sent_hid_nkro, sizeof(sent_hid_nkro)); tud_hid_n_report(1, 0, &sent_hid_nkro, sizeof(sent_hid_nkro));
} }
} }
} }
@ -189,15 +183,4 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
hid_report_type_t report_type, uint8_t const *buffer, hid_report_type_t report_type, uint8_t const *buffer,
uint16_t bufsize) uint16_t bufsize)
{ {
if (report_type == HID_REPORT_TYPE_OUTPUT) {
last_hid_time = time_us_64();
return;
}
if (report_type == HID_REPORT_TYPE_FEATURE) {
if (report_id == REPORT_ID_LED_COMPRESSED) {
}
last_hid_time = time_us_64();
return;
}
} }

View File

@ -96,7 +96,7 @@ extern "C" {
#endif #endif
//------------- CLASS -------------// //------------- CLASS -------------//
#define CFG_TUD_HID 3 #define CFG_TUD_HID 2
#define CFG_TUD_CDC 3 #define CFG_TUD_CDC 3
#define CFG_TUD_MSC 0 #define CFG_TUD_MSC 0
#define CFG_TUD_MIDI 0 #define CFG_TUD_MIDI 0

View File

@ -27,21 +27,6 @@
#include "pico/unique_id.h" #include "pico/unique_id.h"
#include "tusb.h" #include "tusb.h"
/* A combination of interfaces must have a unique product id, since PC will save
* device driver after the first plug. Same VID/PID with different interface e.g
* MSC (first), then CDC (later) will possibly cause system error on PC.
*
* Auto ProductID layout's Bitmap:
* [MSB] HID | MSC | CDC [LSB]
*/
#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))
#define USB_PID \
(0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4))
//--------------------------------------------------------------------+
// Device Descriptors
//--------------------------------------------------------------------+
tusb_desc_device_t desc_device_joy = { tusb_desc_device_t desc_device_joy = {
.bLength = sizeof(tusb_desc_device_t), .bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE, .bDescriptorType = TUSB_DESC_DEVICE,
@ -51,18 +36,16 @@ tusb_desc_device_t desc_device_joy = {
.bDeviceProtocol = 0x00, .bDeviceProtocol = 0x00,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
// To match CrazyRedMachine dll .idVendor = 0x1f2d,
// vid 0x0f0d, pid 0x0092, interface 1 .idProduct = 0x0123,
.idVendor = 0x0f0d,
.idProduct = 0x0092,
.bcdDevice = 0x0100, .bcdDevice = 0x0100,
.iManufacturer = 0x01, .iManufacturer = 0x01,
.iProduct = 0x02, .iProduct = 0x02,
.iSerialNumber = 0x03, .iSerialNumber = 0x03,
.bNumConfigurations = 0x01}; .bNumConfigurations = 0x01
};
// Invoked when received GET DEVICE DESCRIPTOR // Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor // Application return pointer to descriptor
@ -75,20 +58,11 @@ uint8_t const* tud_descriptor_device_cb(void) {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint8_t const desc_hid_report_joy[] = { uint8_t const desc_hid_report_joy[] = {
CHUPICO_REPORT_DESC_JOYSTICK, MAIPICO_REPORT_DESC_JOYSTICK,
};
uint8_t const desc_hid_report_led[] = {
CHUPICO_LED_HEADER,
CHUPICO_REPORT_DESC_LED_TOUCH_16,
CHUPICO_REPORT_DESC_LED_TOUCH_15,
CHUPICO_REPORT_DESC_LED_TOWER_6,
CHUPICO_REPORT_DESC_LED_COMPRESSED,
CHUPICO_LED_FOOTER
}; };
uint8_t const desc_hid_report_nkro[] = { uint8_t const desc_hid_report_nkro[] = {
CHUPICO_REPORT_DESC_NKRO, MAIPICO_REPORT_DESC_NKRO,
}; };
// Invoked when received GET HID REPORT DESCRIPTOR // Invoked when received GET HID REPORT DESCRIPTOR
@ -100,8 +74,6 @@ uint8_t const* tud_hid_descriptor_report_cb(uint8_t itf)
case 0: case 0:
return desc_hid_report_joy; return desc_hid_report_joy;
case 1: case 1:
return desc_hid_report_led;
case 2:
return desc_hid_report_nkro; return desc_hid_report_nkro;
default: default:
return NULL; return NULL;
@ -111,26 +83,28 @@ uint8_t const* tud_hid_descriptor_report_cb(uint8_t itf)
// Configuration Descriptor // Configuration Descriptor
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
enum { ITF_NUM_JOY, ITF_NUM_LED, ITF_NUM_NKRO, enum { ITF_NUM_JOY, ITF_NUM_NKRO,
ITF_NUM_CDC, ITF_NUM_CDC_DATA, ITF_NUM_CDC_CLI, ITF_NUM_CDC_CLI_DATA,
ITF_NUM_CDC_TOUCH, ITF_NUM_CDC_DATA_TOUCH, ITF_NUM_CDC_TOUCH, ITF_NUM_CDC_TOUCH_DATA,
ITF_NUM_CDC_LED, ITF_NUM_CDC_DATA_LED, ITF_NUM_CDC_LED, ITF_NUM_CDC_LED_DATA,
ITF_NUM_TOTAL }; ITF_NUM_TOTAL };
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN * 3 + TUD_CDC_DESC_LEN * 3) #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN * 2 + TUD_CDC_DESC_LEN * 3)
#define EPNUM_JOY 0x81
#define EPNUM_KEY 0x82
#define EPNUM_CDC_CLI_NOTIF 0x83
#define EPNUM_CDC_CLI_OUT 0x04
#define EPNUM_CDC_CLI_IN 0x84
#define EPNUM_CDC_TOUCH_NOTIF 0x85
#define EPNUM_CDC_TOUCH_OUT 0x06
#define EPNUM_CDC_TOUCH_IN 0x86
#define EPNUM_CDC_LED_NOTIF 0x87
#define EPNUM_CDC_LED_OUT 0x08
#define EPNUM_CDC_LED_IN 0x88
#define EPNUM_JOY 0x84
#define EPNUM_LED 0x85
#define EPNUM_KEY 0x86
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x82
#define EPNUM_CDC_TOUCH_NOTIF 0x87
#define EPNUM_CDC_TOUCH_OUT 0x03
#define EPNUM_CDC_TOUCH_IN 0x88
#define EPNUM_CDC_LED_NOTIF 0x89
#define EPNUM_CDC_LED_OUT 0x04
#define EPNUM_CDC_LED_IN 0x8a
uint8_t const desc_configuration_joy[] = { uint8_t const desc_configuration_joy[] = {
// Config number, interface count, string index, total length, attribute, // Config number, interface count, string index, total length, attribute,
@ -144,22 +118,18 @@ uint8_t const desc_configuration_joy[] = {
sizeof(desc_hid_report_joy), EPNUM_JOY, sizeof(desc_hid_report_joy), EPNUM_JOY,
CFG_TUD_HID_EP_BUFSIZE, 1), CFG_TUD_HID_EP_BUFSIZE, 1),
TUD_HID_DESCRIPTOR(ITF_NUM_LED, 5, HID_ITF_PROTOCOL_NONE, TUD_HID_DESCRIPTOR(ITF_NUM_NKRO, 5, HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report_led), EPNUM_LED,
CFG_TUD_HID_EP_BUFSIZE, 4),
TUD_HID_DESCRIPTOR(ITF_NUM_NKRO, 6, HID_ITF_PROTOCOL_NONE,
sizeof(desc_hid_report_nkro), EPNUM_KEY, sizeof(desc_hid_report_nkro), EPNUM_KEY,
CFG_TUD_HID_EP_BUFSIZE, 1), CFG_TUD_HID_EP_BUFSIZE, 1),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 7, EPNUM_CDC_NOTIF, TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_CLI, 6, EPNUM_CDC_CLI_NOTIF,
8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64), 8, EPNUM_CDC_CLI_OUT, EPNUM_CDC_CLI_IN, 64),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_TOUCH, 8, EPNUM_CDC_TOUCH_NOTIF, TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_TOUCH, 7, EPNUM_CDC_TOUCH_NOTIF,
8, EPNUM_CDC_TOUCH_OUT, EPNUM_CDC_TOUCH_IN, 64), 8, EPNUM_CDC_TOUCH_OUT, EPNUM_CDC_TOUCH_IN, 64),
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_LED, 9, EPNUM_CDC_LED_NOTIF, TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_LED, 8, EPNUM_CDC_LED_NOTIF,
8, EPNUM_CDC_LED_OUT, EPNUM_CDC_LED_IN, 64) 8, EPNUM_CDC_LED_OUT, EPNUM_CDC_LED_IN, 64),
}; };
// Invoked when received GET CONFIGURATION DESCRIPTOR // Invoked when received GET CONFIGURATION DESCRIPTOR
@ -184,7 +154,7 @@ static const char *string_desc_arr[] = {
"Mai Pico Joystick", "Mai Pico Joystick",
"Mai Pico LED", "Mai Pico LED",
"Mai Pico NKRO", "Mai Pico NKRO",
"Mai Pico Serial Port", "Mai Pico Command Serial Port",
"Mai Pico Touch Serial Port", "Mai Pico Touch Serial Port",
"Mai Pico LED Serial Port", "Mai Pico LED Serial Port",
}; };

View File

@ -6,10 +6,6 @@
enum { enum {
REPORT_ID_JOYSTICK = 1, REPORT_ID_JOYSTICK = 1,
REPORT_ID_LED_TOUCH_16 = 4,
REPORT_ID_LED_TOUCH_15 = 5,
REPORT_ID_LED_TOWER_6 = 6,
REPORT_ID_LED_COMPRESSED = 11,
}; };
// because they are missing from tusb_hid.h // because they are missing from tusb_hid.h
@ -22,87 +18,21 @@ enum {
// Joystick Report Descriptor Template - Based off Drewol/rp2040-gamecon // Joystick Report Descriptor Template - Based off Drewol/rp2040-gamecon
// Button Map | X | Y // Button Map | X | Y
#define CHUPICO_REPORT_DESC_JOYSTICK \ #define MAIPICO_REPORT_DESC_JOYSTICK \
HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), \ HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), \
HID_USAGE(HID_USAGE_DESKTOP_JOYSTICK), \ HID_USAGE(HID_USAGE_DESKTOP_JOYSTICK), \
HID_COLLECTION(HID_COLLECTION_APPLICATION), \ HID_COLLECTION(HID_COLLECTION_APPLICATION), \
HID_REPORT_ID(REPORT_ID_JOYSTICK) \ HID_REPORT_ID(REPORT_ID_JOYSTICK) \
HID_USAGE_PAGE(HID_USAGE_PAGE_BUTTON), \ HID_USAGE_PAGE(HID_USAGE_PAGE_BUTTON), \
HID_USAGE_MIN(1), HID_USAGE_MAX(16), \ HID_USAGE_MIN(1), HID_USAGE_MAX(10), \
HID_LOGICAL_MIN(0), HID_LOGICAL_MAX(1), \ HID_LOGICAL_MIN(0), HID_LOGICAL_MAX(1), \
HID_REPORT_COUNT(16), HID_REPORT_SIZE(1), \ HID_REPORT_COUNT(10), HID_REPORT_SIZE(1), \
HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
\
HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), \
HID_USAGE(HID_USAGE_DESKTOP_HAT_SWITCH), \
HID_LOGICAL_MIN(1), HID_LOGICAL_MAX(8), \
HID_PHYSICAL_MIN(0), HID_PHYSICAL_MAX_N(315, 2), \
HID_REPORT_SIZE(8), HID_REPORT_COUNT(1), \
HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
\
HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), \
HID_USAGE(HID_USAGE_DESKTOP_X), HID_USAGE(HID_USAGE_DESKTOP_Y), \
HID_USAGE(HID_USAGE_DESKTOP_Z), HID_USAGE(HID_USAGE_DESKTOP_RX), \
HID_LOGICAL_MIN(0x00), HID_LOGICAL_MAX(0xff), /* Analog */ \
HID_REPORT_SIZE(8), HID_REPORT_COUNT(4), \
HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
\
HID_USAGE_PAGE_N(HID_USAGE_PAGE_VENDOR, 2), \
HID_USAGE(0), \
HID_LOGICAL_MIN(0x00), HID_LOGICAL_MAX(0xff), \
HID_REPORT_SIZE(8), HID_REPORT_COUNT(1), \
HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \ HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
HID_REPORT_COUNT(1), HID_REPORT_SIZE(16 - 10), /*Padding*/ \
HID_INPUT(HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE), \
HID_COLLECTION_END HID_COLLECTION_END
#define CHUPICO_LED_HEADER \ #define MAIPICO_REPORT_DESC_NKRO \
HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), HID_USAGE(0x00), \
HID_COLLECTION(HID_COLLECTION_APPLICATION), \
HID_REPORT_COUNT(1), HID_REPORT_SIZE(8), \
HID_INPUT(HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE)
#define CHUPICO_LED_FOOTER \
HID_COLLECTION_END
// Slider First 16 LEDs (48 rgb zones, BRG order)
#define CHUPICO_REPORT_DESC_LED_TOUCH_16 \
HID_REPORT_ID(REPORT_ID_LED_TOUCH_16) \
HID_REPORT_COUNT(48), HID_REPORT_SIZE(8), \
HID_LOGICAL_MIN(0x00), HID_LOGICAL_MAX_N(0x00ff, 2), \
HID_USAGE_PAGE(HID_USAGE_PAGE_ORDINAL), \
HID_USAGE_MIN(1), HID_USAGE_MAX(48), \
HID_STRING_MINIMUM(8), HID_STRING_MAXIMUM(55), \
HID_OUTPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE)
// Slider Remaining 15 LEDs (45 rgb zones, BRG order)
#define CHUPICO_REPORT_DESC_LED_TOUCH_15 \
HID_REPORT_ID(REPORT_ID_LED_TOUCH_15) \
HID_REPORT_COUNT(45), HID_REPORT_SIZE(8), \
HID_LOGICAL_MIN(0x00), HID_LOGICAL_MAX_N(0x00ff, 2), \
HID_USAGE_PAGE(HID_USAGE_PAGE_ORDINAL), \
HID_USAGE_MIN(49), HID_USAGE_MAX(93), \
HID_STRING_MINIMUM(8), HID_STRING_MAXIMUM(52), /* Delta to previous */ \
HID_OUTPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE)
// Tower LEDs (18 rgb zones, BRG order)
#define CHUPICO_REPORT_DESC_LED_TOWER_6 \
HID_REPORT_ID(REPORT_ID_LED_TOWER_6) \
HID_REPORT_COUNT(18), HID_REPORT_SIZE(8), \
HID_LOGICAL_MIN(0x00), HID_LOGICAL_MAX_N(0x00ff, 2), \
HID_USAGE_PAGE(HID_USAGE_PAGE_ORDINAL), \
HID_USAGE_MIN(94), HID_USAGE_MAX(111), \
HID_STRING_MINIMUM(8), HID_STRING_MAXIMUM(25), /* Delta to previous */ \
HID_OUTPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE)
// LEDs Compressed
#define CHUPICO_REPORT_DESC_LED_COMPRESSED \
HID_REPORT_ID(REPORT_ID_LED_COMPRESSED) \
HID_USAGE_PAGE(HID_USAGE_PAGE_ORDINAL), \
HID_USAGE(0x00), \
HID_LOGICAL_MIN(0x00), HID_LOGICAL_MAX_N(0x00ff, 2), \
HID_REPORT_SIZE(8), HID_REPORT_COUNT(63), \
HID_FEATURE(HID_DATA | HID_VARIABLE | HID_ABSOLUTE)
#define CHUPICO_REPORT_DESC_NKRO \
HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), \ HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), \
HID_USAGE(HID_USAGE_DESKTOP_KEYBOARD), \ HID_USAGE(HID_USAGE_DESKTOP_KEYBOARD), \
HID_COLLECTION(HID_COLLECTION_APPLICATION), \ HID_COLLECTION(HID_COLLECTION_APPLICATION), \