SapientHetero / TRNG-for-ATSAMD51J19A-Adafruit-Metro-M4-

Arduino example demonstrating initialization and use of the SAMD51 True Random Number Generator peripheral
MIT License
5 stars 1 forks source link

Don't need to enable interrupts. #2

Closed WestfW closed 4 years ago

WestfW commented 4 years ago

I believe that the code is more complicated than it needs to be. You don't need to actually enable the interrupts to use the TRNG - you can just poll the flag without using an ISR. See https://forums.adafruit.com/viewtopic.php?f=62&t=142811&p=705696

You'd use the interrupt if you were trying to collect a queue of random bits in the "background", I guess. (Every 84 clocks is relatively inconvenient timing - slower than you'd like it to be for a fully polled routine, but faster than you'd want to continuously interrupt.)

SapientHetero commented 4 years ago

You're right; it doesn't need to enable interrupts.

My original version worked a bit differently. Upon being initialized it grabbed a random number when the first interrupt occurred and stored it in static memory. When the "get a random number" function was called grabbed the previously-stored one, enabled the TRNG interrupt and returned the just-grabbed value to the caller. The next interrupt stored a new random number and disabled the interrupt, thus eliminating the 84 clock latency. But this implementation creates a small theoretical security risk in my particular application, so I eliminated the static storage before I posted it here.

I kept the interrupt feature in case I decided to revert in the future. I suppose I could've added preprocessor directives to switch back and forth between the two, but I had more pressing issues at the time. Maybe someday when I'm not busy...