1 /*
2  * Copyright (c) 2022 - 2024, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <nrfx_example.h>
35 #include <nrfx_rng.h>
36 
37 #define NRFX_LOG_MODULE                 EXAMPLE
38 #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
39 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL   3
40 #include <nrfx_log.h>
41 
42 /**
43  * @defgroup nrfx_rng_example Basic RNG example
44  * @{
45  * @ingroup nrfx_rng_examples
46  *
47  * @brief Example showing basic functionality of nrfx_rng driver.
48  *
49  * @details Application initializes nrfx_rng driver. The @ref rng_handler() is executed with relevant
50  *          log message, until a number of generated numbers is equal to the value specified by the user.
51  */
52 
53 /** @brief Symbol specifying number of random values to generate. */
54 #define RNG_ITERATIONS 5UL
55 
56 /**
57  * @brief Function for handling RNG driver events.
58  *
59  * @param[in] rng_data Data passed to the event handler.
60  */
rng_handler(uint8_t rng_data)61 static void rng_handler(uint8_t rng_data)
62 {
63     static uint32_t i = 0;
64 
65     if (i < RNG_ITERATIONS)
66     {
67         NRFX_LOG_INFO("Generated value: %d", rng_data);
68         i++;
69     }
70     else
71     {
72         nrfx_rng_stop();
73     }
74 }
75 
76 /**
77  * @brief Function for application main entry.
78  *
79  * @return Nothing.
80  */
main(void)81 int main(void)
82 {
83     nrfx_err_t status;
84     (void)status;
85 
86 #if defined(__ZEPHYR__)
87     IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_RNG), IRQ_PRIO_LOWEST, nrfx_rng_irq_handler, 0, 0);
88 #endif
89 
90     NRFX_EXAMPLE_LOG_INIT();
91 
92     NRFX_LOG_INFO("Starting nrfx_rng example:");
93     NRFX_EXAMPLE_LOG_PROCESS();
94 
95     nrfx_rng_config_t config = NRFX_RNG_DEFAULT_CONFIG;
96     status = nrfx_rng_init(&config, rng_handler);
97     NRFX_ASSERT(status == NRFX_SUCCESS);
98 
99     nrfx_rng_start();
100 
101     while (1)
102     {
103         NRFX_EXAMPLE_LOG_PROCESS();
104     }
105 }
106 
107 /** @} */
108