Second round to make it work for ESP32S3.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2024-04-08 16:47:16 +02:00
parent 49f05e9e13
commit 06fd241f49
13 changed files with 196 additions and 164 deletions

View File

@@ -21,7 +21,10 @@ if(ESP_PLATFORM)
set(EXTRA_COMPONENT_DIRS src) set(EXTRA_COMPONENT_DIRS src)
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(USB_ITF_CCID 1) 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) include(pico_keys_sdk_import.cmake)
project(pico_keys_sdk) project(pico_keys_sdk)
else() else()

View File

@@ -1,6 +1,4 @@
# Name, Type, SubType, Offset, Size, Flags # Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, 0x9000, 0x6000, factory, app, factory, 0x10000, 1M,
phy_init, data, phy, 0xf000, 0x1000, part0, data, nvs, 0x200000, 1M,
factory, app, factory, 0x10000, 1M,
storage, data, fat, , 1M,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 nvs, data, nvs, 0x9000, 0x6000, factory, app, factory, 0x10000, 1M,
4 phy_init, data, phy, 0xf000, 0x1000, part0, data, nvs, 0x200000, 1M,
factory, app, factory, 0x10000, 1M,
storage, data, fat, , 1M,

View File

@@ -11,10 +11,3 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_WL_SECTOR_SIZE_512=y CONFIG_WL_SECTOR_SIZE_512=y
CONFIG_WL_SECTOR_MODE_PERF=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

View File

@@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS ${INTERNAL_SOURCES} SRCS ${INTERNAL_SOURCES}
INCLUDE_DIRS . fs rng usb ../mbedtls/include INCLUDE_DIRS . fs rng usb ../mbedtls/include
REQUIRES bootloader_support esp_partition tinyusb REQUIRES bootloader_support esp_partition esp_tinyusb
) )

View File

@@ -19,6 +19,9 @@
#include "pico_keys.h" #include "pico_keys.h"
#include "usb.h" #include "usb.h"
#include <stdio.h> #include <stdio.h>
#ifdef ESP_PLATFORM
#include "esp_compat.h"
#endif
uint8_t *rdata_gr = NULL; uint8_t *rdata_gr = NULL;
uint16_t rdata_bk = 0x0; uint16_t rdata_bk = 0x0;
@@ -196,8 +199,7 @@ void apdu_thread() {
card_init_core1(); card_init_core1();
while (1) { while (1) {
uint32_t m = 0; uint32_t m = 0;
#if defined(ESP_PLATFORM) #ifndef ENABLE_EMULATION
#else
queue_remove_blocking(&usb_to_card_q, &m); queue_remove_blocking(&usb_to_card_q, &m);
#endif #endif
@@ -216,9 +218,11 @@ done: ;
finished_data_size = apdu_next(); finished_data_size = apdu_next();
uint32_t flag = EV_EXEC_FINISHED; uint32_t flag = EV_EXEC_FINISHED;
#if defined(ESP_PLATFORM) #ifndef ENABLE_EMULATION
#else
queue_add_blocking(&card_to_usb_q, &flag); queue_add_blocking(&card_to_usb_q, &flag);
#endif
#ifdef ESP_PLATFORM
vTaskDelay(pdMS_TO_TICKS(10));
#endif #endif
} }
//printf("EXIT !!!!!!\r\n"); //printf("EXIT !!!!!!\r\n");
@@ -226,6 +230,9 @@ done: ;
current_app->unload(); current_app->unload();
current_app = NULL; current_app = NULL;
} }
#ifdef ESP_PLATFORM
vTaskDelete(NULL);
#endif
} }
#endif #endif

View File

@@ -103,10 +103,8 @@ int sm_unwrap() {
if (r != CCID_OK) { if (r != CCID_OK) {
return r; return r;
} }
uint16_t le = sm_get_le(); apdu.ne = sm_get_le();
if (le >= 0) {
apdu.ne = le;
}
uint8_t *body = NULL; uint8_t *body = NULL;
uint16_t body_size = 0; uint16_t body_size = 0;
bool is87 = false; 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 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--) { for (; i >= 0 && data[i] == 0; i--) {
; ;
} }
if (i < 0 || data[i] != 0x80) { if (i < 0 || data[i] != 0x80) {
return 0; return 0;
} }
return i; return (uint16_t)i;
} }

