Reading STM32F4 unique device ID from MBED compiler

When working with the STM32 family of microcontrollers, it can be useful to evaluate the factory-programmed 96-bit UUID. The electronic signature is stored in the Flash memory area. It can be read using the JTAG/SWD or the CPU. It contains factory-programmed identification data that allow the user firmware or other external devices to automatically match its interface to the characteristics of the STM32F4xx microcontrollers.

The unique device identifier is ideally suited:
* for use as serial numbers (for example USB string serial numbers or other end applications)
* 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 96-bit unique device identifier provides a reference number which is unique for any device and in any context. These bits can never be altered by the user.
The 96-bit unique device identifier can also be read in single bytes/half-words/words in different ways and then be concatenated using a custom algorithm.
According to the STM32F4 reference manual, the UUID is stored in memory at address 0x1FFF7A10.

Examples:

1. STM32-UID.h
/**
* A simple header for reading the STM32 device UUID
* Tested with STM32F4 and STM32F0 families
*
* Version 1.0
* Written by Uli Koehler
* Published on http://techoverflow.net
* Licensed under CC0 (public domain):
* https://creativecommons.org/publicdomain/zero/1.0/
*
* Posted by "Electrical engineering and programming notepad"
* URL: https://ee-programming-notepad.blogspot.com/2017/06/reading-stm32f4-unique-device-id-from.html
*/
#ifndef __UUID_H
#define __UUID_H
#include <stdint.h>
/**
* The STM32 factory-programmed UUID memory.
* Three values of 32 bits each starting at this address
* Use like this: STM32_UUID[0], STM32_UUID[1], STM32_UUID[2]
*/
#define STM32_UUID ((uint32_t *)0x1FFF7A10)
#endif //__UUID_H
view raw STM32-UID.h hosted with ❤ by GitHub

2. Usage

#include "STM32-UID.h"
void main()
{
uint32_t idPart1 = STM32_UUID[0];
uint32_t idPart2 = STM32_UUID[1];
uint32_t idPart3 = STM32_UUID[2];
// do something with the overall 96 bits
printf("idPart1: 0x%08X\n", idPart1);
printf("idPart2: 0x%08X\n", idPart2);
printf("idPart3: 0x%08X\n", idPart3);
}
view raw main.cpp hosted with ❤ by GitHub
References: techoverflow 1, techoverflow 2, alternative link for STM32F4 reference manual