Skip to content

Commit 6f98677

Browse files
committed
hw/ipc_nrf5340: Support for flexible memory sharing
This adds easy way to add application specific shared memory regions that will be defined on application core and can be easily found on network core. This is done so linker scripts for network and application core does not have to be carefully synchronized. Signed-off-by: Jerzy Kasenberg <jerzy.kasenberg@codecoup.pl>
1 parent bdfb665 commit 6f98677

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

hw/drivers/ipc_nrf5340/include/ipc_nrf5340/ipc_nrf5340.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,74 @@ const void *ipc_nrf5340_net_image_get(uint32_t *size);
213213
volatile struct hci_ipc_shm *ipc_nrf5340_hci_shm_get(void);
214214
#endif
215215

216+
struct shm_memory_region {
217+
uint32_t region_id;
218+
void *region_start;
219+
uint32_t region_size;
220+
};
221+
222+
#if MYNEWT_VAL(MCU_APP_CORE)
223+
224+
#define __REGION_ID(id) shm_region_ ## id
225+
/**
226+
* Macro for shared memory region declaration
227+
*
228+
* It should be used on application core to specify memory region that should be accessible
229+
* on the network core.
230+
* example declaration form application core:
231+
* \code
232+
* struct application_shared_data {
233+
* int anything;
234+
* uint8_t buffer[1234]
235+
* } shared_data;
236+
*
237+
* #define MY_REGION_ID 112233
238+
* SHM_REGION(MY_REGION_ID, &shared_data, sizeof(shared_data));
239+
* \endcode
240+
*
241+
* @param id number that will be used on netcore to locate this region by
242+
* @param address start of shared memory region
243+
* @param size size of shared memory region
244+
*/
245+
#define SHM_REGION(id, address, size) \
246+
static struct shm_memory_region __attribute__((section(".shm_descriptor"), used)) __REGION_ID(id) = { \
247+
.region_id = id, \
248+
.region_start = address, \
249+
.region_size = size, \
250+
}
251+
252+
#else
253+
/**
254+
* Find shared memory region by it's ID.
255+
*
256+
* Region should be declared on application core with SHM_REGION macro.
257+
* example declaration form application core:
258+
* \code
259+
* struct application_shared_data {
260+
* int anything;
261+
* uint8_t buffer[1234]
262+
* } shared_data;
263+
*
264+
* SHM_REGION(112233, &shared_data, sizeof(shared_data));
265+
* \endcode
266+
* access on netcode:
267+
* \code
268+
* ...
269+
* const struct shm_memory_region *shared_region;
270+
* region = ipc_nrf5340_find_region(122233);
271+
* if (region) {
272+
* struct application_shared_data *shared_data = region->region_start;
273+
* shared_data->anything = 1;
274+
* ...
275+
* }
276+
* \endcode
277+
* @param region_id Region ID to find.
278+
*
279+
* @return Pointer to region, NULL if not present
280+
*/
281+
const struct shm_memory_region *ipc_nrf5340_find_region(uint32_t region_id);
282+
#endif
283+
216284
#ifdef __cplusplus
217285
}
218286
#endif

hw/drivers/ipc_nrf5340/pkg.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ pkg.deps:
3232
pkg.init:
3333
ipc_nrf5340_init: 'MYNEWT_VAL(IPC_NRF5340_SYSINIT_STAGE)'
3434
ipc_nrf5340_netcore_init: 'MYNEWT_VAL(IPC_NRF5340_NETCORE_SYSINIT_STAGE)'
35+
36+
pkg.link_tables:
37+
- shm_descriptor

hw/drivers/ipc_nrf5340/src/ipc_nrf5340.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct ipc_shared {
5454
APP_AND_NET_RUNNING,
5555
NET_RESTARTED,
5656
} ipc_state;
57+
struct shm_memory_region *region_descriptor_start;
58+
struct shm_memory_region *region_descriptor_end;
5759
#if MYNEWT_PKG_apache_mynewt_nimble__nimble_transport_common_hci_ipc
5860
volatile struct hci_ipc_shm hci_shm;
5961
#endif
@@ -96,6 +98,9 @@ static struct ipc_channel ipcs[IPC_MAX_CHANS];
9698
__attribute__((section(".ipc"))) static struct ipc_shared ipc_shared[1];
9799

98100
#if MYNEWT_VAL(MCU_APP_CORE)
101+
extern struct shm_memory_region __shm_descriptor_start__[];
102+
extern struct shm_memory_region __shm_descriptor_end__[];
103+
99104
static struct ipc_shm shms[IPC_MAX_CHANS];
100105
static uint8_t shms_bufs[IPC_MAX_CHANS][IPC_BUF_SIZE];
101106

@@ -289,6 +294,8 @@ ipc_nrf5340_init(void)
289294
}
290295
}
291296
#endif
297+
ipc_shared[0].region_descriptor_start = __shm_descriptor_start__;
298+
ipc_shared[0].region_descriptor_end = __shm_descriptor_end__;
292299

293300
for (i = 0; i < IPC_MAX_CHANS; ++i) {
294301
shms[i].buf = shms_bufs[i];
@@ -381,6 +388,18 @@ ipc_nrf5340_netcore_init(void)
381388
ipc_shared->ipc_state = APP_AND_NET_RUNNING;
382389
}
383390
}
391+
392+
const struct shm_memory_region *
393+
ipc_nrf5340_find_region(uint32_t region_id)
394+
{
395+
const struct shm_memory_region *region;
396+
for (region = ipc_shared->region_descriptor_start; region != ipc_shared->region_descriptor_end; ++region) {
397+
if (region->region_id == region_id) {
398+
return region;
399+
}
400+
}
401+
return NULL;
402+
}
384403
#endif
385404

386405
#if MYNEWT_VAL(MCU_APP_CORE)

0 commit comments

Comments
 (0)