-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nrf,sam,rp2040: add machine.HardwareID function
- Loading branch information
Showing
8 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"machine" | ||
"time" | ||
) | ||
|
||
func main() { | ||
time.Sleep(2 * time.Second) | ||
|
||
id := machine.HardwareID() | ||
|
||
for { | ||
println("Hardware ID:", hex.EncodeToString(id)) | ||
time.Sleep(1 * time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build rp2040 || nrf || sam | ||
|
||
package machine | ||
|
||
// HardwareID returns an identifier that is unique within | ||
// a particular chipset. | ||
// | ||
// The identity is one burnt into the MCU itself, or the | ||
// flash chip at time of manufacture. | ||
// | ||
// It's possible that two different vendors may allocate | ||
// the same HardwareID, so callers should take this into | ||
// account if needing to generate a globally unique id. | ||
// | ||
// The length of the hardware ID is vendor-specific, but | ||
// 8 bytes (64 bits) and 16 bytes (128 bits) are common. | ||
var _ = (func() []byte)(HardwareID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build sam | ||
|
||
package machine | ||
|
||
import ( | ||
"runtime/volatile" | ||
"unsafe" | ||
) | ||
|
||
var hardwareID [16]byte | ||
|
||
// HardwareID returns an identifier that is unique within | ||
// a particular chipset. | ||
// | ||
// The identity is one burnt into the MCU itself, or the | ||
// flash chip at time of manufacture. | ||
// | ||
// It's possible that two different vendors may allocate | ||
// the same HardwareID, so callers should take this into | ||
// account if needing to generate a globally unique id. | ||
// | ||
// The length of the hardware ID is vendor-specific, but | ||
// 8 bytes (64 bits) and 16 bytes (128 bits) are common. | ||
func HardwareID() []byte { | ||
for i := 0; i < len(hardwareID); i++ { | ||
word := (*volatile.Register32)(unsafe.Pointer(hardwareIDAddr[i/4])).Get() | ||
hardwareID[i] = byte(word >> ((i % 4) * 8)) | ||
} | ||
|
||
return hardwareID[:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters