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_ccm.h"
9 #include "bs_tracing.h"
10 #include "NHW_54_AAR_CCM_ECB.h"
11 
ccm_inst_from_ptr(NRF_CCM_Type * p_reg)12 static int ccm_inst_from_ptr(NRF_CCM_Type * p_reg)
13 {
14   union NRF_AARCCM_regs *p = (union NRF_AARCCM_regs *)p_reg;
15 
16   int i = ( (int)p - (int)NRF_AARCCM_regs ) / sizeof(union NRF_AARCCM_Type);
17   return i;
18 }
19 
nrf_ccm_task_trigger(NRF_CCM_Type * p_reg,nrf_ccm_task_t task)20 void nrf_ccm_task_trigger(NRF_CCM_Type * p_reg, nrf_ccm_task_t task)
21 {
22   int inst = ccm_inst_from_ptr(p_reg);
23   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
24 
25   if (task == NRF_CCM_TASK_START) {
26     nhw_CCM_regw_sideeffects_TASKS_START(inst);
27   } else if (task == NRF_CCM_TASK_STOP) {
28     nhw_CCM_regw_sideeffects_TASKS_STOP(inst);
29   } else if (task == NRF_CCM_TASK_RATEOVERRIDE) {
30     nhw_CCM_regw_sideeffects_TASKS_RATEOVERRIDE(inst);
31   } else {
32     bs_trace_error_line_time("Not supported task started in nrf_ccm\n");
33   }
34 }
35 
nrf_ccm_int_enable(NRF_CCM_Type * p_reg,uint32_t mask)36 void nrf_ccm_int_enable(NRF_CCM_Type * p_reg, uint32_t mask)
37 {
38   int inst = ccm_inst_from_ptr(p_reg);
39   p_reg->INTENSET = mask;
40   nhw_CCM_regw_sideeffects_INTENSET(inst);
41 }
42 
nrf_ccm_int_disable(NRF_CCM_Type * p_reg,uint32_t mask)43 void nrf_ccm_int_disable(NRF_CCM_Type * p_reg, uint32_t mask)
44 {
45   int inst = ccm_inst_from_ptr(p_reg);
46   p_reg->INTENCLR = mask;
47   nhw_CCM_regw_sideeffects_INTENCLR(inst);
48 }
49 
nrf_ccm_enable(NRF_CCM_Type * p_reg)50 void nrf_ccm_enable(NRF_CCM_Type * p_reg)
51 {
52   int inst = ccm_inst_from_ptr(p_reg);
53   p_reg->ENABLE = CCM_ENABLE_ENABLE_Enabled << CCM_ENABLE_ENABLE_Pos;
54   nhw_AARCCM_regw_sideeffects_ENABLE(inst);
55 }
56 
nrf_ccm_disable(NRF_CCM_Type * p_reg)57 void nrf_ccm_disable(NRF_CCM_Type * p_reg)
58 {
59   int inst = ccm_inst_from_ptr(p_reg);
60   p_reg->ENABLE = (CCM_ENABLE_ENABLE_Disabled << CCM_ENABLE_ENABLE_Pos);
61   nhw_AARCCM_regw_sideeffects_ENABLE(inst);
62 }
63 
nrf_ccm_event_clear(NRF_CCM_Type * p_reg,nrf_ccm_event_t event)64 void nrf_ccm_event_clear(NRF_CCM_Type * p_reg, nrf_ccm_event_t event)
65 {
66   int inst = ccm_inst_from_ptr(p_reg);
67   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
68   nhw_CCM_regw_sideeffects_EVENTS_all(inst);
69 }
70 
nrf_ccm_subscribe_common(NRF_CCM_Type * p_reg,nrf_ccm_task_t task)71 static void nrf_ccm_subscribe_common(NRF_CCM_Type * p_reg,
72                                      nrf_ccm_task_t task)
73 {
74   int inst = ccm_inst_from_ptr(p_reg);
75 
76   if (task == NRF_CCM_TASK_START) {
77       nhw_CCM_regw_sideeffects_SUBSCRIBE_START(inst);
78   } else if (task == NRF_CCM_TASK_STOP) {
79       nhw_CCM_regw_sideeffects_SUBSCRIBE_STOP(inst);
80   } else if (task == NRF_CCM_TASK_RATEOVERRIDE) {
81       nhw_CCM_regw_sideeffects_SUBSCRIBE_RATEOVERRIDE(inst);
82   } else {
83       bs_trace_error_line_time("Attempted to subscribe to an not-supported task in the nrf_ccm (%i)\n",
84                                task);
85   }
86 }
87 
nrf_ccm_subscribe_set(NRF_CCM_Type * p_reg,nrf_ccm_task_t task,uint8_t channel)88 void nrf_ccm_subscribe_set(NRF_CCM_Type * p_reg,
89                            nrf_ccm_task_t task,
90                            uint8_t        channel)
91 {
92     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
93             ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
94     nrf_ccm_subscribe_common(p_reg, task);
95 }
96 
nrf_ccm_subscribe_clear(NRF_CCM_Type * p_reg,nrf_ccm_task_t task)97 void nrf_ccm_subscribe_clear(NRF_CCM_Type * p_reg,
98                              nrf_ccm_task_t task)
99 {
100     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
101     nrf_ccm_subscribe_common(p_reg, task);
102 }
103