Skip to content

Commit b812626

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 c65fe7c commit b812626

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-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/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

@@ -285,6 +290,8 @@ ipc_nrf5340_init(void)
285290
}
286291
}
287292
#endif
293+
ipc_shared[0].region_descriptor_start = __shm_descriptor_start__;
294+
ipc_shared[0].region_descriptor_end = __shm_descriptor_end__;
288295

289296
/* Make sure network core if off when we set up IPC */
290297
NRF_RESET->NETWORK.FORCEOFF = RESET_NETWORK_FORCEOFF_FORCEOFF_Hold;
@@ -366,6 +373,18 @@ ipc_nrf5340_init(void)
366373
ipc_shared->ipc_state = APP_AND_NET_RUNNING;
367374
}
368375
}
376+
377+
const struct shm_memory_region *
378+
ipc_nrf5340_find_region(uint32_t region_id)
379+
{
380+
const struct shm_memory_region *region;
381+
for (region = ipc_shared->region_descriptor_start; region != ipc_shared->region_descriptor_end; ++region) {
382+
if (region->region_id == region_id) {
383+
return region;
384+
}
385+
}
386+
return NULL;
387+
}
369388
#endif
370389

371390
#if MYNEWT_VAL(MCU_APP_CORE)

hw/mcu/nordic/nrf5340/nrf5340.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ SECTIONS
9999
*(SORT(.dtors.*))
100100
*(.dtors)
101101

102+
. = ALIGN(4);
103+
__shm_descriptor_start__ = .;
104+
KEEP(*(.shm.descriptor))
105+
__shm_descriptor_end__ = .;
106+
102107
*(.rodata*)
103108

104109
*(.eh_frame*)

0 commit comments

Comments
 (0)