serial.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "serial.h"
  2. #include "pci.h"
  3. void serial_init(unsigned int port)
  4. {
  5. port_outb(port + 1, 0x00);
  6. port_outb(port + 3, 0x80);
  7. port_outb(port + 0, 0x0C);
  8. port_outb(port + 1, 0x00);
  9. port_outb(port + 3, 0x03);
  10. port_outb(port + 2, 0xC7);
  11. port_outb(port + 4, 0x03);
  12. }
  13. void serial_putc(unsigned int port, char c)
  14. {
  15. while (!(port_inb(port + 5) & 0x20))
  16. ;
  17. port_outb(port, (unsigned char)c);
  18. }
  19. void serial_puts(unsigned int port, const char *s)
  20. {
  21. while (*s) {
  22. if (*s == '\n')
  23. serial_putc(port, '\r');
  24. serial_putc(port, *s++);
  25. }
  26. }
  27. void serial_puthex(unsigned int port, unsigned int n)
  28. {
  29. static const char hex[] = "0123456789ABCDEF";
  30. int i;
  31. serial_puts(port, "0x");
  32. for (i = 28; i >= 0; i -= 4)
  33. serial_putc(port, hex[(n >> i) & 0xF]);
  34. }
  35. void serial_putdec(unsigned int port, unsigned int n)
  36. {
  37. char buf[12];
  38. char *p = buf + sizeof(buf) - 1;
  39. *p = '\0';
  40. if (n == 0) {
  41. serial_putc(port, '0');
  42. return;
  43. }
  44. while (n) {
  45. *--p = (char)('0' + (n % 10));
  46. n /= 10;
  47. }
  48. serial_puts(port, p);
  49. }
  50. int serial_has_data(unsigned int port)
  51. {
  52. return (port_inb(port + 5) & 0x01) != 0;
  53. }
  54. char serial_getc_nb(unsigned int port)
  55. {
  56. if (!serial_has_data(port))
  57. return 0;
  58. return (char)port_inb(port);
  59. }