mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-22 13:29:01 +00:00
machine/samd51: implement TRNG for randomness
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//go:build stm32wlx
|
//go:build stm32wlx || (sam && atsamd51) || (sam && atsame5x)
|
||||||
// +build stm32wlx
|
// +build stm32wlx sam,atsamd51 sam,atsame5x
|
||||||
|
|
||||||
package rand
|
package rand
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build (sam && atsamd51) || (sam && atsame5x)
|
||||||
// +build sam,atsamd51 sam,atsame5x
|
// +build sam,atsamd51 sam,atsame5x
|
||||||
|
|
||||||
// Peripheral abstraction layer for the atsamd51.
|
// Peripheral abstraction layer for the atsamd51.
|
||||||
@@ -2733,3 +2734,20 @@ func syncDAC() {
|
|||||||
for sam.DAC.SYNCBUSY.HasBits(sam.DAC_SYNCBUSY_DATA0) {
|
for sam.DAC.SYNCBUSY.HasBits(sam.DAC_SYNCBUSY_DATA0) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rngInitDone = false
|
||||||
|
|
||||||
|
// GetRNG returns 32 bits of cryptographically secure random data
|
||||||
|
func GetRNG() (uint32, error) {
|
||||||
|
if !rngInitDone {
|
||||||
|
// Turn on clock for TRNG
|
||||||
|
sam.MCLK.APBCMASK.SetBits(sam.MCLK_APBCMASK_TRNG_)
|
||||||
|
|
||||||
|
// enable
|
||||||
|
sam.TRNG.CTRLA.Set(sam.TRNG_CTRLA_ENABLE)
|
||||||
|
|
||||||
|
rngInitDone = true
|
||||||
|
}
|
||||||
|
ret := sam.TRNG.DATA.Get()
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user