Skip to content

Commit fb4fc43

Browse files
committed
extmod/modremoteproc: Add OpenAMP's Python modules.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent c85334b commit fb4fc43

File tree

3 files changed

+269
-0
lines changed

3 files changed

+269
-0
lines changed

extmod/extmod.mk

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SRC_EXTMOD_C += \
2929
extmod/modlwip.c \
3030
extmod/modnetwork.c \
3131
extmod/modonewire.c \
32+
extmod/modopenamp.c \
3233
extmod/modos.c \
3334
extmod/modplatform.c\
3435
extmod/modrandom.c \
@@ -41,6 +42,7 @@ SRC_EXTMOD_C += \
4142
extmod/moductypes.c \
4243
extmod/modwebrepl.c \
4344
extmod/modwebsocket.c \
45+
extmod/modremoteproc.c \
4446
extmod/network_cyw43.c \
4547
extmod/network_esp_hosted.c \
4648
extmod/network_lwip.c \
@@ -415,3 +417,85 @@ include $(TOP)/extmod/btstack/btstack.mk
415417
endif
416418

417419
endif
420+
421+
################################################################################
422+
# openamp
423+
424+
ifeq ($(MICROPY_PY_OPENAMP),1)
425+
OPENAMP_DIR = lib/open-amp
426+
LIBMETAL_DIR = lib/libmetal
427+
GIT_SUBMODULES += $(LIBMETAL_DIR) $(OPENAMP_DIR)
428+
429+
INC += -I$(TOP)/$(OPENAMP_DIR)
430+
CFLAGS += -DMICROPY_PY_OPENAMP=1
431+
432+
ifeq ($(MICROPY_PY_REMOTEPROC),1)
433+
CFLAGS += -DMICROPY_PY_RPROC=1
434+
endif
435+
436+
CFLAGS_THIRDPARTY += \
437+
-I$(TOP)/$(LIBMETAL_DIR) \
438+
-I$(TOP)/$(OPENAMP_DIR) \
439+
-I$(TOP)/$(OPENAMP_DIR)/lib/include/ \
440+
-DMETAL_INTERNAL \
441+
-DVIRTIO_DRIVER_ONLY \
442+
-DNO_ATOMIC_64_SUPPORT \
443+
-DRPMSG_BUFFER_SIZE=2000 \
444+
445+
# Libmetal's source files.
446+
SRC_THIRDPARTY_C += $(addprefix $(LIBMETAL_DIR)/metal/,\
447+
device.c \
448+
dma.c \
449+
init.c \
450+
io.c \
451+
irq.c \
452+
log.c \
453+
shmem.c \
454+
softirq.c \
455+
version.c \
456+
device.c \
457+
system/micropython/condition.c \
458+
system/micropython/device.c \
459+
system/micropython/io.c \
460+
system/micropython/irq.c \
461+
system/micropython/shmem.c \
462+
system/micropython/time.c \
463+
)
464+
465+
466+
# OpenAMP's source files.
467+
ifeq (,1)
468+
SRC_THIRDPARTY_C += $(addprefix $(OPENAMP_DIR)/,\
469+
condition.c \
470+
device.c \
471+
generic_device.c \
472+
generic_init.c \
473+
generic_io.c \
474+
init.c \
475+
io.c \
476+
irq.c \
477+
log.c \
478+
mailbox_hsem_if.c \
479+
openamp.c \
480+
remoteproc_virtio.c \
481+
rpmsg.c \
482+
rpmsg_virtio.c \
483+
rsc_table.c \
484+
shmem.c \
485+
sys.c \
486+
virtio.c \
487+
virtqueue.c \
488+
)
489+
endif
490+
491+
# OpenAMP's remoteproc source files.
492+
ifeq ($(MICROPY_PY_REMOTEPROC),1)
493+
SRC_THIRDPARTY_C += $(addprefix $(OPENAMP_DIR)/lib/remoteproc/,\
494+
elf_loader.c \
495+
remoteproc.c \
496+
remoteproc_virtio.c \
497+
rsc_table_parser.c \
498+
)
499+
endif # MICROPY_PY_REMOTEPROC
500+
501+
endif # MICROPY_PY_OPENAMP

extmod/modopenamp.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Arduino SA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
* OpenAMP's Python module.
27+
*/
28+
#if MICROPY_PY_OPENAMP
29+
30+
#include "py/obj.h"
31+
#include "py/nlr.h"
32+
#include "py/runtime.h"
33+
34+
#include <openamp/remoteproc.h>
35+
#include "lib/oofatfs/ff.h"
36+
#include "lib/oofatfs/diskio.h"
37+
38+
extern mp_obj_type_t remoteproc_type;
39+
40+
STATIC const mp_rom_map_elem_t globals_dict_table[] = {
41+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_remoteproc) },
42+
{ MP_ROM_QSTR(MP_QSTR_remoteproc), MP_ROM_PTR(&remoteproc_type) },
43+
};
44+
STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table);
45+
46+
const mp_obj_module_t openamp_module = {
47+
.base = { &mp_type_module },
48+
.globals = (mp_obj_t)&globals_dict,
49+
};
50+
51+
MP_REGISTER_MODULE(MP_QSTR_openamp, openamp_module);
52+
#endif // MICROPY_PY_OPENAMP

