RISC-V MCU中文社区

【分享】 蜂鸟E203与电脑实现UART通信实现代码并实现上位机控制代码分享

发表于 开源蜂鸟E203 2021-07-25 22:16:48
0
4297
4

        大家好,我们是这是一件好事队,队伍编号为CICC1859,这里想和大家分享一下蜂鸟E203与电脑的UART通信代码,并通过UART实现上位机控制。

电脑端UART代码:

  1. #include "stdio.h"
  2. HANDLE GetSerialPortHandle(const char *name) {
  3. printf("Opening %s...\n", name);
  4. fflush(stdout);
  5. HANDLE hCom = CreateFileA(name,
  6. GENERIC_READ | GENERIC_WRITE,
  7. 0,
  8. NULL,
  9. OPEN_EXISTING,
  10. FILE_ATTRIBUTE_NORMAL,
  11. NULL);
  12. if (hCom == INVALID_HANDLE_VALUE) {
  13. printf("Open COM failed!\n");
  14. fflush(stdout);
  15. return NULL;
  16. }
  17. printf("Configuring Timeout...\n");
  18. fflush(stdout);
  19. COMMTIMEOUTS TimeOuts;
  20. TimeOuts.ReadIntervalTimeout = 0;
  21. TimeOuts.ReadTotalTimeoutMultiplier = 0;
  22. TimeOuts.ReadTotalTimeoutConstant = 0;
  23. TimeOuts.WriteTotalTimeoutMultiplier = 1;
  24. TimeOuts.WriteTotalTimeoutConstant = 1;
  25. SetCommTimeouts(hCom, &TimeOuts);
  26. printf("Configuring serial port...\n");
  27. fflush(stdout);
  28. DCB dcb;
  29. GetCommState(hCom, &dcb);
  30. dcb.BaudRate = 115200;
  31. dcb.ByteSize = 8;
  32. dcb.Parity = NOPARITY;
  33. dcb.StopBits = ONESTOPBIT;
  34. SetCommState(hCom, &dcb);
  35. return hCom;
  36. }
  37. void CloseSerialPortHandle(HANDLE handle) {
  38. CloseHandle(handle);
  39. }
  40. int32_t ReadSerialPort(HANDLE hCom, uint8_t *pMessage, size_t length) {
  41. WINBOOL bReadStat;
  42. DWORD wCount;
  43. bReadStat = ReadFile(hCom, pMessage, length, &wCount, NULL);
  44. if (!bReadStat)
  45. {
  46. printf("Failed to read serial port\n");
  47. fflush(stdout);
  48. }
  49. return bReadStat;
  50. }
  51. int32_t WriteSerialPort(HANDLE hCom, const uint8_t *pMessage, size_t length) {
  52. WINBOOL bReadStat;
  53. DWORD wCount;
  54. bReadStat = WriteFile(hCom, pMessage, length, &wCount, NULL);
  55. if (!bReadStat)
  56. {
  57. printf("Failed to read serial port\n");
  58. fflush(stdout);
  59. }
  60. return bReadStat;
  61. }

        代码主要包含开启UARTGetSerialPortHandle(),关闭UART:CloseSerialPortHandle()以及UART读写WriteSerialPort()ReadSerialPort()

        蜂鸟E203 UART配置采用官方代码:


  1. int32_t uart_init(UART_TypeDef *uart, uint32_t baudrate)
  2. {
  3. if (__RARELY(uart == NULL)) {
  4. return -1;
  5. }
  6. unsigned int uart_div = SystemCoreClock / baudrate - 1;
  7. uart->LCR = 0x80;
  8. uart->DLM = (uart_div >> 8) & 0xFF;
  9. uart->DLL = uart_div & 0xFF;
  10. uart->FCR = 0xC6;
  11. uart->LCR = 0x03;
  12. return 0;
  13. }
  14. int32_t uart_config_stopbit(UART_TypeDef *uart, UART_STOP_BIT stopbit)
  15. {
  16. if (__RARELY(uart == NULL)) {
  17. return -1;
  18. }
  19. uart->LCR &= 0xFFFFFFFB;
  20. uart->LCR |= (stopbit << 2);
  21. return 0;
  22. }
  23. int32_t uart_enable_paritybit(UART_TypeDef *uart)
  24. {
  25. if (__RARELY(uart == NULL)) {
  26. return -1;
  27. }
  28. uart->LCR |= 0x8;
  29. return 0;
  30. }
  31. int32_t uart_disable_paritybit(UART_TypeDef *uart)
  32. {
  33. if (__RARELY(uart == NULL)) {
  34. return -1;
  35. }
  36. uart->LCR &= 0xFFFFFFF7;
  37. return 0;
  38. }
  39. int32_t uart_set_parity(UART_TypeDef *uart, UART_PARITY_BIT paritybit)
  40. {
  41. if (__RARELY(uart == NULL)) {
  42. return -1;
  43. }
  44. uart->LCR &= 0xFFFFFFCF;
  45. uart->LCR |= (paritybit << 4);
  46. return 0;
  47. }
  48. int32_t uart_write(UART_TypeDef *uart, uint8_t val)
  49. {
  50. if (__RARELY(uart == NULL)) {
  51. return -1;
  52. }
  53. #ifndef SIMULATION_SPIKE
  54. #ifndef SIMULATION_XLSPIKE
  55. while ((uart->LSR & 0x20) == 0);
  56. #endif
  57. uart->THR = val;
  58. #else
  59. extern void htif_putc(char ch);
  60. htif_putc(val);
  61. #endif
  62. return 0;
  63. }
  64. uint8_t uart_read(UART_TypeDef *uart)
  65. {
  66. uint32_t reg;
  67. if (__RARELY(uart == NULL)) {
  68. return -1;
  69. }
  70. while ((uart->LSR & 0x1) == 0);
  71. reg = uart->RBR;
  72. return (uint8_t)(reg & 0xFF);
  73. }
  74. int32_t uart_enable_tx_empt_int(UART_TypeDef *uart)
  75. {
  76. if (__RARELY(uart == NULL)) {
  77. return -1;
  78. }
  79. uart->IER |= 0x2;
  80. return 0;
  81. }
  82. int32_t uart_disable_tx_empt_int(UART_TypeDef *uart)
  83. {
  84. if (__RARELY(uart == NULL)) {
  85. return -1;
  86. }
  87. uart->IER &= 0xFFFFFFFD;
  88. return 0;
  89. }
  90. int32_t uart_set_rx_th(UART_TypeDef *uart, uint8_t th)
  91. {
  92. if (__RARELY(uart == NULL)) {
  93. return -1;
  94. }
  95. if(th > 3) {
  96. th = 3;
  97. }
  98. uart->FCR &= 0xFFFFFF3F;
  99. uart->FCR |= (th << 6);
  100. return 0;
  101. }
  102. int32_t uart_enable_rx_th_int(UART_TypeDef *uart)
  103. {
  104. if (__RARELY(uart == NULL)) {
  105. return -1;
  106. }
  107. uart->IER |= 0x1;
  108. return 0;
  109. }
  110. int32_t uart_disable_rx_th_int(UART_TypeDef *uart)
  111. {
  112. if (__RARELY(uart == NULL)) {
  113. return -1;
  114. }
  115. uart->IER &= 0xFFFFFFFE;
  116. return 0;
  117. }
  118. int32_t uart_enable_rx_err_int(UART_TypeDef *uart)
  119. {
  120. if (__RARELY(uart == NULL)) {
  121. return -1;
  122. }
  123. uart->IER |= 0x4;
  124. return 0;
  125. }
  126. int32_t uart_disable_rx_err_int(UART_TypeDef *uart)
  127. {
  128. if (__RARELY(uart == NULL)) {
  129. return -1;
  130. }
  131. uart->IER &= 0xFFFFFFFB;
  132. return 0;
  133. }
  134. int32_t uart_get_int_status(UART_TypeDef *uart)
  135. {
  136. if (__RARELY(uart == NULL)) {
  137. return -1;
  138. }
  139. return uart->IIR;
  140. }
  141. int32_t uart_get_status(UART_TypeDef *uart)
  142. {
  143. if (__RARELY(uart == NULL)) {
  144. return -1;
  145. }
  146. return uart->LSR;
  147. }

 

        由此配置双方UART,并在双方主函数中调用写入读取函数即可完成电脑与蜂鸟E203的通信。下面为实测图:



        双方通过UART完成了Kyber(本作品设计)的通信过程。


喜欢4
用户评论

未通过实名认证

懒的都不写签名

积分
问答
粉丝
关注
  • RV-STAR 开发板
  • RISC-V处理器设计系列课程
  • 培养RISC-V大学土壤 共建RISC-V教育生态
RV-STAR 开发板