Here is a random little post for you guys. Down at the bottom of your reference manual just before the ending sections usually reserved for debug support and revision history there is the "Device Electronic Signature" section. Its provides you with some device specific addresses where you will find a 96-bit unique device ID as well as the flash memory size for your device. Check out the code below to get an idea of how to read these values.
Memory Size RegisterThe first thing we do is head over to the Reference manual page 825 for STM32L0 and page 1072 for STM32F1. This is where the Device ID and Flash Memory Size resister addresses are given. Observe below I have highlighted the physical address of this read-only register which yields the flash size of the device.
To print this via something like UART which accepts 8 bit data we must print the 16 bit number in two bytes. The simple uart_send( ) command below simply sends 1 byte of data. The important thing to take from the following two lines is that we shift the number pointed to by the pointer 8 times to the right , then we AND it with 0xFF to extract the 1s and 0s. This gives us the top 8 bits and the following line does the same thing except without the shifting so it yields the lower 8 bits.
96-bit Unique Device ID
The following magic number we will extract is a 96-bit unique device ID. According to the datasheet "this unique device identifier provides a reference number which is unique for any device in any context. These bits can never be changed by the user"
Further more the datasheet states:
The unique device identifier is ideally suited:
- for use as serial numbers
- for use as security keys in order to increase the security of code in Flash memory while using and combining this unique ID with software cryptographic primitives and protocols before programming the internal Flash memory
- to activate secure boot processes, etc.
The number is spread across three 32-bit registers. Each byte and sometimes word telling us something different about the chip.
- Bits 0 - 23 : Lot number
- Bits 24 -31 : Silicon wafer number
- Bits 32 -63 : Another lot number
- Bits 64 -95 : Unique ID bits
You might think that the unique ID bits are the important part but the lot numbers and wafer numbers add even more uniqueness.
The first register has a base address of 0x1FF80050 for the STM32L0 and the second address is the same with an offset of 0x04 , the third one has an offset of 0x14. We must make some variables with these addresses and their offsets. The STM32F1 has slightly different offsets so make sure to whip open that datasheet.
Since these are 32-bit wide registers we need to take the same approach as before by extracting the bits in chunks of 8-bits.
Below you can find all the code which was made using Atollic True Studio, if you are using Keil, you only need to change the chose the right alternate function macros. I am not sure why TrueStudio and Keil define them differently, they seem to be using different versions of the header file.