Pyboard / STM32F405 Hardware ID / Serial Number

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Pyboard / STM32F405 Hardware ID / Serial Number

Post by doublevee » Sat Mar 02, 2019 2:58 pm

Please could you someone explain how the serial number is generated on the Pyboard?

From a RPi host, I can easily detect the hardware ID/serial number/USB iSerialNumber using the following code:

>>> import usb
>>> dev = usb.core.find(idVendor = 0xf055)
>>> print(dev)
DEVICE ID f055:9801 on Bus 001 Address 006 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x200 USB 2.0
bDeviceClass : 0xef Miscellaneous
bDeviceSubClass : 0x2
bDeviceProtocol : 0x1
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0xf055
idProduct : 0x9801
bcdDevice : 0x200 Device 2.0
iManufacturer : 0x1 MicroPython
iProduct : 0x2 Pyboard Virtual Comm Port in FS Mode
iSerialNumber : 0x3 385038633134
bNumConfigurations : 0x1

On the Pyboard itself, I am running this code (having read through other forum posts first):

>>> import ubinascii
>>> import machine
>>> machine.unique_id()
b'/\x00 \x00\x03Q414808'
>>> ubinascii.hexlify(machine.unique_id()).decode()
'2f0020000351343134383038'

How do I get from this 24 digit number to the 12 digit serial number above?
Last edited by doublevee on Sun Mar 03, 2019 12:32 pm, edited 1 time in total.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard / STM32F405 Hardware ID

Post by doublevee » Sun Mar 03, 2019 12:29 pm

So I also contacted an old colleague to see if I was doing anything odd and he suggested the serial number was actually derived using a function. He was correct and pointed me at github.com/micropython/micropython/blob/master/ports/stm32/usbd_desc.c

Under the section "case USBD_IDX_SERIAL_STR" there is the function that takes certain bytes from the 12 byte hardware ID to produce the final 6 byte serial number.

I created the following code and have tested on two different Pyboards and it returns the correct Serial Number. I am complete novice programmer but thought I would post nonetheless to try and help others. I certainly spent a few hours getting to the bottom of this.

I have no doubt my code is very inefficient and ugly but you get the idea.

import machine
import ubinascii

h = (ubinascii.hexlify(machine.unique_id()).decode())
h2 = [h[i:i+2] for i in range(0, len(h), 2)]

#id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]

s1 = h2[11]
s2 = hex(int(h2[10],16)+int(h2[2],16))[-2:]
s3 = h2[9]
s4 = hex(int(h2[8],16)+int(h2[0],16))[-2:]
s5 = h2[7]
s6 = h2[6]

serial_no = s1+s2+s3+s4+s5+s6

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pyboard / STM32F405 Hardware ID / Serial Number

Post by dhylands » Sun Mar 03, 2019 5:15 pm

Here's a more direct implementation:

Code: Select all

>>> import machine
>>> import ubinascii
>>> id = machine.unique_id()
>>> serial_bytes = bytes([id[11], id[10] + id[2], id[9], id[8] + id[0], id[7], id[6]])
>>> serial = ubinascii.hexlify(serial_bytes).upper()
>>> serial
b'3650326B3432'

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: Pyboard / STM32F405 Hardware ID / Serial Number

Post by doublevee » Mon Mar 04, 2019 1:21 pm

:lol:

Thanks so much Dave - I see your name all over this forum and I appreciate all the posts you submit.

Your code is obviously far superior and neater!! I can follow that perfectly and it's very neat.

It's great to learn. I have found the whole 'bytes' and data representation in Python quite challenging but I'm slowly getting there.

Thanks again.

Post Reply