Skip to content

esp32s2: Use the device's EUI-48 address as unique ID #3156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ports/esp32s2/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion ports/esp32s2/common-hal/microcontroller/Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion ports/esp32s2/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down