Skip to content

Commit 3610520

Browse files
authored
Merge pull request #3156 from jepler/esp32s2-serial-number
esp32s2: Use the device's EUI-48 address as unique ID
2 parents a6c2020 + a2919a6 commit 3610520

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

ports/esp32s2/common-hal/microcontroller/Processor.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
*/
2727

2828
#include <math.h>
29+
#include <string.h>
30+
2931
#include "common-hal/microcontroller/Processor.h"
3032
#include "py/runtime.h"
3133
#include "supervisor/shared/translate.h"
3234

35+
#include "soc/efuse_reg.h"
36+
3337
float common_hal_mcu_processor_get_temperature(void) {
3438
return NAN;
3539
}
@@ -42,5 +46,23 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
4246
return 0;
4347
}
4448

49+
STATIC uint8_t swap_nibbles(uint8_t v) {
50+
return ((v << 4) | (v >> 4)) & 0xff;
51+
}
52+
4553
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
54+
memset(raw_id, 0, COMMON_HAL_MCU_PROCESSOR_UID_LENGTH);
55+
56+
uint8_t *ptr = &raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH-1];
57+
// MAC address contains 48 bits (6 bytes), 32 in the low order word
58+
uint32_t mac_address_part = REG_READ(EFUSE_RD_MAC_SPI_SYS_0_REG);
59+
*ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
60+
*ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
61+
*ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
62+
*ptr-- = swap_nibbles(mac_address_part & 0xff);
63+
64+
// and 16 in the high order word
65+
mac_address_part = REG_READ(EFUSE_RD_MAC_SPI_SYS_1_REG);
66+
*ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
67+
*ptr-- = swap_nibbles(mac_address_part & 0xff);
4668
}

ports/esp32s2/common-hal/microcontroller/Processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2828
#define MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
2929

30-
#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 15
30+
#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 6
3131

3232
#include "py/obj.h"
3333

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz
77
INTERNAL_LIBM = 1
88

99
# Chip supplied serial number, in bytes
10-
USB_SERIAL_NUMBER_LENGTH = 30
10+
USB_SERIAL_NUMBER_LENGTH = 12
1111

1212
# Longints can be implemented as mpz, as longlong, or not
1313
LONGINT_IMPL = MPZ

0 commit comments

Comments
 (0)