diff --git a/Makefile.firmware b/Makefile.firmware index 301d7d74..d3c89383 100644 --- a/Makefile.firmware +++ b/Makefile.firmware @@ -313,6 +313,7 @@ OBJ_MP += $(addprefix $(BUILD_MP)/,\ # OBJ micropython/ OBJ_FW += $(addprefix $(BUILD_FW)/, \ firmware/main.o \ + firmware/mphalport.o \ trezorhal/flash.o \ trezorhal/rng.o \ trezorhal/sdcard.o \ diff --git a/micropython/firmware/main.c b/micropython/firmware/main.c index c55830f6..5cf675df 100644 --- a/micropython/firmware/main.c +++ b/micropython/firmware/main.c @@ -20,10 +20,6 @@ #include "usb.h" void SystemClock_Config(void); -void USBD_CDC_TxAlways(const uint8_t * buf, uint32_t len); -int USBD_CDC_Rx(uint8_t * buf, uint32_t len, uint32_t timeout); - -// Errors void NORETURN nlr_jump_fail(void *val) { for (;;) {} @@ -60,6 +56,11 @@ int main(void) { } RCC->CSR |= RCC_CSR_RMVF; + // Enable CPU ticks + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; + DWT->CYCCNT = 0; + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; + pendsv_init(); if (0 != flash_init()) { @@ -114,7 +115,7 @@ void MP_WEAK __assert_func(const char *file, int line, const char *func, const c } #endif -// I/O +// Micropython file I/O stubs mp_lexer_t *mp_lexer_new_from_file(const char *filename) { return NULL; @@ -129,32 +130,6 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) { } MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); -int mp_hal_stdin_rx_chr(void) { - for (;;) { - byte c; - if (USBD_CDC_Rx(&c, 1, 0) != 0) { - return c; - } - } -} - -void mp_hal_stdout_tx_strn(const char *str, size_t len) { - USBD_CDC_TxAlways((const uint8_t*)str, len); -} - int mp_reader_new_file(mp_reader_t *reader, const char *filename) { return 2; // assume error was "file not found" } - -// Time - -bool mp_hal_ticks_cpu_enabled; - -void mp_hal_ticks_cpu_enable(void) { - if (!mp_hal_ticks_cpu_enabled) { - // CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; - // DWT->CYCCNT = 0; - // DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; - mp_hal_ticks_cpu_enabled = true; - } -} diff --git a/micropython/firmware/mpconfigport.h b/micropython/firmware/mpconfigport.h index 4ff5cb6e..21aa0f24 100644 --- a/micropython/firmware/mpconfigport.h +++ b/micropython/firmware/mpconfigport.h @@ -24,12 +24,8 @@ * THE SOFTWARE. */ -#include -#include -#include -#include - -#define MICROPY_DEBUG_PRINTERS (1) +#include // bool +#include // alloca // Memory allocation policies #define MICROPY_ALLOC_PATH_MAX (128) @@ -117,21 +113,16 @@ #include STM32_HAL_H #define BYTES_PER_WORD (4) -#define MP_HAL_UNIQUE_ID_ADDRESS (0x1fff7a10) + #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) -#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len) -// This port is intended to be 32-bit, but unfortunately, int32_t for -// different targets may be defined in different ways - either as int -// or as long. This requires different printf formatting specifiers -// to print such value. So, we avoid int32_t and use int directly. +#define INT_FMT "%d" #define UINT_FMT "%u" -#define INT_FMT "%d" -typedef int mp_int_t; // must be pointer size -typedef unsigned mp_uint_t; // must be pointer size -typedef long mp_off_t; +#define MP_SSIZE_MAX (0x0fffffff) -#define MP_SSIZE_MAX INT_MAX +typedef long mp_off_t; +typedef int mp_int_t; +typedef unsigned mp_uint_t; static inline void enable_irq(mp_uint_t state) { __set_PRIMASK(state); @@ -147,15 +138,15 @@ static inline mp_uint_t disable_irq(void) { #define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state) #define MICROPY_EVENT_POLL_HOOK __WFI(); -#define MICROPY_MIN_USE_CORTEX_CPU (1) -#define MICROPY_MIN_USE_STM32_MCU (1) #define MICROPY_HW_BOARD_NAME "TREZORv2" #define MICROPY_HW_MCU_NAME "STM32F405VG" #define MICROPY_PY_SYS_PLATFORM "trezor" -#define MP_STATE_PORT MP_STATE_VM #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; @@ -163,8 +154,6 @@ extern const struct _mp_obj_module_t mp_module_TrezorDebug; 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; - -// Extra built in modules to add to the list of known ones #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 }, \ @@ -178,17 +167,7 @@ extern const struct _mp_obj_module_t mp_module_TrezorUtils; #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 #define malloc(n) m_malloc(n) #define free(p) m_free(p) #define realloc(p, n) m_realloc(p, n) - -extern bool mp_hal_ticks_cpu_enabled; - -void mp_hal_ticks_cpu_enable(void); - -static inline mp_uint_t mp_hal_ticks_cpu(void) { - if (!mp_hal_ticks_cpu_enabled) { - mp_hal_ticks_cpu_enable(); - } - return DWT->CYCCNT; -} diff --git a/micropython/firmware/mphalport.c b/micropython/firmware/mphalport.c new file mode 100644 index 00000000..8cc1f850 --- /dev/null +++ b/micropython/firmware/mphalport.c @@ -0,0 +1,17 @@ +#include "py/mphal.h" + +void USBD_CDC_TxAlways(const uint8_t *buf, uint32_t len); +int USBD_CDC_Rx(uint8_t *buf, uint32_t len, uint32_t timeout); + +int mp_hal_stdin_rx_chr(void) { + for (;;) { + byte c; + if (USBD_CDC_Rx(&c, 1, 0) != 0) { + return c; + } + } +} + +void mp_hal_stdout_tx_strn(const char *str, size_t len) { + USBD_CDC_TxAlways((const uint8_t*)str, len); +} diff --git a/micropython/firmware/mphalport.h b/micropython/firmware/mphalport.h index 0db02a8c..ef957457 100644 --- a/micropython/firmware/mphalport.h +++ b/micropython/firmware/mphalport.h @@ -1 +1,11 @@ -static inline void mp_hal_set_interrupt_char(char c) {} +#include STM32_HAL_H + +#include "lib/utils/interrupt_char.h" + +#define MP_HAL_UNIQUE_ID_ADDRESS (0x1fff7a10) + +#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; +}