diff --git a/src/machine/machine_stm32.go b/src/machine/machine_stm32.go index 4f04cae4..1edaa2cd 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 e7593829..66a74d04 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 3b8923cb..42193a73 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 a08d083a..11eff110 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 f3d213c4..844cfccb 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 f60a77e7..85632091 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 d85157db..faa583c9 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 010d038e..d42ef2e3 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