Skip to content

Commit

Permalink
machine/stm32: implement DeviceID() with unique ID per processor
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <ron@hybridgroup.com>
  • Loading branch information
deadprogram committed Oct 27, 2023
1 parent 9fb5a5b commit 938ce22
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/machine/machine_stm32.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

package machine

import "device/stm32"
import (
"device/stm32"

"runtime/volatile"
"unsafe"
)

const deviceName = stm32.Device

Expand Down Expand Up @@ -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[:]
}
2 changes: 2 additions & 0 deletions src/machine/machine_stm32f103.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine_stm32f4.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"unsafe"
)

var deviceIDAddr = []uintptr{0x1FFF7A10, 0x1FFF7A14, 0x1FFF7A18}

const (
PA0 = portA + 0
PA1 = portA + 1
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine_stm32f7.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"unsafe"
)

var deviceIDAddr = []uintptr{0x1FF0F420, 0x1FF0F424, 0x1FF0F428}

// Alternative peripheral pin functions
const (
AF0_SYSTEM = 0
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine_stm32l0.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine_stm32l4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine_stm32l5.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"unsafe"
)

var deviceIDAddr = []uintptr{0x0BFA0590, 0x0BFA0594, 0x0BFA0598}

const (
AF0_SYSTEM = 0
AF1_TIM1_2_5_8_LPTIM1 = 1
Expand Down
2 changes: 2 additions & 0 deletions src/machine/machine_stm32wlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"unsafe"
)

var deviceIDAddr = []uintptr{0x1FFF7590, 0x1FFF7594, 0x1FFF7598}

const (
AF0_SYSTEM = 0
AF1_TIM1_2_LPTIM1 = 1
Expand Down

0 comments on commit 938ce22

Please sign in to comment.