1 /*
2  * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef NRFX_IPC_H__
32 #define NRFX_IPC_H__
33 
34 #include <nrfx.h>
35 #include <hal/nrf_ipc.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrfx_ipc IPC driver
43  * @{
44  * @ingroup nrf_ipc
45  * @brief   Interprocessor Communication (IPC) peripheral driver.
46  */
47 
48 /**
49  * @brief IPC driver handler type.
50  *
51  * @param[in] event_mask Bitmask with events that triggered the interrupt.
52  * @param[in] p_context  Context passed to the interrupt handler, set on initialization.
53  */
54 typedef void (*nrfx_ipc_handler_t)(uint32_t event_mask, void * p_context);
55 
56 /** @brief IPC configuration structure. */
57 typedef struct
58 {
59     uint32_t send_task_config[IPC_CONF_NUM];     ///< Configuration of the connection between signals and IPC channels.
60     uint32_t receive_event_config[IPC_CONF_NUM]; ///< Configuration of the connection between events and IPC channels.
61     uint32_t receive_events_enabled;             ///< Bitmask with events to be enabled to generate interrupt.
62 } nrfx_ipc_config_t;
63 
64 /**
65  * @brief Function for initializing the IPC driver.
66  *
67  * @param irq_priority Interrupt priority.
68  * @param handler      Event handler provided by the user. Cannot be NULL.
69  * @param p_context    Context passed to event handler.
70  *
71  * @retval NRFX_SUCCESS             Initialization was successful.
72  * @retval NRFX_ERROR_INVALID_STATE Driver is already initialized.
73  */
74 nrfx_err_t nrfx_ipc_init(uint8_t irq_priority, nrfx_ipc_handler_t handler, void * p_context);
75 
76 /**
77  * @brief Function for loading configuration directly into IPC peripheral.
78  *
79  * @param p_config Pointer to the structure with the initial configuration.
80  */
81 void nrfx_ipc_config_load(nrfx_ipc_config_t const * p_config);
82 
83 /**
84  * @brief Function for convey signal on configured channels.
85  *
86  * Events connected to the IPC channels configured within this signal will
87  * be set and can generate interrupts when configured.
88  *
89  * @param send_index Index of the SEND task to trigger.
90  */
91 NRFX_STATIC_INLINE void nrfx_ipc_signal(uint8_t send_index);
92 
93 /**
94  * @brief Function for storing data in GPMEM register in the IPC peripheral.
95  *
96  * @param mem_index Index of the memory cell.
97  * @param data      Data to be saved.
98  */
99 NRFX_STATIC_INLINE void nrfx_ipc_gpmem_set(uint8_t mem_index, uint32_t data);
100 
101 /**
102  * @brief Function for getting data from the GPMEM register in the IPC peripheral.
103  *
104  * @param mem_index Index of the memory cell.
105  *
106  * @return Saved data.
107  */
108 NRFX_STATIC_INLINE uint32_t nrfx_ipc_mem_get(uint8_t mem_index);
109 
110 /** @brief Function for uninitializing the IPC module. */
111 void nrfx_ipc_uninit(void);
112 
113 /**
114  * @brief Function for enabling events to generate interrupt.
115  *
116  * @param event_index Index of event to be enabled.
117  */
118 void nrfx_ipc_receive_event_enable(uint8_t event_index);
119 
120 /**
121  * @brief Function for disabling events from generate interrupt.
122  *
123  * @param event_index Index of event to be disabled.
124  */
125 void nrfx_ipc_receive_event_disable(uint8_t event_index);
126 
127 /**
128  * @brief Function for enabling set of events to generate interrupt.
129  *
130  * @param event_bitmask Bitmask with events to be enabled.
131  */
132 void nrfx_ipc_receive_event_group_enable(uint32_t event_bitmask);
133 
134 /**
135  * @brief Function for disabling set of events from generate interrupt.
136  *
137  * @param event_bitmask Bitmask with events to be disabled.
138  */
139 void nrfx_ipc_receive_event_group_disable(uint32_t event_bitmask);
140 
141 /**
142  * @brief Function for assigning event to the IPC channel.
143  *
144  * @param event_index   Index of the event to be configured.
145  * @param channel_index Index of the channel to which event will be connected.
146  */
147 void nrfx_ipc_receive_event_channel_assign(uint8_t event_index, uint8_t channel_index);
148 
149 /**
150  * @brief Function for assigning signal to the IPC channel.
151  *
152  * @param send_index    Index of the signal to be configured.
153  * @param channel_index Index of the instance of channel.
154  */
155 void nrfx_ipc_send_task_channel_assign(uint8_t send_index, uint8_t channel_index);
156 
157 /**
158  * @brief Function for assigning event to the IPC channels.
159  *
160  * @param event_index     Index of the event to be configured.
161  * @param channel_bitmask Bitmask with channels to which event will be connected.
162  */
163 NRFX_STATIC_INLINE void nrfx_ipc_receive_config_set(uint8_t event_index, uint32_t channel_bitmask);
164 
165 /**
166  * @brief Function for assigning signal to the IPC channels.
167  *
168  * @param send_index      Index of the signal to be configured.
169  * @param channel_bitmask Bitmask with channels to which signal will be connected.
170  */
171 NRFX_STATIC_INLINE void nrfx_ipc_send_config_set(uint8_t send_index, uint32_t channel_bitmask);
172 
173 /** @} */
174 
175 
176 #ifndef NRFX_DECLARE_ONLY
177 
nrfx_ipc_gpmem_set(uint8_t mem_index,uint32_t data)178 NRFX_STATIC_INLINE void nrfx_ipc_gpmem_set(uint8_t mem_index, uint32_t data)
179 {
180     NRFX_ASSERT(mem_index < NRFX_ARRAY_SIZE(NRF_IPC->GPMEM));
181     nrf_ipc_gpmem_set(NRF_IPC, mem_index, data);
182 }
183 
nrfx_ipc_mem_get(uint8_t mem_index)184 NRFX_STATIC_INLINE uint32_t nrfx_ipc_mem_get(uint8_t mem_index)
185 {
186     NRFX_ASSERT(mem_index < NRFX_ARRAY_SIZE(NRF_IPC->GPMEM));
187     return nrf_ipc_gpmem_get(NRF_IPC, mem_index);
188 }
189 
nrfx_ipc_signal(uint8_t send_index)190 NRFX_STATIC_INLINE void nrfx_ipc_signal(uint8_t send_index)
191 {
192     NRFX_ASSERT(send_index < IPC_CONF_NUM);
193     nrf_ipc_task_trigger(NRF_IPC, nrf_ipc_send_task_get(send_index));
194 }
195 
nrfx_ipc_receive_config_set(uint8_t event_index,uint32_t channel_bitmask)196 NRFX_STATIC_INLINE void nrfx_ipc_receive_config_set(uint8_t event_index, uint32_t channel_bitmask)
197 {
198     NRFX_ASSERT(event_index < IPC_CONF_NUM);
199     nrf_ipc_receive_config_set(NRF_IPC, event_index, channel_bitmask);
200 }
201 
nrfx_ipc_send_config_set(uint8_t send_index,uint32_t channel_bitmask)202 NRFX_STATIC_INLINE void nrfx_ipc_send_config_set(uint8_t send_index, uint32_t channel_bitmask)
203 {
204     NRFX_ASSERT(send_index < IPC_CONF_NUM);
205     nrf_ipc_send_config_set(NRF_IPC, send_index, channel_bitmask);
206 }
207 
208 #endif // NRFX_DECLARE_ONLY
209 
210 
211 void nrfx_ipc_irq_handler(void);
212 
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif // NRFX_IPC_H__
219