From 416676120b539d556c8d741667772a6d98437eb8 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 21 Mar 2017 01:41:49 +0100 Subject: [PATCH] extract common stuff into trezorhal/common.[ch] --- Makefile.bootloader | 1 + Makefile.firmware | 2 + Makefile.loader | 1 + micropython/bootloader/main.c | 42 ++++--------------- .../extmod/modtrezorui/display-stmhal.h | 4 +- micropython/extmod/modtrezorui/display-unix.h | 3 +- micropython/extmod/modtrezorui/display.h | 2 +- micropython/firmware/main.c | 16 +++---- micropython/loader/main.c | 19 +-------- micropython/trezorhal/common.c | 25 +++++++++++ micropython/trezorhal/common.h | 10 +++++ 11 files changed, 61 insertions(+), 64 deletions(-) create mode 100644 micropython/trezorhal/common.c create mode 100644 micropython/trezorhal/common.h diff --git a/Makefile.bootloader b/Makefile.bootloader index 4812231e0..86ae89a48 100644 --- a/Makefile.bootloader +++ b/Makefile.bootloader @@ -64,6 +64,7 @@ OBJ_FW += $(addprefix $(BUILD_FW)/, \ bootloader/main.o \ extmod/modtrezorui/display.o \ extmod/modtrezorui/font_bitmap.o \ + trezorhal/common.o \ trezorhal/sdcard.o \ trezorhal/stm32_it.o \ trezorhal/stm32_system.o \ diff --git a/Makefile.firmware b/Makefile.firmware index 04e48cefd..121513623 100644 --- a/Makefile.firmware +++ b/Makefile.firmware @@ -315,6 +315,7 @@ OBJ_MP += $(addprefix $(BUILD_MP)/,\ OBJ_FW += $(addprefix $(BUILD_FW)/, \ firmware/main.o \ firmware/mphalport.o \ + trezorhal/common.o \ trezorhal/flash.o \ trezorhal/rng.o \ trezorhal/sdcard.o \ @@ -341,6 +342,7 @@ SRC_MOD = $(patsubst $(BUILD_FW)%.o, $(SRCDIR_FW)%.c, $(OBJ_MOD)) CROSS_COMPILE = arm-none-eabi- INC += -I. +INC += -I$(SRCDIR_FW)/extmod/modtrezorui INC += -I$(SRCDIR_FW)/firmware INC += -I$(SRCDIR_FW)/trezorhal INC += -I$(SRCDIR_FW)/trezorhal/hal diff --git a/Makefile.loader b/Makefile.loader index 74c54c729..73feefbed 100644 --- a/Makefile.loader +++ b/Makefile.loader @@ -63,6 +63,7 @@ OBJ_FW += $(addprefix $(BUILD_FW)/, \ loader/main.o \ extmod/modtrezorui/display.o \ extmod/modtrezorui/font_bitmap.o \ + trezorhal/common.o \ trezorhal/stm32_it.o \ trezorhal/stm32_system.o \ trezorhal/hal/stm32f4xx_hal_sram.o \ diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index d3b50a11e..9d38fb153 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -3,46 +3,22 @@ #include #include "crypto.h" + +#include "common.h" #include "display.h" #include "sdcard.h" #define STAGE2_START 0x08010000 -#define BOOTLOADER_PRINT(X) do { display_print(X, -1); display_print_out(0xFFFF, 0x001F); } while(0) -#define BOOTLOADER_PRINTLN(X) do { display_print(X "\n", -1); display_print_out(0xFFFF, 0x001F); } while(0) +#define BOOTLOADER_FGCOLOR 0xFFFF +#define BOOTLOADER_BGCOLOR 0x0000 -void SystemClock_Config(void); - -void __attribute__((noreturn)) nlr_jump_fail(void *val) { - for (;;) {} -} - -void __attribute__((noreturn)) __fatal_error(const char *msg) { - for (volatile uint32_t delay = 0; delay < 10000000; delay++) { - } - display_print("FATAL ERROR:\n", -1); - display_print(msg, -1); - display_print("\n", -1); - display_print_out(0xFFFF, 0x001F); - for (;;) { - __WFI(); - } -} +#define BOOTLOADER_PRINT(X) do { display_print(X, -1); display_print_out(BOOTLOADER_FGCOLOR, BOOTLOADER_BGCOLOR); } while(0) +#define BOOTLOADER_PRINTLN(X) do { display_print(X "\n", -1); display_print_out(BOOTLOADER_FGCOLOR, BOOTLOADER_BGCOLOR); } while(0) void mp_hal_stdout_tx_str(const char *str) { } -void halt(void) -{ - BOOTLOADER_PRINTLN("HALT!"); - for (;;) { - display_backlight(255); - HAL_Delay(950); - display_backlight(0); - HAL_Delay(50); - } -} - void periph_init(void) { HAL_Init(); @@ -165,7 +141,7 @@ int main(void) if (check_sdcard()) { if (!copy_sdcard()) { - halt(); + __fatal_error("halt"); } } @@ -176,7 +152,7 @@ int main(void) BOOTLOADER_PRINTLN("valid stage 2 signature"); BOOTLOADER_PRINTLN("JUMP!"); // TODO: jump to second stage - halt(); + __fatal_error("halt"); } else { BOOTLOADER_PRINTLN("invalid stage 2 signature"); } @@ -184,7 +160,7 @@ int main(void) BOOTLOADER_PRINTLN("invalid stage 2 header"); } - halt(); + __fatal_error("halt"); return 0; } diff --git a/micropython/extmod/modtrezorui/display-stmhal.h b/micropython/extmod/modtrezorui/display-stmhal.h index e280d8f1f..e0a304183 100644 --- a/micropython/extmod/modtrezorui/display-stmhal.h +++ b/micropython/extmod/modtrezorui/display-stmhal.h @@ -118,7 +118,7 @@ void display_set_backlight(int val) __HAL_TIM_SetCompare(&TIM1_Handle, TIM_CHANNEL_1, LED_PWM_TIM_PERIOD * val / 255); } -void display_init(void) { +int display_init(void) { // init peripherials __GPIOE_CLK_ENABLE(); __TIM1_CLK_ENABLE(); @@ -273,6 +273,8 @@ void display_init(void) { // clear buffer display_clear(); display_unsleep(); + + return 0; } static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { diff --git a/micropython/extmod/modtrezorui/display-unix.h b/micropython/extmod/modtrezorui/display-unix.h index 49fc5cbfa..7d909d0c0 100644 --- a/micropython/extmod/modtrezorui/display-unix.h +++ b/micropython/extmod/modtrezorui/display-unix.h @@ -35,7 +35,7 @@ void DATA(uint8_t x) { #define DATA(X) (void)(X); #endif -void display_init(void) +int display_init(void) { #ifndef TREZOR_NOUI if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -59,6 +59,7 @@ void display_init(void) SDL_SetTextureBlendMode(TEXTURE, SDL_BLENDMODE_NONE); SDL_SetTextureAlphaMod(TEXTURE, 0); #endif + return 0; } static void display_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) diff --git a/micropython/extmod/modtrezorui/display.h b/micropython/extmod/modtrezorui/display.h index 866c27b19..444463beb 100644 --- a/micropython/extmod/modtrezorui/display.h +++ b/micropython/extmod/modtrezorui/display.h @@ -21,7 +21,7 @@ // provided by port -void display_init(void); +int display_init(void); void display_refresh(void); void display_save(const char *filename); diff --git a/micropython/firmware/main.c b/micropython/firmware/main.c index 0132f6837..69bd3204d 100644 --- a/micropython/firmware/main.c +++ b/micropython/firmware/main.c @@ -13,22 +13,14 @@ #include "gccollect.h" #include "pendsv.h" +#include "common.h" +#include "display.h" #include "flash.h" #include "rng.h" #include "sdcard.h" #include "touch.h" #include "usb.h" -void SystemClock_Config(void); - -void NORETURN nlr_jump_fail(void *val) { - for (;;) {} -} - -void NORETURN __fatal_error(const char *msg) { - for (;;) {} -} - int main(void) { // STM32F4xx HAL library initialization: @@ -61,6 +53,10 @@ int main(void) { pendsv_init(); + if (0 != display_init()) { + __fatal_error("display_init failed"); + } + if (0 != flash_init()) { __fatal_error("flash_init failed"); } diff --git a/micropython/loader/main.c b/micropython/loader/main.c index 85bfe2e3f..a69f55bc2 100644 --- a/micropython/loader/main.c +++ b/micropython/loader/main.c @@ -1,25 +1,8 @@ #include STM32_HAL_H +#include "common.h" #include "display.h" -void SystemClock_Config(void); - -void __attribute__((noreturn)) nlr_jump_fail(void *val) { - for (;;) {} -} - -void __attribute__((noreturn)) __fatal_error(const char *msg) { - for (volatile uint32_t delay = 0; delay < 10000000; delay++) { - } - display_print("FATAL ERROR:\n", -1); - display_print(msg, -1); - display_print("\n", -1); - display_print_out(0xFFFF, 0x001F); - for (;;) { - __WFI(); - } -} - void mp_hal_stdout_tx_str(const char *str) { } diff --git a/micropython/trezorhal/common.c b/micropython/trezorhal/common.c new file mode 100644 index 000000000..3263e966f --- /dev/null +++ b/micropython/trezorhal/common.c @@ -0,0 +1,25 @@ +#include STM32_HAL_H + +#include "display.h" + +#define FATAL_FGCOLOR 0xFFFF +#define FATAL_BGCOLOR 0x001F + +void __attribute__((noreturn)) __fatal_error(const char *msg) { + for (volatile uint32_t delay = 0; delay < 10000000; delay++) { + } + display_print("FATAL ERROR:\n", -1); + display_print(msg, -1); + display_print("\n", -1); + display_print_out(FATAL_FGCOLOR, FATAL_BGCOLOR); + for (;;) { + display_backlight(255); + HAL_Delay(950); + display_backlight(128); + HAL_Delay(50); + } +} + +void __attribute__((noreturn)) nlr_jump_fail(void *val) { + __fatal_error("uncaught exception"); +} diff --git a/micropython/trezorhal/common.h b/micropython/trezorhal/common.h new file mode 100644 index 000000000..e742bbd24 --- /dev/null +++ b/micropython/trezorhal/common.h @@ -0,0 +1,10 @@ +#ifndef __TREZORHAL_COMMON_H__ +#define __TREZORHAL_COMMON_H__ + +void SystemClock_Config(void); // defined in stm32_system.c + +void __attribute__((noreturn)) nlr_jump_fail(void *val); + +void __attribute__((noreturn)) __fatal_error(const char *msg); + +#endif