Skip to content

Commit 446d370

Browse files
committed
examples/natmod/deepcraft: Test implementation.
Signed-off-by: NikhitaR-IFX <nikhita.rajasekhar@infineon.com>
1 parent de1ff13 commit 446d370

3 files changed

Lines changed: 66 additions & 47 deletions

File tree

examples/natmod/deepcraft/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ ARCH = armv7emsp
1414
LINK_RUNTIME = 1
1515

1616
# Include to get the rules for compiling and linking the module
17-
include $(MPY_DIR)/py/dynruntime.mk
17+
include $(MPY_DIR)/py/dynruntime.mk
18+
19+
# Custom clean target
20+
clean:
21+
rm -rf .mpy_ld_cache/ build/ $(MOD).mpy
Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
1-
#define MICROPY_PY_DEEPCRAFT_MPY (1)
2-
31
#include "py/dynruntime.h"
42

5-
#if !defined(__linux__)
6-
void *memcpy(void *dst, const void *src, size_t n) {
7-
return mp_fun_table.memmove_(dst, src, n);
8-
}
9-
void *memset(void *s, int c, size_t n) {
10-
return mp_fun_table.memset_(s, c, n);
11-
}
12-
#endif
13-
14-
int native_errno=0;
15-
#if defined(__linux__)
16-
int *__errno_location (void)
17-
#else
18-
int *__errno (void)
19-
#endif
20-
{
21-
return &native_errno;
22-
}
23-
24-
mp_obj_full_type_t dcmodel_type;
253

264
#include "examples/natmod/deepcraft/mp_src.c"
275

28-
mp_map_elem_t dcmodel_locals_dict_table[3];
29-
static MP_DEFINE_CONST_DICT(dcmodel_locals_dict, dcmodel_locals_dict_table);
6+
typedef struct _dc_obj_t {
7+
mp_obj_base_t base;
8+
} dc_obj_t;
309

31-
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
32-
MP_DYNRUNTIME_INIT_ENTRY
10+
// Forward declaration of type
11+
mp_obj_full_type_t dc_type;
3312

34-
dcmodel_type.base.type = mp_fun_table.type_type;
35-
dcmodel_type.name = MP_QSTR_DEEPCRAFT;
36-
//MP_OBJ_TYPE_SET_SLOT(&dcmodel_type, make_new, &deflateio_make_new, 0);
37-
dcmodel_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_init), MP_OBJ_FROM_PTR(&init_obj) };
38-
dcmodel_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_enqueue), MP_OBJ_FROM_PTR(&enqueue_obj) };
39-
dcmodel_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_dequeue), MP_OBJ_FROM_PTR(&dequeue_obj) };
40-
MP_OBJ_TYPE_SET_SLOT(&dcmodel_type, locals_dict, (void*)&dcmodel_locals_dict, 2);
13+
mp_map_elem_t dc_locals_dict_table[3];
14+
static MP_DEFINE_CONST_DICT(dc_locals_dict, dc_locals_dict_table);
4115

42-
mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_deepcraft));
43-
mp_store_global(MP_QSTR_DEEPCRAFT, MP_OBJ_FROM_PTR(&dcmodel_type));
16+
// Constructor
17+
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) {
18+
dc_obj_t *self = mp_obj_malloc(dc_obj_t, type);
19+
return MP_OBJ_FROM_PTR(self);
20+
}
4421