View File

@@ -25,14 +25,17 @@
typedef QueueHandle_t queue_t; typedef QueueHandle_t queue_t;
#define queue_init(a,b,c) do { *(a) = xQueueCreate(c, b); } while(0) #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_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_try_add(a,b) xQueueSend(*(a), b, 0)
#define queue_is_empty(a) (uxQueueMessagesWaiting(*(a)) == 0) #define queue_is_empty(a) (uxQueueMessagesWaiting(*(a)) == 0)
#define queue_try_remove(a,b) xQueueReceive(*(a), b, 0) #define queue_try_remove(a,b) xQueueReceive(*(a), b, 0)
extern TaskHandle_t hcore0, hcore1; extern TaskHandle_t hcore0, hcore1;
#define multicore_launch_core1(a) xTaskCreate(a, "core1", 512, NULL, tskIDLE_PRIORITY, &hcore1) #define multicore_launch_core1(a) xTaskCreate((void(*)(void *))a, "core1", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 2, &hcore1)
#define multicore_reset_core1 vTaskEndScheduler #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 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 #define PICO_UNIQUE_BOARD_ID_SIZE_BYTES 8
typedef struct { uint8_t id[PICO_UNIQUE_BOARD_ID_SIZE_BYTES]; } pico_unique_board_id_t; 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)) #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; } vTaskResume(hcore1); return true; }
#define save_and_disable_interrupts() 1 #define save_and_disable_interrupts() 1
#include "esp_partition.h" #define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b)
extern esp_partition_t part0; #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 #define restore_interrupts(a) (void)a
#endif #endif

View File

@@ -40,7 +40,8 @@
#else #else
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
#include "esp_compat.h" #include "esp_compat.h"
esp_partition_t part0; #include "esp_partition.h"
const esp_partition_t *part0;
#else #else
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
@@ -156,14 +157,17 @@ sem_release(&sem_wait);
//this function has to be called from the core 0 //this function has to be called from the core 0
void low_flash_init() { void low_flash_init() {
memset(flash_pages, 0, sizeof(page_flash_t) * TOTAL_FLASH_PAGES); memset(flash_pages, 0, sizeof(page_flash_t) * TOTAL_FLASH_PAGES);
#ifndef ENABLE_EMULATION #if defined(ENABLE_EMULATION)
mutex_init(&mtx_flash);
sem_init(&sem_wait, 0, 1);
#else
fd_map = open("memory.flash", O_RDWR | O_CREAT, (mode_t) 0600); fd_map = open("memory.flash", O_RDWR | O_CREAT, (mode_t) 0600);
lseek(fd_map, PICO_FLASH_SIZE_BYTES - 1, SEEK_SET); lseek(fd_map, PICO_FLASH_SIZE_BYTES - 1, SEEK_SET);
write(fd_map, "", 1); write(fd_map, "", 1);
map = mmap(0, PICO_FLASH_SIZE_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd_map, 0); 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 #endif
} }

View File

@@ -1,3 +1,5 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/tinyusb: "^0.15.0" espressif/esp_tinyusb: "^1.4.4"
#espressif/tinyusb: "^0.15.0"
zorxx/neopixel: "^1.0.4"

View File

