Skip to content

Commit

Permalink
examples/imagimob: Adding support for imagimob integration.
Browse files Browse the repository at this point in the history
Signed-off-by: NikhitaR-IFX <[email protected]>
  • Loading branch information
NikhitaR-IFX committed Aug 21, 2024
1 parent 484febb commit a2b7c72
Show file tree
Hide file tree
Showing 9 changed files with 4,091 additions and 3 deletions.
12 changes: 12 additions & 0 deletions examples/imagimob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# MicroPython ImagiMob Integration for Infineon's PSoC6 port

This directory contains the imagimob generated model files and the interface for it's enablement in micropython. To access your edge ai models developed for PSoC6 based kit from a micropython application, please follow the description below.

## Folder structure

model.c : Imagimob generated source code in C for your developed model in the studio.
model.h : Imagimob generated source code in C for your developed model in the studio.

imai_mp_iface.c : MicroPython interface for ImagiMob models.

micropython.mk : Makefile configurations for firmware generation.
69 changes: 69 additions & 0 deletions examples/imagimob/imai_mp_iface.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Include MicroPython API.
#include "py/runtime.h"

#include "py/mphal.h"
#include "model.h"

static mp_obj_t init(void){
IMAI_init();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(init_obj, init);

static mp_obj_t enqueue(const mp_obj_t data_in_obj){
float data_in[IMAI_DATA_IN_COUNT];
mp_obj_t *data_in_items;
size_t len;
mp_obj_get_array(data_in_obj, &len, &data_in_items);
if (len != IMAI_DATA_IN_COUNT) {
mp_raise_ValueError("data_in must be a list of IMAI_DATA_IN_COUNT floats");
}
for (int i = 0; i < IMAI_DATA_IN_COUNT; i++) {
data_in[i] = mp_obj_float_get(data_in_items[i]);
}
int result = IMAI_enqueue(data_in);
return MP_OBJ_NEW_SMALL_INT(result);
}
MP_DEFINE_CONST_FUN_OBJ_1(enqueue_obj, enqueue);

static mp_obj_t dequeue(mp_obj_t data_out_obj) {
mp_buffer_info_t buf_info;
mp_get_buffer(data_out_obj, &buf_info, MP_BUFFER_WRITE);
float *data_out = (float *)buf_info.buf;
int result = IMAI_dequeue(data_out);
if (result == 0) {
return MP_OBJ_NEW_SMALL_INT(result);
} else if (result == -1) {
mp_printf(&mp_plat_print, "No data is currently available\n");
return MP_OBJ_NEW_SMALL_INT(result);
} else if (result == -2) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Internal memory allocation error"), result);
}
return MP_OBJ_NEW_SMALL_INT(result);
}
MP_DEFINE_CONST_FUN_OBJ_1(dequeue_obj, dequeue);

static const mp_rom_map_elem_t example_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_imai) },

// imagimob fns
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&init_obj) },
{ MP_ROM_QSTR(MP_QSTR_enqueue), MP_ROM_PTR(&enqueue_obj) },
{ MP_ROM_QSTR(MP_QSTR_dequeue), MP_ROM_PTR(&dequeue_obj) },

// constants
{ MP_ROM_QSTR(MP_QSTR_DATA_IN), MP_ROM_INT(IMAI_DATA_IN_COUNT) },
{ MP_ROM_QSTR(MP_QSTR_DATA_OUT), MP_ROM_INT(IMAI_DATA_OUT_COUNT) },
};
static MP_DEFINE_CONST_DICT(example_module_globals, example_module_globals_table);

// Define module object.
const mp_obj_module_t imai_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&example_module_globals,
};

// Register the module to make it available in Python.
#if MICROPY_PY_IMAI
MP_REGISTER_MODULE(MP_QSTR_imai, imai_module);
#endif
8 changes: 8 additions & 0 deletions examples/imagimob/micropython.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMAI_MOD_DIR := $(USERMOD_DIR)

# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(IMAI_MOD_DIR)/imai_mp_iface.c $(IMAI_MOD_DIR)/model.c

# We can add our module folder to include paths if needed
# This is not actually needed in this example.
CFLAGS_USERMOD += -I$(IMAI_MOD_DIR)
3,624 changes: 3,624 additions & 0 deletions examples/imagimob/model.c

Large diffs are not rendered by default.

360 changes: 360 additions & 0 deletions examples/imagimob/model.h

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions ports/psoc6/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ MICROPY_FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py
FROZEN_MANIFEST ?= $(MICROPY_FROZEN_MANIFEST)



CMAKE_ARGS =

ifneq ($(FROZEN_MANIFEST),)
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
CFLAGS += -DMICROPY_MODULE_FROZEN_MPY=1
Expand All @@ -49,12 +52,12 @@ CONFIG ?= Debug

# include py core make definitions
include ../../py/mkenv.mk
include $(TOP)/examples/imagimob/micropython.mk
include mpconfigport.mk
include $(BOARD_DIR)/mpconfigboard.mk
include $(TOP)/py/py.mk
include $(TOP)/extmod/extmod.mk


INC += -I.
INC += -I$(TOP)
INC += -I$(BUILD)
Expand Down Expand Up @@ -178,19 +181,26 @@ CFLAGS += -DMICROPY_PY_NETWORK=1 -DMICROPY_PY_NETWORK_IFX_WCM=1 -Wno-stringop-tr
MOD_SRC_C += network_ifx_wcm.c
endif

IMAGIMOB_SRC_C += $(addprefix examples/,\
imagimob/imai_mp_iface.c \
imagimob/model.c \
)

SRC_C = help.c \
main.c \
mphalport.c \
frozen_content.c \
pins_$(BOARD).c
pins_$(BOARD).c \


SRC_ASM += shared/runtime/gchelper_thumb1.s

SRC_QSTR += $(SHARED_SRC_C) $(MOD_SRC_C)
SRC_QSTR += $(SHARED_SRC_C) $(MOD_SRC_C) $(IMAGIMOB_SRC_C)

OBJ += $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(IMAGIMOB_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(MOD_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_ASM:.s=.o))
Expand Down
2 changes: 2 additions & 0 deletions ports/psoc6/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ extern void machine_deinit();

#define MICROPY_LOGGER_DEBUG (0)

// Enable imagimob ai porting
#define MICROPY_PY_IMAI (1)
// extern void lwip_lock_acquire(void);
// extern void lwip_lock_release(void);

Expand Down
1 change: 1 addition & 0 deletions ports/psoc6/mpconfigport.mk
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
MICROPY_VFS_LFS2=1
MICROPY_PY_IMAI=1
2 changes: 2 additions & 0 deletions ports/psoc6/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
Q(/)
Q(/flash)
Q(/lib)
Q(imai_model)
Q(add_ints)

0 comments on commit a2b7c72

Please sign in to comment.