From 9a6ca01c850785363ea5051ffc18697c406109f8 Mon Sep 17 00:00:00 2001 From: Stephane D'Alu Date: Tue, 9 Feb 2016 18:54:05 +0100 Subject: cleanup, added comments --- os/hal/ports/NRF51/NRF51822/rng_lld.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'os/hal/ports/NRF51') diff --git a/os/hal/ports/NRF51/NRF51822/rng_lld.c b/os/hal/ports/NRF51/NRF51822/rng_lld.c index 12a75e7..74bba64 100644 --- a/os/hal/ports/NRF51/NRF51822/rng_lld.c +++ b/os/hal/ports/NRF51/NRF51822/rng_lld.c @@ -35,7 +35,6 @@ */ static const RNGConfig default_config = { .digital_error_correction = 1, - .power_on_write = 1, }; /*===========================================================================*/ @@ -84,15 +83,16 @@ void rng_lld_start(RNGDriver *rngp) { if (rngp->config == NULL) rngp->config = &default_config; - rngp->rng->POWER = 1; + rngp->rng->POWER = 1; if (rngp->config->digital_error_correction) rngp->rng->CONFIG |= RNG_CONFIG_DERCEN_Msk; else rngp->rng->CONFIG &= ~RNG_CONFIG_DERCEN_Msk; - rngp->rng->INTENSET = RNG_INTENSET_VALRDY_Msk; - rngp->rng->TASKS_START = 1; + rngp->rng->EVENTS_VALRDY = 0; + rngp->rng->INTENSET = RNG_INTENSET_VALRDY_Msk; + rngp->rng->TASKS_START = 1; } @@ -105,7 +105,7 @@ void rng_lld_start(RNGDriver *rngp) { */ void rng_lld_stop(RNGDriver *rngp) { rngp->rng->TASKS_STOP = 1; - rngp->rng->POWER = 0; + rngp->rng->POWER = 0; } @@ -121,23 +121,27 @@ void rng_lld_stop(RNGDriver *rngp) { msg_t rng_lld_write(RNGDriver *rngp, uint8_t *buf, size_t n, systime_t timeout) { size_t i; - if (n == 0) - return MSG_OK; - - NRF_RNG->EVENTS_VALRDY = 0; for (i = 0 ; i < n ; i++) { - /* wait for next byte */ + /* Wait for byte ready + * It take about 677µs to generate a new byte, not sure if + * forcing a context switch will be a benefit + */ while (NRF_RNG->EVENTS_VALRDY == 0) { + /* Sleep and wakeup on ARM event (interrupt) */ SCB->SCR |= SCB_SCR_SEVONPEND_Msk; __SEV(); __WFE(); __WFE(); } + /* Read byte */ buf[i] = (char)NRF_RNG->VALUE; + /* Mark as read */ NRF_RNG->EVENTS_VALRDY = 0; + + /* Clear interrupt so we can wake up again */ nvicClearPending(RNG_IRQn); } return MSG_OK; -- cgit v1.2.3