1 /* 2 * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA 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 are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 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 * 15 * 3. Neither the name of the copyright holder nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <nrfx.h> 33 34 #if NRFX_CHECK(NRFX_RNG_ENABLED) 35 36 #include <nrfx_rng.h> 37 38 #define NRFX_LOG_MODULE RNG 39 #include <nrfx_log.h> 40 41 /** 42 * @brief Internal state of RNG driver. 43 */ 44 static nrfx_drv_state_t m_rng_state; 45 46 /** 47 * @brief Pointer to handler calling from interrupt routine. 48 */ 49 static nrfx_rng_evt_handler_t m_rng_hndl; 50 51 nrfx_err_t nrfx_rng_init(nrfx_rng_config_t const * p_config, nrfx_rng_evt_handler_t handler) 52 { 53 NRFX_ASSERT(p_config); 54 NRFX_ASSERT(handler); 55 if (m_rng_state != NRFX_DRV_STATE_UNINITIALIZED) 56 { 57 return NRFX_ERROR_ALREADY_INITIALIZED; 58 } 59 60 m_rng_hndl = handler; 61 62 if (p_config->error_correction) 63 { 64 nrf_rng_error_correction_enable(); 65 } 66 nrf_rng_shorts_disable(NRF_RNG_SHORT_VALRDY_STOP_MASK); 67 NRFX_IRQ_PRIORITY_SET(RNG_IRQn, p_config->interrupt_priority); 68 NRFX_IRQ_ENABLE(RNG_IRQn); 69 70 m_rng_state = NRFX_DRV_STATE_INITIALIZED; 71 72 return NRFX_SUCCESS; 73 } 74 75 void nrfx_rng_start(void) 76 { 77 NRFX_ASSERT(m_rng_state == NRFX_DRV_STATE_INITIALIZED); 78 nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); 79 nrf_rng_int_enable(NRF_RNG_INT_VALRDY_MASK); 80 nrf_rng_task_trigger(NRF_RNG_TASK_START); 81 } 82 83 void nrfx_rng_stop(void) 84 { 85 NRFX_ASSERT(m_rng_state == NRFX_DRV_STATE_INITIALIZED); 86 nrf_rng_int_disable(NRF_RNG_INT_VALRDY_MASK); 87 nrf_rng_task_trigger(NRF_RNG_TASK_STOP); 88 } 89 90 void nrfx_rng_uninit(void) 91 { 92 NRFX_ASSERT(m_rng_state == NRFX_DRV_STATE_INITIALIZED); 93 94 nrf_rng_int_disable(NRF_RNG_INT_VALRDY_MASK); 95 nrf_rng_task_trigger(NRF_RNG_TASK_STOP); 96 NRFX_IRQ_DISABLE(RNG_IRQn); 97 98 m_rng_state = NRFX_DRV_STATE_UNINITIALIZED; 99 NRFX_LOG_INFO("Uninitialized."); 100 } 101 102 void nrfx_rng_irq_handler(void) 103 { 104 nrf_rng_event_clear(NRF_RNG_EVENT_VALRDY); 105 106 uint8_t rng_value = nrf_rng_random_value_get(); 107 108 m_rng_hndl(rng_value); 109 110 NRFX_LOG_DEBUG("Event: NRF_RNG_EVENT_VALRDY."); 111 } 112 113 #endif // NRFX_CHECK(NRFX_RNG_ENABLED) 114