From a263f836f2d322c0f4a5db4e1303e282e741c31b Mon Sep 17 00:00:00 2001 From: Jacob Pease Date: Wed, 31 Jul 2024 10:58:15 -0500 Subject: [PATCH] Added extra UART macros and functions for code readability and the ability to print decimal numbers. --- fpga/zsbl/uart.c | 68 +++++++++++++++++++++++++++++++++++------------- fpga/zsbl/uart.h | 20 +++++++++++++- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/fpga/zsbl/uart.c b/fpga/zsbl/uart.c index 1330bc1d9..d7ac9b558 100644 --- a/fpga/zsbl/uart.c +++ b/fpga/zsbl/uart.c @@ -62,10 +62,10 @@ void bin_to_hex(uint8_t inp, uint8_t res[2]) return; } -void print_uart_int(uint32_t addr) +void print_uart_hex(uint64_t addr, int n) { int i; - for (i = 3; i > -1; i--) { + for (i = n - 1; i > -1; i--) { uint8_t cur = (addr >> (i * 8)) & 0xff; uint8_t hex[2]; bin_to_hex(cur, hex); @@ -74,22 +74,54 @@ void print_uart_int(uint32_t addr) } } -void print_uart_addr(uint64_t addr) -{ - int i; - for (i = 7; i > -1; i--) { - uint8_t cur = (addr >> (i * 8)) & 0xff; - uint8_t hex[2]; - bin_to_hex(cur, hex); - write_serial(hex[0]); - write_serial(hex[1]); +void print_uart_dec(uint64_t addr) { + + // floor(log(2^64)) = 19 + char str[19] = {'\0'}; + uint8_t length = 0; + + uint64_t cur = addr; + while (cur != 0) { + char digit = bin_to_hex_table[cur % 10]; + // write_serial(digit); + str[length] = digit; + cur = cur/10; + length++; + } + + for (int i = length; i > -1; i--) { + write_serial(str[i]); } } -void print_uart_byte(uint8_t byte) -{ - uint8_t hex[2]; - bin_to_hex(byte, hex); - write_serial(hex[0]); - write_serial(hex[1]); -} +/* void print_uart_int(uint32_t addr) */ +/* { */ +/* int i; */ +/* for (i = 3; i > -1; i--) { */ +/* uint8_t cur = (addr >> (i * 8)) & 0xff; */ +/* uint8_t hex[2]; */ +/* bin_to_hex(cur, hex); */ +/* write_serial(hex[0]); */ +/* write_serial(hex[1]); */ +/* } */ +/* } */ + +/* void print_uart_addr(uint64_t addr) */ +/* { */ +/* int i; */ +/* for (i = 7; i > -1; i--) { */ +/* uint8_t cur = (addr >> (i * 8)) & 0xff; */ +/* uint8_t hex[2]; */ +/* bin_to_hex(cur, hex); */ +/* write_serial(hex[0]); */ +/* write_serial(hex[1]); */ +/* } */ +/* } */ + +/* void print_uart_byte(uint8_t byte) */ +/* { */ +/* uint8_t hex[2]; */ +/* bin_to_hex(byte, hex); */ +/* write_serial(hex[0]); */ +/* write_serial(hex[1]); */ +/* } */ diff --git a/fpga/zsbl/uart.h b/fpga/zsbl/uart.h index 18710e76b..ee399885f 100644 --- a/fpga/zsbl/uart.h +++ b/fpga/zsbl/uart.h @@ -16,11 +16,29 @@ #define UART_DLL UART_BASE + 0x00 #define UART_DLM UART_BASE + 0x01 -void init_uart(); +void init_uart(uint32_t freq, uint32_t baud); void write_reg_u8(uintptr_t addr, uint8_t value); uint8_t read_reg_u8(uintptr_t addr); int read_serial(uint8_t *res); void print_uart(const char* str); void print_uart_int(uint32_t addr); +void print_uart_dec(uint64_t addr); void print_uart_addr(uint64_t addr); +void print_uart_hex(uint64_t addr, int n); void print_uart_byte(uint8_t byte); + +#define print_uart_int(addr) print_uart_hex(addr, 4) +#define print_uart_addr(addr) print_uart_hex(addr, 8) +#define print_uart_byte(addr) print_uart_hex(addr, 1) +#define print_r7(addr) print_uart_hex(addr, 5) +#define print_r1(addr) print_uart_byte(addr) + +// Print line with numbers utility macros +#define println(msg) print_uart(msg "\r\n"); +#define println_with_dec(msg, num) print_uart(msg); print_uart_dec(num); print_uart("\r\n") +#define println_with_byte(msg, num) print_uart(msg); print_uart_byte(num); print_uart("\r\n") +#define println_with_int(msg, num) print_uart(msg); print_uart_int(num); print_uart("\r\n") +#define println_with_addr(msg, num) print_uart(msg); print_uart_addr(num); print_uart("\r\n") +#define println_with_r1(msg, num) print_uart(msg); print_r1(num); print_uart("\r\n") +#define println_with_r7(msg, num) print_uart(msg); print_r7(num); print_uart("\r\n") +