LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
serial.c
Go to the documentation of this file.
1
9#include <stddef.h>
10#include <stdbool.h>
11#include <stdint.h>
12#include <drivers/serial.h>
13#include <arch/x86_64/io.h>
14
15bool serial_enabled = false;
17{
18 return serial_enabled;
19}
20
21int serial_init(void) {
22 if (serial_enabled) {
23 return 1;
24 }
25
26 outb(COM1 + 1, 0x00); // Disable UART Interrupts
27 outb(COM1 + 3, 0x80); // Set DLAB
28 outb(COM1, SERIAL_BAUD_DIVISOR & 0xFF); // Set speed by setting divisor (low byte)
29 outb(COM1 + 1, SERIAL_BAUD_DIVISOR >> 8); // Divisor high byte
30 outb(COM1 + 3, 0x03); // Clear DLAB, and 8 data bits, no parity, 1 stop bit ("8N1")
31 outb(COM1 + 2, 0xC7); // Enable & clear FIFOs with 14 byte threshold
32 outb(COM1 + 4, 0x0B); // Set modem control bits
33
34 // Self test
35 outb(COM1 + 4, 0x1E); // Set loopback mode
36 outb(COM1, 0xAE); // Send test byte
37
38 if (inb(COM1) != 0xAE) {
39 // Failed
40 serial_enabled = false;
41 return 1;
42 }
43
44 serial_enabled = true;
45 outb(COM1 + 4, 0x0F); // Return to normal mode
46 return 0;
47
48}
49
50void serial_puts(char *str) {
51 int index = 0; // Holds the index of the string character
52
53 while (true) {
54 if (str[index] == '\0') {
55 break; // If the character is \0 terminator, stop the loop
56 }
57
58 serial_putc(str[index]); // Output character to the correct serial port
59 index++; // Increase index
60 }
61}
62
63void serial_putc(char c) {
64 if (!serial_enabled) return;
65
66 if (c == '\n') {
67 serial_putc('\r'); // If the character is a new line, also return the carriage
68 }
69
70 while ((inb(COM1 + 5) & 0x20) == 0) { // Wait until the serial port is ready to send data
71 continue;
72 }
73
74 outb(COM1, c);
75}
76
77void serial_print_int(uint64_t value) {
78 uint64_t next = value; // Holds the next value to calculate
79 uint64_t index = 0; // Holds how many characters have been calculated
80
81 // Get number length
82 while (next != 0) { // Loop until the next value is 0, so the number is done
83 next /= 10; // move the decimal point over by one to the left
84 index++; // continue, increase length counter
85 }
86
87 // If the value started as 0, set the length to 1
88 if (value == 0)
89 index = 1;
90
91 uint64_t MAX_LENGTH = index; // Hold the length of the number
92 char chars[MAX_LENGTH]; // Hold the number (backwards) as chars
93
94 next = value; // Reset next value
95 index = 0; // Reset the index
96
97 while (next != 0) {
98 if (index >= MAX_LENGTH) // Check if the index passed the length
99 {
100 break; // Stop the loop if so
101 }
102 chars[index] = ((next % 10) + '0'); // add the remainder of next/10, + '0' to turn it into the corresponding char to the chars array
103
104 next /= 10; // move the decimal, using lossy division
105 index++;
106 }
107
108 if (value == 0) { // If the number started as one
109 chars[0] = '0'; // set the character in chars to 0
110 index = 1; // set index to 1
111 }
112
113 for (int i = index - 1; i >= 0; i--) { // For each char in the chars array
114 serial_putc(chars[i]); // Print the char
115 }
116}
117
118void serial_print_hex(uint64_t address, uint64_t digit_count) {
119 serial_putc('0'); // Print "0x"
120 serial_putc('x');
121
122 for (int i = digit_count; i > 0; i--) // For each digit (byte)...
123 {
124 address = address << 4 | (address >> (((digit_count / 2) * 8) - 4)); // Shift bytes left by 4
125 int digit = address & 0xF; // address AND 0xF
126
127 char c;
128
129 // Get correct digit
130 if (digit < 10) {
131 c = '0' + digit;
132 } else {
133 c = 'A' + (digit - 10);
134 }
135
136 serial_putc(c); // Output character to serial port
137 }
138}
Provides utilities for IO operations with the hardware.
uint8_t inb(uint16_t port)
Inputs a byte from the specified port.
Definition io.h:31
void outb(uint16_t port, uint8_t value)
Outputs a byte to the specified port.
Definition io.h:22
void serial_print_hex(uint64_t address, uint64_t digit_count)
Print a hex number to serial output.
Definition serial.c:118
void serial_puts(char *str)
Prints a string to the serial port.
Definition serial.c:50
int serial_init(void)
Initializes the serial port & output.
Definition serial.c:21
bool get_serial_status(void)
Returns the status of the serial output (enabled or disabled).
Definition serial.c:16
void serial_putc(char c)
Prints a character to serial port.
Definition serial.c:63
void serial_print_int(uint64_t value)
Prints a number to the serial port.
Definition serial.c:77
Serial utilities and configuration.