Skip to content

Commit

Permalink
Make it possible to use printf during bare-metal execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvan Tortorella committed Dec 5, 2023
1 parent d5eb867 commit 6d0c87f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 4 deletions.
13 changes: 13 additions & 0 deletions sw/include/cheshire_io.h
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 5 additions & 1 deletion sw/include/dif/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
9 changes: 9 additions & 0 deletions sw/include/init.h
Original file line number Diff line number Diff line change
@@ -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();
29 changes: 29 additions & 0 deletions sw/lib/cheshire_io.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
//
// 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
// .
// .
// .
};
8 changes: 7 additions & 1 deletion sw/lib/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions sw/lib/dif/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Nils Wistoff <[email protected]>
// Paul Scheffler <[email protected]>

#include "regs/cheshire.h"
#include "dif/clint.h"
#include "dif/uart.h"
#include "util.h"
#include "params.h"
Expand Down Expand Up @@ -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);
};
18 changes: 18 additions & 0 deletions sw/lib/init.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
//
// Library containing general SoC init/close functions.

#include "init.h"

void soc_init() {
// IO initialization
cheshire_init_io();
};

void soc_close() {
cheshire_close_io();
};

0 comments on commit 6d0c87f

Please sign in to comment.