Contiki-NG
random.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Nordic Semiconductor
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30/**
31 * \addtogroup nrf52832
32 * @{
33 *
34 * \addtogroup nrf52832-dev Device drivers
35 * @{
36 *
37 * \addtogroup nrf52832-rng Hardware random number generator
38 * @{
39 *
40 * \file
41 * Random number generator routines exploiting the nRF52 hardware
42 * capabilities.
43 *
44 * This file overrides os/lib/random.c.
45 *
46 * \author
47 * Wojciech Bober <wojciech.bober@nordicsemi.no>
48 */
49#include <stddef.h>
50#include <nrf_drv_rng.h>
51#include "app_error.h"
52/*---------------------------------------------------------------------------*/
53/**
54 * \brief Generates a new random number using the nRF52 RNG.
55 * \return a random number.
56 */
57unsigned short
59{
60 unsigned short value = 42;
61 uint8_t available;
62 ret_code_t err_code;
63
64 do {
65 nrf_drv_rng_bytes_available(&available);
66 } while (available < sizeof(value));
67
68 err_code = nrf_drv_rng_rand((uint8_t *)&value, sizeof(value));
69 APP_ERROR_CHECK(err_code);
70
71 return value;
72}
73/*---------------------------------------------------------------------------*/
74/**
75 * \brief Initialize the nRF52 random number generator.
76 * \param seed Ignored. It's here because the function prototype is in core.
77 *
78 */
79void
80random_init(unsigned short seed)
81{
82 (void)seed;
83 ret_code_t err_code = nrf_drv_rng_init(NULL);
84 APP_ERROR_CHECK(err_code);
85}
86/**
87 * @}
88 * @}
89 * @}
90 */
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:84
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58