diff --git a/lib/usb_dfu.c b/lib/usb_dfu.c index 878c3a4..c8ea755 100644 --- a/lib/usb_dfu.c +++ b/lib/usb_dfu.c @@ -13,9 +13,9 @@ * */ /** library for USB DFU to write on internal flash (code) - * @file usb_dfu.c + * @file * @author King Kévin - * @date 2017 + * @date 2017-2019 */ /* standard libraries */ @@ -60,7 +60,7 @@ static const struct usb_device_descriptor usb_dfu_device = { .bcdDevice = 0x0000, /**< Device Release Number: board version number */ .iManufacturer = 1, /**< the index of the string in the string table that represents the name of the manufacturer of this device */ .iProduct = 2, /**< the index of the string in the string table that represents the name of the product */ - .iSerialNumber = 0, /**< the index of the string in the string table that represents the serial number of this item in string form */ + .iSerialNumber = 3, /**< the index of the string in the string table that represents the serial number of this item in string form */ .bNumConfigurations = 1, /**< the number of possible configurations this device has */ }; @@ -88,7 +88,7 @@ static const struct usb_interface_descriptor usb_dfu_interface = { .bInterfaceClass = 0xFE, /**< DFU interface class (not defined in libopencm3 dfu lib) */ .bInterfaceSubClass = 1, /**< DFU interface subclass (not defined in libopencm3 dfu lib) */ .bInterfaceProtocol = 2, /**< DFU interface mode protocol (not defined in libopencm3 dfu lib) */ - .iInterface = 3, /**< the index of the string in the string table that represents interface description */ + .iInterface = 4, /**< the index of the string in the string table that represents interface description */ .extra = &usb_dfu_functional, /**< point to functional descriptor */ .extralen = sizeof(usb_dfu_functional), /**< size of functional descriptor */ }; @@ -115,12 +115,16 @@ static const struct usb_config_descriptor usb_dfu_configuration = { .interface = usb_dfu_interfaces, /**< pointer to an array of interfaces */ }; +/** device ID used as serial number */ +static char usb_serial[] = "00112233445566778899aabb"; + /** USB string table * @note starts with index 1 */ static const char *usb_dfu_strings[] = { "CuVoodoo", /**< manufacturer string */ "CuVoodoo STM32F1xx DFU bootloader", /**< product string */ + (const char*)usb_serial, /**< device ID used as serial number */ "DFU bootloader (DFU mode)", /**< DFU interface string */ }; @@ -284,6 +288,27 @@ static enum usbd_request_return_codes usb_dfu_control_request(usbd_device *usbd_ void usb_dfu_setup(void) { + // set serial + for (uint8_t i = 0; i < LENGTH(usb_serial) - 1; i++) { // write the serial + uint32_t id; // current ID part + if (i < 8) { + id = DESIG_UNIQUE_ID0; + } else if (i < 16) { + id = DESIG_UNIQUE_ID1; + } else { + id = DESIG_UNIQUE_ID2; + } + // transform into character + char c = (id >> ((7 - (i % 8)) * 4)) & 0x0f; // get nibble + if (c < 10) { + c = '0' + c; + } else { + c = 'a' + (c - 10); + } + // set character + usb_serial[i] = c; + } + rcc_periph_reset_pulse(RST_USB); // reset USB peripheral usb_disconnect(); // disconnect to force re-enumeration rcc_periph_clock_enable(RCC_GPIOA); // enable clock for GPIO used for USB diff --git a/lib/usb_dfu.h b/lib/usb_dfu.h index 30ca731..ab7f487 100644 --- a/lib/usb_dfu.h +++ b/lib/usb_dfu.h @@ -13,9 +13,9 @@ * */ /** library for USB DFU to write on internal flash (API) - * @file usb_dfu.h + * @file * @author King Kévin - * @date 2017 + * @date 2017-2019 */ #pragma once