From 6d0c87f30d35b5303369cb5038d6497529429eda Mon Sep 17 00:00:00 2001 From: Yvan Tortorella Date: Mon, 4 Dec 2023 08:55:22 +0100 Subject: [PATCH] Make it possible to use printf during bare-metal execution. --- sw/include/cheshire_io.h | 13 +++++++++++++ sw/include/dif/uart.h | 6 +++++- sw/include/init.h | 9 +++++++++ sw/lib/cheshire_io.c | 29 +++++++++++++++++++++++++++++ sw/lib/crt0.S | 8 +++++++- sw/lib/dif/uart.c | 16 ++++++++++++++-- sw/lib/init.c | 18 ++++++++++++++++++ 7 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 sw/include/cheshire_io.h create mode 100644 sw/include/init.h create mode 100644 sw/lib/cheshire_io.c create mode 100644 sw/lib/init.c diff --git a/sw/include/cheshire_io.h b/sw/include/cheshire_io.h new file mode 100644 index 00000000..53a85eae --- /dev/null +++ b/sw/include/cheshire_io.h @@ -0,0 +1,13 @@ +// Copyright 2022 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 + +#include "regs/cheshire.h" +#include "dif/clint.h" +#include "dif/uart.h" +#include "util.h" +#include "params.h" + +void cheshire_init_io(); + +void cheshire_close_io(); diff --git a/sw/include/dif/uart.h b/sw/include/dif/uart.h index 134ec814..9f4c6e95 100644 --- a/sw/include/dif/uart.h +++ b/sw/include/dif/uart.h @@ -42,6 +42,10 @@ uint8_t uart_read(void *uart_base); void uart_read_str(void *uart_base, void *dst, uint64_t len); // Default UART provides console -void _putchar(char byte); +void uart_open(); + +void _putchar(char character); char _getchar(); + +void uart_close(); diff --git a/sw/include/init.h b/sw/include/init.h new file mode 100644 index 00000000..9b471ec5 --- /dev/null +++ b/sw/include/init.h @@ -0,0 +1,9 @@ +// Copyright 2022 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 + +#include "cheshire_io.h" + +void soc_init(); + +void soc_close(); diff --git a/sw/lib/cheshire_io.c b/sw/lib/cheshire_io.c new file mode 100644 index 00000000..319701b2 --- /dev/null +++ b/sw/lib/cheshire_io.c @@ -0,0 +1,29 @@ +// Copyright 2022 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// Yvan Tortorella +// +// Library containing IO init/close functions. + +#include "cheshire_io.h" + +void cheshire_init_io() { + // Initialize UART first + uart_open(); + + // Initialize other IOs + // . + // . + // . +}; + +void cheshire_close_io() { + // Close UART first + uart_close(); + + // Close other IOs + // . + // . + // . +}; diff --git a/sw/lib/crt0.S b/sw/lib/crt0.S index ebf372ad..c16dc390 100644 --- a/sw/lib/crt0.S +++ b/sw/lib/crt0.S @@ -111,8 +111,12 @@ _fp_init: // Set FS state to "Clean" csrrc x0, mstatus, t1 - // Full fence, then jump to main + // Full fence, then init SoC fence + + // Init SoC, than jump to main + jal x1, soc_init + call main // If main returns, we end up here @@ -127,6 +131,8 @@ _exit: ori t0, t0, 1 la t1, __base_regs sw t0, 8(t1) // regs.SCRATCH[2] + // Flush everything before exiting. + jal x1, soc_close // Hand over to whatever called us, passing return ret diff --git a/sw/lib/dif/uart.c b/sw/lib/dif/uart.c index 09cef30a..73cbef79 100644 --- a/sw/lib/dif/uart.c +++ b/sw/lib/dif/uart.c @@ -5,6 +5,8 @@ // Nils Wistoff // Paul Scheffler +#include "regs/cheshire.h" +#include "dif/clint.h" #include "dif/uart.h" #include "util.h" #include "params.h" @@ -64,10 +66,20 @@ void uart_read_str(void *uart_base, void *dst, uint64_t len) { } // Default UART provides console -void _putchar(char byte) { - uart_write(&__base_uart, byte); +void uart_open(){ + uint32_t rtc_freq = *reg32(&__base_regs, CHESHIRE_RTC_FREQ_REG_OFFSET); + uint64_t reset_freq = clint_get_core_freq(rtc_freq, 2500); + uart_init(&__base_uart, reset_freq, 115200); +}; + +void _putchar(char character) { + uart_write(&__base_uart, character); }; char _getchar() { return uart_read(&__base_uart); }; + +void uart_close() { + uart_write_flush(&__base_uart); +}; diff --git a/sw/lib/init.c b/sw/lib/init.c new file mode 100644 index 00000000..07ba1f1b --- /dev/null +++ b/sw/lib/init.c @@ -0,0 +1,18 @@ +// Copyright 2022 ETH Zurich and University of Bologna. +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 +// +// Yvan Tortorella +// +// Library containing general SoC init/close functions. + +#include "init.h" + +void soc_init() { + // IO initialization + cheshire_init_io(); +}; + +void soc_close() { + cheshire_close_io(); +};