1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00

trezorhal: adjust to new structure

This commit is contained in:
Jan Pochyla 2017-03-06 17:33:42 +01:00
parent 28e62c6270
commit 78d82386d1
14 changed files with 543 additions and 217 deletions

View File

@ -3,9 +3,9 @@
JOBS=4
MAKE=make -j $(JOBS)
TREZORHAL_BUILD_DIR=micropython/trezorhal/build
TREZORHAL_BUILD_DIR=micropython/firmware/build
TREZORHAL_PORT_OPTS=FROZEN_MPY_DIR=../../../src
TREZORHAL_PORT_OPTS=FROZEN_MPY_DIR=src
UNIX_PORT_OPTS=MICROPY_FORCE_32BIT=1 MICROPY_PY_BTREE=0 MICROPY_PY_TERMIOS=0 MICROPY_PY_FFI=0 MICROPY_PY_USSL=0 MICROPY_SSL_AXTLS=0
CROSS_PORT_OPTS=MICROPY_FORCE_32BIT=1
@ -13,7 +13,7 @@ help: ## show this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36mmake %-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
vendor: ## update git submodules
git submodule update --init
# git submodule update --init
res: ## update resources
./tools/res_collect
@ -21,10 +21,10 @@ res: ## update resources
build: build_trezorhal build_unix build_cross ## build trezorhal, unix and mpy-cross micropython ports
build_trezorhal: vendor res build_cross ## build trezorhal port with frozen modules
$(MAKE) -C vendor/micropython/trezorhal $(TREZORHAL_PORT_OPTS)
$(MAKE) -f Makefile.firmware $(TREZORHAL_PORT_OPTS)
build_trezorhal_debug: vendor res build_cross ## build trezorhal port with frozen modules and debug symbols
$(MAKE) -C vendor/micropython/trezorhal $(TREZORHAL_PORT_OPTS) DEBUG=1
$(MAKE) -f Makefile.firmware $(TREZORHAL_PORT_OPTS) DEBUG=1
build_unix: vendor ## build unix port
$(MAKE) -C vendor/micropython/unix $(UNIX_PORT_OPTS)
@ -44,7 +44,7 @@ emu: ## run emulator
clean: clean_trezorhal clean_unix clean_cross ## clean all builds
clean_trezorhal: ## clean trezorhal build
$(MAKE) -C vendor/micropython/trezorhal clean $(TREZORHAL_PORT_OPTS)
$(MAKE) -f Makefile.firmware clean $(TREZORHAL_PORT_OPTS)
clean_unix: ## clean unix build
$(MAKE) -C vendor/micropython/unix clean $(UNIX_PORT_OPTS)

456
Makefile.firmware Normal file
View File

