Esempio n. 1
0
static int entropy_nrf5_get_entropy_isr(struct device *dev, u8_t *buf, u16_t len,
					u32_t flags)
{
	u16_t cnt = len;

	/* Check if this API is called on correct driver instance. */
	__ASSERT_NO_MSG(&entropy_nrf5_data == DEV_DATA(dev));

	if (likely((flags & ENTROPY_BUSYWAIT) == 0)) {
		return rng_pool_get((struct rng_pool *)(entropy_nrf5_data.isr),
				    buf, len);
	}

	if (len) {
		unsigned int key;
		int irq_enabled;

		key = irq_lock();
		irq_enabled = irq_is_enabled(RNG_IRQn);
		irq_disable(RNG_IRQn);
		irq_unlock(key);

		nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
		nrf_rng_task_trigger(NRF_RNG_TASK_START);

		do {
			int byte;

			while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) {
				__WFE();
				__SEV();
				__WFE();
			}

			byte = random_byte_get();
			NVIC_ClearPendingIRQ(RNG_IRQn);

			if (byte < 0) {
				continue;
			}

			buf[--len] = byte;
		} while (len);

		if (irq_enabled) {
			irq_enable(RNG_IRQn);
		}
	}

	return cnt;
}
void RNG_IRQHandler(void)
{
    if (nrf_rng_event_get(NRF_RNG_EVENT_VALRDY) &&
        nrf_rng_int_get(NRF_RNG_INT_VALRDY_MASK))
    {
        nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
        uint32_t nrf_error = app_fifo_put(&m_rng_cb.rand_pool, nrf_rng_random_value_get());

        if ((FIFO_LENGTH(m_rng_cb.rand_pool) > m_rng_cb.rand_pool.buf_size_mask) || (nrf_error == NRF_ERROR_NO_MEM))
        {
            rng_stop();
        }
    }
}
Esempio n. 3
0
void RNG_IRQHandler(void)
{
    if (nrf_rng_event_get(NRF_RNG_EVENT_VALRDY) && nrf_rng_int_get(NRF_RNG_INT_VALRDY_MASK))
    {
        nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);

        bufferPut(nrf_rng_random_value_get());

        if (bufferIsFull())
        {
            generatorStop();
        }
    }
}
Esempio n. 4
0
static inline u8_t entropy_nrf5_get_u8(void)
{
	while (!nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) {
		__WFE();
		__SEV();
		__WFE();
	}
	nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);

	/* Clear the Pending status of the RNG interrupt so that it could
	 * wake up the core when the VALRDY event occurs again. */
	NVIC_ClearPendingIRQ(RNG_IRQn);

	return nrf_rng_random_value_get();
}
Esempio n. 5
0
static int random_byte_get(void)
{
	int retval = -EAGAIN;
	unsigned int key;

	key = irq_lock();

	if (nrf_rng_event_get(NRF_RNG_EVENT_VALRDY)) {
		retval = nrf_rng_random_value_get();
		nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY);
	}

	irq_unlock(key);

	return retval;
}