Skip to content

Commit

Permalink
Merge pull request #4840 from haukepetersen/opt_nrf_hwrng
Browse files Browse the repository at this point in the history
cpu/nrf5x: unified hwrng driver
  • Loading branch information
PeterKietzmann committed Mar 3, 2016
2 parents 65f9bcc + eb79646 commit 367d8af
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 70 deletions.
3 changes: 3 additions & 0 deletions cpu/cortexm_common/cortexm_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ void cortexm_init(void)
for (IRQn_Type i = 0; i < (int) CPU_IRQ_NUMOF; i++) {
NVIC_SetPriority(i, CPU_DEFAULT_IRQ_PRIO);
}

/* enable wake up on events for __WFE CPU sleep */
SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
}
12 changes: 12 additions & 0 deletions cpu/cortexm_common/include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ static inline void cpu_print_last_instruction(void)
printf("%p\n", (void*) lr_ptr);
}

/**
* @brief Put the CPU into the 'wait for event' sleep mode
*
* This function is meant to be used for short periods of time, where it is not
* feasible to switch to the idle thread and back.
*/
static inline void cpu_sleep_until_event(void)
{
__SEV();
__WFE();
}

#ifdef __cplusplus
}
#endif
Expand Down
65 changes: 0 additions & 65 deletions cpu/nrf51/periph/hwrng.c

This file was deleted.

24 changes: 19 additions & 5 deletions cpu/nrf52/periph/hwrng.c → cpu/nrf5x_common/periph/hwrng.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
/*
* Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
* 2016 Freie Universität Berlin
* Copyright (C) 2014-2016 Freie Universität Berlin
* 2015 Jan Wagner <mail@jwagner.eu>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup cpu_nrf52
* @ingroup cpu_nrf5x_common
* @{
*
* @file
* @brief Implementation of the hardware random number generator interface
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Frank Holtz <frank-riot2015@holtznet.de>
* @author Jan Wagner <mail@jwagner.eu>
*
* @}
Expand All @@ -32,14 +33,27 @@ void hwrng_read(uint8_t *buf, unsigned int num)
{
unsigned int count = 0;

/* power on RNG */
#ifdef CPU_FAM_NRF51
NRF_RNG->POWER = 1;
#endif
NRF_RNG->TASKS_START = 1;

/* read the actual random data */
while (count < num) {
while (NRF_RNG->EVENTS_VALRDY == 0);
/* sleep until number is generated */
while (NRF_RNG->EVENTS_VALRDY == 0) {
cpu_sleep_until_event();
}

NRF_RNG->EVENTS_VALRDY = 0;
buf[count++] = (uint8_t)NRF_RNG->VALUE;
/* NRF51 PAN #21 -> read value before clearing VALRDY */
NRF_RNG->EVENTS_VALRDY = 0;
}

/* power off RNG */
NRF_RNG->TASKS_STOP = 1;
#ifdef CPU_FAM_NRF51
NRF_RNG->POWER = 0;
#endif
}

0 comments on commit 367d8af

Please sign in to comment.