summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2020-07-16 11:00:02 -0700
committerGitHub <noreply@github.com>2020-07-16 11:00:02 -0700
commit3610520ca4ed851e5a33b8c0f073d56d76ce5eaf (patch)
tree588e5815bcfaad1738ef3a53369bd9f5c132fd10
parenta6c20208b3be3bd9e34afb7112a2a1497bc92d23 (diff)
parenta2919a6fb2c433cafa5c528ee03ee9c86efb93d8 (diff)
Merge pull request #3156 from jepler/esp32s2-serial-number
esp32s2: Use the device's EUI-48 address as unique ID
-rw-r--r--ports/esp32s2/common-hal/microcontroller/Processor.c22
-rw-r--r--ports/esp32s2/common-hal/microcontroller/Processor.h2
-rw-r--r--ports/esp32s2/mpconfigport.mk2
3 files changed, 24 insertions, 2 deletions
diff --git a/ports/esp32s2/common-hal/microcontroller/Processor.c b/ports/esp32s2/common-hal/microcontroller/Processor.c
index 8eaf1a33d..39b85a18b 100644
--- a/ports/esp32s2/common-hal/microcontroller/Processor.c
+++ b/ports/esp32s2/common-hal/microcontroller/Processor.c
@@ -26,10 +26,14 @@
*/
#include <math.h>
+#include <string.h>
+
#include "common-hal/microcontroller/Processor.h"
#include "py/runtime.h"
#include "supervisor/shared/translate.h"
+#include "soc/efuse_reg.h"
+
float common_hal_mcu_processor_get_temperature(void) {
return NAN;
}
@@ -42,5 +46,23 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
return 0;
}
+STATIC uint8_t swap_nibbles(uint8_t v) {
+ return ((v << 4) | (v >> 4)) & 0xff;
+}
+
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
+ memset(raw_id, 0, COMMON_HAL_MCU_PROCESSOR_UID_LENGTH);
+
+ uint8_t *ptr = &raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH-1];
+ // MAC address contains 48 bits (6 bytes), 32 in the low order word
+ uint32_t mac_address_part = REG_READ(EFUSE_RD_MAC_SPI_SYS_0_REG);
+ *ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
+ *ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
+ *ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
+ *ptr-- = swap_nibbles(mac_address_part & 0xff);
+
+ // and 16 in the high order word
+ mac_address_part = REG_READ(EFUSE_RD_MAC_SPI_SYS_1_REG);
+ *ptr-- = swap_nibbles(mac_address_part & 0xff); mac_address_part >>= 8;
+ *ptr-- = swap_nibbles(mac_address_part & 0xff);
}
diff --git a/ports/esp32s2/common-hal/microcontroller/Processor.h b/ports/esp32s2/common-hal/microcontroller/Processor.h
index a2ea261c8..f6636b333 100644
--- a/ports/esp32s2/common-hal/microcontroller/Processor.h
+++ b/ports/esp32s2/common-hal/microcontroller/Processor.h
@@ -27,7 +27,7 @@
#ifndef MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
#define MICROPY_INCLUDED_LITEX_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H
-#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 15
+#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH 6
#include "py/obj.h"
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index 5686579b4..713ccbb09 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -7,7 +7,7 @@ MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz
INTERNAL_LIBM = 1
# Chip supplied serial number, in bytes
-USB_SERIAL_NUMBER_LENGTH = 30
+USB_SERIAL_NUMBER_LENGTH = 12
# Longints can be implemented as mpz, as longlong, or not
LONGINT_IMPL = MPZ