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   (void) p_reg;
30   NRF_RNG_regs.INTENSET = mask;
31   nhw_RNG_regw_sideeffects_INTENSET();
32 }
33 
nrf_rng_int_disable(NRF_RNG_Type * p_reg,uint32_t mask)34 void nrf_rng_int_disable(NRF_RNG_Type * p_reg, uint32_t mask)
35 {
36   (void) p_reg;
37   NRF_RNG_regs.INTENCLR = mask;
38   nhw_RNG_regw_sideeffects_INTENCLR();
39 }
40 
nrf_rng_event_clear(NRF_RNG_Type * p_reg,nrf_rng_event_t rng_event)41 void nrf_rng_event_clear(NRF_RNG_Type * p_reg, nrf_rng_event_t rng_event)
42 {
43   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)rng_event)) = 0x0UL;
44   nhw_RNG_regw_sideeffects_EVENTS_all(0);
45 }
46 
47 #if defined(DPPI_PRESENT)
48 
nrf_rng_subscribe_common(NRF_RNG_Type * p_reg,nrf_rng_task_t task)49 static void nrf_rng_subscribe_common(NRF_RNG_Type * p_reg,
50                                      nrf_rng_task_t task)
51 {
52   (void) p_reg;
53   if (task == NRF_RNG_TASK_START) {
54       nhw_RNG_regw_sideeffects_SUBSCRIBE_START(0);
55   } else if ( task == NRF_RNG_TASK_STOP ) {
56       nhw_RNG_regw_sideeffects_SUBSCRIBE_STOP(0);
57   } else {
58       bs_trace_error_line_time("Attempted to subscribe to an not-supported task in the nrf_rng (%i)\n",
59                                task);
60   }
61 }
62 
nrf_rng_subscribe_set(NRF_RNG_Type * p_reg,nrf_rng_task_t task,uint8_t channel)63 void nrf_rng_subscribe_set(NRF_RNG_Type * p_reg,
64                            nrf_rng_task_t task,
65                            uint8_t        channel)
66 {
67     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
68             ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
69     nrf_rng_subscribe_common(p_reg, task);
70 }
71 
nrf_rng_subscribe_clear(NRF_RNG_Type * p_reg,nrf_rng_task_t task)72 void nrf_rng_subscribe_clear(NRF_RNG_Type * p_reg,
73                                                nrf_rng_task_t task)
74 {
75     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
76     nrf_rng_subscribe_common(p_reg, task);
77 }
78 
79 #endif /* defined(DPPI_PRESENT) */
80