Skip to content

Commit d5db8f0

Browse files
committed
nrf: Use correct IRAM address for native code execution on nRF52.
On nRF52, the physical SRAM is mapped to 0x20000000 for data access and 0x00800000 for instruction access. So, while native code is allocated and written using addresses in the 0x20000000 range, it must execute from the 0x00800000 range. This commit makes this work correctly on nRF52 MCUs by adjusting the address. Signed-off-by: Damien George <[email protected]>
1 parent 3d7edbd commit d5db8f0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

ports/nrf/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "py/stackctrl.h"
4040
#include "py/gc.h"
4141
#include "py/compile.h"
42+
#include "py/persistentcode.h"
4243
#include "extmod/modmachine.h"
4344
#include "shared/runtime/pyexec.h"
4445
#include "readline.h"
@@ -369,3 +370,16 @@ void MP_WEAK __assert_func(const char *file, int line, const char *func, const c
369370
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
370371
__fatal_error("Assertion failed");
371372
}
373+
374+
#if MICROPY_EMIT_MACHINE_CODE
375+
void *nrf_native_code_commit(void *buf, unsigned int len, void *reloc) {
376+
(void)len;
377+
if (reloc) {
378+
// Native code in RAM must execute from the IRAM region at 0x00800000, and so relocations
379+
// to text must also point to this region. The MICROPY_MAKE_POINTER_CALLABLE macro will
380+
// adjust the `buf` address from RAM to IRAM.
381+
mp_native_relocate(reloc, buf, (uintptr_t)MICROPY_MAKE_POINTER_CALLABLE(buf) & ~1);
382+
}
383+
return buf;
384+
}
385+
#endif

ports/nrf/mpconfigport.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,17 @@
320320

321321
// type definitions for the specific machine
322322

323-
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1))
323+
#if defined(NRF52832) || defined(NRF52840)
324+
// On nRF52, the physical SRAM is mapped to 0x20000000 for data access and 0x00800000
325+
// for instruction access. So convert addresses to make them executable.
326+
#define MICROPY_PERSISTENT_CODE_TRACK_FUN_DATA (1)
327+
#define MICROPY_PERSISTENT_CODE_TRACK_BSS_RODATA (0)
328+
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)(((uintptr_t)(p) - 0x20000000 + 0x00800000) | 1))
329+
void *nrf_native_code_commit(void *, unsigned int, void *);
330+
#define MP_PLAT_COMMIT_EXEC(buf, len, reloc) nrf_native_code_commit(buf, len, reloc)
331+
#else
332+
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((uintptr_t)(p) | 1))
333+
#endif
324334

325335
#define MP_SSIZE_MAX (0x7fffffff)
326336

0 commit comments

Comments
 (0)