1 /*
2  * Copyright (c) 2017 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Note that the function prototypes are taken from the NRFx HAL
7  */
8 #include "hal/nrf_rng.h"
9 #include "bs_tracing.h"
10 #include "NHW_RNG.h"
11 
12 extern NRF_RNG_Type NRF_RNG_regs;
13 
nrf_rng_task_trigger(NRF_RNG_Type * p_reg,nrf_rng_task_t rng_task)14 void nrf_rng_task_trigger(NRF_RNG_Type * p_reg, nrf_rng_task_t rng_task)
15 {
16   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)rng_task)) = 0x1UL;
17 
18   if ( rng_task == NRF_RNG_TASK_START ) {
19     nhw_RNG_regw_sideeffects_TASKS_START();
20   } else if ( rng_task == NRF_RNG_TASK_STOP ) {
21     nhw_RNG_regw_sideeffects_TASKS_STOP();
22   } else {
23     bs_trace_error_line_time("Not supported task started in nrf_rng\n");
24   }
25 }
26 
nrf_rng_int_enable(NRF_RNG_Type * p_reg,uint32_t mask)27 void nrf_rng_int_enable(NRF_RNG_Type * p_reg, uint32_t mask)
28 {
29   NRF_RNG_regs.INTENSET = mask;
30   nhw_RNG_regw_sideeffects_INTENSET();
31 }
32 
nrf_rng_int_disable(NRF_RNG_Type * p_reg,uint32_t mask)33 void nrf_rng_int_disable(NRF_RNG_Type * p_reg, uint32_t mask)
34 {
35   NRF_RNG_regs.INTENCLR = mask;
36   nhw_RNG_regw_sideeffects_INTENCLR();
37 }
38 
nrf_rng_event_clear(NRF_RNG_Type * p_reg,nrf_rng_event_t rng_event)39 void nrf_rng_event_clear(NRF_RNG_Type * p_reg, nrf_rng_event_t rng_event)
40 {
41   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)rng_event)) = 0x0UL;
42   nhw_RNG_regw_sideeffects_EVENTS_all(0);
43 }
44 
45 #if defined(DPPI_PRESENT)
46 
nrf_rng_subscribe_common(NRF_RNG_Type * p_reg,nrf_rng_task_t task)47 static void nrf_rng_subscribe_common(NRF_RNG_Type * p_reg,
48                                      nrf_rng_task_t task)
49 {
50   if (task == NRF_RNG_TASK_START) {
51       nhw_RNG_regw_sideeffects_SUBSCRIBE_START(0);
52   } else if ( task == NRF_RNG_TASK_STOP ) {
53       nhw_RNG_regw_sideeffects_SUBSCRIBE_STOP(0);
54   } else {
55       bs_trace_error_line_time("Attempted to subscribe to an not-supported task in the nrf_rng (%i)\n",
56                                task);
57   }
58 }
59 
nrf_rng_subscribe_set(NRF_RNG_Type * p_reg,nrf_rng_task_t task,uint8_t channel)60 void nrf_rng_subscribe_set(NRF_RNG_Type * p_reg,
61                            nrf_rng_task_t task,
62                            uint8_t        channel)
63 {
64     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
65             ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
66     nrf_rng_subscribe_common(p_reg, task);
67 }
68 
nrf_rng_subscribe_clear(NRF_RNG_Type * p_reg,nrf_rng_task_t task)69 void nrf_rng_subscribe_clear(NRF_RNG_Type * p_reg,
70                                                nrf_rng_task_t task)
71 {
72     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
73     nrf_rng_subscribe_common(p_reg, task);
74 }
75 
76 #endif /* defined(DPPI_PRESENT) */
77