1 /*
2  * Copyright (c) 2022 - 2025, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <nrfx.h>
35 
36 #if NRFX_CHECK(NRFX_TBM_ENABLED)
37 
38 #include <nrfx_tbm.h>
39 #include <haly/nrfy_tbm.h>
40 
41 static nrfx_drv_state_t state;
42 static nrfx_tbm_event_handler_t evt_handler;
43 static const uint32_t m_int_flags = NRF_TBM_INT_HALFFULL_MASK |
44                                     NRF_TBM_INT_FULL_MASK     |
45                                     NRF_TBM_INT_FLUSH_MASK;
46 
nrfx_tbm_init(nrfx_tbm_config_t const * p_config,nrfx_tbm_event_handler_t handler)47 nrfx_err_t nrfx_tbm_init(nrfx_tbm_config_t const * p_config, nrfx_tbm_event_handler_t handler)
48 {
49     if (state != NRFX_DRV_STATE_UNINITIALIZED)
50     {
51         return NRFX_ERROR_ALREADY;
52     }
53 
54     nrfy_tbm_configure(NRF_TBM, p_config->size);
55 
56     if (handler)
57     {
58         evt_handler = handler;
59         nrfy_tbm_int_init(NRF_TBM, m_int_flags, p_config->interrupt_priority, true);
60     }
61 
62     state = NRFX_DRV_STATE_INITIALIZED;
63 
64     return NRFX_SUCCESS;
65 }
66 
nrfx_tbm_start(void)67 void nrfx_tbm_start(void)
68 {
69     NRFX_ASSERT(state == NRFX_DRV_STATE_INITIALIZED);
70     nrfy_tbm_task_trigger(NRF_TBM, NRF_TBM_TASK_START);
71 }
72 
nrfx_tbm_stop(void)73 void nrfx_tbm_stop(void)
74 {
75     NRFX_ASSERT(state == NRFX_DRV_STATE_INITIALIZED);
76     nrfy_tbm_task_trigger(NRF_TBM, NRF_TBM_TASK_STOP);
77 }
78 
nrfx_tbm_count_get(void)79 uint32_t nrfx_tbm_count_get(void)
80 {
81     NRFX_ASSERT(state == NRFX_DRV_STATE_INITIALIZED);
82     return nrfy_tbm_count_get(NRF_TBM);
83 }
84 
nrfx_tbm_uninit(void)85 void nrfx_tbm_uninit(void)
86 {
87     NRFX_ASSERT(state == NRFX_DRV_STATE_INITIALIZED);
88     nrfx_tbm_stop();
89     nrfy_tbm_int_uninit(NRF_TBM);
90 
91     state = NRFX_DRV_STATE_UNINITIALIZED;
92 }
93 
nrfx_tbm_init_check(void)94 bool nrfx_tbm_init_check(void)
95 {
96     return (state != NRFX_DRV_STATE_UNINITIALIZED);
97 }
98 
nrfx_tbm_irq_handler(void)99 void nrfx_tbm_irq_handler(void)
100 {
101     uint32_t evts = nrfy_tbm_events_process(NRF_TBM, m_int_flags);
102 
103     if (evts & NRF_TBM_INT_HALFFULL_MASK)
104     {
105         evt_handler(NRF_TBM_EVENT_HALFFULL);
106     }
107 
108     if (evts & NRF_TBM_INT_FULL_MASK)
109     {
110         evt_handler(NRF_TBM_EVENT_FULL);
111     }
112 
113     if (evts & NRF_TBM_INT_FLUSH_MASK)
114     {
115         evt_handler(NRF_TBM_EVENT_FLUSH);
116     }
117 }
118 
119 #endif // NRFX_CHECK(NRFX_TBM_ENABLED)
120