extmod/modremoteproc.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Arduino SA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
* OpenAMP's remoteproc Python module.
27+
*/
28+
#if MICROPY_PY_RPROC
29+
30+
#include "py/obj.h"
31+
#include "py/nlr.h"
32+
#include "py/runtime.h"
33+
34+
#include <openamp/remoteproc.h>
35+
#include "lib/oofatfs/ff.h"
36+
#include "lib/oofatfs/diskio.h"
37+
38+
typedef struct _remoteproc_obj_t {
39+
mp_obj_base_t base;
40+
struct remoteproc rproc;
41+
} remoteproc_obj_t;
42+
43+
const mp_obj_type_t remoteproc_type;
44+
45+
// Port-defined remoteproc operations.
46+
extern struct remoteproc_ops mp_rproc_ops;
47+
48+
// Port-defined image store operations, such as open the image
49+
// file, read image from storage, and close the image file.
50+
extern struct image_store_ops mp_store_ops;
51+
52+
STATIC mp_obj_t mod_remoteproc_load(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
53+
enum { ARG_path };
54+
static const mp_arg_t allowed_args[] = {
55+
{ MP_QSTR_path, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE } },
56+
};
57+
58+
// Parse args.
59+
remoteproc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
60+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
61+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
62+
63+
// Load firmware.
64+
const char *path = mp_obj_str_get_str(args[ARG_path].u_obj);
65+
if (remoteproc_load(&self->rproc, path, self->rproc.priv, &mp_store_ops, NULL) != 0) {
66+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to load firmware"));
67+
}
68+
return mp_const_none;
69+
}
70+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_remoteproc_load_obj, 2, mod_remoteproc_load);
71+
72+
STATIC mp_obj_t mod_remoteproc_start(mp_obj_t self_in) {
73+
remoteproc_obj_t *self = MP_OBJ_TO_PTR(self_in);
74+
75+
// Start the processor to run the application.
76+
if (remoteproc_start(&self->rproc) != 0) {
77+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to start remote processor"));
78+
}
79+
return mp_const_none;
80+
}
81+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_remoteproc_start_obj, mod_remoteproc_start);
82+
83+
STATIC mp_obj_t mod_remoteproc_stop(mp_obj_t self_in) {
84+
remoteproc_obj_t *self = MP_OBJ_TO_PTR(self_in);
85+
86+
// Stop the processor, but the processor is not powered down.
87+
if (remoteproc_stop(&self->rproc) != 0) {
88+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to stop remote processor"));
89+
}
90+
return mp_const_none;
91+
}
92+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_remoteproc_stop_obj, mod_remoteproc_stop);
93+
94+
STATIC mp_obj_t mod_remoteproc_deinit(mp_obj_t self_in) {
95+
remoteproc_obj_t *self = MP_OBJ_TO_PTR(self_in);
96+
(void)self;
97+
return mp_const_none;
98+
}
99+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_remoteproc_deinit_obj, mod_remoteproc_deinit);
100+
101+
mp_obj_t mod_remoteproc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
102+
remoteproc_obj_t *self = m_new_obj_with_finaliser(remoteproc_obj_t);
103+
self->base.type = &remoteproc_type;
104+
105+
// Instantiate the remoteproc instance
106+
// NOTE: ports should use rproc->priv to allocate the image store,
107+
// which gets passed to remoteproc_load(), and all of the store ops.
108+
remoteproc_init(&self->rproc, &mp_rproc_ops, NULL);
109+
110+
// Configure the remote before loading applications (optional).
111+
remoteproc_config(&self->rproc, NULL);
112+
113+
return MP_OBJ_FROM_PTR(self);
114+
}
115+
116+
STATIC const mp_rom_map_elem_t remoteproc_locals_dict_table[] = {
117+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_remoteproc) },
118+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mod_remoteproc_deinit_obj) },
119+
{ MP_ROM_QSTR(MP_QSTR_load), MP_ROM_PTR(&mod_remoteproc_load_obj) },
120+
{ MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&mod_remoteproc_start_obj) },
121+
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&mod_remoteproc_stop_obj) },
122+
// { MP_ROM_QSTR(MP_QSTR_get_shutdown), MP_ROM_PTR(&remoteproc_shutdown_obj) },
123+
};
124+
STATIC MP_DEFINE_CONST_DICT(remoteproc_locals_dict, remoteproc_locals_dict_table);
125+
126+
MP_DEFINE_CONST_OBJ_TYPE(
127+
remoteproc_type,
128+
MP_QSTR_remoteproc,
129+
MP_TYPE_FLAG_NONE,
130+
make_new, mod_remoteproc_make_new,
131+
locals_dict, &remoteproc_locals_dict
132+
);
133+
#endif // MICROPY_PY_RPROC

0 commit comments

Comments
 (0)