Added extra UART macros and functions for code readability and the ability to print decimal numbers.

This commit is contained in:
Jacob Pease 2024-07-31 10:58:15 -05:00
parent 3975f60299
commit a263f836f2
2 changed files with 69 additions and 19 deletions

View File

@ -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]); */
/* } */

View File

@ -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")