Skip to content

Commit

Permalink
Merge pull request #18 from silbe/usb-serno
Browse files Browse the repository at this point in the history
Add USB serial number based on unique chip id
  • Loading branch information
daniel-thompson authored Nov 10, 2023
2 parents 7b6201e + 9c4fccd commit 51861b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/i2c-stm32f1-usb/i2c-stm32f1-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static const struct usb_device_descriptor dev = {
.bcdDevice = 0x0205,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 0,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};

Expand Down Expand Up @@ -94,9 +94,13 @@ static const struct usb_config_descriptor config = {
.interface = ifaces,
};

/* 96 bit as hex, no prefix */
static char serial_nr[28] = { 0 };

static const char *usb_strings[] = {
"redfelineninja.org.uk",
"i2c-stm32f1-usb",
serial_nr,
};

/* Buffer to be used for control requests. */
Expand Down Expand Up @@ -341,7 +345,7 @@ static int usb_fibre(fibre_t *fibre)
PT_WAIT_UNTIL(fibre_timeout(t));

usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config,
usb_strings, 2,
usb_strings, 3,
usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbd_dev, usb_set_config);

Expand Down Expand Up @@ -383,10 +387,7 @@ static void jump_to_bootloader(void)

static pt_state_t do_id(console_t *c)
{
char serial_no[25];

desig_get_unique_id_as_string(serial_no, sizeof(serial_no));
fprintf(c->out, "%s\n", serial_no);
fprintf(c->out, "%s\n", serial_nr);

return PT_EXITED;
}
Expand All @@ -412,6 +413,7 @@ int main(void)
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);

desig_get_unique_id_as_string(serial_nr, sizeof(serial_nr));
console_init(&uart_console, stdout);
for (i=0; i<lengthof(cmds); i++)
console_register(&cmds[i]);
Expand Down
10 changes: 8 additions & 2 deletions src/i2c-stm32f4-usb/i2c-stm32f4-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/desig.h>
#include <libopencm3/stm32/i2c.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
Expand All @@ -47,7 +48,7 @@ static const struct usb_device_descriptor dev = {
.bcdDevice = 0x0205,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 0,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};

Expand Down Expand Up @@ -92,9 +93,13 @@ static const struct usb_config_descriptor config = {
.interface = ifaces,
};

/* 96 bit as hex, no prefix */
static char serial_nr[28] = { 0 };

static const char *usb_strings[] = {
"redfelineninja.org.uk",
"i2c-stm32f4-usb",
serial_nr,
};

/* Buffer to be used for control requests. */
Expand Down Expand Up @@ -322,7 +327,7 @@ static int usb_fibre(fibre_t *fibre)
gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12);

usbd_dev = usbd_init(&otgfs_usb_driver, &dev, &config,
usb_strings, 2,
usb_strings, 3,
usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbd_dev, usb_set_config);

Expand Down Expand Up @@ -367,6 +372,7 @@ int main(void)
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);

desig_get_unique_id_as_string(serial_nr, sizeof(serial_nr));
i2c_init();
time_init();

Expand Down

0 comments on commit 51861b9

Please sign in to comment.