Skip to content

Commit 728622f

Browse files
authored
Merge pull request #1139 from hathach/nrf52_usb_serial
use unique DevicID for usb serial
2 parents c135c7f + 4a8cd03 commit 728622f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

ports/nrf/usb/usb.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void usb_init(void) {
5858

5959
sd_power_usbregstatus_get(&usb_reg);
6060
}else
61-
#else
61+
#endif
6262
{
6363
// Power module init
6464
const nrfx_power_config_t pwr_cfg = { 0 };
@@ -72,7 +72,6 @@ void usb_init(void) {
7272

7373
usb_reg = NRF_POWER->USBREGSTATUS;
7474
}
75-
#endif
7675

7776
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
7877
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
@@ -82,6 +81,21 @@ void usb_init(void) {
8281
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
8382
}
8483

84+
// create serial number based on device unique id
85+
extern uint16_t usb_desc_str_serial[1 + 16];
86+
87+
char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
88+
89+
for (int i = 0; i < 2; i++) {
90+
for (int j = 0; j < 8; j++) {
91+
uint8_t nibble = (NRF_FICR->DEVICEID[i] >> j * 4) & 0xf;
92+
93+
// Invert order since it is LE, +1 for skipping descriptor header
94+
uint8_t const idx = (15 - (i * 8 + j)) + 1;
95+
usb_desc_str_serial[idx] = nibble_to_hex[nibble];
96+
}
97+
}
98+
8599
tusb_init();
86100

87101
#if MICROPY_KBD_EXCEPTION

ports/nrf/usb/usb_desc.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ enum {
7878
// STRING DESCRIPTORS
7979
//--------------------------------------------------------------------+
8080

81+
uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) };
82+
8183
// array of pointer to string descriptors
8284
uint16_t const * const string_desc_arr [] =
8385
{
@@ -91,7 +93,7 @@ uint16_t const * const string_desc_arr [] =
9193
TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','n','R','F','5','2'),
9294

9395
// 3 Serials TODO use chip ID
94-
TUD_DESC_STRCONV('1', '2', '3', '4', '5'),
96+
usb_desc_str_serial,
9597

9698
// 4 CDC Interface
9799
TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','S','e','r','i','a','l'),

0 commit comments

Comments
 (0)