Contiki-NG
Loading...
Searching...
No Matches
nrf-ipc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden AB.
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
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup nrf
33 * @{
34 *
35 * \addtogroup nrf-ipc nRF5340 Inter-Processor Communication
36 * @{
37 *
38 * \file
39 * IPC protocol definitions for nRF5340 dual-core communication
40 * \author
41 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
42 */
43/*---------------------------------------------------------------------------*/
44#ifndef NRF_IPC_H_
45#define NRF_IPC_H_
46/*---------------------------------------------------------------------------*/
47#include <stdint.h>
48#include <stdbool.h>
49/*---------------------------------------------------------------------------*/
50/**
51 * Protocol version. Both cores must agree on this value;
52 * the net core checks it at startup and refuses to proceed
53 * on mismatch.
54 */
55#define NRF_IPC_PROTOCOL_VERSION 1
56/*---------------------------------------------------------------------------*/
57/**
58 * Shared memory address. Placed at the top of the application core's
59 * RAM1 region (0x20060000--0x2007FFFF, 128 KB), which is accessible
60 * from both cores. The nRF5340 application core has 512 KB total
61 * SRAM (0x20000000--0x2007FFFF). This address must not overlap with
62 * any linker-allocated region on either core.
63 *
64 * See nRF5340 Product Specification, Section "Memory Map" for details.
65 */
66#define NRF_IPC_SHARED_MEM_ADDR 0x20070000UL
67/*---------------------------------------------------------------------------*/
68/**
69 * Maximum 802.15.4 frame size carried over IPC.
70 *
71 * The 802.15.4 PHY payload is 127 bytes; the nRF radio driver
72 * strips the 2-byte FCS, yielding up to 125 data bytes. We use
73 * 128 to provide a small margin for alignment and future use.
74 */
75#define NRF_IPC_MAX_FRAME_LEN 128
76/*---------------------------------------------------------------------------*/
77/**
78 * Maximum data size in a command or response message.
79 */
80#define NRF_IPC_MAX_DATA_LEN 140
81/*---------------------------------------------------------------------------*/
82/**
83 * Timeout in milliseconds for IPC command responses. If the net core
84 * does not respond within this time, send_command() returns -1.
85 */
86#ifndef NRF_IPC_CMD_TIMEOUT_MS
87#define NRF_IPC_CMD_TIMEOUT_MS 100
88#endif
89/*---------------------------------------------------------------------------*/
90/**
91 * Interval in seconds between net core heartbeat checks on the app core.
92 * If the heartbeat counter has not advanced within this many seconds,
93 * the app core logs a warning.
94 */
95#ifndef NRF_IPC_HEARTBEAT_INTERVAL_SEC
96#define NRF_IPC_HEARTBEAT_INTERVAL_SEC 10
97#endif
98/*---------------------------------------------------------------------------*/
99/**
100 * Size of the log ring buffer for forwarding net core output to the app core.
101 */
102#define NRF_IPC_LOG_BUF_SIZE 2048
103/*---------------------------------------------------------------------------*/
104/**
105 * IPC command types (app core -> net core).
106 *
107 * Each command is sent via the shared memory mailbox. The response
108 * carries the result code in rsp.data[0]; additional response
109 * payload (if any) starts at rsp.data[1].
110 */
112 NRF_IPC_CMD_INIT = 1, /**< Initialize the radio driver. */
113 NRF_IPC_CMD_ON, /**< Turn the radio on. */
114 NRF_IPC_CMD_OFF, /**< Turn the radio off. */
115 NRF_IPC_CMD_SEND, /**< Transmit a frame (data in cmd.data). */
116 NRF_IPC_CMD_CCA, /**< Perform Clear Channel Assessment. */
117 NRF_IPC_CMD_RECEIVING, /**< Check if a frame is being received. */
118 NRF_IPC_CMD_PENDING, /**< Check for pending received frames. */
119 NRF_IPC_CMD_GET_VALUE, /**< Get a radio parameter (radio_value_t). */
120 NRF_IPC_CMD_SET_VALUE, /**< Set a radio parameter (radio_value_t). */
121 NRF_IPC_CMD_GET_OBJECT, /**< Get a radio parameter (object/blob). */
122 NRF_IPC_CMD_SET_OBJECT, /**< Set a radio parameter (object/blob). */
123 /**
124 * Read radio diagnostic registers (nRF5340-specific).
125 * Response payload (20 bytes):
126 * [0-3] RADIO STATE register
127 * [4-7] EVENTS_CRCOK
128 * [8-11] EVENTS_CRCERROR
129 * [12-15] PACKETPTR
130 * [16-19] FREQUENCY register
131 */
133};
134/*---------------------------------------------------------------------------*/
135/**
136 * IPC message structure used for both commands and responses.
137 */
139 volatile uint8_t type;
140 volatile uint8_t len;
141 volatile uint8_t data[NRF_IPC_MAX_DATA_LEN];
142};
143/*---------------------------------------------------------------------------*/
144/**
145 * Shared memory layout between the application core and the network core.
146 *
147 * The command/response mailbox is used for synchronous operations:
148 * the app core writes a command, signals the net core, and busy-waits
149 * for the response.
150 *
151 * The RX area is used for asynchronous frame reception: the net core
152 * writes a received frame and signals the app core.
153 */
155 /** Protocol version for compatibility checking. */
156 uint32_t version;
157
158 /** Set to 1 by the net core when it has initialized the radio. */
159 volatile uint32_t net_ready;
160
161 /** Command mailbox (app -> net). */
163 volatile uint8_t cmd_pending;
164 volatile uint8_t cmd_seq; /**< Sequence number set by app core. */
165
166 /** Response mailbox (net -> app). */
168 volatile uint8_t rsp_ready;
169 volatile uint8_t rsp_seq; /**< Echoed from cmd_seq by net core. */
170
171 /** Received data frame (net -> app, asynchronous).
172 * Used for non-ACK frames delivered via the process thread. */
173 struct {
174 volatile uint8_t len;
175 volatile uint8_t data[NRF_IPC_MAX_FRAME_LEN];
176 volatile int8_t rssi;
177 volatile uint8_t lqi;
178 volatile uint8_t pending;
179 } rx;
180
181 /** Received ACK frame (net -> app, asynchronous).
182 * Separate from rx to avoid CSMA's ACK detection loop consuming
183 * data frames. Only 3-byte 802.15.4 ACK frames go here. */
184 struct {
185 volatile uint8_t data[3];
186 volatile uint8_t pending;
188
189 /** Heartbeat counter (used as IPC MAC RX frame counter). */
190 volatile uint32_t heartbeat;
191
192 /** Log ring buffer (net -> app). Single-producer, single-consumer. */
193 struct {
194 volatile uint16_t head; /**< Written by net core. */
195 volatile uint16_t tail; /**< Written by app core. */
196 volatile uint32_t overflow; /**< Characters dropped due to full buffer. */
197 volatile char data[NRF_IPC_LOG_BUF_SIZE];
199};
200/*---------------------------------------------------------------------------*/
201/*
202 * Ensure the shared memory structure fits within the available region.
203 * The region from NRF_IPC_SHARED_MEM_ADDR to end of RAM (0x20080000)
204 * is 64 KB. This catches accidental growth from buffer size changes.
205 */
206_Static_assert(sizeof(struct nrf_ipc_shared_mem) <= 0x10000,
207 "nrf_ipc_shared_mem exceeds 64 KB shared memory region");
208/*---------------------------------------------------------------------------*/
209/**
210 * Get a pointer to the shared memory structure.
211 */
212#define NRF_IPC_SHARED_MEM \
213 ((volatile struct nrf_ipc_shared_mem *)NRF_IPC_SHARED_MEM_ADDR)
214/*---------------------------------------------------------------------------*/
215/**
216 * Initialize the IPC transport layer.
217 *
218 * Configures IPC channels and enables interrupts. The callback
219 * process will be polled when an IPC signal is received.
220 *
221 * \param callback_proc Process to poll on IPC signal, or NULL.
222 */
223void nrf_ipc_init(struct process *callback_proc);
224/*---------------------------------------------------------------------------*/
225/**
226 * Send an IPC signal to the other core.
227 */
228void nrf_ipc_signal(void);
229/*---------------------------------------------------------------------------*/
230#endif /* NRF_IPC_H_ */
231/*---------------------------------------------------------------------------*/
232/**
233 * @}
234 * @}
235 */
#define NRF_IPC_LOG_BUF_SIZE
Size of the log ring buffer for forwarding net core output to the app core.
Definition nrf-ipc.h:102
#define NRF_IPC_MAX_FRAME_LEN
Maximum 802.15.4 frame size carried over IPC.
Definition nrf-ipc.h:75
#define NRF_IPC_MAX_DATA_LEN
Maximum data size in a command or response message.
Definition nrf-ipc.h:80
void nrf_ipc_init(struct process *callback_proc)
Initialize the IPC transport layer.
void nrf_ipc_signal(void)
Send an IPC signal to the other core.
nrf_ipc_cmd_type
IPC command types (app core -> net core).
Definition nrf-ipc.h:111
@ NRF_IPC_CMD_GET_VALUE
Get a radio parameter (radio_value_t).
Definition nrf-ipc.h:119
@ NRF_IPC_CMD_GET_OBJECT
Get a radio parameter (object/blob).
Definition nrf-ipc.h:121
@ NRF_IPC_CMD_RECEIVING
Check if a frame is being received.
Definition nrf-ipc.h:117
@ NRF_IPC_CMD_SET_OBJECT
Set a radio parameter (object/blob).
Definition nrf-ipc.h:122
@ NRF_IPC_CMD_ON
Turn the radio on.
Definition nrf-ipc.h:113
@ NRF_IPC_CMD_SEND
Transmit a frame (data in cmd.data).
Definition nrf-ipc.h:115
@ NRF_IPC_CMD_SET_VALUE
Set a radio parameter (radio_value_t).
Definition nrf-ipc.h:120
@ NRF_IPC_CMD_INIT
Initialize the radio driver.
Definition nrf-ipc.h:112
@ NRF_IPC_CMD_OFF
Turn the radio off.
Definition nrf-ipc.h:114
@ NRF_IPC_CMD_CCA
Perform Clear Channel Assessment.
Definition nrf-ipc.h:116
@ NRF_IPC_CMD_PENDING
Check for pending received frames.
Definition nrf-ipc.h:118
@ NRF_IPC_CMD_DIAG
Read radio diagnostic registers (nRF5340-specific).
Definition nrf-ipc.h:132
IPC message structure used for both commands and responses.
Definition nrf-ipc.h:138
Shared memory layout between the application core and the network core.
Definition nrf-ipc.h:154
volatile uint32_t heartbeat
Heartbeat counter (used as IPC MAC RX frame counter).
Definition nrf-ipc.h:190
struct nrf_ipc_shared_mem::@9 log
Log ring buffer (net -> app).
volatile uint32_t overflow
Characters dropped due to full buffer.
Definition nrf-ipc.h:196
struct nrf_ipc_msg rsp
Response mailbox (net -> app).
Definition nrf-ipc.h:167
volatile uint16_t tail
Written by app core.
Definition nrf-ipc.h:195
volatile uint8_t rsp_seq
Echoed from cmd_seq by net core.
Definition nrf-ipc.h:169
volatile uint8_t cmd_seq
Sequence number set by app core.
Definition nrf-ipc.h:164
struct nrf_ipc_shared_mem::@7 rx
Received data frame (net -> app, asynchronous).
struct nrf_ipc_shared_mem::@8 rx_ack
Received ACK frame (net -> app, asynchronous).
volatile uint32_t net_ready
Set to 1 by the net core when it has initialized the radio.
Definition nrf-ipc.h:159
volatile uint16_t head
Written by net core.
Definition nrf-ipc.h:194
uint32_t version
Protocol version for compatibility checking.
Definition nrf-ipc.h:156
struct nrf_ipc_msg cmd
Command mailbox (app -> net).
Definition nrf-ipc.h:162