@ -0,0 +1,456 @@
# directory containing scripts to be frozen as bytecode
FROZEN_MPY_DIR ?= src
# target directory
BUILD ?= micropython/firmware/build
# include py core make definitions
include vendor/micropython/py/mkenv.mk
MAKE_FROZEN = vendor/micropython/tools/make-frozen.py
MPY_CROSS = vendor/micropython/mpy-cross/mpy-cross
MPY_TOOL = vendor/micropython/tools/mpy-tool.py
# sources
# =====================================
SRCDIR_MP = vendor/micropython
SRCDIR_FW = micropython
BUILD_MP = $(BUILD)/$(SRCDIR_MP)
BUILD_FW = $(BUILD)/$(SRCDIR_FW)
BUILD_HDR = $(BUILD)/genhdr
MICROPY_PY_TREZORCONFIG = 1
MICROPY_PY_TREZORCRYPTO = 1
MICROPY_PY_TREZORDEBUG = 1
MICROPY_PY_TREZORMSG = 1
MICROPY_PY_TREZORUI = 1
MICROPY_PY_TREZORUTILS = 1
MICROPY_PY_UTIME = 1
# OBJ micropython/extmod/modtrezorconfig
ifeq ($(MICROPY_PY_TREZORCONFIG),1)
OBJ_MOD += \
$(BUILD_FW)/extmod/modtrezorconfig/modtrezorconfig.o \
$(BUILD_FW)/extmod/modtrezorconfig/norcow.o
endif
# OBJ micropython/extmod/modtrezorcrypto
ifeq ($(MICROPY_PY_TREZORCRYPTO),1)
CFLAGS_MOD += \
-I$(SRCDIR_FW)/extmod/modtrezorcrypto/trezor-crypto \
-I$(SRCDIR_FW)/extmod/modtrezorcrypto/trezor-crypto/curve25519-donna \
-I$(SRCDIR_FW)/extmod/modtrezorcrypto/trezor-crypto/ed25519-donna \
-DED25519_CUSTOMRANDOM=1 \
-DED25519_CUSTOMHASH=1 \
-DED25519_NO_INLINE_ASM \
-DED25519_FORCE_32BIT=1 \
-DAES_128 \
-DAES_192 \
-DUSE_KECCAK=1 \
-Wno-sequence-point
OBJ_MOD += \
$(BUILD_FW)/extmod/modtrezorcrypto/modtrezorcrypto.o \
$(BUILD_FW)/extmod/modtrezorcrypto/rand.o \
$(BUILD_FW)/extmod/modtrezorcrypto/ssss.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/address.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/aescrypt.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/aeskey.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/aes_modes.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/aestab.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/base58.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/bignum.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/bip32.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/bip39.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/blake2s.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/curve25519-donna/curve25519.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/curves.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/ecdsa.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/ed25519-donna/ed25519.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/hmac.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/nist256p1.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/pbkdf2.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/ripemd160.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/secp256k1.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/sha2.o \
$(BUILD_FW)/extmod/modtrezorcrypto/trezor-crypto/sha3.o
endif
# OBJ micropython/extmod/modtrezordebug
ifeq ($(MICROPY_PY_TREZORDEBUG),1)
OBJ_MOD += $(BUILD_FW)/extmod/modtrezordebug/modtrezordebug.o
endif
# OBJ micropython/extmod/modtrezormsg
ifeq ($(MICROPY_PY_TREZORMSG),1)
OBJ_MOD += $(BUILD_FW)/extmod/modtrezormsg/modtrezormsg.o
endif
# OBJ micropython/extmod/modtrezorui
ifeq ($(MICROPY_PY_TREZORUI),1)
CFLAGS_MOD += -DQR_MAX_VERSION=0
OBJ_MOD += \
$(BUILD_FW)/extmod/modtrezorui/display.o \
$(BUILD_FW)/extmod/modtrezorui/inflate.o \
$(BUILD_FW)/extmod/modtrezorui/font_bitmap.o \
$(BUILD_FW)/extmod/modtrezorui/font_roboto_bold_20.o \
$(BUILD_FW)/extmod/modtrezorui/font_roboto_regular_20.o \
$(BUILD_FW)/extmod/modtrezorui/font_robotomono_regular_20.o \
$(BUILD_FW)/extmod/modtrezorui/modtrezorui.o \
$(BUILD_FW)/extmod/modtrezorui/trezor-qrenc/qr_encode.o
endif
# OBJ micropython/extmod/modtrezorutils
ifeq ($(MICROPY_PY_TREZORUTILS),1)
OBJ_MOD += $(BUILD_FW)/extmod/modtrezorutils/modtrezorutils.o
endif
# OBJ micropython/extmod/modutime
ifeq ($(MICROPY_PY_UTIME),1)
OBJ_MOD += $(BUILD_FW)/extmod/modutime/modutime.o
SRC_QSTR += $(SRCDIR_FW)/extmod/modutime/modutime.c
endif
# OBJ vendor/micropython
OBJ_MP += $(addprefix $(BUILD_MP)/,\
\
extmod/modubinascii.o \
extmod/moductypes.o \
extmod/moduheapq.o \
extmod/modutimeq.o \
extmod/moduzlib.o \
extmod/utime_mphal.o \
\
lib/embed/abort_.o \
lib/libc/string0.o \
lib/libc/string0.o \
lib/libm/acoshf.o \
lib/libm/asinfacosf.o \
lib/libm/asinhf.o \
lib/libm/atan2f.o \
lib/libm/atanf.o \
lib/libm/atanhf.o \
lib/libm/ef_rem_pio2.o \
lib/libm/erf_lgamma.o \
lib/libm/fmodf.o \
lib/libm/kf_cos.o \
lib/libm/kf_rem_pio2.o \
lib/libm/kf_sin.o \
lib/libm/kf_tan.o \
lib/libm/log1pf.o \
lib/libm/math.o \
lib/libm/roundf.o \
lib/libm/sf_cos.o \
lib/libm/sf_erf.o \
lib/libm/sf_frexp.o \
lib/libm/sf_ldexp.o \
lib/libm/sf_modf.o \
lib/libm/sf_sin.o \
lib/libm/sf_tan.o \
lib/libm/thumb_vfp_sqrtf.o \
lib/libm/wf_lgamma.o \
lib/libm/wf_tgamma.o \
lib/utils/interrupt_char.o \
lib/utils/printf.o \
lib/utils/pyexec.o \
lib/utils/stdout_helpers.o \
\
py/argcheck.o \
py/asmarm.o \
py/asmbase.o \
py/asmthumb.o \
py/asmx64.o \
py/asmx86.o \
py/asmxtensa.o \
py/bc.o \
py/binary.o \
py/builtinevex.o \
py/builtinhelp.o \
py/builtinimport.o \
py/compile.o \
py/emitbc.o \
py/emitcommon.o \
py/emitglue.o \
py/emitinlinethumb.o \
py/emitinlinextensa.o \
py/formatfloat.o \
py/frozenmod.o \
py/gc.o \
py/lexer.o \
py/malloc.o \
py/map.o \
py/modarray.o \
py/modbuiltins.o \
py/modcmath.o \
py/modcollections.o \
py/modgc.o \
py/modio.o \
py/modmath.o \
py/modmicropython.o \
py/modstruct.o \
py/modsys.o \
py/modthread.o \
py/moduerrno.o \
py/mpprint.o \
py/mpstate.o \
py/mpz.o \
py/nativeglue.o \
py/nlrsetjmp.o \
py/nlrthumb.o \
py/nlrx64.o \
py/nlrx86.o \
py/nlrxtensa.o \
py/obj.o \
py/objarray.o \
py/objattrtuple.o \
py/objbool.o \
py/objboundmeth.o \
py/objcell.o \
py/objclosure.o \
py/objcomplex.o \
py/objdict.o \
py/objenumerate.o \
py/objexcept.o \
py/objfilter.o \
py/objfloat.o \
py/objfun.o \
py/objgenerator.o \
py/objgetitemiter.o \
py/objint_longlong.o \
py/objint_mpz.o \
py/objint.o \
py/objlist.o \
py/objmap.o \
py/objmodule.o \
py/objnamedtuple.o \
py/objnone.o \
py/objobject.o \
py/objpolyiter.o \
py/objproperty.o \
py/objrange.o \
py/objreversed.o \
py/objset.o \
py/objsingleton.o \
py/objslice.o \
py/objstr.o \
py/objstringio.o \
py/objstrunicode.o \
py/objtuple.o \
py/objtype.o \
py/objzip.o \
py/opmethods.o \
py/parse.o \
py/parsenum.o \
py/parsenumbase.o \
py/persistentcode.o \
py/qstr.o \
py/reader.o \
py/repl.o \
py/runtime_utils.o \
py/runtime.o \
py/scope.o \
py/sequence.o \
py/showbc.o \
py/smallint.o \
py/stackctrl.o \
py/stream.o \
py/unicode.o \
py/vm.o \
py/vstr.o \
py/warning.o \
\
stmhal/hal/f4/src/stm32f4xx_hal_adc_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_adc.o \
stmhal/hal/f4/src/stm32f4xx_hal_can.o \
stmhal/hal/f4/src/stm32f4xx_hal_cortex.o \
stmhal/hal/f4/src/stm32f4xx_hal_dac_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_dac.o \
stmhal/hal/f4/src/stm32f4xx_hal_dma.o \
stmhal/hal/f4/src/stm32f4xx_hal_flash_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_flash.o \
stmhal/hal/f4/src/stm32f4xx_hal_gpio.o \
stmhal/hal/f4/src/stm32f4xx_hal_i2c.o \
stmhal/hal/f4/src/stm32f4xx_hal_pcd_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_pcd.o \
stmhal/hal/f4/src/stm32f4xx_hal_pwr_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_pwr.o \
stmhal/hal/f4/src/stm32f4xx_hal_rcc_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_rcc.o \
stmhal/hal/f4/src/stm32f4xx_hal_rng.o \
stmhal/hal/f4/src/stm32f4xx_hal_rtc_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_rtc.o \
stmhal/hal/f4/src/stm32f4xx_hal_sd.o \
stmhal/hal/f4/src/stm32f4xx_hal_spi.o \
stmhal/hal/f4/src/stm32f4xx_hal_tim_ex.o \
stmhal/hal/f4/src/stm32f4xx_hal_tim.o \
stmhal/hal/f4/src/stm32f4xx_hal_uart.o \
stmhal/hal/f4/src/stm32f4xx_hal.o \
stmhal/hal/f4/src/stm32f4xx_ll_sdmmc.o \
stmhal/hal/f4/src/stm32f4xx_ll_usb.o \
\
stmhal/gccollect.o \
stmhal/gchelper.o \
stmhal/pendsv.o \
stmhal/startup_stm32.o \
stmhal/system_stm32.o \
stmhal/systick.o \
\
stmhal/usbd_cdc_interface.o \
stmhal/usbd_conf.o \
stmhal/usbd_desc.o \
stmhal/usbd_hid_interface.o \
\
stmhal/usbdev/class/src/usbd_cdc_msc_hid.o \
stmhal/usbdev/class/src/usbd_msc_bot.o \
stmhal/usbdev/class/src/usbd_msc_data.o \
stmhal/usbdev/class/src/usbd_msc_scsi.o \
stmhal/usbdev/core/src/usbd_core.o \
stmhal/usbdev/core/src/usbd_ctlreq.o \
stmhal/usbdev/core/src/usbd_ioreq.o \
)
# OBJ micropython/
OBJ_FW += $(addprefix $(BUILD_FW)/, \
firmware/main.o \
trezorhal/stm32_it.o \
trezorhal/stm32f4xx_hal_sram.o \
trezorhal/stm32f4xx_ll_fsmc.o \
)
# make a list of all the .py files that need compiling and freezing
FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | $(SED) -e 's=^$(FROZEN_MPY_DIR)/==')
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
OBJ = $(OBJ_MOD) $(OBJ_MP) $(OBJ_FW) $(BUILD)/frozen_mpy.o
SRC_MP = $(patsubst $(BUILD_MP)%.o, $(SRCDIR_MP)%.c, $(OBJ_MP))
SRC_FW = $(patsubst $(BUILD_FW)%.o, $(SRCDIR_FW)%.c, $(OBJ_FW))
SRC_MOD = $(patsubst $(BUILD_FW)%.o, $(SRCDIR_FW)%.c, $(OBJ_MOD))
# comp flags
# =====================================
CROSS_COMPILE = arm-none-eabi-
INC += -I.
INC += -Imicropython/firmware
INC += -Imicropython/trezorhal
INC += -Ivendor/micropython
INC += -Ivendor/micropython/stmhal
INC += -Ivendor/micropython/stmhal/cmsis
INC += -Ivendor/micropython/stmhal/hal/f4/inc
INC += -Ivendor/micropython/stmhal/usbdev/core/inc
INC += -Ivendor/micropython/stmhal/usbdev/class/inc
INC += -Ivendor/micropython/lib/cmsis/inc
INC += -I$(BUILD)
ifeq ($(DEBUG), 1)
CFLAGS += -O0 -ggdb
else
CFLAGS += -Os -DNDEBUG
endif
CFLAGS += $(INC) $(CFLAGS_MOD) $(CFLAGS_EXTRA)
CFLAGS += -std=gnu99 -nostdlib -Wall -Werror -Wdouble-promotion -Wpointer-arith
CFLAGS += -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant
CFLAGS += -DSTM32F405xx -DMCU_SERIES_F4
CFLAGS += -DSTM32_HAL_H='<stm32f4xx_hal.h>'
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
LDFLAGS = -nostdlib -T micropython/trezorhal/trezorv2.ld -Map=$@.map --cref
# remove uncalled code from the final image
CFLAGS += -fdata-sections -ffunction-sections
LDFLAGS += --gc-sections
# QSTR file locations
QSTR_DEFS = $(SRCDIR_MP)/py/qstrdefs.h
QSTR_DEFS_COLLECTED = $(BUILD_HDR)/qstrdefs.collected.h
# SRC files for QSTR extraction
SRC_QSTR_IGNORE = $(addprefix $(SRCDIR_MP)/py/, \
nlr% emitnx86% emitnx64% emitnthumb% emitnarm% emitnxtensa%)
SRC_QSTR += $(SRC_MOD)
SRC_QSTR += $(filter $(SRCDIR_MP)/lib/%,$(SRC_MP))
SRC_QSTR += $(filter $(SRCDIR_MP)/extmod/%,$(SRC_MP))
# SRC_QSTR += $(filter $(SRCDIR_MP)/stmhal/%,$(filter-out %/gchelper.o,$(SRC_MP)))
SRC_QSTR += $(filter-out $(SRC_QSTR_IGNORE),$(filter $(SRCDIR_MP)/py/%,$(SRC_MP)))
# extra cpp CFLAGS for QSTR extraction
QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA
# comp rules
# =====================================
all: $(BUILD)/firmware.bin
$(BUILD)/firmware.elf: $(OBJ)
$(ECHO) "LINK $@"
$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
$(Q)$(SIZE) $@
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
$(Q)$(OBJCOPY) -O binary -j .flash -j .data $^ $(BUILD)/firmware.bin
$(BUILD)/%.o: %.S
$(ECHO) "CC $<"
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
$(BUILD)/%.o: %.s
$(ECHO) "AS $<"
$(Q)$(AS) -o $@ $<
$(BUILD)/%.o: %.c
$(ECHO) "CC $<"
$(Q)$(CC) $(CFLAGS) -c -MD -o $@ $<
OBJ_DIRS = $(sort $(dir $(OBJ)))
$(OBJ): | $(OBJ_DIRS)
$(OBJ_DIRS) $(BUILD_HDR):
$(MKDIR) -p $@
$(OBJ): | $(BUILD_HDR)/qstrdefs.generated.h $(BUILD_HDR)/mpversion.h
$(BUILD_HDR)/qstr.i.last: $(SRC_QSTR) | $(BUILD_HDR)/mpversion.h
$(ECHO) "GEN $@"
$(Q)$(CPP) $(QSTR_GEN_EXTRA_CFLAGS) $(CFLAGS) $^ >$(BUILD_HDR)/qstr.i.last; \
$(BUILD_HDR)/qstr.split: $(BUILD_HDR)/qstr.i.last
$(ECHO) "GEN $@"
$(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py split $(BUILD_HDR)/qstr.i.last $(BUILD_HDR)/qstr $(QSTR_DEFS_COLLECTED)
$(Q)touch $@
$(QSTR_DEFS_COLLECTED): $(BUILD_HDR)/qstr.split
$(ECHO) "GEN $@"
$(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py cat $(BUILD_HDR)/qstr.i.last $(BUILD_HDR)/qstr $(QSTR_DEFS_COLLECTED)
$(BUILD_HDR)/qstrdefs.generated.h: $(QSTR_DEFS) $(QSTR_DEFS_COLLECTED) $(SRCDIR_FW)/firmware/mpconfigport.h | $(BUILD_HDR)
$(ECHO) "GEN $@"
$(Q)cat $(QSTR_DEFS) $(QSTR_DEFS_COLLECTED) \
| $(SED) 's/^Q(.*)/"&"/' \
| $(CPP) $(CFLAGS) - \
| $(SED) 's/^"\(Q(.*)\)"/\1/' \
> $(BUILD_HDR)/qstrdefs.preprocessed.h
$(Q)$(PYTHON) $(PY_SRC)/makeqstrdata.py $(BUILD_HDR)/qstrdefs.preprocessed.h > $@
$(BUILD_HDR)/mpversion.h: | $(BUILD_HDR)
$(Q)$(PYTHON) $(PY_SRC)/makeversionhdr.py $@
$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py
@$(ECHO) "MPY $<"
$(Q)$(MKDIR) -p $(dir $@)
$(Q)$(MPY_CROSS) -o $@ -s $(^:$(FROZEN_MPY_DIR)/%=%) $^
$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
@$(ECHO) "Creating $@"
$(Q)PYTHONPATH=$(SRCDIR_MP)/py $(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@
clean:
$(RM) -rf $(BUILD)
.PHONY: all clean

View File

@ -1,10 +1,11 @@
extern I2C_HandleTypeDef I2CHandle1;
extern void i2c_init(I2C_HandleTypeDef *i2c);
extern HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
static I2C_HandleTypeDef touch_i2c_handle;
void touch_init(void)
{
I2C_InitTypeDef *init = &(I2CHandle1.Init);
I2C_InitTypeDef *init = &(touch_i2c_handle.Init);
init->OwnAddress1 = 0xFE; // master
init->ClockSpeed = 400000;
init->DutyCycle = I2C_DUTYCYCLE_16_9;
@ -13,13 +14,13 @@ void touch_init(void)
init->GeneralCallMode = I2C_GENERALCALL_DISABLED;
init->NoStretchMode = I2C_NOSTRETCH_DISABLED;
init->OwnAddress2 = 0;
i2c_init(&I2CHandle1);
i2c_init(&touch_i2c_handle);
}
uint32_t touch_read(void)
{
static uint8_t data[16], old_data[16];
if (HAL_OK != HAL_I2C_Master_Receive(&I2CHandle1, 56 << 1, data, 16, 1)) {
if (HAL_OK != HAL_I2C_Master_Receive(&touch_i2c_handle, 56 << 1, data, 16, 1)) {
return 0; // read failure
}
if (0 == memcmp(data, old_data, 16)) {

View File

@ -24,8 +24,6 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include STM32_HAL_H
#include "py/nlr.h"

View File

@ -152,7 +152,7 @@ void usb_init(void) {
// I2C
I2C_HandleTypeDef *I2CHandle1;
I2C_HandleTypeDef *i2c_handle = 0;
void i2c_init(I2C_HandleTypeDef *i2c) {
@ -177,7 +177,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
}
// Enable IRQs
I2CHandle1 = i2c;
i2c_handle = i2c;
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
}
@ -234,6 +234,10 @@ 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;

View File

@ -1,8 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2015 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
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <limits.h>
#include <stdint.h>
#include <stdbool.h>
#include <alloca.h>
#include "mpconfigboard.h"
#define MICROPY_DEBUG_PRINTERS (1)
// Memory allocation policies
#define MICROPY_ALLOC_PATH_MAX (128)
@ -12,13 +42,12 @@
#define MICROPY_EMIT_INLINE_THUMB (0)
// Compiler configuration
#define MICROPY_ENABLE_COMPILER (1)
#define MICROPY_COMP_MODULE_CONST (1)
#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (1)
#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1)
// Optimisations
#define MICROPY_OPT_COMPUTED_GOTO (1)
#define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0)
#define MICROPY_OPT_MPZ_BITWISE (1)
// Python internal features
@ -37,12 +66,10 @@
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
#define MICROPY_STREAMS_NON_BLOCK (1)
#define MICROPY_MODULE_WEAK_LINKS (1)
#define MICROPY_MODULE_FROZEN_MPY (1)
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
#define MICROPY_USE_INTERNAL_ERRNO (1)
#define MICROPY_VFS (0)
#define MICROPY_VFS_FAT (0)
#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
// Control over Python builtins
#define MICROPY_PY_FUNCTION_ATTRS (1)
@ -91,16 +118,6 @@
#include STM32_HAL_H
static inline void enable_irq(uint32_t state) {
__set_PRIMASK(state);
}
static inline uint32_t disable_irq(void) {
uint32_t st = __get_PRIMASK();
__disable_irq();
return st;
}
#define BYTES_PER_WORD (4)
#define MP_HAL_UNIQUE_ID_ADDRESS (0x1fff7a10)
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1))
@ -117,6 +134,21 @@ typedef unsigned mp_uint_t; // must be pointer size
typedef long mp_off_t;
#define MP_SSIZE_MAX INT_MAX
static inline void enable_irq(mp_uint_t state) {
__set_PRIMASK(state);
}
static inline mp_uint_t disable_irq(void) {
mp_uint_t state = __get_PRIMASK();
__disable_irq();
return state;
}
#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq()
#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"
@ -124,7 +156,8 @@ typedef long mp_off_t;
#define MICROPY_PY_SYS_PLATFORM "trezor"
#define MP_STATE_PORT MP_STATE_VM
#define MICROPY_PORT_ROOT_POINTERS const char *readline_hist[8];
#define MICROPY_PORT_ROOT_POINTERS
#define USE_DEVICE_MODE
extern const struct _mp_obj_module_t mp_module_utime;
extern const struct _mp_obj_module_t mp_module_TrezorConfig;
@ -148,6 +181,10 @@ 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 },
#define malloc(n) m_malloc(n)
#define free(p) m_free(p)
#define realloc(p, n) m_realloc(p, n)
// Timing functions
#include "stmhal/systick.h"

View File

@ -1,175 +0,0 @@
# include default settings
include ../py/mkenv.mk
# qstr definitions target, used in py.mk
QSTR_DEFS = qstrdefsport.h
# directory containing scripts to be frozen as bytecode
FROZEN_MPY_DIR ?= modules
# enable external modules, used in py.mk
MICROPY_PY_TREZORCONFIG = 1
MICROPY_PY_TREZORCRYPTO = 1
MICROPY_PY_TREZORDEBUG = 1
MICROPY_PY_TREZORMSG = 1
MICROPY_PY_TREZORUI = 1
MICROPY_PY_TREZORUTILS = 1
# include py core make definitions
include ../py/py.mk
CROSS_COMPILE = arm-none-eabi-
INC += -I.
INC += -I..
INC += -I../stmhal
INC += -I../stmhal/cmsis
INC += -I../stmhal/hal/f4/inc
INC += -I../stmhal/usbdev/core/inc
INC += -I../stmhal/usbdev/class/inc
INC += -I../lib/cmsis/inc
INC += -I../lib/mp-readline
INC += -I$(BUILD)
ifeq ($(DEBUG), 1)
COPT = -O0 -ggdb
else
COPT = -Os -DNDEBUG
endif
CFLAGS += $(INC) $(COPT) $(CFLAGS_MOD) $(CFLAGS_EXTRA)
CFLAGS += -std=gnu99 -nostdlib -Wall -Werror -Wdouble-promotion -Wpointer-arith
CFLAGS += -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant
CFLAGS += -DSTM32F405xx -DMCU_SERIES_F4
CFLAGS += -DSTM32_HAL_H='<stm32f4xx_hal.h>'
LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
LDFLAGS = -nostdlib -T trezorv2.ld -Map=$@.map --cref
# remove uncalled code from the final image
CFLAGS += -fdata-sections -ffunction-sections
LDFLAGS += --gc-sections
SRC_LIB = $(addprefix lib/,\
libc/string0.c \
libm/math.c \
libm/thumb_vfp_sqrtf.c \
libm/asinfacosf.c \
libm/atanf.c \
libm/atan2f.c \
libm/fmodf.c \
libm/roundf.c \
libm/log1pf.c \
libm/acoshf.c \
libm/asinhf.c \
libm/atanhf.c \
libm/kf_rem_pio2.c \
libm/kf_sin.c \
libm/kf_cos.c \
libm/kf_tan.c \
libm/ef_rem_pio2.c \
libm/erf_lgamma.c \
libm/sf_sin.c \
libm/sf_cos.c \
libm/sf_tan.c \
libm/sf_frexp.c \
libm/sf_modf.c \
libm/sf_ldexp.c \
libm/sf_erf.c \
libm/wf_lgamma.c \
libm/wf_tgamma.c \
libc/string0.c \
mp-readline/readline.c \
utils/stdout_helpers.c \
utils/pyexec.c \
)
SRC_HAL = $(addprefix stmhal/hal/f4/src/stm32f4xx_,\
hal.c \
hal_adc.c \
hal_adc_ex.c \
hal_can.c \
hal_cortex.c \
hal_dac.c \
hal_dac_ex.c \
hal_dma.c \
hal_flash.c \
hal_flash_ex.c \
hal_gpio.c \
hal_i2c.c \
hal_pcd.c \
hal_pcd_ex.c \
hal_pwr.c \
hal_pwr_ex.c \
hal_rcc.c \
hal_rcc_ex.c \
hal_rng.c \
hal_rtc.c \
hal_rtc_ex.c \
hal_sd.c \
hal_spi.c \
hal_sram.c \
hal_tim.c \
hal_tim_ex.c \
hal_uart.c \
ll_fsmc.c \
ll_sdmmc.c \
ll_usb.c \
)
SRC_USBDEV = $(addprefix stmhal/usbdev/,\
core/src/usbd_core.c \
core/src/usbd_ctlreq.c \
core/src/usbd_ioreq.c \
class/src/usbd_cdc_msc_hid.c \
class/src/usbd_msc_bot.c \
class/src/usbd_msc_scsi.c \
class/src/usbd_msc_data.c \
)
SRC_C = \
main.c \
stm32_it.c \
modutime.c \
lib/utils/stdout_helpers.c \
lib/utils/pyexec.c \
lib/libc/string0.c \
lib/mp-readline/readline.c \
stmhal/pendsv.c \
stmhal/systick.c \
stmhal/gccollect.c \
stmhal/system_stm32.c \
stmhal/usbd_conf.c \
stmhal/usbd_desc.c \
stmhal/usbd_cdc_interface.c \
stmhal/usbd_hid_interface.c \
$(BUILD)/frozen_mpy.c
SRC_QSTR += modutime.c
SRC_O = \
stmhal/startup_stm32.o \
stmhal/gchelper.o \
OBJ = \
$(PY_O) \
$(addprefix $(BUILD)/, $(SRC_O)) \
$(addprefix $(BUILD)/, $(SRC_C:.c=.o)) \
$(addprefix $(BUILD)/, $(SRC_LIB:.c=.o)) \
$(addprefix $(BUILD)/, $(SRC_HAL:.c=.o)) \
$(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o)) \
$(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
all: $(BUILD)/firmware.bin
$(BUILD)/firmware.elf: $(OBJ)
$(ECHO) "LINK $@"
$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)
$(Q)$(SIZE) $@
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf
$(Q)$(OBJCOPY) -O binary -j .flash -j .data $^ $(BUILD)/firmware.bin
include ../py/mkrules.mk

View File

@ -1 +0,0 @@
// qstrs specific to this port

View File

@ -736,19 +736,20 @@ void FLASH_IRQHandler(void) {
// }
// #endif // MICROPY_HW_ENABLE_CAN
#if defined(MICROPY_HW_I2C1_SCL)
extern I2C_HandleTypeDef *i2c_handle;
void I2C1_EV_IRQHandler(void) {
IRQ_ENTER(I2C1_EV_IRQn);
HAL_I2C_EV_IRQHandler(i2c_handle);
if (i2c_handle) {
HAL_I2C_EV_IRQHandler(i2c_handle);
}
IRQ_EXIT(I2C1_EV_IRQn);
}
void I2C1_ER_IRQHandler(void) {
IRQ_ENTER(I2C1_ER_IRQn);
HAL_I2C_ER_IRQHandler(i2c_handle);
if (i2c_handle) {
HAL_I2C_ER_IRQHandler(i2c_handle);
}
IRQ_EXIT(I2C1_ER_IRQn);
}
#endif // defined(MICROPY_HW_I2C1_SCL)

View File

@ -1,8 +1,8 @@
import sys
if sys.platform in ('trezor', 'pyboard'): # stmhal
if sys.platform == 'trezor':
from .config_mock import Config
_config = Config('/sd/trezor.config')
_config = Config(None)
else:
from TrezorConfig import Config
_config = Config()

View File

@ -8,6 +8,11 @@ class Config:
def __init__(self, filename):
self._data = {}
self._file = filename
self._load()
def _load(self):
if not self._file:
return
try:
with open(self._file, 'rb') as f:
while True:
@ -21,6 +26,8 @@ class Config:
pass
def _save(self):
if not self._file:
return
with open(self._file, 'wb') as f:
for k, v in self._data.items():
f.write(ustruct.pack('<HH', k, len(v)))

View File

@ -1,5 +1,4 @@
from micropython import const
import uio
import sys
import utime
@ -27,7 +26,7 @@ def _log(name, mlevel, msg, *args):
fmt = '%d \x1b[35m%s\x1b[0m %s \x1b[' + _leveldict[mlevel][1] + 'm' + msg + '\x1b[0m'
else:
fmt = '%d %s %s ' + msg
print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args), file=sys.stderr)
print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args))
def debug(name, msg, *args):
_log(name, DEBUG, msg, *args)
@ -42,9 +41,8 @@ def error(name, msg, *args):
_log(name, ERROR, msg, *args)
def exception(name, exc):
out = uio.StringIO()
sys.print_exception(exc, out)
_log(name, ERROR, out.getvalue())
_log(name, ERROR, 'exception:')
sys.print_exception(exc)
def critical(name, msg, *args):
_log(name, CRITICAL, msg, *args)