summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-08-23 09:30:31 -0400
committerGitHub <noreply@github.com>2018-08-23 09:30:31 -0400
commit728622f09d99faf99d4208e5be8b36e7c479d7b1 (patch)
tree1360130d8e31dd7150abdf2257b9192255215486 /ports
parentc135c7f376339e77b22675205899ed957885f696 (diff)
parent4a8cd03de1068061f0dc98ef59fb43d84c952662 (diff)
Merge pull request #1139 from hathach/nrf52_usb_serial
use unique DevicID for usb serial
Diffstat (limited to 'ports')
-rw-r--r--ports/nrf/usb/usb.c18
-rw-r--r--ports/nrf/usb/usb_desc.c4
2 files changed, 19 insertions, 3 deletions
diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c
index 7168cf53d..f8b5fd663 100644
--- a/ports/nrf/usb/usb.c
+++ b/ports/nrf/usb/usb.c
@@ -58,7 +58,7 @@ void usb_init(void) {
sd_power_usbregstatus_get(&usb_reg);
}else
-#else
+#endif
{
// Power module init
const nrfx_power_config_t pwr_cfg = { 0 };
@@ -72,7 +72,6 @@ void usb_init(void) {
usb_reg = NRF_POWER->USBREGSTATUS;
}
-#endif
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
@@ -82,6 +81,21 @@ void usb_init(void) {
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
}
+ // create serial number based on device unique id
+ extern uint16_t usb_desc_str_serial[1 + 16];
+
+ char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
+ for (int i = 0; i < 2; i++) {
+ for (int j = 0; j < 8; j++) {
+ uint8_t nibble = (NRF_FICR->DEVICEID[i] >> j * 4) & 0xf;
+
+ // Invert order since it is LE, +1 for skipping descriptor header
+ uint8_t const idx = (15 - (i * 8 + j)) + 1;
+ usb_desc_str_serial[idx] = nibble_to_hex[nibble];
+ }
+ }
+
tusb_init();
#if MICROPY_KBD_EXCEPTION
diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c
index 31f80b80d..10d603528 100644
--- a/ports/nrf/usb/usb_desc.c
+++ b/ports/nrf/usb/usb_desc.c
@@ -78,6 +78,8 @@ enum {
// STRING DESCRIPTORS
//--------------------------------------------------------------------+
+uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) };
+
// array of pointer to string descriptors
uint16_t const * const string_desc_arr [] =
{
@@ -91,7 +93,7 @@ uint16_t const * const string_desc_arr [] =
TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','n','R','F','5','2'),
// 3 Serials TODO use chip ID
- TUD_DESC_STRCONV('1', '2', '3', '4', '5'),
+ usb_desc_str_serial,
// 4 CDC Interface
TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','S','e','r','i','a','l'),