uart.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "uart.h"
  2. #include <common/kprint.h>
  3. #define UART_MAX_BITS_RATE 115200
  4. /**
  5. * @brief 当前是否有数据到达
  6. *
  7. */
  8. #define serial_received(p) ((io_in8(p + 5) & 1))
  9. /**
  10. * @brief 当前是否有数据正等待发送
  11. *
  12. */
  13. // todo: 这里有bug,在真机上,一直认为端口是忙的
  14. #define is_transmit_empty(p) ((io_in8(p + 5) & 0x20))
  15. // #define is_transmit_empty(p) (1)
  16. /**
  17. * @brief 初始化com口
  18. *
  19. * @param port com口的端口号
  20. * @param bits_rate 通信的比特率
  21. */
  22. int uart_init(uint32_t port, uint32_t bits_rate)
  23. {
  24. // 错误的比特率
  25. if (bits_rate > UART_MAX_BITS_RATE || UART_MAX_BITS_RATE % bits_rate != 0)
  26. return E_UART_BITS_RATE_ERROR;
  27. io_out8(port + 1, 0x00); // Disable all interrupts
  28. io_out8(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
  29. uint16_t divisor = E_UART_BITS_RATE_ERROR / bits_rate;
  30. io_out8(port + 0, divisor & 0xff); // Set divisor (lo byte)
  31. io_out8(port + 1, (divisor >> 8) & 0xff); // (hi byte)
  32. io_out8(port + 3, 0x03); // 8 bits, no parity, one stop bit
  33. io_out8(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
  34. io_out8(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
  35. io_out8(port + 4, 0x1E); // Set in loopback mode, test the serial chip
  36. io_out8(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
  37. // Check if serial is faulty (i.e: not same byte as sent)
  38. if (io_in8(port + 0) != 0xAE)
  39. {
  40. return E_UART_SERIAL_FAULT;
  41. }
  42. // If serial is not faulty set it in normal operation mode
  43. // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
  44. io_out8(port + 4, 0x0F);
  45. char init_text2[] = "uart initialized.";
  46. for (int i = 0; i < sizeof(init_text2) - 1; ++i)
  47. uart_send(COM1, init_text2[i]);
  48. return UART_SUCCESS;
  49. /*
  50. Notice that the initialization code above writes to [PORT + 1]
  51. twice with different values. This is once to write to the Divisor
  52. register along with [PORT + 0] and once to write to the Interrupt
  53. register as detailed in the previous section.
  54. The second write to the Line Control register [PORT + 3]
  55. clears the DLAB again as well as setting various other bits.
  56. */
  57. }
  58. /**
  59. * @brief 发送数据
  60. *
  61. * @param port 端口号
  62. * @param c 要发送的数据
  63. */
  64. void uart_send(uint32_t port, char c)
  65. {
  66. while (is_transmit_empty(port) == 0)
  67. pause();
  68. io_out8(port, c);
  69. }
  70. /**
  71. * @brief 从uart接收数据
  72. *
  73. * @param port 端口号
  74. * @return uchar 接收到的数据
  75. */
  76. uchar uart_read(uint32_t port)
  77. {
  78. while (serial_received(port) == 0)
  79. pause();
  80. return io_in8(port);
  81. }
  82. /**
  83. * @brief 通过串口发送整个字符串
  84. *
  85. * @param port 串口端口
  86. * @param str 字符串
  87. */
  88. void uart_send_str(uint32_t port, const char *str)
  89. {
  90. if ((unlikely(str == NULL)))
  91. return;
  92. while (1)
  93. {
  94. if (unlikely(*str == '\0'))
  95. return;
  96. uart_send(port, *str);
  97. ++str;
  98. }
  99. }