Contiki-NG
random-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Toshiba BRIL
3  * Copyright (C) 2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 /**
33  * \addtogroup nrf
34  * @{
35  *
36  * \addtogroup nrf-os OS drivers
37  * @{
38  *
39  * \addtogroup nrf-random Random driver
40  * @{
41  *
42  * \file
43  * Random driver for the nRF.
44  * \author
45  * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
46  *
47  */
48 /*---------------------------------------------------------------------------*/
49 #include <stdlib.h>
50 
51 #include "nrfx.h"
52 
53 /*---------------------------------------------------------------------------*/
54 /**
55  * @brief Generates a new random number
56  * @return The random number.
57  */
58 unsigned short
60 {
61  return (unsigned short)rand();
62 }
63 /*---------------------------------------------------------------------------*/
64 /**
65  * @brief Initialize the random number generator
66  * @param seed Ignored if NRF_RNG is present.
67  */
68 void
69 random_init(unsigned short seed)
70 {
71 /* If board supports RNG uses it to seed srand */
72 #ifdef NRF_RNG
73  (void)seed;
74 
75  unsigned short hwrng = 0;
76  NRF_RNG->TASKS_START = 1;
77 
78  NRF_RNG->EVENTS_VALRDY = 0;
79  while(!NRF_RNG->EVENTS_VALRDY);
80  hwrng = (NRF_RNG->VALUE & 0xFF);
81 
82  NRF_RNG->EVENTS_VALRDY = 0;
83  while(!NRF_RNG->EVENTS_VALRDY);
84  hwrng |= ((NRF_RNG->VALUE & 0xFF) << 8);
85 
86  NRF_RNG->TASKS_STOP = 1;
87 
88  srand(hwrng);
89 #else /* NRF_RNG */
90  srand(seed);
91 #endif /* NRF_RNG */
92 }
93 /*---------------------------------------------------------------------------*/
94 /**
95  * @}
96  * @}
97  * @}
98  */
unsigned short random_rand(void)
Generates a new random number.
Definition: random-arch.c:59
void random_init(unsigned short seed)
Initialize the random number generator.
Definition: random-arch.c:69