summaryrefslogtreecommitdiffstats
path: root/watch-library/watch/watch_private.c
diff options
context:
space:
mode:
authorJoey Castillo <jose.castillo@gmail.com>2021-12-06 22:47:38 -0500
committerJoey Castillo <jose.castillo@gmail.com>2021-12-06 22:47:38 -0500
commitef428bd4e1dc165e1a981a6b4c1c55e1e6d1a547 (patch)
tree0854d8cb5a61db9e859d01f70e38a643759b1dc6 /watch-library/watch/watch_private.c
parente3f88d7714326a1fd3e5051a94bc89c92e03793f (diff)
parentc5400e437f919843e0235ed66f7840fd3ddf6ea0 (diff)
downloadSensor-Watch-ef428bd4e1dc165e1a981a6b4c1c55e1e6d1a547.tar.gz
Sensor-Watch-ef428bd4e1dc165e1a981a6b4c1c55e1e6d1a547.tar.bz2
Sensor-Watch-ef428bd4e1dc165e1a981a6b4c1c55e1e6d1a547.zip
Merge branch 'main' of github.com:joeycastillo/Sensor-Watch
Diffstat (limited to 'watch-library/watch/watch_private.c')
-rw-r--r--watch-library/watch/watch_private.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/watch-library/watch/watch_private.c b/watch-library/watch/watch_private.c
index 270208dc..e652b271 100644
--- a/watch-library/watch/watch_private.c
+++ b/watch-library/watch/watch_private.c
@@ -65,6 +65,38 @@ void _watch_init() {
a4_callback = NULL;
}
+static inline void _watch_wait_for_entropy() {
+ while (!hri_trng_get_INTFLAG_reg(TRNG, TRNG_INTFLAG_DATARDY));
+}
+
+// this function is called by arc4random to get entropy for random number generation.
+// let's use the SAM L22's true random number generator to seed the PRNG!
+int getentropy(void *buf, size_t buflen) {
+ hri_mclk_set_APBCMASK_TRNG_bit(MCLK);
+ hri_trng_set_CTRLA_ENABLE_bit(TRNG);
+
+ size_t i = 0;
+ while(i < buflen / 4) {
+ _watch_wait_for_entropy();
+ ((uint32_t *)buf)[i++] = hri_trng_read_DATA_reg(TRNG);
+ }
+
+ // but what if they asked for an awkward number of bytes?
+ if (buflen % 4) {
+ // all good: let's fill in one, two or three bytes at the end of the buffer.
+ _watch_wait_for_entropy();
+ uint32_t last_little_bit = hri_trng_read_DATA_reg(TRNG);
+ for(size_t j = 0; j <= (buflen % 4); j++) {
+ ((uint8_t *)buf)[i * 4 + j] = (last_little_bit >> (j * 8)) & 0xFF;
+ }
+ }
+
+ hri_trng_clear_CTRLA_ENABLE_bit(TRNG);
+ hri_mclk_clear_APBCMASK_TRNG_bit(MCLK);
+
+ return 0;
+}
+
void _watch_enable_tcc() {
// clock TCC0 with the main clock (8 MHz) and enable the peripheral clock.
hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, GCLK_PCHCTRL_GEN_GCLK0_Val | GCLK_PCHCTRL_CHEN);