22+
// Create type and methods at runtime
23+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
24+
// This must be first, it sets up the globals dict and other things
25+
MP_DYNRUNTIME_INIT_ENTRY
26+
27+
// Populate type
28+
dc_type.base.type = (void*)&mp_type_type;
29+
dc_type.flags = MP_TYPE_FLAG_NONE;
30+
dc_type.name = MP_QSTR_DEEPCRAFT;
31+
MP_OBJ_TYPE_SET_SLOT(&dc_type, make_new, dc_make_new, 0);
32+
33+
dc_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_init), MP_OBJ_FROM_PTR(&init_obj) };
34+
dc_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_enqueue), MP_OBJ_FROM_PTR(&enqueue_obj) };
35+
dc_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_dequeue), MP_OBJ_FROM_PTR(&dequeue_obj) };
36+
37+
38+
MP_OBJ_TYPE_SET_SLOT(&dc_type, locals_dict, (void*)&dc_locals_dict, 2);
39+
40+
// Expose constructor as DEEPCRAFT
41+
mp_store_global(MP_QSTR_DEEPCRAFT, MP_OBJ_FROM_PTR(&dc_type));
42+
// This must be last, it restores the globals dict
4543
MP_DYNRUNTIME_INIT_EXIT
44+
4645
}

examples/natmod/deepcraft/mp_src.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
#include "model.h"
33
#include <string.h>
44

5-
static mp_obj_t init(void){
5+
mp_obj_t init(void){
66
//assign();
7-
IMAI_init();
7+
//IMAI_init();
88
return mp_const_none;
99
}
10-
MP_DEFINE_CONST_FUN_OBJ_0(init_obj, init);
1110

12-
static mp_obj_t enqueue(const mp_obj_t data_in_obj){
13-
float data_in[IMAI_DATA_IN_COUNT];
11+
mp_obj_t enqueue(const mp_obj_t data_in_obj){
12+
/*float data_in[IMAI_DATA_IN_COUNT];
1413
mp_obj_t *data_in_items;
1514
size_t len;
1615
mp_obj_get_array(data_in_obj, &len, &data_in_items);
@@ -21,12 +20,12 @@ static mp_obj_t enqueue(const mp_obj_t data_in_obj){
2120
data_in[i] = mp_obj_get_float(data_in_items[i]);
2221
}
2322
int result = IMAI_enqueue(data_in);
24-
return MP_OBJ_NEW_SMALL_INT(result);
23+
return MP_OBJ_NEW_SMALL_INT(result);*/
24+
return mp_const_none;
2525
}
26-
MP_DEFINE_CONST_FUN_OBJ_1(enqueue_obj, enqueue);
2726

28-
static mp_obj_t dequeue(mp_obj_t data_out_obj) {
29-
mp_buffer_info_t buf_info;
27+
mp_obj_t dequeue(mp_obj_t data_out_obj) {
28+
/*mp_buffer_info_t buf_info;
3029
mp_get_buffer(data_out_obj, &buf_info, MP_BUFFER_WRITE);
3130
float *data_out = (float *)buf_info.buf;
3231
int result = IMAI_dequeue(data_out);
@@ -37,6 +36,23 @@ static mp_obj_t dequeue(mp_obj_t data_out_obj) {
3736
} else if (result == -2) {
3837
mp_raise_ValueError(MP_ERROR_TEXT("Internal memory allocation error"));
3938
}
40-
return MP_OBJ_NEW_SMALL_INT(result);
39+
return MP_OBJ_NEW_SMALL_INT(result);*/
40+
return mp_const_none;
4141
}
42-
MP_DEFINE_CONST_FUN_OBJ_1(dequeue_obj, dequeue);
42+
43+
44+
static const mp_obj_fun_builtin_fixed_t init_obj = {
45+
.base = { &mp_type_fun_builtin_0 },
46+
.fun._0 = (mp_fun_0_t)init,
47+
};
48+
49+
50+
static const mp_obj_fun_builtin_fixed_t enqueue_obj = {
51+
.base = { &mp_type_fun_builtin_1 },
52+
.fun._1 = (mp_fun_1_t)enqueue,
53+
};
54+
55+
static const mp_obj_fun_builtin_fixed_t dequeue_obj = {
56+
.base = { &mp_type_fun_builtin_1 },
57+
.fun._1 = (mp_fun_1_t)dequeue,
58+
};

0 commit comments

Comments
 (0)