@@ -16,6 +16,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
// Pico // Pico
@@ -222,6 +223,22 @@ bool wait_button() {
struct apdu apdu; 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() { void led_blinking_task() {
static uint32_t start_ms = 0; static uint32_t start_ms = 0;
static uint8_t led_state = false; static uint8_t led_state = false;
@@ -254,6 +271,8 @@ void led_blinking_task() {
} }
#elif defined(CYW43_WL_GPIO_LED_PIN) #elif defined(CYW43_WL_GPIO_LED_PIN)
cyw43_arch_gpio_put(led_color, led_state); cyw43_arch_gpio_put(led_color, led_state);
#elif ESP_PLATFORM
neopixel_SetPixel(neopixel, &pixel[led_state], 1);
#endif #endif
led_state ^= 1; // toggle led_state ^= 1; // toggle
} }
@@ -299,10 +318,10 @@ void init_rtc() {
extern void neug_task(); extern void neug_task();
extern void usb_task(); extern void usb_task();
void execute_tasks()
void execute_tasks() { {
usb_task(); usb_task();
#ifndef ENABLE_EMULATION #if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
tud_task(); // tinyusb device task tud_task(); // tinyusb device task
#endif #endif
led_blinking_task(); led_blinking_task();
@@ -332,23 +351,31 @@ void core0_loop() {
button_pressed_time = button_press = 0; button_pressed_time = button_press = 0;
} }
} }
#endif
#ifdef ESP_PLATFORM
vTaskDelay(pdMS_TO_TICKS(10));
#endif #endif
} }
} }
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
#include "tinyusb.h"
extern const tinyusb_config_t tusb_cfg;
TaskHandle_t hcore0 = NULL, hcore1 = NULL; TaskHandle_t hcore0 = NULL, hcore1 = NULL;
int app_main() { int app_main() {
#else #else
int main(void) { int main(void) {
#endif #endif
#ifndef ENABLE_EMULATION #ifndef ENABLE_EMULATION
#ifdef ESP_PLATFORM
tinyusb_driver_install(&tusb_cfg);
#endif
usb_init(); usb_init();
#ifndef ESP_PLATFORM #ifndef ESP_PLATFORM
board_init(); board_init();
stdio_init_all(); stdio_init_all();
#endif #endif
#ifdef PIMORONI_TINY2040 #ifdef PIMORONI_TINY2040
gpio_init(TINY2040_LED_R_PIN); gpio_init(TINY2040_LED_R_PIN);
gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT); gpio_set_dir(TINY2040_LED_R_PIN, GPIO_OUT);
@@ -365,7 +392,6 @@ int main(void) {
led_off_all(); led_off_all();
tusb_init();
//prepare_ccid(); //prepare_ccid();
#else #else
@@ -379,10 +405,12 @@ int main(void) {
init_rtc(); init_rtc();
//ccid_prepare_receive(&ccid); //ccid_prepare_receive(&ccid);
#ifdef ESP_PLATFORM
neopixel = neopixel_Init(1, GPIO_NUM_48);
#endif
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
xTaskCreate(core0_loop, "core0", 512, NULL, tskIDLE_PRIORITY, &hcore0); xTaskCreate(core0_loop, "core0", 4096, NULL, CONFIG_TINYUSB_TASK_PRIORITY + 1, &hcore0);
vTaskStartScheduler();
#else #else
core0_loop(); core0_loop();
#endif #endif

View File

@@ -19,11 +19,11 @@
#define _PICO_KEYS_H_ #define _PICO_KEYS_H_
#include "file.h" #include "file.h"
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
#include "pico/unique_id.h"
#else
#include <stdint.h> #include <stdint.h>
#ifdef ENABLE_EMULATION
extern uint32_t board_millis(); extern uint32_t board_millis();
#endif
#if !defined(MIN) #if !defined(MIN)
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define MIN(a,b) (((a)<(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b))
@@ -44,6 +44,8 @@ extern uint32_t board_millis();
_a > _b ? _a : _b; }) _a > _b ? _a : _b; })
#endif #endif
#endif #endif
#else
#include "pico/unique_id.h"
#endif #endif
#include <string.h> #include <string.h>
@@ -59,10 +61,10 @@ extern bool wait_button();
extern void low_flash_init_core1(); 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; 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]); return make_uint16_t(b[offset], b[offset + 1]);
} }
static inline void put_uint16_t(uint16_t n, uint8_t *b) { static inline void put_uint16_t(uint16_t n, uint8_t *b) {

View File

@@ -45,7 +45,7 @@ extern "C" {
#endif #endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// COMMON CONFIGURATION // Common Configuration
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// defined by compiler flags for flexibility // defined by compiler flags for flexibility
@@ -53,20 +53,20 @@ extern "C" {
#error CFG_TUSB_MCU must be defined #error CFG_TUSB_MCU must be defined
#endif #endif
#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || \ #ifndef CFG_TUSB_OS
CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \ #if CFG_TUSB_MCU == OPT_MCU_RP2040
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 #define CFG_TUSB_OS OPT_OS_PICO
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) #elif CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
#else #define CFG_TUSB_OS OPT_OS_FREERTOS
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE #endif
#endif #endif
#ifndef CFG_TUSB_OS #ifndef CFG_TUSB_RHPORT0_MODE
#define CFG_TUSB_OS OPT_OS_FREERTOS #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
#endif #endif
#ifndef CFG_TUSB_DEBUG #ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 1 #define CFG_TUSB_DEBUG 0
#endif #endif
// Enable Device stack // Enable Device stack
@@ -113,11 +113,17 @@ extern "C" {
#ifdef USB_ITF_CCID #ifdef USB_ITF_CCID
#define CFG_TUD_VENDOR 2 #define CFG_TUD_VENDOR 2
#else #else
#define CFG_TUD_VENDOR 2 #define CFG_TUD_VENDOR 0
#endif #endif
// HID buffer size Should be sufficient to hold ID (if any) + Data // HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_EP_BUFSIZE 64 #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 #ifdef __cplusplus
} }

View File

@@ -20,6 +20,9 @@
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM) #if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/unique_id.h" #include "pico/unique_id.h"
#endif #endif
#ifdef ESP_PLATFORM
#include "tinyusb.h"
#endif
#include "pico_keys_version.h" #include "pico_keys_version.h"
#include "usb.h" #include "usb.h"
@@ -61,37 +64,91 @@ tusb_desc_device_t const desc_device = {
.bNumConfigurations = 1 .bNumConfigurations = 1
}; };
#ifndef ESP_PLATFORM
uint8_t const *tud_descriptor_device_cb(void) { uint8_t const *tud_descriptor_device_cb(void) {
return (uint8_t const *) &desc_device; return (uint8_t const *) &desc_device;
} }
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Configuration Descriptor // Configuration Descriptor
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
tusb_desc_configuration_t const desc_config = { #define TUD_INTERFACE_DESC_LEN 9
.bLength = sizeof(tusb_desc_configuration_t), #define TUD_ENDPOINT_DESC_LEN 7
.bDescriptorType = TUSB_DESC_CONFIGURATION, #define TUSB_SMARTCARD_LEN 54
.wTotalLength = (sizeof(tusb_desc_configuration_t) #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))
#ifdef USB_ITF_CCID
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + enum {
3 * sizeof(tusb_desc_endpoint_t)) TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN
+ (sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
2 * sizeof(tusb_desc_endpoint_t))
#endif
#ifdef USB_ITF_HID #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 #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 #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 = { static const struct ccid_class_descriptor desc_ccid = {
.bLength = sizeof(struct ccid_class_descriptor), .bLength = sizeof(struct ccid_class_descriptor),
.bDescriptorType = 0x21, .bDescriptorType = 0x21,
@@ -118,7 +175,7 @@ static const struct ccid_class_descriptor desc_ccid = {
.bPINSupport = 0x0, .bPINSupport = 0x0,
.bMaxCCIDBusySlots = 0x01, .bMaxCCIDBusySlots = 0x01,
}; };
*/
tusb_desc_interface_t const desc_interface = { tusb_desc_interface_t const desc_interface = {
.bLength = sizeof(tusb_desc_interface_t), .bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE, .bDescriptorType = TUSB_DESC_INTERFACE,
@@ -170,62 +227,9 @@ tusb_desc_endpoint_t const desc_ep3 = {
.bInterval = 0 .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 #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 #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" #include "apdu.h"
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) { uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
printf("report_cb %d\n", itf); printf("report_cb %d\n", itf);
@@ -239,39 +243,15 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
return NULL; return NULL;
} }
#endif #endif
#ifndef ESP_PLATFORM
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
(void) index; // for multiple configurations (void) index; // for multiple configurations
printf("tud_descriptor_configuration_cb %d\n",index);
static uint8_t initd = 0; return desc_config;
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;
} }
#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 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 #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"); printf("!!!!!!!!!!!! tud_descriptor_bos_cb\n");
return desc_bos; return desc_bos;
} }
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// String Descriptors // String Descriptors
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@@ -397,7 +377,16 @@ char const *string_desc_arr [] = {
, "Pico Key WebCCID Interface" , "Pico Key WebCCID Interface"
#endif #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]; static uint16_t _desc_str[32];
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { 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; return _desc_str;
} }
#endif