diff --git a/src/machine/machine.go b/src/machine/machine.go index cc2049eb..def03958 100644 --- a/src/machine/machine.go +++ b/src/machine/machine.go @@ -4,6 +4,8 @@ import "errors" var ( ErrTimeoutRNG = errors.New("machine: RNG Timeout") + ErrClockRNG = errors.New("machine: RNG Clock Error") + ErrSeedRNG = errors.New("machine: RNG Seed Error") ErrInvalidInputPin = errors.New("machine: invalid input pin") ErrInvalidOutputPin = errors.New("machine: invalid output pin") ErrInvalidClockPin = errors.New("machine: invalid clock pin") diff --git a/src/machine/machine_stm32_rng.go b/src/machine/machine_stm32_rng.go index 490c2e42..5eaff961 100644 --- a/src/machine/machine_stm32_rng.go +++ b/src/machine/machine_stm32_rng.go @@ -15,6 +15,13 @@ func GetRNG() (uint32, error) { rngInitDone = true } + if stm32.RNG.SR.HasBits(stm32.RNG_SR_CECS) { + return 0, ErrClockRNG + } + if stm32.RNG.SR.HasBits(stm32.RNG_SR_SECS) { + return 0, ErrSeedRNG + } + cnt := RNG_MAX_READ_RETRIES for !stm32.RNG.SR.HasBits(stm32.RNG_SR_DRDY) { cnt-- diff --git a/src/machine/machine_stm32wlx.go b/src/machine/machine_stm32wlx.go index ff1745d4..32b3b582 100644 --- a/src/machine/machine_stm32wlx.go +++ b/src/machine/machine_stm32wlx.go @@ -407,7 +407,12 @@ func (t *TIM) enableMainOutput() { func initRNG() { stm32.RCC.AHB3ENR.SetBits(stm32.RCC_AHB3ENR_RNGEN) - stm32.RNG.CR.SetBits(stm32.RNG_CR_RNGEN) + + // Enable RNG with config.A (See RM0453 22.6.2) + stm32.RNG.CR.Set(0x40F00D40) // RNG Config. A + stm32.RNG.HTCR.Set(0x17590ABC) // MAGIC NUMBER + stm32.RNG.HTCR.Set(0x0000AA74) // HTCR VALUE + stm32.RNG.CR.Set(0x00F00D4C) // CONFIG A + RNG_EN=1 + IE=1 } //----------