Skip to content

Commit fb4bb45

Browse files
committed
ports/psoc6: Enable ntwk and rtos modules.
Signed-off-by: NikhitaR-IFX <nikhita.rajasekhar@infineon.com>
1 parent 76ef9a7 commit fb4bb45

8 files changed

Lines changed: 588 additions & 15 deletions

File tree

examples/natmod/deepcraft/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Location of top-level MicroPython directory
2+
MPY_DIR = ../../..
3+
4+
# Name of module
5+
MOD = deepcraft_model
6+
7+
# Source files (.c or .py)
8+
SRC = dc_mp_iface.c model.c
9+
10+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin, rv32imc)
11+
ARCH = armv7emsp
12+
13+
# Link with libm.a and libgcc.a from the toolchain
14+
LINK_RUNTIME = 1
15+
16+
override LIBGCC_PATH := gcc/lib/gcc/arm-none-eabi/11.3.1/thumb/v7e-m+fp/hard/libgcc.a
17+
override LIBM_PATH := gcc/arm-none-eabi/lib/thumb/v7e-m+fp/hard/libm.a
18+
19+
include $(MPY_DIR)/py/dynruntime.mk
20+
21+
# Custom clean target
22+
clean:
23+
rm -rf .mpy_ld_cache/ build/ $(MOD).mpy
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "py/dynruntime.h"
2+
3+
#if !defined(__linux__)
4+
void *memcpy(void *dst, const void *src, size_t n) {
5+
return mp_fun_table.memmove_(dst, src, n);
6+
}
7+
void *memset(void *s, int c, size_t n) {
8+
return mp_fun_table.memset_(s, c, n);
9+
}
10+
#endif
11+
12+
int native_errno=0;
13+
#if defined(__linux__)
14+
int *__errno_location (void)
15+
#else
16+
int *__errno (void)
17+
#endif
18+
{
19+
return &native_errno;
20+
}
21+
22+
#include "examples/natmod/deepcraft/mp_src.c"
23+
24+
// Forward declaration of type
25+
mp_obj_full_type_t dc_type;
26+
27+
mp_map_elem_t dc_locals_dict_table[5];
28+
static MP_DEFINE_CONST_DICT(dc_locals_dict, dc_locals_dict_table);
29+
30+
// Constructor
31+
static mp_obj_t dc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args_in) {
32+
dc_obj_t *self = mp_obj_malloc(dc_obj_t, type);
33+
return MP_OBJ_FROM_PTR(self);
34+
}
35+
36+
// Create type and methods at runtime
37+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
38+
// This must be first, it sets up the globals dict and other things
39+
MP_DYNRUNTIME_INIT_ENTRY
40+
41+
// Populate type
42+
dc_type.base.type = (void*)&mp_type_type;
43+
dc_type.flags = MP_TYPE_FLAG_NONE;
44+
dc_type.name = MP_QSTR_DEEPCRAFT;
45+
MP_OBJ_TYPE_SET_SLOT(&dc_type, make_new, dc_make_new, 0);
46+
47+
dc_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_init), MP_OBJ_FROM_PTR(&init_obj) };
48+
dc_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_enqueue), MP_OBJ_FROM_PTR(&enqueue_obj) };
49+
dc_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_dequeue), MP_OBJ_FROM_PTR(&dequeue_obj) };
50+
dc_locals_dict_table[3] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get_model_input_dim), MP_OBJ_FROM_PTR(&get_model_input_dim_obj) };
51+
dc_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get_model_output_dim), MP_OBJ_FROM_PTR(&get_model_output_dim_obj) };
52+
53+
MP_OBJ_TYPE_SET_SLOT(&dc_type, locals_dict, (void*)&dc_locals_dict, 5);
54+
55+
// Expose constructor as DEEPCRAFT
56+
mp_store_global(MP_QSTR_DEEPCRAFT, MP_OBJ_FROM_PTR(&dc_type));
57+
// This must be last, it restores the globals dict
58+
MP_DYNRUNTIME_INIT_EXIT
59+
60+
}

