From 938ce22307194504431d91d162608c47e4b4ffd3 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 27 Oct 2023 16:06:03 +0200 Subject: [PATCH] machine/stm32: implement DeviceID() with unique ID per processor Signed-off-by: deadprogram --- src/machine/machine_stm32.go | 24 +++++++++++++++++++++++- src/machine/machine_stm32f103.go | 2 ++ src/machine/machine_stm32f4.go | 2 ++ src/machine/machine_stm32f7.go | 2 ++ src/machine/machine_stm32l0.go | 2 ++ src/machine/machine_stm32l4.go | 2 ++ src/machine/machine_stm32l5.go | 2 ++ src/machine/machine_stm32wlx.go | 2 ++ 8 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/machine/machine_stm32.go b/src/machine/machine_stm32.go index 4f04cae4ce..1edaa2cd06 100644 --- a/src/machine/machine_stm32.go +++ b/src/machine/machine_stm32.go @@ -2,7 +2,12 @@ package machine -import "device/stm32" +import ( + "device/stm32" + + "runtime/volatile" + "unsafe" +) const deviceName = stm32.Device @@ -80,3 +85,20 @@ func (p Pin) PortMaskClear() (*uint32, uint32) { pin := uint8(p) % 16 return &port.BSRR.Reg, 1 << (pin + 16) } + +var deviceID [12]byte + +// DeviceID returns an identifier that is unique within +// a particular chipset. +// +// The identity is one burnt into the MCU itself. +// +// The length of the device ID for STM32 is 12 bytes (96 bits). +func DeviceID() []byte { + for i := 0; i < len(deviceID); i++ { + word := (*volatile.Register32)(unsafe.Pointer(deviceIDAddr[i/4])).Get() + deviceID[i] = byte(word >> ((i % 4) * 8)) + } + + return deviceID[:] +} diff --git a/src/machine/machine_stm32f103.go b/src/machine/machine_stm32f103.go index e7593829bf..66a74d04b6 100644 --- a/src/machine/machine_stm32f103.go +++ b/src/machine/machine_stm32f103.go @@ -15,6 +15,8 @@ func CPUFrequency() uint32 { return 72000000 } +var deviceIDAddr = []uintptr{0x1FFFF7E8, 0x1FFFF7EC, 0x1FFFF7F0} + // Internal use: configured speed of the APB1 and APB2 timers, this should be kept // in sync with any changes to runtime package which configures the oscillators // and clock frequencies diff --git a/src/machine/machine_stm32f4.go b/src/machine/machine_stm32f4.go index 3b8923cb76..42193a7397 100644 --- a/src/machine/machine_stm32f4.go +++ b/src/machine/machine_stm32f4.go @@ -14,6 +14,8 @@ import ( "unsafe" ) +var deviceIDAddr = []uintptr{0x1FFF7A10, 0x1FFF7A14, 0x1FFF7A18} + const ( PA0 = portA + 0 PA1 = portA + 1 diff --git a/src/machine/machine_stm32f7.go b/src/machine/machine_stm32f7.go index a08d083a23..11eff11081 100644 --- a/src/machine/machine_stm32f7.go +++ b/src/machine/machine_stm32f7.go @@ -11,6 +11,8 @@ import ( "unsafe" ) +var deviceIDAddr = []uintptr{0x1FF0F420, 0x1FF0F424, 0x1FF0F428} + // Alternative peripheral pin functions const ( AF0_SYSTEM = 0 diff --git a/src/machine/machine_stm32l0.go b/src/machine/machine_stm32l0.go index f3d213c483..844cfccb49 100644 --- a/src/machine/machine_stm32l0.go +++ b/src/machine/machine_stm32l0.go @@ -13,6 +13,8 @@ func CPUFrequency() uint32 { return 32000000 } +var deviceIDAddr = []uintptr{0x1FF80050, 0x1FF80054, 0x1FF80058} + // Internal use: configured speed of the APB1 and APB2 timers, this should be kept // in sync with any changes to runtime package which configures the oscillators // and clock frequencies diff --git a/src/machine/machine_stm32l4.go b/src/machine/machine_stm32l4.go index f60a77e700..856320911b 100644 --- a/src/machine/machine_stm32l4.go +++ b/src/machine/machine_stm32l4.go @@ -13,6 +13,8 @@ import ( // Peripheral abstraction layer for the stm32l4 +var deviceIDAddr = []uintptr{0x1FFF7590, 0x1FFF7594, 0x1FFF7598} + const ( AF0_SYSTEM = 0 AF1_TIM1_2_LPTIM1 = 1 diff --git a/src/machine/machine_stm32l5.go b/src/machine/machine_stm32l5.go index d85157dbe9..faa583c9c3 100644 --- a/src/machine/machine_stm32l5.go +++ b/src/machine/machine_stm32l5.go @@ -11,6 +11,8 @@ import ( "unsafe" ) +var deviceIDAddr = []uintptr{0x0BFA0590, 0x0BFA0594, 0x0BFA0598} + const ( AF0_SYSTEM = 0 AF1_TIM1_2_5_8_LPTIM1 = 1 diff --git a/src/machine/machine_stm32wlx.go b/src/machine/machine_stm32wlx.go index 010d038e03..d42ef2e383 100644 --- a/src/machine/machine_stm32wlx.go +++ b/src/machine/machine_stm32wlx.go @@ -14,6 +14,8 @@ import ( "unsafe" ) +var deviceIDAddr = []uintptr{0x1FFF7590, 0x1FFF7594, 0x1FFF7598} + const ( AF0_SYSTEM = 0 AF1_TIM1_2_LPTIM1 = 1