Skip to content

Commit 560988e

Browse files
committed
micropython: Bring back running program from storage RAM.
This was missed in 65d5196 when upgrading MicroPython. This would lead to programs being copied to RAM before running. How it was found: If all slots were full, then the program would not run as there would not be enough heap to load yet another program. Also, MicroPython has a hardcoded test for mp_reader_mem_readbyte in mp_reader_try_read_rom, so it would ignore our reader even with MICROPY_VFS_ROM enabled. Fixes pybricks/support#2382
1 parent 4cc7241 commit 560988e

File tree

2 files changed

+5
-34
lines changed

2 files changed

+5
-34
lines changed

bricks/_common/micropython.c

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/mphal.h"
3535
#include "py/objmodule.h"
3636
#include "py/persistentcode.h"
37+
#include "py/reader.h"
3738
#include "py/repl.h"
3839
#include "py/runtime.h"
3940
#include "py/stackctrl.h"
@@ -74,35 +75,6 @@ bool pbsys_main_stdin_event(uint8_t c) {
7475
return false;
7576
}
7677

77-
// The following defines a reader for use by micropython/py/persistentcode.c.
78-
typedef struct _mp_vfs_map_minimal_t {
79-
const byte *cur;
80-
const byte *end;
81-
} mp_vfs_map_minimal_t;
82-
83-
mp_uint_t mp_vfs_map_minimal_readbyte(void *data) {
84-
mp_vfs_map_minimal_t *blob = (mp_vfs_map_minimal_t *)data;
85-
return (blob->cur < blob->end) ? *blob->cur++ : MP_READER_EOF;
86-
}
87-
88-
const uint8_t *mp_vfs_map_minimal_read_bytes(mp_reader_t *reader, size_t len) {
89-
mp_vfs_map_minimal_t *blob = (mp_vfs_map_minimal_t *)reader->data;
90-
const uint8_t *ptr = blob->cur;
91-
blob->cur += len;
92-
return ptr;
93-
}
94-
95-
static void mp_vfs_map_minimal_close(void *data) {
96-
}
97-
98-
static void mp_vfs_map_minimal_new_reader(mp_reader_t *reader, mp_vfs_map_minimal_t *data, const byte *buf, size_t len) {
99-
data->cur = buf;
100-
data->end = buf + len;
101-
reader->data = data;
102-
reader->readbyte = mp_vfs_map_minimal_readbyte;
103-
reader->close = mp_vfs_map_minimal_close;
104-
}
105-
10678
// Prints the exception that ended the program.
10779
static void print_final_exception(mp_obj_t exc, int ret) {
10880
nlr_buf_t nlr;
@@ -270,8 +242,7 @@ static void run_user_program(void) {
270242

271243
// This is similar to __import__ except we don't push/pop globals
272244
mp_reader_t reader;
273-
mp_vfs_map_minimal_t data;
274-
mp_vfs_map_minimal_new_reader(&reader, &data, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size));
245+
mp_reader_new_mem(&reader, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size), MP_READER_IS_ROM);
275246
mp_module_context_t *context = m_new_obj(mp_module_context_t);
276247
context->module.globals = mp_globals_get();
277248
mp_compiled_module_t compiled_module;
@@ -472,8 +443,7 @@ mp_obj_t pb_builtin_import(size_t n_args, const mp_obj_t *args) {
472443
if (info) {
473444
// Parse the static script data.
474445
mp_reader_t reader;
475-
mp_vfs_map_minimal_t data;
476-
mp_vfs_map_minimal_new_reader(&reader, &data, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size));
446+
mp_reader_new_mem(&reader, mpy_data_get_buf(info), pbio_get_uint32_le(info->mpy_size), MP_READER_IS_ROM);
477447

478448
// Create new module and execute in its own context.
479449
mp_obj_t module_obj = mp_obj_new_module(module_name_qstr);

bricks/_common/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116
#define MICROPY_PERSISTENT_CODE_LOAD (1)
117117
#define MICROPY_ENABLE_EXTERNAL_IMPORT (0)
118118
#define MICROPY_HAS_FILE_READER (0)
119-
#define MICROPY_VFS_MAP_MINIMAL (1)
119+
#define MICROPY_VFS_ROM (1)
120+
#define MICROPY_VFS_ROM_IOCTL (0)
120121
#if PYBRICKS_OPT_CUSTOM_IMPORT
121122
#define mp_builtin___import__ pb_builtin_import
122123
#endif

0 commit comments

Comments
 (0)