serial.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <kernel/hw/serial.h>
  2. #include <asm/port_io.h>
  3. int32_t init_serial_port(port_id_t port)
  4. {
  5. // taken from osdev.org
  6. asm_outb(port + 1, 0x00); // Disable all interrupts
  7. asm_outb(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
  8. // TODO: set baud rate
  9. asm_outb(port + 0, 0x00); // Set divisor to 0 -3- (lo byte) 115200 -38400- baud
  10. asm_outb(port + 1, 0x00); // (hi byte)
  11. asm_outb(port + 3, 0x03); // 8 bits, no parity, one stop bit
  12. asm_outb(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
  13. asm_outb(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
  14. asm_outb(port + 4, 0x1E); // Set in loopback mode, test the serial chip
  15. asm_outb(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
  16. // Check if serial is faulty (i.e: not same byte as sent)
  17. if(asm_inb(port + 0) != 0xAE) {
  18. return 1;
  19. }
  20. // If serial is not faulty set it in normal operation mode
  21. // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
  22. asm_outb(port + 4, 0x0F);
  23. return 0;
  24. }
  25. int32_t is_serial_has_data(port_id_t port)
  26. {
  27. return asm_inb(port + 5) & 1;
  28. }
  29. uint8_t serial_read_data(port_id_t port)
  30. {
  31. while (is_serial_has_data(port) == 0)
  32. ;
  33. return asm_inb(port);
  34. }
  35. int32_t is_serial_ready_for_transmition(port_id_t port)
  36. {
  37. return asm_inb(port + 5) & 0x20;
  38. }
  39. void serial_send_data(port_id_t port, uint8_t data)
  40. {
  41. while (is_serial_ready_for_transmition(port) == 0)
  42. ;
  43. return asm_outb(port, data);
  44. }