diff --git a/Makefile.firmware b/Makefile.firmware index 0a9f25f4..fc981483 100644 --- a/Makefile.firmware +++ b/Makefile.firmware @@ -159,6 +159,7 @@ OBJ_MICROPYTHON += $(addprefix $(BUILD_MP)/,\ lib/libm/thumb_vfp_sqrtf.o \ lib/libm/wf_lgamma.o \ lib/libm/wf_tgamma.o \ + lib/mp-readline/readline.o \ lib/utils/interrupt_char.o \ lib/utils/printf.o \ lib/utils/pyexec.o \ diff --git a/micropython/firmware/main.c b/micropython/firmware/main.c index 3590e77d..5926827f 100644 --- a/micropython/firmware/main.c +++ b/micropython/firmware/main.c @@ -65,6 +65,13 @@ int main(void) { // Run the main script pyexec_frozen_module("main.py"); + // Run REPL + for (;;) { + if (pyexec_friendly_repl() != 0) { + break; + } + } + // Clean up mp_deinit(); } diff --git a/micropython/firmware/mpconfigport.h b/micropython/firmware/mpconfigport.h index 4b3d5cfe..252995ca 100644 --- a/micropython/firmware/mpconfigport.h +++ b/micropython/firmware/mpconfigport.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2013-2015 Damien P. George + * Copyright (c) 2013-2017 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,22 +24,27 @@ * THE SOFTWARE. */ -#include // bool -#include // alloca +// Options to control how MicroPython is built for this port, +// overriding defaults in py/mpconfig.h. -// Memory allocation policies +#pragma once +#ifndef __INCLUDED_MPCONFIGPORT_H +#define __INCLUDED_MPCONFIGPORT_H + +// memory allocation policies #define MICROPY_ALLOC_PATH_MAX (128) -// Emitters +// emitters #define MICROPY_PERSISTENT_CODE_LOAD (0) #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) -// Compiler configuration +// compiler configuration #define MICROPY_COMP_MODULE_CONST (1) #define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1) +#define MICROPY_COMP_RETURN_IF_EXPR (1) -// Optimisations +// optimisations #define MICROPY_OPT_COMPUTED_GOTO (1) #define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0) #define MICROPY_OPT_MPZ_BITWISE (1) @@ -62,10 +67,12 @@ #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_USE_INTERNAL_ERRNO (1) +#define MICROPY_ENABLE_SCHEDULER (0) +#define MICROPY_SCHEDULER_DEPTH (0) #define MICROPY_VFS (0) #define MICROPY_VFS_FAT (0) -// Control over Python builtins +// control over Python builtins #define MICROPY_PY_FUNCTION_ATTRS (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) #define MICROPY_PY_BUILTINS_STR_CENTER (1) @@ -79,6 +86,7 @@ #define MICROPY_PY_BUILTINS_EXECFILE (1) #define MICROPY_PY_BUILTINS_POW3 (0) #define MICROPY_PY_BUILTINS_HELP (0) +#define MICROPY_PY_BUILTINS_HELP_TEXT stmhal_help_text #define MICROPY_PY_BUILTINS_HELP_MODULES (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) @@ -95,13 +103,13 @@ #define MICROPY_PY_THREAD (0) #define MICROPY_PY_THREAD_GIL (0) -// Extended modules -#define MICROPY_PY_UBINASCII (1) -#define MICROPY_PY_UBINASCII_CRC32 (1) +// extended modules #define MICROPY_PY_UCTYPES (1) #define MICROPY_PY_UZLIB (1) -#define MICROPY_PY_UTIME_MP_HAL (1) +#define MICROPY_PY_UBINASCII (1) +#define MICROPY_PY_UBINASCII_CRC32 (1) #define MICROPY_PY_UTIMEQ (1) +#define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_TREZORCONFIG (1) #define MICROPY_PY_TREZORCRYPTO (1) #define MICROPY_PY_TREZORIO (1) @@ -109,21 +117,57 @@ #define MICROPY_PY_TREZORUI (1) #define MICROPY_PY_TREZORUTILS (1) -// Type definitions for the specific machine +// extra built in names to add to the global namespace +#define MICROPY_PORT_BUILTINS \ + { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, -#include STM32_HAL_H +// extra built in modules to add to the list of known ones +extern const struct _mp_obj_module_t mp_module_utime; +extern const struct _mp_obj_module_t mp_module_TrezorConfig; +extern const struct _mp_obj_module_t mp_module_TrezorCrypto; +extern const struct _mp_obj_module_t mp_module_TrezorIO; +extern const struct _mp_obj_module_t mp_module_TrezorMsg; +extern const struct _mp_obj_module_t mp_module_TrezorUi; +extern const struct _mp_obj_module_t mp_module_TrezorUtils; -#define BYTES_PER_WORD (4) +#define MICROPY_PORT_BUILTIN_MODULES \ + { MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorConfig), (mp_obj_t)&mp_module_TrezorConfig }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorCrypto), (mp_obj_t)&mp_module_TrezorCrypto }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorIO), (mp_obj_t)&mp_module_TrezorIO }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorMsg), (mp_obj_t)&mp_module_TrezorMsg }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorUi), (mp_obj_t)&mp_module_TrezorUi }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorUtils), (mp_obj_t)&mp_module_TrezorUtils }, + +#define MP_STATE_PORT MP_STATE_VM + +#define MICROPY_PORT_ROOT_POINTERS \ + const char *readline_hist[8]; \ + +// type definitions for the specific machine #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) -#define INT_FMT "%d" -#define UINT_FMT "%u" #define MP_SSIZE_MAX (0x0fffffff) +#define UINT_FMT "%u" +#define INT_FMT "%d" + +typedef int mp_int_t; // must be pointer size +typedef unsigned int mp_uint_t; // must be pointer size typedef long mp_off_t; -typedef int mp_int_t; -typedef unsigned mp_uint_t; + +#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) + +// We have inlined IRQ functions for efficiency (they are generally +// 1 machine instruction). +// +// Note on IRQ state: you should not need to know the specific +// value of the state variable, but rather just pass the return +// value from disable_irq back to enable_irq. If you really need +// to know the machine-specific values, see irq.h. + +#include STM32_HAL_H static inline void enable_irq(mp_uint_t state) { __set_PRIMASK(state); @@ -143,32 +187,15 @@ static inline mp_uint_t disable_irq(void) { #define MICROPY_HW_MCU_NAME "STM32F405VG" #define MICROPY_PY_SYS_PLATFORM "trezor" -#define MICROPY_PORT_ROOT_POINTERS - -#define MP_STATE_PORT MP_STATE_VM - -// Extra built in modules to add to the list of known ones -extern const struct _mp_obj_module_t mp_module_utime; -extern const struct _mp_obj_module_t mp_module_TrezorConfig; -extern const struct _mp_obj_module_t mp_module_TrezorCrypto; -extern const struct _mp_obj_module_t mp_module_TrezorIO; -extern const struct _mp_obj_module_t mp_module_TrezorMsg; -extern const struct _mp_obj_module_t mp_module_TrezorUi; -extern const struct _mp_obj_module_t mp_module_TrezorUtils; -#define MICROPY_PORT_BUILTIN_MODULES \ - { MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&mp_module_utime }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorConfig), (mp_obj_t)&mp_module_TrezorConfig }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorCrypto), (mp_obj_t)&mp_module_TrezorCrypto }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorIO), (mp_obj_t)&mp_module_TrezorIO }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorMsg), (mp_obj_t)&mp_module_TrezorMsg }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorUi), (mp_obj_t)&mp_module_TrezorUi }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_TrezorUtils), (mp_obj_t)&mp_module_TrezorUtils }, - -// Extra built in names to add to the global namespace -#define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, - -// Make sure all allocations are done through GC +// There is no classical C heap in bare-metal ports, only Python +// garbage-collected heap. For completeness, emulate C heap via +// GC heap. Note that MicroPython core never uses malloc() and friends, +// so these defines are mostly to help extension module writers. #define malloc(n) m_malloc(n) #define free(p) m_free(p) #define realloc(p, n) m_realloc(p, n) + +// We need to provide a declaration/definition of alloca() +#include + +#endif // __INCLUDED_MPCONFIGPORT_H diff --git a/micropython/firmware/mphalport.h b/micropython/firmware/mphalport.h index 99b7ac75..c172fa5d 100644 --- a/micropython/firmware/mphalport.h +++ b/micropython/firmware/mphalport.h @@ -2,8 +2,6 @@ #include "lib/utils/interrupt_char.h" -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) - static inline mp_uint_t mp_hal_ticks_cpu(void) { return DWT->CYCCNT; } diff --git a/micropython/unix/Makefile b/micropython/unix/Makefile index 110ed1bb..0ab56702 100644 --- a/micropython/unix/Makefile +++ b/micropython/unix/Makefile @@ -316,7 +316,7 @@ minimal: BUILD=build-minimal PROG=micropython_minimal FROZEN_DIR= \ MICROPY_PY_BTREE=0 MICROPY_PY_FFI=0 MICROPY_PY_SOCKET=0 MICROPY_PY_THREAD=0 \ MICROPY_PY_TERMIOS=0 MICROPY_PY_USSL=0 \ - MICROPY_USE_READLINE=0 MICROPY_FATFS=0 + MICROPY_USE_READLINE=0 # build interpreter with nan-boxing as object model nanbox: diff --git a/micropython/unix/mpconfigport.h b/micropython/unix/mpconfigport.h index 9495d2e1..d8d5eb99 100644 --- a/micropython/unix/mpconfigport.h +++ b/micropython/unix/mpconfigport.h @@ -45,6 +45,7 @@ #endif #define MICROPY_COMP_MODULE_CONST (1) #define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1) +#define MICROPY_COMP_RETURN_IF_EXPR (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_STACK_CHECK (1) @@ -100,6 +101,7 @@ #endif #define MICROPY_PY_CMATH (1) #define MICROPY_PY_IO_FILEIO (1) +#define MICROPY_PY_IO_RESOURCE_STREAM (1) #define MICROPY_PY_GC_COLLECT_RETVAL (1) #define MICROPY_MODULE_FROZEN_STR (1) @@ -163,6 +165,7 @@ #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (256) +#define MICROPY_KBD_EXCEPTION (1) #define MICROPY_ASYNC_KBD_INTR (1) extern const struct _mp_obj_module_t mp_module_machine; @@ -177,7 +180,7 @@ extern const struct _mp_obj_module_t mp_module_jni; extern const struct _mp_obj_module_t mp_module_TrezorConfig; extern const struct _mp_obj_module_t mp_module_TrezorCrypto; -extern const struct _mp_obj_module_t mp_module_TrezorDebug; +extern const struct _mp_obj_module_t mp_module_TrezorIO; extern const struct _mp_obj_module_t mp_module_TrezorMsg; extern const struct _mp_obj_module_t mp_module_TrezorUi; extern const struct _mp_obj_module_t mp_module_TrezorUtils; @@ -284,8 +287,6 @@ typedef unsigned int mp_uint_t; // must be pointer size #endif #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Cannot include , as it may lead to symbol name clashes #if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) typedef long long mp_off_t; @@ -336,7 +337,6 @@ void mp_unix_mark_exec(void); #define MICROPY_PORT_ROOT_POINTERS \ const char *readline_hist[50]; \ - mp_obj_t keyboard_interrupt_obj; \ void *mmap_region_head; \ // We need to provide a declaration/definition of alloca() diff --git a/vendor/micropython b/vendor/micropython index 6e6c01b9..d5713c86 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit 6e6c01b97189a95b59a037fa7dabcf8217f199af +Subproject commit d5713c8618e8d809f354b6be72b7410dc5ac840a