mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-28 17:11:23 +02:00
Second round to make it work for ESP32S3.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -45,7 +45,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// COMMON CONFIGURATION
|
||||
// Common Configuration
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
// defined by compiler flags for flexibility
|
||||
@@ -53,20 +53,20 @@ extern "C" {
|
||||
#error CFG_TUSB_MCU must be defined
|
||||
#endif
|
||||
|
||||
#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || \
|
||||
CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
|
||||
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56
|
||||
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
|
||||
#else
|
||||
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
|
||||
#ifndef CFG_TUSB_OS
|
||||
#if CFG_TUSB_MCU == OPT_MCU_RP2040
|
||||
#define CFG_TUSB_OS OPT_OS_PICO
|
||||
#elif CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
|
||||
#define CFG_TUSB_OS OPT_OS_FREERTOS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_OS
|
||||
#define CFG_TUSB_OS OPT_OS_FREERTOS
|
||||
#ifndef CFG_TUSB_RHPORT0_MODE
|
||||
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_DEBUG
|
||||
#define CFG_TUSB_DEBUG 1
|
||||
#define CFG_TUSB_DEBUG 0
|
||||
#endif
|
||||
|
||||
// Enable Device stack
|
||||
@@ -113,11 +113,17 @@ extern "C" {
|
||||
#ifdef USB_ITF_CCID
|
||||
#define CFG_TUD_VENDOR 2
|
||||
#else
|
||||
#define CFG_TUD_VENDOR 2
|
||||
#define CFG_TUD_VENDOR 0
|
||||
#endif
|
||||
|
||||
// HID buffer size Should be sufficient to hold ID (if any) + Data
|
||||
#define CFG_TUD_HID_EP_BUFSIZE 64
|
||||
// CDC FIFO size of TX and RX
|
||||
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
|
||||
// CDC Endpoint transfer buffer size, more is faster
|
||||
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||
#include "pico/unique_id.h"
|
||||
#endif
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "tinyusb.h"
|
||||
#endif
|
||||
#include "pico_keys_version.h"
|
||||
#include "usb.h"
|
||||
|
||||
@@ -61,37 +64,91 @@ tusb_desc_device_t const desc_device = {
|
||||
.bNumConfigurations = 1
|
||||
};
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
uint8_t const *tud_descriptor_device_cb(void) {
|
||||
return (uint8_t const *) &desc_device;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
//--------------------------------------------------------------------+
|
||||
// Configuration Descriptor
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
tusb_desc_configuration_t const desc_config = {
|
||||
.bLength = sizeof(tusb_desc_configuration_t),
|
||||
.bDescriptorType = TUSB_DESC_CONFIGURATION,
|
||||
.wTotalLength = (sizeof(tusb_desc_configuration_t)
|
||||
#ifdef USB_ITF_CCID
|
||||
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
3 * sizeof(tusb_desc_endpoint_t))
|
||||
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
2 * sizeof(tusb_desc_endpoint_t))
|
||||
#endif
|
||||
#define TUD_INTERFACE_DESC_LEN 9
|
||||
#define TUD_ENDPOINT_DESC_LEN 7
|
||||
#define TUSB_SMARTCARD_LEN 54
|
||||
#define TUD_SMARTCARD_DESC_LEN ((TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 3 * TUD_ENDPOINT_DESC_LEN) + (TUD_INTERFACE_DESC_LEN + TUSB_SMARTCARD_LEN + 2 * TUD_ENDPOINT_DESC_LEN))
|
||||
|
||||
enum {
|
||||
TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN
|
||||
#ifdef USB_ITF_HID
|
||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
+ TUD_SMARTCARD_DESC_LEN
|
||||
#endif
|
||||
),
|
||||
.bNumInterfaces = ITF_TOTAL,
|
||||
.bConfigurationValue = 1,
|
||||
.iConfiguration = 4,
|
||||
.bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP,
|
||||
.bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER + 1),
|
||||
};
|
||||
|
||||
|
||||
#ifdef USB_ITF_HID
|
||||
uint8_t const desc_hid_report[] = {
|
||||
TUD_HID_REPORT_DESC_FIDO_U2F(CFG_TUD_HID_EP_BUFSIZE)
|
||||
};
|
||||
uint8_t const desc_hid_report_kb[] = {
|
||||
TUD_HID_REPORT_DESC_KEYBOARD(HID_USAGE(HID_USAGE_DESKTOP_GAMEPAD), HID_LOGICAL_MIN(0), HID_LOGICAL_MAX_N(255, 2), HID_REPORT_COUNT(8), HID_REPORT_SIZE(8), HID_FEATURE( HID_DATA | HID_VARIABLE | HID_ABSOLUTE), )
|
||||
};
|
||||
#define EPNUM_HID 0x04
|
||||
#endif
|
||||
|
||||
#ifdef USB_ITF_CCID
|
||||
#define TUD_SMARTCARD_DESCRIPTOR_WEB(_itf, _strix, _epout, _epin, _epsize) \
|
||||
9, TUSB_DESC_INTERFACE, _itf, 0, 2, 0xFF, 0, 0, _strix, \
|
||||
54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
|
||||
#define TUD_SMARTCARD_DESCRIPTOR(_itf, _strix, _epout, _epin, _epint, _epsize) \
|
||||
9, TUSB_DESC_INTERFACE, _itf, 0, 3, TUSB_CLASS_SMART_CARD, 0, 0, _strix, \
|
||||
54, 0x21, U16_TO_U8S_LE(0x0110), 0, 0x1, U32_TO_U8S_LE(0x1 | 0x2), U32_TO_U8S_LE(0xDFC), U32_TO_U8S_LE(0xDFC), 0, U32_TO_U8S_LE(0x2580), U32_TO_U8S_LE(0x2580), 0, U32_TO_U8S_LE(0xFE), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0), U32_TO_U8S_LE(0x40840), U32_TO_U8S_LE(65544+10), 0xFF, 0xFF, U16_TO_U8S_LE(0x0), 0, 0x1, \
|
||||
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
|
||||
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0, \
|
||||
7, TUSB_DESC_ENDPOINT, _epint, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), 0
|
||||
#endif
|
||||
|
||||
const uint8_t desc_config[] = {
|
||||
TUD_CONFIG_DESCRIPTOR(1, ITF_TOTAL, 4, TUSB_DESC_TOTAL_LEN, USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
|
||||
#ifdef USB_ITF_HID
|
||||
TUD_HID_INOUT_DESCRIPTOR(ITF_HID, ITF_HID + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, TUSB_DIR_IN_MASK | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10),
|
||||
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), TUSB_DIR_IN_MASK | (EPNUM_HID + 1), 16, 5),
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
TUD_SMARTCARD_DESCRIPTOR(ITF_CCID, ITF_CCID+5, 1, TUSB_DIR_IN_MASK | 1, TUSB_DIR_IN_MASK | 2, 64),
|
||||
TUD_SMARTCARD_DESCRIPTOR_WEB(ITF_WCID, ITF_WCID+5, 3, TUSB_DIR_IN_MASK | 3, 64),
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
tusb_desc_configuration_t const desc_config = {
|
||||
.bLength = sizeof(tusb_desc_configuration_t),
|
||||
.bDescriptorType = TUSB_DESC_CONFIGURATION,
|
||||
.wTotalLength = (sizeof(tusb_desc_configuration_t)
|
||||
#ifdef USB_ITF_CCID
|
||||
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
3 * sizeof(tusb_desc_endpoint_t)) +
|
||||
(sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
2 * sizeof(tusb_desc_endpoint_t))
|
||||
#endif
|
||||
#ifdef USB_ITF_HID
|
||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||
#endif
|
||||
),
|
||||
.bNumInterfaces = ITF_TOTAL,
|
||||
.bConfigurationValue = 1,
|
||||
.iConfiguration = 4,
|
||||
.bmAttributes = USB_CONFIG_ATT_ONE | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP,
|
||||
.bMaxPower = TUSB_DESC_CONFIG_POWER_MA(MAX_USB_POWER + 1),
|
||||
};
|
||||
*/
|
||||
#ifdef USB_ITF_CCID
|
||||
/*
|
||||
static const struct ccid_class_descriptor desc_ccid = {
|
||||
.bLength = sizeof(struct ccid_class_descriptor),
|
||||
.bDescriptorType = 0x21,
|
||||
@@ -118,7 +175,7 @@ static const struct ccid_class_descriptor desc_ccid = {
|
||||
.bPINSupport = 0x0,
|
||||
.bMaxCCIDBusySlots = 0x01,
|
||||
};
|
||||
|
||||
*/
|
||||
tusb_desc_interface_t const desc_interface = {
|
||||
.bLength = sizeof(tusb_desc_interface_t),
|
||||
.bDescriptorType = TUSB_DESC_INTERFACE,
|
||||
@@ -170,62 +227,9 @@ tusb_desc_endpoint_t const desc_ep3 = {
|
||||
.bInterval = 0
|
||||
};
|
||||
|
||||
tusb_desc_endpoint_t const desc_ep1_wcid = {
|
||||
.bLength = sizeof(tusb_desc_endpoint_t),
|
||||
.bDescriptorType = TUSB_DESC_ENDPOINT,
|
||||
.bEndpointAddress = (TUSB_DIR_IN_MASK | 1) + 3,
|
||||
.bmAttributes.xfer = TUSB_XFER_BULK,
|
||||
.wMaxPacketSize = (64),
|
||||
.bInterval = 0
|
||||
};
|
||||
|
||||
tusb_desc_endpoint_t const desc_ep2_wcid = {
|
||||
.bLength = sizeof(tusb_desc_endpoint_t),
|
||||
.bDescriptorType = TUSB_DESC_ENDPOINT,
|
||||
.bEndpointAddress = 1 + 3,
|
||||
.bmAttributes.xfer = TUSB_XFER_BULK,
|
||||
.wMaxPacketSize = (64),
|
||||
.bInterval = 0
|
||||
};
|
||||
#endif
|
||||
|
||||
static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t)
|
||||
#ifdef USB_ITF_CCID
|
||||
+ (sizeof(tusb_desc_interface_t) +
|
||||
sizeof(struct ccid_class_descriptor) + 3 *
|
||||
sizeof(tusb_desc_endpoint_t))
|
||||
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||
2 * sizeof(tusb_desc_endpoint_t))
|
||||
#endif
|
||||
#ifdef USB_ITF_HID
|
||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||
#endif
|
||||
];
|
||||
|
||||
#ifdef USB_ITF_HID
|
||||
uint8_t const desc_hid_report[] = {
|
||||
TUD_HID_REPORT_DESC_FIDO_U2F(CFG_TUD_HID_EP_BUFSIZE)
|
||||
};
|
||||
uint8_t const desc_hid_report_kb[] = {
|
||||
TUD_HID_REPORT_DESC_KEYBOARD(HID_USAGE(HID_USAGE_DESKTOP_GAMEPAD), HID_LOGICAL_MIN(0), HID_LOGICAL_MAX_N(255, 2), HID_REPORT_COUNT(8), HID_REPORT_SIZE(8), HID_FEATURE( HID_DATA | HID_VARIABLE | HID_ABSOLUTE), )
|
||||
};
|
||||
#define EPNUM_HID 0x03
|
||||
|
||||
static uint8_t desc_hid[] = {
|
||||
TUD_HID_INOUT_DESCRIPTOR(ITF_HID,
|
||||
ITF_HID + 5,
|
||||
HID_ITF_PROTOCOL_NONE,
|
||||
sizeof(desc_hid_report),
|
||||
EPNUM_HID,
|
||||
0x80 | EPNUM_HID,
|
||||
CFG_TUD_HID_EP_BUFSIZE,
|
||||
10)
|
||||
};
|
||||
|
||||
static uint8_t desc_hid_kb[] = {
|
||||
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD + 5, HID_ITF_PROTOCOL_NONE,
|
||||
sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID + 1), 16, 5)
|
||||
};
|
||||
#include "apdu.h"
|
||||
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
|
||||
printf("report_cb %d\n", itf);
|
||||
@@ -239,39 +243,15 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
|
||||
(void) index; // for multiple configurations
|
||||
|
||||
static uint8_t initd = 0;
|
||||
if (initd == 0) {
|
||||
uint8_t *p = desc_config_extended;
|
||||
memcpy(p, &desc_config, sizeof(tusb_desc_configuration_t));
|
||||
p += sizeof(tusb_desc_configuration_t);
|
||||
#ifdef USB_ITF_HID
|
||||
memcpy(p, &desc_hid, sizeof(desc_hid)); p += sizeof(desc_hid);
|
||||
memcpy(p, &desc_hid_kb, sizeof(desc_hid_kb)); p += sizeof(desc_hid_kb);
|
||||
#endif
|
||||
#ifdef USB_ITF_CCID
|
||||
memcpy(p, &desc_interface, sizeof(tusb_desc_interface_t));
|
||||
p += sizeof(tusb_desc_interface_t);
|
||||
memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor));
|
||||
p += sizeof(struct ccid_class_descriptor);
|
||||
memcpy(p, &desc_ep1, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||
memcpy(p, &desc_ep2, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||
memcpy(p, &desc_ep3, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||
|
||||
memcpy(p, &desc_interface_wcid, sizeof(tusb_desc_interface_t));
|
||||
p += sizeof(tusb_desc_interface_t);
|
||||
memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor));
|
||||
p += sizeof(struct ccid_class_descriptor);
|
||||
memcpy(p, &desc_ep1_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||
memcpy(p, &desc_ep2_wcid, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||
#endif
|
||||
initd = 1;
|
||||
}
|
||||
return (const uint8_t *) desc_config_extended;
|
||||
printf("tud_descriptor_configuration_cb %d\n",index);
|
||||
return desc_config;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USB_ITF_CCID
|
||||
|
||||
#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)
|
||||
#define MS_OS_20_DESC_LEN 0xB2
|
||||
@@ -376,7 +356,7 @@ uint8_t const *tud_descriptor_bos_cb(void) {
|
||||
printf("!!!!!!!!!!!! tud_descriptor_bos_cb\n");
|
||||
return desc_bos;
|
||||
}
|
||||
|
||||
#endif
|
||||
//--------------------------------------------------------------------+
|
||||
// String Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -397,7 +377,16 @@ char const *string_desc_arr [] = {
|
||||
, "Pico Key WebCCID Interface"
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
const tinyusb_config_t tusb_cfg = {
|
||||
.device_descriptor = &desc_device,
|
||||
.string_descriptor = string_desc_arr,
|
||||
.string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]),
|
||||
.external_phy = false,
|
||||
.configuration_descriptor = desc_config,
|
||||
};
|
||||
#endif
|
||||
#ifndef ESP_PLATFORM
|
||||
static uint16_t _desc_str[32];
|
||||
|
||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
@@ -441,3 +430,4 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
|
||||
return _desc_str;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user