From eea23fa7023b310e135abc1dac275625858b813b Mon Sep 17 00:00:00 2001 From: dweller Date: Tue, 17 Sep 2024 23:24:18 +0300 Subject: seems to be working read from EEPROM --- CMakeLists.txt | 15 ++- main.c | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- tusb_config.h | 114 ++++++++++++++++++ tusb_desc.c | 204 ++++++++++++++++++++++++++++++++ 4 files changed, 682 insertions(+), 16 deletions(-) create mode 100644 tusb_config.h create mode 100644 tusb_desc.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ec8beae..9ceb9ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,6 @@ project(eeprom2blk) enable_language(C CXX ASM) add_executable(eeprom2blk main.c) - set_source_files_properties(main.c PROPERTIES COMPILE_FLAGS -Wall -Wextra -pedantic C_STANDARD 90 @@ -15,11 +14,19 @@ set_source_files_properties(main.c PROPERTIES file(MAKE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/generated) pico_generate_pio_header(eeprom2blk ${CMAKE_CURRENT_LIST_DIR}/ws2812.pio OUTPUT_DIR ${CMAKE_CURRENT_LIST_DIR}/generated) -target_link_libraries(eeprom2blk pico_stdlib hardware_pio hardware_dma) +target_include_directories(eeprom2blk PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +target_link_libraries(eeprom2blk PUBLIC + pico_stdlib + hardware_pio + hardware_dma + pico_unique_id + tinyusb_device + tinyusb_board +) # change default stdout -pico_enable_stdio_usb(eeprom2blk 1) -pico_enable_stdio_uart(eeprom2blk 0) +pico_enable_stdio_usb(eeprom2blk 0) +pico_enable_stdio_uart(eeprom2blk 1) # create map/bin/hex/uf2 file in addition to ELF. pico_add_extra_outputs(eeprom2blk) diff --git a/main.c b/main.c index 8a7250e..9985c59 100644 --- a/main.c +++ b/main.c @@ -12,38 +12,379 @@ #include "ws2812.pio.h" +#include "bsp/board.h" +#include "tusb.h" + +#include "tusb_desc.c" + #define PIN_RGB 16 +#define COL_GRN 0x33000000 +#define COL_RED 0x00330000 +#define COL_BLU 0x00003300 +#define COL_ORG 0x05330000 + +/* + * Needed Pins: + * 8 data 5v + * 1 eeprom ce 5v + * 1 eeprom oe 5v + * 1 eeprom we 5v + * 1 shift oe 5v + * 1 shift clr 5v + * 1 shift sclk 5v + * 1 shift rclk 5v + * 1 shift in 5v + * 1 FET power 3v3 + * ----------------- + * 16 pins 5v + * 1 pin 3v3 + * ----------------- + * 17 pins + */ + +enum +{ + SR_IN = 0, + SR_nOE, + SR_RCLK, + SR_SCLK, + SR_nCLR, + + SR_PIN_CNT +}; + +const uint8_t sr_pins[SR_PIN_CNT] = +{ + [SR_IN] = 9, + [SR_nOE] = 10, + [SR_RCLK] = 11, + [SR_SCLK] = 12, + [SR_nCLR] = 13, +}; + +const uint8_t sr_pins_def[SR_PIN_CNT] = +{ + [SR_IN] = 0, + [SR_nOE] = 1, + [SR_RCLK] = 0, + [SR_SCLK] = 0, + [SR_nCLR] = 1, +}; + + +enum +{ + ROM_nWE = 0, + ROM_nOE, + ROM_nCE, + + ROM_PIN_CNT +}; + +const uint8_t rom_pins[ROM_PIN_CNT] = +{ + [ROM_nWE] = 14, + [ROM_nOE] = 15, + [ROM_nCE] = 26, +}; + +const uint8_t rom_pins_def[ROM_PIN_CNT] = +{ + [ROM_nWE] = 1, + [ROM_nOE] = 1, + [ROM_nCE] = 0, +}; + + +#define PIN_IO_OE 29 +#define PIN_IO_OE_DEF 1 + +const uint8_t io_pins[8] = +{ + 3, + 4, + 5, + 6, + 7, + 8, + 27, + 28 +}; + + + +#define EEPROM_BLK_SZ 64 +#define BLKS 64 +#define BLK_SZ 512 + +uint8_t data[BLK_SZ * BLKS]; +uint64_t access = 0; + +#define sleep_ns(ns) \ + do{ for(int i = 0; i < ((ns) / 8); i++) __asm volatile ("nop\n"); }while(0) + +static void set_addr(uint16_t addr) +{ + gpio_put(sr_pins[SR_nOE], 1); + gpio_put(sr_pins[SR_RCLK], 0); + + gpio_put(sr_pins[SR_nCLR], 0); + sleep_ns(24); + + gpio_put(sr_pins[SR_nCLR], 1); + + for(int i = 0; i < 16; i++) + { + gpio_put(sr_pins[SR_IN], addr & 1); + addr = addr >> 1; + + gpio_put(sr_pins[SR_SCLK], 0); + sleep_ns(17); + gpio_put(sr_pins[SR_SCLK], 1); + sleep_ns(17); + } + + gpio_put(sr_pins[SR_RCLK], 1); + gpio_put(sr_pins[SR_nOE], 0); + sleep_ns(24); +} + + int main(void) { + board_init(); + tusb_init(); + stdio_init_all(); gpio_init(PIN_RGB); gpio_set_dir(PIN_RGB, GPIO_OUT); - gpio_put(PIN_RGB, 1); + gpio_put(PIN_RGB, 0); - PIO pio = pio0; - int sm = 0; // XXX: ??? - uint32_t offset = pio_add_program(pio, &ws2812_program); - ws2812_program_init(pio, sm, offset, PIN_RGB, 800000, false); + gpio_init(PIN_IO_OE); + gpio_set_dir(PIN_IO_OE, GPIO_OUT); + gpio_put(PIN_IO_OE, PIN_IO_OE_DEF); + + for(int i = 0; i < SR_PIN_CNT; i++) + { + gpio_init(sr_pins[i]); + gpio_set_dir(sr_pins[i], GPIO_OUT); + gpio_put(sr_pins[i], sr_pins_def[i]); + } - uint32_t col = 0; - pio_sm_put_blocking(pio0, 0, col << 8u); + for(int i = 0; i < ROM_PIN_CNT; i++) + { + gpio_init(rom_pins[i]); + gpio_set_dir(rom_pins[i], GPIO_OUT); + gpio_put(rom_pins[i], rom_pins_def[i]); + } + + for(int i = 0; i < 8; i++) + { + gpio_init(io_pins[i]); + gpio_set_dir(io_pins[i], GPIO_IN); + gpio_pull_up(io_pins[i]); + } + + gpio_put(PIN_IO_OE, 1); + + + int sm = 0; // XXX: ??? + uint32_t offset = pio_add_program(pio0, &ws2812_program); + + ws2812_program_init(pio0, sm, offset, PIN_RGB, 800000, false); + pio_sm_put_blocking(pio0, 0, 0); sleep_ms(1000); - col = 0x0000FF; + uint32_t prev = access; + uint32_t start_ms = 0; + uint32_t interval = 60; + int state = 1; + int prevs = state; + printf("Hell, world...\n"); + + const char msg[] = "Hell, world!\n" + "This is a test string embedded into fake block device to serve as a test\n" + "of USB Mass Storage on Raspberry Pi Pico.\n" + "\n" + "- dweller from cabin.digital\n"; + + memcpy(data, msg, sizeof(msg)); + while(1) { - pio_sm_put_blocking(pio0, 0, col); - col = col << 8u; - if(!col) col = 0x0000FF; - sleep_ms(250); + tud_task(); + + uint32_t diff = board_millis() - start_ms; + if(diff >= interval) + { + start_ms += diff; + + if(prev != access) + { + prev = access; + + prevs = state; + state = !state; + if(state) pio_sm_put_blocking(pio0, 0, COL_GRN); + else pio_sm_put_blocking(pio0, 0, 0); + } + else if(prevs != state) + { + state = 1; + prevs = state; + pio_sm_put_blocking(pio0, 0, COL_GRN); + } + } } return 0; } + +uint8_t tud_msc_get_maxlun_cb(void) +{ + return 1; +} + +void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], + uint8_t product_rev[4]) +{ + const char vid[] = "CAB_DIG"; + const char pid[] = "EEPROM Progrmmr"; + const char rev[] = "0dev"; + + memcpy(vendor_id , vid, strlen(vid)); + memcpy(product_id , pid, strlen(pid)); + memcpy(product_rev, rev, strlen(rev)); + + printf("scsi: inq\n"); +} + +bool tud_msc_test_unit_ready_cb(uint8_t lun) +{ + printf("scsi: rdy\n"); + + return true; +} + +void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) +{ + *block_count = BLKS; + *block_size = BLK_SZ; + + printf("scsi: cap\n"); +} + +bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) +{ + printf("scsi: stop\n"); + return true; +} + +int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { + if(lba >= BLK_SZ) return -1; + + int addr = (lba * BLK_SZ) + offset; + //memcpy(buffer, data + addr, bufsize); + access++; + + printf("scsi: read @ %04X\n", addr); + + uint8_t* bytes = buffer; + for(int i = 0; i < bufsize; i++) + { + set_addr(addr + i); + + gpio_put(rom_pins[ROM_nOE], 0); + sleep_ns(150); + + uint8_t byte = 0; + for(int i = 0; i < 8; i++) + byte |= gpio_get(io_pins[i]) << i; + + bytes[i] = byte; + + gpio_put(rom_pins[ROM_nOE], 1); + sleep_ns(150); + + } + + return (int32_t)bufsize; +} + +bool tud_msc_is_writable_cb(uint8_t lun) +{ + printf("scsi: rw?\n"); + return true; +} + +int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, + uint32_t bufsize) +{ + if(lba >= BLK_SZ) return -1; + + int addr = (lba * BLK_SZ) + offset; + memcpy(data + addr, buffer, bufsize); + access++; + + printf("scsi: write @ %04x\n", addr); + + for(int i = 0; i < BLK_SZ; i++) + set_addr(addr + i); + + return (int32_t) bufsize; +} + +int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) +{ + void const* response = NULL; + int32_t resplen = 0; + + // most scsi handled is input + bool in_xfer = true; + + switch (scsi_cmd[0]) { + default: + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); + return -1; + } + + if (resplen > bufsize) resplen = bufsize; + + if (response && (resplen > 0)) { + if (in_xfer) { + memcpy(buffer, response, (size_t) resplen); + } else { + // SCSI output + } + } + + printf("scsi: generic\n"); + return resplen; +} + +void tud_mount_cb(void) +{ + pio_sm_put_blocking(pio0, 0, COL_GRN); +} + +void tud_umount_cb(void) +{ + pio_sm_put_blocking(pio0, 0, COL_RED); +} + +void tud_suspend_cb(bool remote_wakeup_en) +{ + pio_sm_put_blocking(pio0, 0, COL_ORG); +} + +void tud_resume_cb(void) +{ + pio_sm_put_blocking(pio0, 0, COL_BLU); +} diff --git a/tusb_config.h b/tusb_config.h new file mode 100644 index 0000000..82e9eea --- /dev/null +++ b/tusb_config.h @@ -0,0 +1,114 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +//--------------------------------------------------------------------+ +// Board Specific Configuration +//--------------------------------------------------------------------+ + +// defined by board.mk +#ifndef CFG_TUSB_MCU + #error CFG_TUSB_MCU must be defined +#endif + +// RHPort number used for device can be defined by board.mk, default to port 0 +#ifndef BOARD_DEVICE_RHPORT_NUM + #define BOARD_DEVICE_RHPORT_NUM 0 +#endif + +// RHPort max operational speed can defined by board.mk +// Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed +#ifndef BOARD_DEVICE_RHPORT_SPEED + #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 || CFG_TUSB_MCU == OPT_MCU_SAMX7X) + #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED + #else + #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED + #endif +#endif + +// Device mode with rhport and speed defined by board.mk +#if BOARD_DEVICE_RHPORT_NUM == 0 + #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) +#elif BOARD_DEVICE_RHPORT_NUM == 1 + #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) +#else + #error "Incorrect RHPort configuration" +#endif + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- + +// defined by compiler flags for flexibility +#ifndef CFG_TUSB_MCU +#error CFG_TUSB_MCU must be defined +#endif + +#ifndef CFG_TUSB_OS +#define CFG_TUSB_OS OPT_OS_NONE +#endif + +#ifndef CFG_TUSB_DEBUG +#define CFG_TUSB_DEBUG 1 +#endif + +#ifndef CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_SECTION +#endif + +#ifndef CFG_TUSB_MEM_ALIGN +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) +#endif + +//-------------------------------------------------------------------- +// CONFIGURATION +//-------------------------------------------------------------------- + +#ifndef CFG_TUD_ENDPOINT0_SIZE +#define CFG_TUD_ENDPOINT0_SIZE 64 +#endif + +//------------- CLASS -------------// +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 + +// MSC Buffer size of Device Mass storage +#define CFG_TUD_MSC_EP_BUFSIZE 512 + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_CONFIG_H_ */ diff --git a/tusb_desc.c b/tusb_desc.c new file mode 100644 index 0000000..5545b39 --- /dev/null +++ b/tusb_desc.c @@ -0,0 +1,204 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#include "pico/unique_id.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 const desc_device = +{ + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + + .idVendor = 0xCafe, + .idProduct = USB_PID, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01 +}; + +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ + +enum +{ + ITF_NUM_MSC, + ITF_NUM_TOTAL +}; + +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN) + +#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX + // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number + // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ... + #define EPNUM_MSC_OUT 0x02 + #define EPNUM_MSC_IN 0x82 + +#elif CFG_TUSB_MCU == OPT_MCU_CXD56 + // CXD56 USB driver has fixed endpoint type (bulk/interrupt/iso) and direction (IN/OUT) by its number + // 0 control (IN/OUT), 1 Bulk (IN), 2 Bulk (OUT), 3 In (IN), 4 Bulk (IN), 5 Bulk (OUT), 6 In (IN) + #define EPNUM_MSC_OUT 0x02 + #define EPNUM_MSC_IN 0x81 + +#elif defined(TUD_ENDPOINT_ONE_DIRECTION_ONLY) + // MCUs that don't support a same endpoint number with different direction IN and OUT defined in tusb_mcu.h + // e.g EP1 OUT & EP1 IN cannot exist together + #define EPNUM_MSC_OUT 0x01 + #define EPNUM_MSC_IN 0x82 + +#else + #define EPNUM_MSC_OUT 0x01 + #define EPNUM_MSC_IN 0x81 + +#endif + +uint8_t const desc_fs_configuration[] = +{ + // Config number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), + + // Interface number, string index, EP Out & EP In address, EP size + TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64), +}; + +#if TUD_OPT_HIGH_SPEED +uint8_t const desc_hs_configuration[] = +{ + // Config number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), + + // Interface number, string index, EP Out & EP In address, EP size + TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC_OUT, EPNUM_MSC_IN, 512), +}; +#endif + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(uint8_t index) +{ + (void) index; // for multiple configurations + +#if TUD_OPT_HIGH_SPEED + // Although we are highspeed, host may be fullspeed. + return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration; +#else + return desc_fs_configuration; +#endif +} + +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ + +// String Descriptor Index +enum { + STRID_LANGID = 0, + STRID_MANUFACTURER, + STRID_PRODUCT, + STRID_SERIAL, +}; + +// array of pointer to string descriptors +char const *string_desc_arr[] = +{ + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "CAB_DIG", // 1: Manufacturer + "EEPROM Programmer", // 2: Product + NULL, // 3: Serials will use unique ID if possible +}; + +static uint16_t _desc_str[32 + 1]; + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { + (void) langid; + size_t chr_count; + + switch ( index ) { + case STRID_LANGID: + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + break; + + case STRID_SERIAL: + chr_count = board_usb_get_serial(_desc_str + 1, 32); + break; + + default: + // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. + // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors + + if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL; + + const char *str = string_desc_arr[index]; + + // Cap at max char + chr_count = strlen(str); + size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type + if ( chr_count > max_count ) chr_count = max_count; + + // Convert ASCII string into UTF-16 + for ( size_t i = 0; i < chr_count; i++ ) { + _desc_str[1 + i] = str[i]; + } + break; + } + + // first byte is length (including header), second byte is string type + _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2)); + + return _desc_str; +} -- cgit v1.2.3