1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
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_aar.h"
9 #include "hal/nrf_ccm.h"
10 #include "bs_tracing.h"
11 #include "NHW_54_AAR_CCM_ECB.h"
12 
aar_inst_from_ptr(NRF_AAR_Type * p_reg)13 static int aar_inst_from_ptr(NRF_AAR_Type * p_reg)
14 {
15   union NRF_AARCCM_regs *p = (union NRF_AARCCM_regs *)p_reg;
16 
17   int i = ( (int)p - (int)NRF_AARCCM_regs ) / sizeof(union NRF_AARCCM_Type);
18   return i;
19 }
20 
nrf_aar_int_enable(NRF_AAR_Type * p_reg,uint32_t mask)21 void nrf_aar_int_enable(NRF_AAR_Type * p_reg, uint32_t mask)
22 {
23   int inst = aar_inst_from_ptr(p_reg);
24   p_reg->INTENSET = mask;
25   nhw_AAR_regw_sideeffects_INTENSET(inst);
26 }
27 
nrf_aar_int_disable(NRF_AAR_Type * p_reg,uint32_t mask)28 void nrf_aar_int_disable(NRF_AAR_Type * p_reg, uint32_t mask)
29 {
30   int inst = aar_inst_from_ptr(p_reg);
31   p_reg->INTENCLR = mask;
32   nhw_AAR_regw_sideeffects_INTENCLR(inst);
33 }
34 
nrf_aar_task_trigger(NRF_AAR_Type * p_reg,nrf_aar_task_t task)35 void nrf_aar_task_trigger(NRF_AAR_Type * p_reg, nrf_aar_task_t task)
36 {
37   int inst = aar_inst_from_ptr(p_reg);
38   *(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task) = 1;
39   if (task == NRF_AAR_TASK_START) {
40     nhw_AAR_regw_sideeffects_TASKS_START(inst);
41   } else if (task == NRF_AAR_TASK_STOP) {
42     nhw_AAR_regw_sideeffects_TASKS_STOP(inst);
43   } else if ( (int)task == (int)NRF_CCM_TASK_RATEOVERRIDE ) {
44     nhw_CCM_regw_sideeffects_TASKS_RATEOVERRIDE(inst);
45   } else {
46     bs_trace_error_line_time("Not supported task started in nrf_aar\n");
47   }
48 }
49 
nrf_aar_enable(NRF_AAR_Type * p_reg)50 void nrf_aar_enable(NRF_AAR_Type * p_reg)
51 {
52   int inst = aar_inst_from_ptr(p_reg);
53   p_reg->ENABLE = AAR_ENABLE_ENABLE_Enabled << AAR_ENABLE_ENABLE_Pos;
54   nhw_AARCCM_regw_sideeffects_ENABLE(inst);
55 }
56 
nrf_aar_disable(NRF_AAR_Type * p_reg)57 void nrf_aar_disable(NRF_AAR_Type * p_reg)
58 {
59   int inst = aar_inst_from_ptr(p_reg);
60   p_reg->ENABLE = AAR_ENABLE_ENABLE_Disabled << AAR_ENABLE_ENABLE_Pos;
61   nhw_AARCCM_regw_sideeffects_ENABLE(inst);
62 }
63 
nrf_aar_event_clear(NRF_AAR_Type * p_reg,nrf_aar_event_t event)64 void nrf_aar_event_clear(NRF_AAR_Type * p_reg, nrf_aar_event_t event)
65 {
66   int inst = aar_inst_from_ptr(p_reg);
67   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
68   nhw_AAR_regw_sideeffects_EVENTS_all(inst);
69 }
70 
nrf_aar_subscribe_common(NRF_AAR_Type * p_reg,nrf_aar_task_t task)71 static void nrf_aar_subscribe_common(NRF_AAR_Type * p_reg,
72                                      nrf_aar_task_t task)
73 {
74   int inst = aar_inst_from_ptr(p_reg);
75   if (task == NRF_AAR_TASK_START) {
76     nhw_AAR_regw_sideeffects_SUBSCRIBE_START(inst);
77   } else if ( task == NRF_AAR_TASK_STOP ) {
78     nhw_AAR_regw_sideeffects_SUBSCRIBE_STOP(inst);
79   } else if ( (int)task == (int)NRF_CCM_TASK_RATEOVERRIDE ) {
80     /* AAR and CCM share the SW register IF, and therefore we may land here also.
81      * Though the START and STOP tasks are the same, RATEOVERRIDE only exists for the CCM */
82     nhw_CCM_regw_sideeffects_SUBSCRIBE_RATEOVERRIDE(inst);
83   } else {
84     bs_trace_error_line_time("Attempted to subscribe to a not-supported task in the nrf_aar (%i)\n",
85                              task);
86   }
87 }
88 
nrf_aar_subscribe_set(NRF_AAR_Type * p_reg,nrf_aar_task_t task,uint8_t channel)89 void nrf_aar_subscribe_set(NRF_AAR_Type * p_reg,
90                            nrf_aar_task_t task,
91                            uint8_t        channel)
92 {
93   *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
94           ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
95   nrf_aar_subscribe_common(p_reg, task);
96 }
97 
nrf_aar_subscribe_clear(NRF_AAR_Type * p_reg,nrf_aar_task_t task)98 void nrf_aar_subscribe_clear(NRF_AAR_Type * p_reg,
99                              nrf_aar_task_t task)
100 {
101   *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
102   nrf_aar_subscribe_common(p_reg, task);
103 }
104