From a9d16aa7fa180d6803c76314ec3056956c7bcb56 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 23 Mar 2017 16:22:58 +0100 Subject: [PATCH] firmware: extract periph_init into common.c --- micropython/bootloader/main.c | 24 ++++++------------------ micropython/firmware/main.c | 28 +--------------------------- micropython/loader/main.c | 16 ++-------------- micropython/trezorhal/common.c | 33 +++++++++++++++++++++++++++++++++ micropython/trezorhal/common.h | 2 +- 5 files changed, 43 insertions(+), 60 deletions(-) diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index 7842caec1a..f6e4732bd7 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -20,24 +20,6 @@ void pendsv_isr_handler(void) { __fatal_error("pendsv"); } -void periph_init(void) -{ - HAL_Init(); - - SystemClock_Config(); - - __GPIOA_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); - __GPIOC_CLK_ENABLE(); - __GPIOD_CLK_ENABLE(); - - sdcard_init(); - - display_init(); - display_clear(); - display_backlight(255); -} - bool check_sdcard(void) { BOOTLOADER_PRINTLN("checking for SD card"); @@ -136,6 +118,12 @@ int main(void) { periph_init(); + sdcard_init(); + + display_init(); + display_clear(); + display_backlight(255); + BOOTLOADER_PRINTLN("TREZOR Bootloader"); BOOTLOADER_PRINTLN("================="); BOOTLOADER_PRINTLN("starting bootloader"); diff --git a/micropython/firmware/main.c b/micropython/firmware/main.c index 69bd3204d2..ceee2f0430 100644 --- a/micropython/firmware/main.c +++ b/micropython/firmware/main.c @@ -23,33 +23,7 @@ int main(void) { - // STM32F4xx HAL library initialization: - // - configure the Flash prefetch, instruction and data caches - // - configure the Systick to generate an interrupt each 1 msec - // - set NVIC Group Priority to 4 - // - global MSP (MCU Support Package) initialization - HAL_Init(); - - // Set the system clock to be HSE - SystemClock_Config(); - - // Enable GPIO clocks - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOD_CLK_ENABLE(); - - // Enable the CCM RAM - __HAL_RCC_CCMDATARAMEN_CLK_ENABLE(); - - // Clear the reset flags - PWR->CR |= PWR_CR_CSBF; - RCC->CSR |= RCC_CSR_RMVF; - - // Enable CPU ticks - CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // Enable DWT - DWT->CYCCNT = 0; // Reset Cycle Count Register - DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // Enable Cycle Count Register + periph_init(); pendsv_init(); diff --git a/micropython/loader/main.c b/micropython/loader/main.c index f515ffccf8..0141e20f3e 100644 --- a/micropython/loader/main.c +++ b/micropython/loader/main.c @@ -15,25 +15,13 @@ void pendsv_isr_handler(void) { __fatal_error("pendsv"); } -void periph_init(void) +int main(void) { - HAL_Init(); - - SystemClock_Config(); - - __GPIOA_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); - __GPIOC_CLK_ENABLE(); - __GPIOD_CLK_ENABLE(); + periph_init(); display_init(); display_clear(); display_backlight(255); -} - -int main(void) -{ - periph_init(); __fatal_error("end reached"); diff --git a/micropython/trezorhal/common.c b/micropython/trezorhal/common.c index e25114e71d..ec9cbd9c19 100644 --- a/micropython/trezorhal/common.c +++ b/micropython/trezorhal/common.c @@ -23,3 +23,36 @@ void __attribute__((noreturn)) __fatal_error(const char *msg) { void __attribute__((noreturn)) nlr_jump_fail(void *val) { __fatal_error("uncaught exception"); } + +extern void SystemClock_Config(void); + +void periph_init(void) { + + // STM32F4xx HAL library initialization: + // - configure the Flash prefetch, instruction and data caches + // - configure the Systick to generate an interrupt each 1 msec + // - set NVIC Group Priority to 4 + // - global MSP (MCU Support Package) initialization + HAL_Init(); + + // Set the system clock to be HSE + SystemClock_Config(); + + // Enable GPIO clocks + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); + + // Enable the CCM RAM + __HAL_RCC_CCMDATARAMEN_CLK_ENABLE(); + + // Clear the reset flags + PWR->CR |= PWR_CR_CSBF; + RCC->CSR |= RCC_CSR_RMVF; + + // Enable CPU ticks + CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // Enable DWT + DWT->CYCCNT = 0; // Reset Cycle Count Register + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // Enable Cycle Count Register +} diff --git a/micropython/trezorhal/common.h b/micropython/trezorhal/common.h index e742bbd244..8df23bb4fa 100644 --- a/micropython/trezorhal/common.h +++ b/micropython/trezorhal/common.h @@ -1,7 +1,7 @@ #ifndef __TREZORHAL_COMMON_H__ #define __TREZORHAL_COMMON_H__ -void SystemClock_Config(void); // defined in stm32_system.c +void periph_init(void); void __attribute__((noreturn)) nlr_jump_fail(void *val);