jankos/serial.c

58 lines
1.5 KiB
C

#include "serial.h"
/*
* SERIAL_LINE_COMMAND_PORT configuration
* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
* Content: | d | b | prty | s | dl |
* d = enable/disable DLAB
* b = enable/disable break control
* prty = # of parity bits to use
* s = # no of stop bits [0 is 1] and [1 is 1.5 or 2]
* dl = length of data
*
*/
void serial_set_baud_rate(const u16 com_port, const u16 divisor) {
// Activate line - ready to send higher 8 bits of data
// send the higher 8 bits first
// send the lower 8 bits next
serial_control(SERIAL_LINE_COMMAND_PORT(com_port),
SERIAL_LINE_ENABLE);
serial_control(SERIAL_DATA_PORT(com_port),
(divisor>>8) & 0x00ff);
serial_control(SERIAL_DATA_PORT(com_port),
divisor & 0x00ff);
}
// Payload described above
void serial_configure_line(const u16 line, const u8 payload) {
serial_control(SERIAL_LINE_COMMAND_PORT(line), payload);
}
u8 serial_fifo_empty(const u16 com_port) {
// If the 5th bit is set then we know that the fifo queue is empty
return serial_read_buffer(SERIAL_LINE_STATUS_PORT(com_port)) & 0x20;
}
u64 serial_write(const s8* buffer, const u64 size) {
/*
* Writes a given buffer to the com1 serial port for debugging in bochs
*/
u64 idx = 0;
serial_set_baud_rate(SERIAL_COM1_BASE, 1); // base baud rate
while(idx < size) {
if(serial_fifo_empty(SERIAL_COM1_BASE) == 0x20) {
serial_control(SERIAL_COM1_BASE, buffer[idx]);
idx++;
}
// otherwise we just wait until the queue is empty
else {
continue;
}
}
return idx;
}