summaryrefslogtreecommitdiff
path: root/src/drivers/uart.cc
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2025-12-31 18:00:55 +0800
committerRichard Qin <richard.qin.x@gmail.com>2025-12-31 18:00:55 +0800
commitdbba25aa24f86c1f8c3a0589c82985840adc1b6b (patch)
tree11d1d7ee552030d953db9f911d5af6e079291c47 /src/drivers/uart.cc
parent7759eef8cdac79028dd5b468bd9ce0cf1c12b162 (diff)
downloadlume-dbba25aa24f86c1f8c3a0589c82985840adc1b6b.tar.gz
lume-dbba25aa24f86c1f8c3a0589c82985840adc1b6b.tar.xz
dev:Refactor Memory Management and Process Management
Diffstat (limited to 'src/drivers/uart.cc')
-rw-r--r--src/drivers/uart.cc71
1 files changed, 48 insertions, 23 deletions
diff --git a/src/drivers/uart.cc b/src/drivers/uart.cc
index 367f442..fe051f8 100644
--- a/src/drivers/uart.cc
+++ b/src/drivers/uart.cc
@@ -14,7 +14,9 @@
#define UART_LSR_RX_READY 0x01
#define UART_LSR_TX_EMPTY 0x20
-namespace Drivers{
+namespace Drivers
+{
+
inline volatile uint8_t &reg(uint32_t offset)
{
return *reinterpret_cast<volatile uint8_t *>(g_uart_base + offset);
@@ -22,36 +24,35 @@ namespace Drivers{
void uart_init()
{
-
if (g_uart_base == 0)
return;
- reg(UART_IER) = 0x00;
-
- reg(UART_LCR) = 0x80;
- reg(0x00) = 0x03;
- reg(0x01) = 0x00;
-
- reg(UART_LCR) = 0x03;
-
+ reg(UART_IER) = 0x00; // Disable interrupt
+ reg(UART_LCR) = 0x80; // Set baud rate (LATCH bit)
+ reg(0x00) = 0x03; // LSB
+ reg(0x01) = 0x00; // MSB
+ reg(UART_LCR) = 0x03; // 8 bits, no parity, one stop bit
reg(UART_FCR) = 0x07; // Enable FIFO, clear TX/RX queues
}
void uart_putc(char c)
{
+ if (g_uart_base == 0)
+ return;
+
if (c == '\n')
{
uart_putc('\r');
}
+ // Waiting for the send buffer to be empty
while ((reg(UART_LSR) & UART_LSR_TX_EMPTY) == 0)
- {
-
- }
+ ;
reg(UART_THR) = c;
}
+
void uart_puts(const char *s)
{
while (*s)
@@ -60,18 +61,42 @@ namespace Drivers{
}
}
- void print_hex(uint64_t val)
+ void uart_put_int(int value)
+ {
+ if (value == 0)
+ {
+ uart_putc('0');
+ return;
+ }
+
+ if (value < 0)
+ {
+ uart_putc('-');
+ value = -value;
+ }
+
+ char buffer[16];
+ int i = 0;
+ while (value > 0)
+ {
+ buffer[i++] = (value % 10) + '0';
+ value /= 10;
+ }
+
+ while (i > 0)
+ {
+ uart_putc(buffer[--i]);
+ }
+ }
+
+ void print_hex(uint64 value)
{
- char buf[20];
- const char *digits = "0123456789ABCDEF";
- buf[0] = '0';
- buf[1] = 'x';
- for (int i = 0; i < 16; i++)
+ uart_puts("0x");
+ char hex[] = "0123456789abcdef";
+ for (int i = 15; i >= 0; i--)
{
- buf[17 - i] = digits[val & 0xF];
- val >>= 4;
+ int nibble = (value >> (i * 4)) & 0xF;
+ uart_putc(hex[nibble]);
}
- buf[18] = 0;
- Drivers::uart_puts(buf);
}
} \ No newline at end of file