drorgl / micropython_embedding

MicroPython Embedding Demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Micropython Embedding

Micropython initialization, execution and disposal can be a bit complicated in multiple threads environment.

  • initialize heap and stack for each instance of micropython
  • execute and stop execution
  • memory cleanup of removed instances

Initialization

  • mp_init / gc_init

  • mp_stack_set_limit

Porting Functions

  • hal printf/stdio
  • mp_hal_ticks_ms(), mp_hal_ticks_us() and mp_hal_ticks_cpu()
  • mp_hal_delay_ms() and mp_hal_delay_us()

Multiple Threads

#if MICROPY_PY_THREAD
extern mp_state_thread_t *mp_thread_get_state(void);
#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
#else
#define MP_STATE_THREAD(x)  MP_STATE_MAIN_THREAD(x)
#endif

mp_obj_type_t / mp_obj_list_append / mp_obj_list_get

mp_obj_t list = mp_obj_new_list(0, NULL);
for (int addr = 0x08; addr < 0x78; ++addr) {
    if (ret == 0) {
        mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));
    }
}
return list;

mp_uint_t path_num;
mp_obj_t *path_items;
mp_obj_list_get(mp_sys_path, &path_num, &path_items);

Execution

Example

    mp_sched_lock();
    gc_lock();
    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        mp_call_function_1(callback, MP_OBJ_FROM_PTR(tim));
        nlr_pop();
    } else {
        mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
    }
    gc_unlock();
    mp_sched_unlock();

Stopping

  • mp_sched_keyboard_interrupt - raises an interrupt in the VM to top the current execution But it's also possible to add hooks in the port to poll for incoming chars and check for ctrl-C, eg in the javascript port, MICROPY_VM_HOOK_POLL calls mp_js_hook() which checks for ctrl-C (should also be checked in MICROPY_EVENT_POLL_HOOK).

Cleanup

  • mp_deinit()

Error Handling

  • mp_obj_print_exception
if (nlr_push(&nlr) == 0) {
    mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT);
    mp_obj_t module_fun = mp_compile(pn, lex->source_name, MP_EMIT_OPT_NONE, false);
    mp_call_function_0(module_fun);
    nlr_pop();
    return 0;
} else {
    // exception
    return (mp_obj_t)nlr.ret_val;
}

// and 
void nlr_jump_fail(void *val) {
    printf("FATAL: uncaught NLR %p\n", val);
    exit(1);
}

Possible Extension Libraries

https://awesome-micropython.com/

MicroPython Library

MicroPython library was installed using the following procedure

Cleanup

Install doit

pip install doit
pio run -e esp32 -t clean
doit clean_micropython -e espressif32
doit clean_micropython -e espressif32 -e native-win32

References:

About

MicroPython Embedding Demo


Languages

Language:C 88.8%Language:Python 9.0%Language:Makefile 1.0%Language:C++ 0.4%Language:CMake 0.3%Language:Shell 0.2%Language:Assembly 0.1%Language:Batchfile 0.0%Language:JavaScript 0.0%