From 2bbd78a99df8fa486a4370e71d9b490c5a6f8cea Mon Sep 17 00:00:00 2001 From: joeycastillo Date: Wed, 18 Sep 2024 17:09:20 -0400 Subject: [PATCH] remove ASF calls for TRNG --- watch-library/hardware/watch/watch_private.c | 29 ++++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/watch-library/hardware/watch/watch_private.c b/watch-library/hardware/watch/watch_private.c index d13c2169..d42eec4f 100644 --- a/watch-library/hardware/watch/watch_private.c +++ b/watch-library/hardware/watch/watch_private.c @@ -91,7 +91,15 @@ void _watch_init(void) { } static inline void _watch_wait_for_entropy() { - while (!hri_trng_get_INTFLAG_reg(TRNG, TRNG_INTFLAG_DATARDY)); + while (!TRNG->INTFLAG.bit.DATARDY); +} + +void watch_disable_TRNG(void) { + // per Microchip datasheet clarification DS80000782, + // silicon erratum 1.16.1 indicates that the TRNG may leave internal components powered after being disabled. + // the workaround is to disable the TRNG by clearing the control register, twice. + TRNG->CTRLA.bit.ENABLE = 0; + TRNG->CTRLA.bit.ENABLE = 0; } // this function is called by arc4random to get entropy for random number generation. @@ -99,40 +107,31 @@ int getentropy(void *buf, size_t buflen); // 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); + MCLK->APBCMASK.bit.TRNG_ = 1; + TRNG->CTRLA.bit.ENABLE = 1; size_t i = 0; while(i < buflen / 4) { _watch_wait_for_entropy(); - ((uint32_t *)buf)[i++] = hri_trng_read_DATA_reg(TRNG); + ((uint32_t *)buf)[i++] = TRNG->DATA.reg; } // 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); + uint32_t last_little_bit = TRNG->DATA.reg; for(size_t j = 0; j <= (buflen % 4); j++) { ((uint8_t *)buf)[i * 4 + j] = (last_little_bit >> (j * 8)) & 0xFF; } } watch_disable_TRNG(); - hri_mclk_clear_APBCMASK_TRNG_bit(MCLK); + MCLK->APBCMASK.bit.TRNG_ = 0; return 0; } -void watch_disable_TRNG(void); -void watch_disable_TRNG(void) { - // per Microchip datasheet clarification DS80000782, - // silicon erratum 1.16.1 indicates that the TRNG may leave internal components powered after being disabled. - // the workaround is to disable the TRNG by clearing the control register, twice. - hri_trng_write_CTRLA_reg(TRNG, 0); - hri_trng_write_CTRLA_reg(TRNG, 0); -} - void _watch_enable_tc0(void) { // before we init TinyUSB, we are going to need a periodic callback to handle TinyUSB tasks. // TC2 and TC3 are reserved for devices on the 9-pin connector, so let's use TC0.