2021-11-03 19:03:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-16 12:57:15 +00:00
|
|
|
#include "tty/kterm.h"
|
2021-12-27 14:26:32 +00:00
|
|
|
#include "drivers/IO/io.h"
|
2021-11-03 19:03:38 +00:00
|
|
|
#define PORT 0x3f8
|
|
|
|
static int init_serial() {
|
2021-12-29 15:15:18 +00:00
|
|
|
|
|
|
|
#ifdef __VERBOSE__
|
|
|
|
printf("Init Serial\n");
|
|
|
|
#endif
|
|
|
|
|
2021-11-03 19:03:38 +00:00
|
|
|
outb(PORT + 1, 0x00); // Disable all interrupts
|
|
|
|
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
|
|
|
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
|
|
|
outb(PORT + 1, 0x00); // (hi byte)
|
|
|
|
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
|
|
|
|
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
|
|
|
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
|
|
|
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
|
|
|
|
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
|
|
|
|
|
|
|
|
// Check if serial is faulty (i.e: not same byte as sent)
|
|
|
|
if(inb(PORT + 0) != 0xAE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If serial is not faulty set it in normal operation mode
|
|
|
|
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
|
|
|
|
outb(PORT + 4, 0x0F);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:08:18 +00:00
|
|
|
inline int is_transmit_empty() {
|
2021-11-03 19:03:38 +00:00
|
|
|
return inb(PORT + 5) & 0x20;
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:08:18 +00:00
|
|
|
inline void write_serial(char a) {
|
2021-11-03 19:03:38 +00:00
|
|
|
while (is_transmit_empty() == 0);
|
|
|
|
|
|
|
|
outb(PORT,a);
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:08:18 +00:00
|
|
|
inline int serial_received() {
|
2021-11-03 19:03:38 +00:00
|
|
|
return inb(PORT + 5) & 1;
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:08:18 +00:00
|
|
|
inline char read_serial() {
|
2021-11-03 19:03:38 +00:00
|
|
|
while (serial_received() == 0);
|
|
|
|
|
|
|
|
return inb(PORT);
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:08:18 +00:00
|
|
|
inline void print_serial(const char* string ){
|
2021-11-03 19:03:38 +00:00
|
|
|
for(size_t i = 0; i < strlen(string); i ++){
|
|
|
|
write_serial(string[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:08:18 +00:00
|
|
|
inline void test_serial(){
|
2021-11-03 19:03:38 +00:00
|
|
|
/** Serial test **/
|
|
|
|
kterm_writestring("Writing to COM1 serial port:");
|
|
|
|
init_serial();
|
|
|
|
write_serial('A');
|
|
|
|
write_serial('B');
|
|
|
|
write_serial('C');
|
|
|
|
write_serial('D');
|
|
|
|
write_serial('E');
|
|
|
|
|
|
|
|
char Character_received = read_serial();
|
|
|
|
kterm_writestring("\n");
|
|
|
|
kterm_writestring("received from COM 1: \n");
|
|
|
|
kterm_put(Character_received);
|
|
|
|
|
|
|
|
kterm_writestring("\n");
|
|
|
|
}
|