diff --git a/unicorn/Makefile b/unicorn/Makefile index 1e572ad..29fe8b7 100644 --- a/unicorn/Makefile +++ b/unicorn/Makefile @@ -53,10 +53,13 @@ SRC_C = \ pyb_switch.c \ pyb_servo.c \ pyb_adc.c \ - lib/utils/stdout_helpers.c \ - lib/utils/interrupt_char.c \ - lib/utils/pyexec.c \ - lib/libc/string0.c \ + shared/libc/string0.c \ + shared/readline/readline.c \ + shared/runtime/interrupt_char.c \ + shared/runtime/pyexec.c \ + shared/runtime/stdout_helpers.c \ + +SRC_C += \ lib/libm/math.c \ lib/libm/fmodf.c \ lib/libm/nearbyintf.c \ @@ -83,7 +86,6 @@ SRC_C = \ lib/libm/asinfacosf.c \ lib/libm/atanf.c \ lib/libm/atan2f.c \ - lib/mp-readline/readline.c \ # List of sources for qstr extraction SRC_QSTR += $(SRC_C) diff --git a/unicorn/help.c b/unicorn/help.c index 04528ac..962b8b5 100644 --- a/unicorn/help.c +++ b/unicorn/help.c @@ -26,7 +26,7 @@ #include "py/builtin.h" -const char *unicorn_help_text = +const char unicorn_help_text[] = "Welcome to MicroPython running under Unicorn!\n" "\n" "Control commands:\n" diff --git a/unicorn/machine_i2c.c b/unicorn/machine_i2c.c index 9cb2790..4b6b116 100644 --- a/unicorn/machine_i2c.c +++ b/unicorn/machine_i2c.c @@ -7,19 +7,19 @@ #include "extmod/machine_i2c.h" #include "unicorn_mcu.h" -typedef struct _machine_hard_i2c_obj_t { +typedef struct _machine_i2c_obj_t { mp_obj_base_t base; -} machine_hard_i2c_obj_t; +} machine_i2c_obj_t; -STATIC const machine_hard_i2c_obj_t machine_hard_i2c_obj[] = { - {{&machine_hard_i2c_type}}, +STATIC const machine_i2c_obj_t machine_i2c_obj[] = { + {{&machine_i2c_type}}, }; -void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void machine_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { mp_printf(print, "I2C(1, freq=unicorn, timeout=unicorn)"); } -mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_id, ARG_scl, ARG_sda, ARG_freq, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, @@ -50,7 +50,7 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz } // get static peripheral object - machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t*)&machine_hard_i2c_obj[i2c_id - 1]; + machine_i2c_obj_t *self = (machine_i2c_obj_t*)&machine_i2c_obj[i2c_id - 1]; // here we would check the scl/sda pins and configure them, but it's not implemented if (args[ARG_scl].u_obj != MP_OBJ_NULL || args[ARG_sda].u_obj != MP_OBJ_NULL) { @@ -58,17 +58,17 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz } // initialise the I2C peripheral - //machine_hard_i2c_init(self, args[ARG_freq].u_int, args[ARG_timeout].u_int); + //machine_i2c_init(self, args[ARG_freq].u_int, args[ARG_timeout].u_int); return MP_OBJ_FROM_PTR(self); } -int machine_hard_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) { +STATIC int machine_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) { printf("READFROM\n"); return 0; } -int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) { +STATIC int machine_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) { I2C->COMMAND = 0; @@ -102,16 +102,24 @@ int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_ return num_acks; } -STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { - .readfrom = machine_hard_i2c_readfrom, - .writeto = machine_hard_i2c_writeto, +STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size_t len, uint8_t *buf, unsigned int flags) { + if (flags & MP_MACHINE_I2C_FLAG_READ) { + return machine_i2c_readfrom(self_in, addr, buf, len, flags & MP_MACHINE_I2C_FLAG_STOP); + } else { + return machine_i2c_writeto(self_in, addr, buf, len, flags & MP_MACHINE_I2C_FLAG_STOP); + } +} + +STATIC const mp_machine_i2c_p_t machine_i2c_p = { + .transfer = mp_machine_i2c_transfer_adaptor, + .transfer_single = machine_i2c_transfer_single, }; -const mp_obj_type_t machine_hard_i2c_type = { +const mp_obj_type_t machine_i2c_type = { { &mp_type_type }, .name = MP_QSTR_I2C, - .print = machine_hard_i2c_print, - .make_new = machine_hard_i2c_make_new, - .protocol = &machine_hard_i2c_p, - .locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict, + .print = machine_i2c_print, + .make_new = machine_i2c_make_new, + .protocol = &machine_i2c_p, + .locals_dict = (mp_obj_dict_t*)&mp_machine_i2c_locals_dict, }; diff --git a/unicorn/main.c b/unicorn/main.c index 3fcdc68..eafe6a5 100644 --- a/unicorn/main.c +++ b/unicorn/main.c @@ -28,15 +28,15 @@ #include #include -#include "py/nlr.h" +#include "py/builtin.h" #include "py/compile.h" #include "py/runtime.h" #include "py/stackctrl.h" #include "py/repl.h" #include "py/gc.h" #include "py/mperrno.h" -#include "lib/utils/interrupt_char.h" -#include "lib/utils/pyexec.h" +#include "shared/runtime/interrupt_char.h" +#include "shared/runtime/pyexec.h" #include "unicorn_mcu.h" void do_str(const char *src, mp_parse_input_kind_t input_kind) { @@ -44,7 +44,7 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { if (nlr_push(&nlr) == 0) { mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); - mp_obj_t module_fun = mp_compile(&parse_tree, MP_QSTR__lt_stdin_gt_, MP_EMIT_OPT_NONE, false); + mp_obj_t module_fun = mp_compile(&parse_tree, MP_QSTR__lt_stdin_gt_, false); mp_call_function_0(module_fun); nlr_pop(); } else { @@ -140,7 +140,7 @@ void Reset_Handler(void) { *dest++ = 0; } - UNICORN_CONTROLLER->PENDING = (uint32_t) &MP_STATE_VM(mp_pending_exception); + UNICORN_CONTROLLER->PENDING = (uint32_t) &MP_STATE_MAIN_THREAD(mp_pending_exception); UNICORN_CONTROLLER->EXCEPTION = (uint32_t) &MP_STATE_VM(mp_kbd_exception); UNICORN_CONTROLLER->INTR_CHAR = (uint32_t) &mp_interrupt_char; diff --git a/unicorn/modmachine.c b/unicorn/modmachine.c index a755ee5..07919c5 100644 --- a/unicorn/modmachine.c +++ b/unicorn/modmachine.c @@ -27,7 +27,6 @@ #include #include "py/obj.h" -#include "mphalport.h" #include "modmachine.h" #include "extmod/machine_mem.h" #include "extmod/machine_i2c.h" @@ -43,6 +42,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, #if MICROPY_PY_MACHINE_I2C + { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, #endif }; @@ -53,4 +53,6 @@ const mp_obj_module_t mp_module_machine = { .globals = (mp_obj_dict_t*)&machine_module_globals, }; +MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine); + #endif // MICROPY_PY_MACHINE diff --git a/unicorn/modmachine.h b/unicorn/modmachine.h index 4c338be..62f90bf 100644 --- a/unicorn/modmachine.h +++ b/unicorn/modmachine.h @@ -40,6 +40,6 @@ extern const mp_obj_type_t machine_pin_type; machine_pin_obj_t *machine_pin_get(mp_obj_t *obj_in); void pin_set(mp_obj_t self_in, int value); -extern const mp_obj_type_t machine_hard_i2c_type; +extern const mp_obj_type_t machine_i2c_type; #endif diff --git a/unicorn/modpyb.c b/unicorn/modpyb.c index 2312152..4daa6f7 100644 --- a/unicorn/modpyb.c +++ b/unicorn/modpyb.c @@ -42,3 +42,5 @@ const mp_obj_module_t pyb_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&pyb_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_pyb, pyb_module); diff --git a/unicorn/modutime.c b/unicorn/modutime.c index 1cad089..fd0156c 100644 --- a/unicorn/modutime.c +++ b/unicorn/modutime.c @@ -30,10 +30,12 @@ #include "py/nlr.h" #include "py/smallint.h" #include "py/obj.h" -#include "lib/timeutils/timeutils.h" +#include "shared/timeutils/timeutils.h" #include "extmod/utime_mphal.h" #include "unicorn_mcu.h" +#if MICROPY_PY_UTIME_MP_HAL + STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, @@ -53,3 +55,7 @@ const mp_obj_module_t mp_module_utime = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&time_module_globals, }; + +MP_REGISTER_MODULE(MP_QSTR_utime, mp_module_utime); + +#endif // MICROPY_PY_UTIME_MP_HAL diff --git a/unicorn/mpconfigport.h b/unicorn/mpconfigport.h index c2cd4b3..2e4c23f 100644 --- a/unicorn/mpconfigport.h +++ b/unicorn/mpconfigport.h @@ -47,6 +47,9 @@ typedef long mp_off_t; #define MICROPY_PORT_BUILTINS \ { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, +// We need an implementation of the log2 function which is not a macro +#define MP_NEED_LOG2 (1) + // We need to provide a declaration/definition of alloca() #include diff --git a/unicorn/mpconfigport_features.h b/unicorn/mpconfigport_features.h index f4ecbce..526ebc8 100644 --- a/unicorn/mpconfigport_features.h +++ b/unicorn/mpconfigport_features.h @@ -27,9 +27,4 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) -extern const struct _mp_obj_module_t mp_module_machine; - -#define MICROPY_PORT_BUILTIN_MODULES \ - { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \ - #include "mpconfigport.h" diff --git a/unicorn/mpconfigport_pyboard.h b/unicorn/mpconfigport_pyboard.h index 1e8ea71..e125fc9 100644 --- a/unicorn/mpconfigport_pyboard.h +++ b/unicorn/mpconfigport_pyboard.h @@ -39,23 +39,11 @@ #define MICROPY_PY_FRAMEBUF (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_I2C (1) -#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new +#define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_HW_I2C1_NAME "X" #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) -extern const struct _mp_obj_module_t mp_module_utime; -extern const struct _mp_obj_module_t mp_module_machine; -extern const struct _mp_obj_module_t pyb_module; - -#define MICROPY_PORT_BUILTIN_MODULES \ - { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, \ - { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \ - { MP_ROM_QSTR(MP_QSTR_pyb), MP_ROM_PTR(&pyb_module) }, \ - -#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&mp_module_utime }, \ - #include "mpconfigport.h" diff --git a/unicorn/mphalport.c b/unicorn/mphalport.c index 32725d5..32c7ff3 100644 --- a/unicorn/mphalport.c +++ b/unicorn/mphalport.c @@ -33,9 +33,8 @@ void mp_hal_delay_ms(mp_uint_t ms) { uint32_t start = mp_hal_ticks_ms(); - extern void mp_handle_pending(void); while (mp_hal_ticks_ms() - start < ms) { - mp_handle_pending(); + mp_handle_pending(true); UNICORN_CONTROLLER->IDLE = 1; } } @@ -43,7 +42,7 @@ void mp_hal_delay_ms(mp_uint_t ms) { void mp_hal_delay_us(mp_uint_t us) { uint32_t start = mp_hal_ticks_us(); while (mp_hal_ticks_us() - start < us) { - mp_handle_pending(); + mp_handle_pending(true); } } diff --git a/unicorn/mphalport.h b/unicorn/mphalport.h index 82c63b0..658fe5b 100644 --- a/unicorn/mphalport.h +++ b/unicorn/mphalport.h @@ -8,7 +8,9 @@ mp_uint_t mp_hal_ticks_us(void); mp_uint_t mp_hal_ticks_cpu(void); void mp_hal_set_interrupt_char(int c); +#define MP_HAL_PIN_FMT "%q" #define mp_hal_pin_obj_t const machine_pin_obj_t* +#define mp_hal_pin_name(p) ((p)->name) #define mp_hal_pin_od_low(p) pin_set((mp_obj_t)p, 0) #define mp_hal_pin_od_high(p) pin_set((mp_obj_t)p, 1) #define mp_hal_get_pin_obj(o) machine_pin_get(o)