uart.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #define is_transmit_empty(p) ((io_in8(p + 5) & 0x20))
  14. /**
  15. * @brief 初始化com口
  16. *
  17. * @param port com口的端口号
  18. * @param bits_rate 通信的比特率
  19. */
  20. int uart_init(uint32_t port, uint32_t bits_rate)
  21. {
  22. // 错误的比特率
  23. if (bits_rate > UART_MAX_BITS_RATE || UART_MAX_BITS_RATE % bits_rate != 0)
  24. return E_UART_BITS_RATE_ERROR;
  25. io_out8(port + 1, 0x00); // Disable all interrupts
  26. io_out8(port + 3, 0x80); // Enable DLAB (set baud rate divisor)
  27. uint16_t divisor = E_UART_BITS_RATE_ERROR / bits_rate;
  28. io_out8(port + 0, divisor & 0xff); // Set divisor (lo byte)
  29. io_out8(port + 1, (divisor >> 8) & 0xff); // (hi byte)
  30. io_out8(port + 3, 0x03); // 8 bits, no parity, one stop bit
  31. io_out8(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
  32. io_out8(port + 4, 0x0B); // IRQs enabled, RTS/DSR set
  33. io_out8(port + 4, 0x1E); // Set in loopback mode, test the serial chip
  34. io_out8(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
  35. // Check if serial is faulty (i.e: not same byte as sent)
  36. if (io_in8(port + 0) != 0xAE)
  37. {
  38. return E_UART_SERIAL_FAULT;
  39. }
  40. // If serial is not faulty set it in normal operation mode
  41. // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
  42. io_out8(port + 4, 0x0F);
  43. return UART_SUCCESS;
  44. /*
  45. Notice that the initialization code above writes to [PORT + 1]
  46. twice with different values. This is once to write to the Divisor
  47. register along with [PORT + 0] and once to write to the Interrupt
  48. register as detailed in the previous section.
  49. The second write to the Line Control register [PORT + 3]
  50. clears the DLAB again as well as setting various other bits.
  51. */
  52. }
  53. /**
  54. * @brief 发送数据
  55. *
  56. * @param port 端口号
  57. * @param c 要发送的数据
  58. */
  59. void uart_send(uint32_t port, char c)
  60. {
  61. while (is_transmit_empty(port) == 0)
  62. pause();
  63. io_out8(port, c);
  64. }
  65. /**
  66. * @brief 从uart接收数据
  67. *
  68. * @param port 端口号
  69. * @return uchar 接收到的数据
  70. */
  71. uchar uart_read(uint32_t port)
  72. {
  73. while (serial_received(port) == 0)
  74. pause();
  75. return io_in8(port);
  76. }