Blame view

RIOT/cpu/nrf5x_common/periph/hwrng.c 1.39 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  /*
   * 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_nrf5x_common
   * @ingroup     drivers_periph_hwrng
   * @{
   *
   * @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>
   *
   * @}
   */
  
  #include "cpu.h"
  #include "periph/hwrng.h"
  
  void hwrng_init(void)
  {
      /* nothing to do here */
  }
  
  void hwrng_read(void *buf, unsigned int num)
  {
      unsigned int count = 0;
      uint8_t *b = (uint8_t *)buf;
  
      /* 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) {
          /* sleep until number is generated */
          while (NRF_RNG->EVENTS_VALRDY == 0) {
              cortexm_sleep_until_event();
          }
  
          b[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
  }