diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a81050..4e9afdc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,10 @@ if(ESP_PLATFORM) set(EXTRA_COMPONENT_DIRS src) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(USB_ITF_CCID 1) -set(USB_ITF_HID 1) +#set(USB_ITF_HID 1) +set(USB_VID 0x20a0) +set(USB_PID 0x4230) +set(DEBUG_APDU 1) include(pico_keys_sdk_import.cmake) project(pico_keys_sdk) else() diff --git a/partitions.csv b/partitions.csv index 1c79321..9d74ef7 100755 --- a/partitions.csv +++ b/partitions.csv @@ -1,6 +1,4 @@ # Name, Type, SubType, Offset, Size, Flags # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap -nvs, data, nvs, 0x9000, 0x6000, -phy_init, data, phy, 0xf000, 0x1000, -factory, app, factory, 0x10000, 1M, -storage, data, fat, , 1M, +factory, app, factory, 0x10000, 1M, +part0, data, nvs, 0x200000, 1M, diff --git a/sdkconfig.defaults b/sdkconfig.defaults index e029fb7..3c810cc 100755 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -11,10 +11,3 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_WL_SECTOR_SIZE_512=y CONFIG_WL_SECTOR_MODE_PERF=y - -CONFIG_TINYUSB_VENDOR_ENABLED=y -CONFIG_TINYUSB_VENDOR_COUNT=2 -CONFIG_TINYUSB_HID_ENABLED=y -CONFIG_TINYUSB_HID_COUNT=2 -# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set -CONFIG_TINYUSB_DESC_CUSTOM_PID=0x4001 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7b3e1f..66ea656 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS ${INTERNAL_SOURCES} INCLUDE_DIRS . fs rng usb ../mbedtls/include - REQUIRES bootloader_support esp_partition tinyusb + REQUIRES bootloader_support esp_partition esp_tinyusb ) diff --git a/src/apdu.c b/src/apdu.c index 999bc20..ad5763d 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -19,6 +19,9 @@ #include "pico_keys.h" #include "usb.h" #include +#ifdef ESP_PLATFORM +#include "esp_compat.h" +#endif uint8_t *rdata_gr = NULL; uint16_t rdata_bk = 0x0; @@ -196,8 +199,7 @@ void apdu_thread() { card_init_core1(); while (1) { uint32_t m = 0; -#if defined(ESP_PLATFORM) -#else +#ifndef ENABLE_EMULATION queue_remove_blocking(&usb_to_card_q, &m); #endif @@ -216,9 +218,11 @@ done: ; finished_data_size = apdu_next(); uint32_t flag = EV_EXEC_FINISHED; -#if defined(ESP_PLATFORM) -#else +#ifndef ENABLE_EMULATION queue_add_blocking(&card_to_usb_q, &flag); +#endif +#ifdef ESP_PLATFORM + vTaskDelay(pdMS_TO_TICKS(10)); #endif } //printf("EXIT !!!!!!\r\n"); @@ -226,6 +230,9 @@ done: ; current_app->unload(); current_app = NULL; } +#ifdef ESP_PLATFORM + vTaskDelete(NULL); +#endif } #endif diff --git a/src/eac.c b/src/eac.c index 77181da..7fef272 100644 --- a/src/eac.c +++ b/src/eac.c @@ -103,10 +103,8 @@ int sm_unwrap() { if (r != CCID_OK) { return r; } - uint16_t le = sm_get_le(); - if (le >= 0) { - apdu.ne = le; - } + apdu.ne = sm_get_le(); + uint8_t *body = NULL; uint16_t body_size = 0; bool is87 = false; @@ -306,12 +304,12 @@ int sm_verify() { } uint16_t sm_remove_padding(const uint8_t *data, uint16_t data_len) { - uint16_t i = data_len - 1; + int32_t i = data_len - 1; for (; i >= 0 && data[i] == 0; i--) { ; } if (i < 0 || data[i] != 0x80) { return 0; } - return i; + return (uint16_t)i; } diff --git a/src/esp_compat.h b/src/esp_compat.h index dba3c20..24736fa 100644 --- a/src/esp_compat.h +++ b/src/esp_compat.h @@ -25,14 +25,17 @@ typedef QueueHandle_t queue_t; #define queue_init(a,b,c) do { *(a) = xQueueCreate(c, b); } while(0) #define queue_add_blocking(a,b) xQueueSend(*(a), b, portMAX_DELAY) +#define queue_remove_blocking(a,b) xQueueReceive(*(a), b, portMAX_DELAY) #define queue_try_add(a,b) xQueueSend(*(a), b, 0) #define queue_is_empty(a) (uxQueueMessagesWaiting(*(a)) == 0) #define queue_try_remove(a,b) xQueueReceive(*(a), b, 0) extern TaskHandle_t hcore0, hcore1; -#define multicore_launch_core1(a) xTaskCreate(a, "core1", 512, NULL, tskIDLE_PRIORITY, &hcore1) -#define multicore_reset_core1 vTaskEndScheduler +#define multicore_launch_core1(a) xTaskCreate((void(*)(void *))a, "core1", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 2, &hcore1) +#define multicore_reset_core1() do { if (hcore1) { eTaskState e = eTaskGetState(hcore1); if (e == eRunning) { vTaskDelete(hcore1); }} }while(0) #define sleep_ms(a) vTaskDelay(a / portTICK_PERIOD_MS) -#define board_millis xTaskGetTickCount +static inline uint32_t board_millis(void) { + return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ ); +} #define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8 typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; #define pico_get_unique_board_id(a) memset(a, 0, sizeof(pico_unique_board_id_t)) @@ -57,10 +60,8 @@ static inline bool multicore_lockout_end_timeout_us(int a) { vTaskResume(hcore1); return true; } #define save_and_disable_interrupts() 1 -#include "esp_partition.h" -extern esp_partition_t part0; -#define flash_range_erase(a,b) esp_partition_erase_range(&part0, a, b) -#define flash_range_program(a,b,c) esp_partition_write(&part0, a, b, c) +#define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b) +#define flash_range_program(a,b,c) esp_partition_write(part0, a, b, c) #define restore_interrupts(a) (void)a #endif diff --git a/src/fs/low_flash.c b/src/fs/low_flash.c index 0b40636..ec96437 100644 --- a/src/fs/low_flash.c +++ b/src/fs/low_flash.c @@ -40,7 +40,8 @@ #else #ifdef ESP_PLATFORM #include "esp_compat.h" -esp_partition_t part0; +#include "esp_partition.h" +const esp_partition_t *part0; #else #include #include @@ -156,14 +157,17 @@ sem_release(&sem_wait); //this function has to be called from the core 0 void low_flash_init() { memset(flash_pages, 0, sizeof(page_flash_t) * TOTAL_FLASH_PAGES); -#ifndef ENABLE_EMULATION - mutex_init(&mtx_flash); - sem_init(&sem_wait, 0, 1); -#else +#if defined(ENABLE_EMULATION) fd_map = open("memory.flash", O_RDWR | O_CREAT, (mode_t) 0600); lseek(fd_map, PICO_FLASH_SIZE_BYTES - 1, SEEK_SET); write(fd_map, "", 1); map = mmap(0, PICO_FLASH_SIZE_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd_map, 0); +#else + mutex_init(&mtx_flash); + sem_init(&sem_wait, 0, 1); +#if defined(ESP_PLATFORM) + part0 = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, "part0"); +#endif #endif } diff --git a/src/idf_component.yml b/src/idf_component.yml index c91e372..81670f8 100755 --- a/src/idf_component.yml +++ b/src/idf_component.yml @@ -1,3 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/tinyusb: "^0.15.0" + espressif/esp_tinyusb: "^1.4.4" + #espressif/tinyusb: "^0.15.0" + zorxx/neopixel: "^1.0.4" diff --git a/src/main.c b/src/main.c index 2531918..11a78c8 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ */ #include +#include // Pico @@ -222,6 +223,22 @@ bool wait_button() { struct apdu apdu; +#ifdef ESP_PLATFORM +#include "driver/gpio.h" +#include "neopixel.h" +tNeopixelContext neopixel = NULL; +tNeopixel pixel[] = + { + { 0, NP_RGB(0, 0, 0) }, /* off */ + { 0, NP_RGB(255, 0, 255) }, /* magenta */ + { 0, NP_RGB(255, 0, 0) }, /* green */ + { 0, NP_RGB(0, 255, 0) }, /* red */ + { 0, NP_RGB(0, 0, 255) }, /* red */ + { 0, NP_RGB(255, 255, 0) }, /* yellow */ + { 0, NP_RGB(0, 255, 255) }, /* cyan */ + { 0, NP_RGB(255, 255, 255) }, /* white */ + }; +#endif void led_blinking_task() { static uint32_t start_ms = 0; static uint8_t led_state = false; @@ -254,6 +271,8 @@ void led_blinking_task() { } #elif defined(CYW43_WL_GPIO_LED_PIN) cyw43_arch_gpio_put(led_color, led_state); +#elif ESP_PLATFORM + neopixel_SetPixel(neopixel, &pixel[led_state], 1); #endif led_state ^= 1; // toggle } @@ -299,10 +318,10 @@ void init_rtc() { extern void neug_task(); extern void usb_task(); - -void execute_tasks() { +void execute_tasks() +{ usb_task(); -#ifndef ENABLE_EMULATION +#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) tud_task(); // tinyusb device task #endif led_blinking_task(); @@ -332,23 +351,31 @@ void core0_loop() { button_pressed_time = button_press = 0; } } +#endif +#ifdef ESP_PLATFORM + vTaskDelay(pdMS_TO_TICKS(10)); #endif } } #ifdef ESP_PLATFORM +#include "tinyusb.h" +extern const tinyusb_config_t tusb_cfg; TaskHandle_t hcore0 = NULL, hcore1 = NULL; int app_main() { #else int main(void) { #endif + #ifndef ENABLE_EMULATION +#ifdef ESP_PLATFORM + tinyusb_driver_install(&tusb_cfg); +#endif usb_init(); #ifndef ESP_PLATFORM board_init(); stdio_init_all(); #endif - #ifdef PIMORONI_TINY2040 gpio_init(TINY2040_LED_R_PIN); gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT); @@ -365,7 +392,6 @@ int main(void) { led_off_all(); - tusb_init(); //prepare_ccid(); #else @@ -379,10 +405,12 @@ int main(void) { init_rtc(); //ccid_prepare_receive(&ccid); +#ifdef ESP_PLATFORM + neopixel = neopixel_Init(1, GPIO_NUM_48); +#endif #ifdef ESP_PLATFORM - xTaskCreate(core0_loop, "core0", 512, NULL, tskIDLE_PRIORITY, &hcore0); - vTaskStartScheduler(); + xTaskCreate(core0_loop, "core0", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 1, &hcore0); #else core0_loop(); #endif diff --git a/src/pico_keys.h b/src/pico_keys.h index 850deb1..3e3ad55 100644 --- a/src/pico_keys.h +++ b/src/pico_keys.h @@ -19,11 +19,11 @@ #define _PICO_KEYS_H_ #include "file.h" -#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) -#include "pico/unique_id.h" -#else +#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM) #include +#ifdef ENABLE_EMULATION extern uint32_t board_millis(); +#endif #if !defined(MIN) #if defined(_MSC_VER) #define MIN(a,b) (((a)<(b))?(a):(b)) @@ -44,6 +44,8 @@ extern uint32_t board_millis(); _a > _b ? _a : _b; }) #endif #endif +#else +#include "pico/unique_id.h" #endif #include @@ -59,10 +61,10 @@ extern bool wait_button(); extern void low_flash_init_core1(); -static inline const uint16_t make_uint16_t(uint8_t b1, uint8_t b2) { +static inline uint16_t make_uint16_t(uint8_t b1, uint8_t b2) { return (b1 << 8) | b2; } -static inline const uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) { +static inline uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) { return make_uint16_t(b[offset], b[offset + 1]); } static inline void put_uint16_t(uint16_t n, uint8_t *b) { diff --git a/src/usb/tusb_config.h b/src/usb/tusb_config.h index c52fdb5..d90a387 100644 --- a/src/usb/tusb_config.h +++ b/src/usb/tusb_config.h @@ -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 } diff --git a/src/usb/usb_descriptors.c b/src/usb/usb_descriptors.c index 5b0d5ed..5ad07cc 100644 --- a/src/usb/usb_descriptors.c +++ b/src/usb/usb_descriptors.c @@ -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