use unique DevicID for usb serial

This commit is contained in:
hathach 2018-08-23 14:00:54 +07:00
parent 6bfff29d22
commit 4a8cd03de1
2 changed files with 19 additions and 3 deletions

View File

@ -58,7 +58,7 @@ void usb_init(void) {
sd_power_usbregstatus_get(&usb_reg); sd_power_usbregstatus_get(&usb_reg);
}else }else
#else #endif
{ {
// Power module init // Power module init
const nrfx_power_config_t pwr_cfg = { 0 }; const nrfx_power_config_t pwr_cfg = { 0 };
@ -72,7 +72,6 @@ void usb_init(void) {
usb_reg = NRF_POWER->USBREGSTATUS; usb_reg = NRF_POWER->USBREGSTATUS;
} }
#endif
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) { if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED); 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); 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(); tusb_init();
#if MICROPY_KBD_EXCEPTION #if MICROPY_KBD_EXCEPTION

View File

@ -78,6 +78,8 @@ enum {
// STRING DESCRIPTORS // STRING DESCRIPTORS
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) };
// array of pointer to string descriptors // array of pointer to string descriptors
uint16_t const * const string_desc_arr [] = 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'), TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','n','R','F','5','2'),
// 3 Serials TODO use chip ID // 3 Serials TODO use chip ID
TUD_DESC_STRCONV('1', '2', '3', '4', '5'), usb_desc_str_serial,
// 4 CDC Interface // 4 CDC Interface
TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','S','e','r','i','a','l'), TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','S','e','r','i','a','l'),