CH55x 串口速度测试

使用上次的工具来测试 Ch55x(Ch551/2/4)开发板的串口速度。

实际测试的是 CH554 ,使用 Ch55xduino 编写代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef USER_USB_RAM
#error "This example needs to be compiled with a USER USB setting"
#endif
 
#include "src/userUsbCdc/USBCDC.h"
 
//This is a fairly large array, store it in external memory with keyword __xdata
__xdata char recvStr[64];
 
// 代码中出现的发送和接收
// 测试 Package 的大小
#define MAX_N 256
 
// 2秒超时
#define TIMEOUT  2000
 
// 接收数据 Buffer
// 设置为实际的2倍大小避免接收超过Buffer 的问题
__xdata byte receiveBuffer[MAX_N];
 
void setup() {
  USBInit();
}
 
// 状态机记录
byte currentStatus = 0;
 
// 收到的数量
int counter = 0;
// 超时计时器
unsigned long Elsp;
// 发送标记,True 时发送,反之不发送
boolean sendOnce = true;
// 发生错误的数量
int failCount;
 
void loop() {
  switch (currentStatus) {
    case 0:
      // 初始状态
      while (USBSerial_available()) {
        byte c = USBSerial_read();
        // q命令查询当前状态(用于调试)
        if (c == 'q') {
          USBSerial_print_s("Current 0");
          USBSerial_flush();
        }
        // 1(接收模式) 2(发送模式)
        if ((c == '1') || (c == '2')) {
          // 转移到下一个状态
          currentStatus = c - '0';
          // 如果是接收模式,那么开始计时
          if (c == '1') {
            Elsp = millis();
            counter = 0;
          }
        }
      }
      break;
 
    case 1: //接收模式
      // 如果接收模式超时,那么直接返回到状态0
      if (millis() - Elsp > TIMEOUT) {
        // 返回状态 0
        currentStatus = 0;
        // 发送 "aaa" 表示当前超时
        USBSerial_write('a'); USBSerial_write('a'); USBSerial_write('a');
        USBSerial_flush();
      }
 
      // 接收数据
      while (USBSerial_available()) {
        byte c = USBSerial_read();
        receiveBuffer[counter] = c;
        // 统计接收到的数据量
        counter++;
 
        // 如果接收到足够的数据,那么转移到状态3 进行校验
        if (counter == MAX_N) {
          currentStatus = 3;
          //Serial.print("St3");
        }
      }
      break;
    case 3:
      // 校验接收模式收到的数据,数据是 0x55 0xaa 0x55....
      failCount = 0;
      for (int i = 0; i < MAX_N; i = i + 2) {
        if (receiveBuffer[i] != 0x55) {
          failCount++;
        }
        if (receiveBuffer[i + 1] != 0xAA) {
          failCount++;
        }
      }
      // 检查是否有错误
      if (failCount == 0) {
        // 返回"ppp" 表示校验通过
        USBSerial_write('p'); USBSerial_write('p'); USBSerial_write('p');
        USBSerial_flush();
      }
      else {
        // 校验失败,返回 'f'+发生错误的数量错误数据
        USBSerial_write('f');
        USBSerial_write((byte)(failCount & 0xFF));
        USBSerial_write((byte)(failCount >> 8));
 
      }
      counter = 0;
      //Serial.println("Switch to status 1");
      Elsp = millis();
      currentStatus = 1;
      break;
    case 2:
      // 发送模式,直接发送 MAX_N 个 0x55 0xaa 0x55 ....
      if (sendOnce) {
        for (int i = 0; i < MAX_N / 2; i++) {
          USBSerial_write(0x55);
          USBSerial_write(0xAA);
          USBSerial_flush();
        }
        sendOnce = false;
      }
      while (USBSerial_available()) {
        char c = USBSerial_read();
        if (c == '0') {
          currentStatus = 0;
          //Serial.print("Switch to 0");
        }
        if (c == '2') {
          sendOnce = true;
        }
        // 查询状态
        if (c == 'q') {
          USBSerial_print_s("Current 2");
          USBSerial_flush();
        }
      }
      break;
    default: {
        USBSerial_println_s("Unknown status");
        USBSerial_flush();
      }
  }

1.PC端接收,Ch554发送,可以看到速度能达到37K/S左右

2.PC端发送,Ch554接收,可以看到速度能达到97K/S左右

从结果可以看出来,比 32U4 这样的芯片还是快一点点的(测试模式是 5V 24Mhz)

《CH55x 串口速度测试》有2个想法

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注