stm32 unique device id

STM32 Unique Device ID: How to Generate and Use

By Andrew Jones
AD

Discovering a unique device ID is crucial when differentiating or identifying STM32 microcontrollers in IoT or embedded systems. This ID aids in device management, security, and remote access. Let's explore the methods to generate and use a unique device ID for STM32 devices.

STM32, a popular range of microcontrollers by STMicroelectronics, doesn't come with a unique ID out of the box. However, we can generate one using built-in hardware features or external EEPROMs.

Generating Unique Device ID from STM32 Hardware

STM32 microcontrollers have unique characteristics that can be harnessed to create a unique ID. Two common methods are using the Unique Device ID (UID) and theCube.AI feature set.

AD
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
Getting started with STM: STM32F429 Discovery
Getting started with STM: STM32F429 Discovery

The UID is a 96-bit number stored in the microcontroller's one-time programmable (OTP) memory. It's virtually unique, making it a popular choice for generating a device ID. However, it's not perfectly reliable for security applications due to the possibility of collision with other devices, especially in large-scale deployments.

Using Unique Device ID (UID)

To retrieve the UID, you can use the STM32CubeMX software or write custom code to access the OTP memory. Once obtained, the UID can be used as the device ID or hashed for better uniqueness.

STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
STM32F4 Discovery Board Pinou Diagram Details, Features and Examples
STM32F4 Discovery Board Pinou Diagram Details, Features and Examples

Here's a simple example of retrieving the UID using STM32CubeMX in C:

uint32_t UID[3];
HAL UniqueId(&UID[0]);
// UID[0], UID[1], UID[2] contain the UID values

Using theCube.AI Feature Set

TheCube.AI is a secure element that provides a unique, cryptographically secure identifier for STM32 microcontrollers. It generates a 128-bit unique key during the first boot, making it more secure than using the UID.

AD

Accessing theCube.AI requires Secure Firmware (SFC) support and specific API calls. Here's an example of generating a unique key using theCube.AI API:

3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
STM32 Serial Communication With PC (STM32 UART To USB)
STM32 Serial Communication With PC (STM32 UART To USB)
Переходим на STM32 | Практическая электроника
Переходим на STM32 | Практическая электроника
an electronic circuit board with all the components labeled
an electronic circuit board with all the components labeled
STM32 tutorials
STM32 tutorials
Introduction to the STM32 Blue Pill (STM32duino)
Introduction to the STM32 Blue Pill (STM32duino)
cubeAI_Status_t status;
uint8_t uniqueKey[16];
status = CUBE_AI_GetUniqueKey(uniqueKey);
// uniqueKey now contains the unique 128-bit key

Using External EEPROM for Storing Device ID

For more reliable and secure unique device IDs, external EEPROMs like I2C or SPI EEPROMs can be used. These devices retain data even when power is lost, and their ID can be securely programmed during manufacturing or after production.

To use an external EEPROM, first, initialize and address it correctly. Then, read the stored device ID. If none exists, generate a new unique ID (e.g., using a random number generator with sufficient entropy) and write it to the EEPROM.

AD

Initializing and Reading EEPROM for Device ID

Here's an example of initializing and reading an I2C EEPROM using HAL driver in STM32:

STM32 Starting Small
STM32 Starting Small
Нестандартный способ подружиться с STM32: не Ардуино и не Discovery
Нестандартный способ подружиться с STM32: не Ардуино и не Discovery
Introduction to STM32 microcontrollers
Introduction to STM32 microcontrollers
STM32F10x Standard Peripherals Library
STM32F10x Standard Peripherals Library
STM32 SPI Communication - The Engineering Projects
STM32 SPI Communication - The Engineering Projects
STM32 vs Arduino
STM32 vs Arduino
STM32F103C8T6 - Purple Pill
STM32F103C8T6 - Purple Pill
Canaduino STM32 boards with MicroPython
Canaduino STM32 boards with MicroPython
a person holding up a tiny electronic device with the words is this the most?
a person holding up a tiny electronic device with the words is this the most?
USB multimedia keys on Arduino STM32
USB multimedia keys on Arduino STM32
Using DAC with STM32 - The Engineering Projects
Using DAC with STM32 - The Engineering Projects
STM32 DMA Tutorial How to Use Direct Memory Access
STM32 DMA Tutorial How to Use Direct Memory Access
uint8_t deviceID[sizeof(uint32_t)];
I2C_HandleTypeDef hi2c;
HAL_I2C_Init(&hi2c); // Initialize I2C
HAL_I2C_Mem_Read(&hi2c, EEPROM_ADDR,Device_ID_ADDR, I2C_MEMADD_SIZE_16BIT, deviceID, sizeof(deviceID), 1000);
// deviceID now contains the stored device ID

Writing a New Device ID to EEPROM

If no device ID was found, generate a new one and write it to the EEPROM:

uint32_t newID = GenerateNewID(); // Implement your generation logic
HAL_I2C_Mem_Write(&hi2c, EEPROM_ADDR, Device_ID_ADDR, I2C_MEMADD_SIZE_16BIT, &newID, sizeof(newID), 1000);
// The new device ID is now stored in the EEPROM

In the vast world of IoT and embedded systems, ensuring your STM32 devices have a unique ID is quintessential. Whether you use the STM32's built-in features or external EEPROMs, generating and managing unique device IDs secures your devices and aids in efficient fleet management. Stay secure, stay unique!

AD
AD