From d29baa337e495c724b56cbb3dd6b76524de7a3f9 Mon Sep 17 00:00:00 2001 From: misson20000 Date: Wed, 28 Oct 2020 00:29:52 -0700 Subject: [PATCH] fusee-primary: deduplicate display initialization code --- fusee/fusee-primary/src/lib/log.c | 35 +++++++++++++++++++++++++++++++ fusee/fusee-primary/src/lib/log.h | 4 ++++ fusee/fusee-primary/src/main.c | 31 +++------------------------ fusee/fusee-primary/src/utils.c | 19 ++--------------- 4 files changed, 44 insertions(+), 45 deletions(-) diff --git a/fusee/fusee-primary/src/lib/log.c b/fusee/fusee-primary/src/lib/log.c index 26e0cb8a3..c4bcd5cb5 100644 --- a/fusee/fusee-primary/src/lib/log.c +++ b/fusee/fusee-primary/src/lib/log.c @@ -17,6 +17,7 @@ #include "log.h" #include "../display/video_fb.h" +#include "../di.h" #include "../uart.h" #include "vsprintf.h" @@ -31,6 +32,40 @@ ScreenLogLevel log_get_log_level() { return g_screen_log_level; } +static uint32_t g_framebuffer[1280*768] __attribute__((section(".framebuffer"))) = {0}; +static int g_log_display_reference_count = 0; + +void log_setup_display() { + if (g_log_display_reference_count++ == 0) { + /* Zero-fill the framebuffer and register it as printk provider. */ + video_init(g_framebuffer); + + /* Initialize the display. */ + display_init(); + + /* Set the framebuffer. */ + display_init_framebuffer(g_framebuffer); + + /* Turn on the backlight after initializing the lfb */ + /* to avoid flickering. */ + display_backlight(true); + } +} + +void log_cleanup_display() { + if (--g_log_display_reference_count == 0) { + /* Turn off the backlight. */ + display_backlight(false); + + /* Terminate the display. */ + display_end(); + } +} + +void *log_get_display_framebuffer() { + return g_framebuffer; +} + void log_to_uart(const char *message) { uart_send(UART_B, message, strlen(message)); } diff --git a/fusee/fusee-primary/src/lib/log.h b/fusee/fusee-primary/src/lib/log.h index eea293e33..fc830c0d2 100644 --- a/fusee/fusee-primary/src/lib/log.h +++ b/fusee/fusee-primary/src/lib/log.h @@ -36,6 +36,10 @@ extern ScreenLogLevel g_screen_log_level; void log_set_log_level(ScreenLogLevel screen_log_level); ScreenLogLevel log_get_log_level(); +void log_setup_display(); +void log_cleanup_display(); +void *log_get_display_framebuffer(); + void vprint(ScreenLogLevel screen_log_level, const char *fmt, va_list args); void print(ScreenLogLevel screen_log_level, const char* fmt, ...); diff --git a/fusee/fusee-primary/src/main.c b/fusee/fusee-primary/src/main.c index ee983c66c..d1ec3df03 100644 --- a/fusee/fusee-primary/src/main.c +++ b/fusee/fusee-primary/src/main.c @@ -18,7 +18,6 @@ #include "exception_handlers.h" #include "panic.h" #include "hwinit.h" -#include "di.h" #include "timers.h" #include "fs_utils.h" #include "stage2.h" @@ -31,7 +30,6 @@ extern void (*__program_exit_callback)(int rc); -static uint32_t g_framebuffer[1280*768] __attribute__((section(".framebuffer"))) = {0}; static char g_bct0_buffer[BCTO_MAX_SIZE] __attribute__((section(".dram"))) = {0}; #define DEFAULT_BCT0 \ @@ -71,29 +69,6 @@ static const char *load_config(void) { return bct0; } -static void setup_display(void) { - /* Zero-fill the framebuffer and register it as printk provider. */ - video_init(g_framebuffer); - - /* Initialize the display. */ - display_init(); - - /* Set the framebuffer. */ - display_init_framebuffer(g_framebuffer); - - /* Turn on the backlight after initializing the lfb */ - /* to avoid flickering. */ - display_backlight(true); -} - -static void cleanup_display(void) { - /* Turn off the backlight. */ - display_backlight(false); - - /* Terminate the display. */ - display_end(); -} - static void setup_env(void) { /* Initialize hardware. */ nx_hwinit(); @@ -140,12 +115,12 @@ int main(void) { if (bct0.log_level != SCREEN_LOG_LEVEL_NONE) { /* Initialize the display for debugging. */ - setup_display(); + log_setup_display(); } /* Say hello. */ print(SCREEN_LOG_LEVEL_DEBUG | SCREEN_LOG_LEVEL_NO_PREFIX, "Welcome to Atmosph\xe8re Fus\xe9" "e!\n"); - print(SCREEN_LOG_LEVEL_DEBUG, "Using color linear framebuffer at 0x%p!\n", g_framebuffer); + print(SCREEN_LOG_LEVEL_DEBUG, "Using color linear framebuffer at 0x%p!\n", log_get_display_framebuffer()); /* Run the MTC binary. */ if (!stage2_run_mtc(&bct0)) { @@ -174,7 +149,7 @@ int main(void) { mdelay(1000); /* Terminate the display for debugging. */ - cleanup_display(); + log_cleanup_display(); } /* Finally, after the cleanup routines (__libc_fini_array, etc.) are called, jump to Stage2. */ diff --git a/fusee/fusee-primary/src/utils.c b/fusee/fusee-primary/src/utils.c index 51033eed7..7e4914233 100644 --- a/fusee/fusee-primary/src/utils.c +++ b/fusee/fusee-primary/src/utils.c @@ -17,7 +17,6 @@ #include #include #include "utils.h" -#include "di.h" #include "se.h" #include "fuse.h" #include "pmc.h" @@ -27,7 +26,6 @@ #include "btn.h" #include "lib/log.h" #include "lib/vsprintf.h" -#include "display/video_fb.h" #include @@ -99,22 +97,9 @@ __attribute__ ((noreturn)) void generic_panic(void) { } __attribute__((noreturn)) void fatal_error(const char *fmt, ...) { - /* Forcefully initialize the screen if logging is disabled. */ - if (log_get_log_level() == SCREEN_LOG_LEVEL_NONE) { - /* Zero-fill the framebuffer and register it as printk provider. */ - video_init((void *)0xC0000000); + /* Forcefully initialize the screen if not already initialized. */ + log_setup_display(); - /* Initialize the display. */ - display_init(); - - /* Set the framebuffer. */ - display_init_framebuffer((void *)0xC0000000); - - /* Turn on the backlight after initializing the lfb */ - /* to avoid flickering. */ - display_backlight(true); - } - /* Override the global logging level. */ log_set_log_level(SCREEN_LOG_LEVEL_ERROR);