examples/natmod/deepcraft/mp_src.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "py/dynruntime.h"
2+
#include "model.h"
3+
#include <string.h>
4+
5+
typedef struct _dc_obj_t {
6+
mp_obj_base_t base;
7+
bool model_state;
8+
uint8_t model_in_dim;
9+
uint8_t model_out_dim;
10+
} dc_obj_t;
11+
12+
mp_obj_t get_model_input_dim(mp_obj_t self_in){
13+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
14+
return MP_OBJ_NEW_SMALL_INT(self->model_in_dim);
15+
}
16+
17+
mp_obj_t get_model_output_dim(mp_obj_t self_in){
18+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
19+
return MP_OBJ_NEW_SMALL_INT(self->model_out_dim);
20+
}
21+
22+
mp_obj_t init(mp_obj_t self_in){
23+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
24+
self->model_state = true;
25+
self->model_in_dim = IMAI_DATA_IN_COUNT;
26+
self->model_out_dim = IMAI_DATA_OUT_COUNT;
27+
IMAI_init();
28+
return mp_const_none;
29+
}
30+
31+
mp_obj_t enqueue(mp_obj_t self_in, const mp_obj_t data_in_obj){
32+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
33+
34+
// Check if model is initialized
35+
if(!self->model_state){
36+
mp_raise_ValueError("Model should be initialized first.");
37+
}
38+
39+
float data_in[self->model_in_dim];
40+
mp_obj_t *data_in_items;
41+
size_t len;
42+
mp_obj_get_array(data_in_obj, &len, &data_in_items);
43+
44+
if (len != self->model_in_dim) {
45+
mp_raise_ValueError("data_in must be a list of floats with size matching to input dimensions to model. Check using get_model_input_dim().");
46+
}
47+
48+
for (int i = 0; i < self->model_in_dim; i++) {
49+
data_in[i] = mp_obj_get_float(data_in_items[i]);
50+
}
51+
int result = IMAI_enqueue(data_in);
52+
return MP_OBJ_NEW_SMALL_INT(result);
53+
}
54+
55+
mp_obj_t dequeue(mp_obj_t self_in, mp_obj_t data_out_obj) {
56+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
57+
58+
mp_buffer_info_t buf_info;
59+
mp_get_buffer(data_out_obj, &buf_info, MP_BUFFER_WRITE);
60+
float *data_out = (float *)buf_info.buf;
61+
size_t len = buf_info.len / sizeof(float);
62+
63+
if (len != self->model_out_dim) {
64+
mp_raise_ValueError("data_out must be a list of floats with size matching to output dimensions of model. Check using get_model_output_dim().");
65+
}
66+
67+
int result = IMAI_dequeue(data_out);
68+
if (result == 0) {
69+
return MP_OBJ_NEW_SMALL_INT(result);
70+
} else if (result == -1) {
71+
return MP_OBJ_NEW_SMALL_INT(result);
72+
} else if (result == -2) {
73+
mp_raise_ValueError(MP_ERROR_TEXT("Internal memory allocation error"));
74+
}
75+
return MP_OBJ_NEW_SMALL_INT(result);
76+
}
77+
78+
79+
static const mp_obj_fun_builtin_fixed_t init_obj = {
80+
.base = { &mp_type_fun_builtin_1 },
81+
.fun._1 = (mp_fun_1_t)init,
82+
};
83+
84+
85+
static const mp_obj_fun_builtin_fixed_t enqueue_obj = {
86+
.base = { &mp_type_fun_builtin_2 },
87+
.fun._2 = (mp_fun_2_t)enqueue,
88+
};
89+
90+
static const mp_obj_fun_builtin_fixed_t dequeue_obj = {
91+
.base = { &mp_type_fun_builtin_2 },
92+
.fun._2 = (mp_fun_2_t)dequeue,
93+
};
94+
95+
static const mp_obj_fun_builtin_fixed_t get_model_input_dim_obj = {
96+
.base = { &mp_type_fun_builtin_1 },
97+
.fun._1 = (mp_fun_1_t)get_model_input_dim,
98+
};
99+
100+
static const mp_obj_fun_builtin_fixed_t get_model_output_dim_obj = {
101+
.base = { &mp_type_fun_builtin_1 },
102+
.fun._1 = (mp_fun_1_t)get_model_output_dim,
103+
};

ports/psoc6/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ build: mtb_get_build_flags $(GENERATED_PINS) $(BUILD)/firmware.hex
222222

223223
all: build
224224

225-
clean: mtb_clean
225+
#clean: mtb_clean
226226

227227
rebuild: clean mtb_clean all
228228

ports/psoc6/boards/CY8CKIT-062S2-AI/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "CY8C-062S2-AI"
3232

33-
#define MICROPY_GC_HEAP_SIZE (256 * 1024) // 256 KB
33+
#define MICROPY_GC_HEAP_SIZE (300 * 1024) // 256 KB
3434

3535
#define MICROPY_PY_HASHLIB (1)
3636
#define MICROPY_PY_HASHLIB_MD5 (1)

ports/psoc6/mtb-libs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ LDFLAGS=
169169
LDLIBS=
170170

171171
# Path to the linker script to use (if empty, use the default linker script).
172-
LINKER_SCRIPT=
172+
LINKER_SCRIPT=mpy-psoc.ld
173173

174174
# Custom pre-build commands to run.
175175
PREBUILD=

0 commit comments

Comments
 (0)