|
| 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