Skip to content

Commit 938ce22

Browse files
committed
machine/stm32: implement DeviceID() with unique ID per processor
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 9fb5a5b commit 938ce22

File tree

8 files changed

+37
-1
lines changed

8 files changed

+37
-1
lines changed

src/machine/machine_stm32.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
package machine
44

5-
import "device/stm32"
5+
import (
6+
"device/stm32"
7+
8+
"runtime/volatile"
9+
"unsafe"
10+
)
611

712
const deviceName = stm32.Device
813

@@ -80,3 +85,20 @@ func (p Pin) PortMaskClear() (*uint32, uint32) {
8085
pin := uint8(p) % 16
8186
return &port.BSRR.Reg, 1 << (pin + 16)
8287
}
88+
89+
var deviceID [12]byte
90+
91+
// DeviceID returns an identifier that is unique within
92+
// a particular chipset.
93+
//
94+
// The identity is one burnt into the MCU itself.
95+
//
96+
// The length of the device ID for STM32 is 12 bytes (96 bits).
97+
func DeviceID() []byte {
98+
for i := 0; i < len(deviceID); i++ {
99+
word := (*volatile.Register32)(unsafe.Pointer(deviceIDAddr[i/4])).Get()
100+
deviceID[i] = byte(word >> ((i % 4) * 8))
101+
}
102+
103+
return deviceID[:]
104+
}

src/machine/machine_stm32f103.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func CPUFrequency() uint32 {
1515
return 72000000
1616
}
1717

18+
var deviceIDAddr = []uintptr{0x1FFFF7E8, 0x1FFFF7EC, 0x1FFFF7F0}
19+
1820
// Internal use: configured speed of the APB1 and APB2 timers, this should be kept
1921
// in sync with any changes to runtime package which configures the oscillators
2022
// and clock frequencies

src/machine/machine_stm32f4.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"unsafe"
1515
)
1616

17+
var deviceIDAddr = []uintptr{0x1FFF7A10, 0x1FFF7A14, 0x1FFF7A18}
18+
1719
const (
1820
PA0 = portA + 0
1921
PA1 = portA + 1

src/machine/machine_stm32f7.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"unsafe"
1212
)
1313

14+
var deviceIDAddr = []uintptr{0x1FF0F420, 0x1FF0F424, 0x1FF0F428}
15+
1416
// Alternative peripheral pin functions
1517
const (
1618
AF0_SYSTEM = 0

src/machine/machine_stm32l0.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ func CPUFrequency() uint32 {
1313
return 32000000
1414
}
1515

16+
var deviceIDAddr = []uintptr{0x1FF80050, 0x1FF80054, 0x1FF80058}
17+
1618
// Internal use: configured speed of the APB1 and APB2 timers, this should be kept
1719
// in sync with any changes to runtime package which configures the oscillators
1820
// and clock frequencies

src/machine/machine_stm32l4.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
// Peripheral abstraction layer for the stm32l4
1515

16+
var deviceIDAddr = []uintptr{0x1FFF7590, 0x1FFF7594, 0x1FFF7598}
17+
1618
const (
1719
AF0_SYSTEM = 0
1820
AF1_TIM1_2_LPTIM1 = 1

src/machine/machine_stm32l5.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"unsafe"
1212
)
1313

14+
var deviceIDAddr = []uintptr{0x0BFA0590, 0x0BFA0594, 0x0BFA0598}
15+
1416
const (
1517
AF0_SYSTEM = 0
1618
AF1_TIM1_2_5_8_LPTIM1 = 1

src/machine/machine_stm32wlx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"unsafe"
1515
)
1616

17+
var deviceIDAddr = []uintptr{0x1FFF7590, 0x1FFF7594, 0x1FFF7598}
18+
1719
const (
1820
AF0_SYSTEM = 0
1921
AF1_TIM1_2_LPTIM1 = 1

0 commit comments

Comments
 (0)