|
| 1 | +/* |
| 2 | + SerialLoop |
| 3 | +
|
| 4 | + Test all hardware serial configs at several speeds |
| 5 | + by sending all possible data range and comparing Tx/Rx. |
| 6 | +
|
| 7 | + Creation 10 Jul 2017 |
| 8 | + by Frederic Pillon <[email protected]> |
| 9 | +
|
| 10 | + This example code is in the public domain. |
| 11 | +*/ |
| 12 | + |
| 13 | +/* |
| 14 | + * 1 - Connect Rx/Tx of the desired Serial |
| 15 | + * 2 - Define SERIAL_PORT_TESTED by settinh Serial number to use 1,2,... |
| 16 | + * ! Ensure this Serialx is enabled thanks 'Serial interface menu'! |
| 17 | + * 3 - Optionnal: comment unwanted speed in serialSpeed array. |
| 18 | + */ |
| 19 | +#define DECL_CONFIG(x) {#x, x} |
| 20 | +#define SERIAL_PORT_TESTED Serial3 |
| 21 | + |
| 22 | +typedef struct serialTest_s serialTest; |
| 23 | +struct serialTest_s { |
| 24 | + const char* name; |
| 25 | + uint32_t config; |
| 26 | +}; |
| 27 | + |
| 28 | +static serialTest serialConfig[] = { |
| 29 | +#ifdef UART_WORDLENGTH_7B |
| 30 | +DECL_CONFIG(SERIAL_7N1), |
| 31 | +DECL_CONFIG(SERIAL_7N2), |
| 32 | +DECL_CONFIG(SERIAL_6E1), |
| 33 | +DECL_CONFIG(SERIAL_6E2), |
| 34 | +DECL_CONFIG(SERIAL_6O1), |
| 35 | +DECL_CONFIG(SERIAL_6O2), |
| 36 | +#endif |
| 37 | +DECL_CONFIG(SERIAL_8N1), |
| 38 | +DECL_CONFIG(SERIAL_8N2), |
| 39 | +DECL_CONFIG(SERIAL_7E1), |
| 40 | +DECL_CONFIG(SERIAL_8E1), |
| 41 | +DECL_CONFIG(SERIAL_7E2), |
| 42 | +DECL_CONFIG(SERIAL_7O1), |
| 43 | +DECL_CONFIG(SERIAL_8O1), |
| 44 | +DECL_CONFIG(SERIAL_7O2), |
| 45 | +DECL_CONFIG(SERIAL_8O2), |
| 46 | +DECL_CONFIG(SERIAL_8E2) |
| 47 | +}; |
| 48 | + |
| 49 | +static uint32_t serialSpeed[] = { |
| 50 | + 300, |
| 51 | + 1200, |
| 52 | + 2400, |
| 53 | + 4800, |
| 54 | + 9600, |
| 55 | + 19200, |
| 56 | + 38400, |
| 57 | + 57600, |
| 58 | + 74880, |
| 59 | + 115200, |
| 60 | + 230400, |
| 61 | + 250000, |
| 62 | + 500000, |
| 63 | + 1000000, |
| 64 | + 2000000 |
| 65 | +}; |
| 66 | + |
| 67 | +static uint32_t start_time = 0; |
| 68 | +static uint32_t configCur = 0; |
| 69 | +static uint32_t configNb = sizeof(serialConfig)/sizeof(serialTest); |
| 70 | +static uint32_t speedNb = sizeof(serialSpeed)/sizeof(uint32_t); |
| 71 | +static uint32_t nbTestOK = 0; |
| 72 | +static uint32_t nbTestKO = 0; |
| 73 | + |
| 74 | +uint32_t dataMask(uint32_t config) { |
| 75 | + uint32_t databits = 0; |
| 76 | + switch(config & 0x07) { |
| 77 | + case 0x02: |
| 78 | + databits = 6; |
| 79 | + break; |
| 80 | + case 0x04: |
| 81 | + databits = 7; |
| 82 | + break; |
| 83 | + case 0x06: |
| 84 | + databits = 8; |
| 85 | + break; |
| 86 | + default: |
| 87 | + databits = 0; |
| 88 | + break; |
| 89 | + } |
| 90 | + return ((1 << databits) - 1); |
| 91 | +} |
| 92 | + |
| 93 | +void test_uart(int val) |
| 94 | +{ |
| 95 | + int recval = 0; |
| 96 | + SERIAL_PORT_TESTED.write(val); |
| 97 | + delay(10); |
| 98 | + while(SERIAL_PORT_TESTED.available()){ |
| 99 | + recval = SERIAL_PORT_TESTED.read(); |
| 100 | + } |
| 101 | + if(val == recval) { |
| 102 | + nbTestOK++; |
| 103 | + } |
| 104 | + else { |
| 105 | + SERIAL_PORT_MONITOR.print("Send: 0x"); |
| 106 | + SERIAL_PORT_MONITOR.print(val,HEX); |
| 107 | + SERIAL_PORT_MONITOR.print("\tReceived: 0x"); |
| 108 | + SERIAL_PORT_MONITOR.print(recval,HEX); |
| 109 | + SERIAL_PORT_MONITOR.println(" --> KO <--"); |
| 110 | + nbTestKO++; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void setup() { |
| 115 | + SERIAL_PORT_MONITOR.begin(115200); |
| 116 | + while(!SERIAL_PORT_MONITOR); |
| 117 | + SERIAL_PORT_MONITOR.print("SerialLoop test on "); |
| 118 | + SERIAL_PORT_MONITOR.println(xstr(SERIAL_PORT_TESTED)); |
| 119 | + SERIAL_PORT_MONITOR.print(configNb); |
| 120 | + SERIAL_PORT_MONITOR.println(" configs to test."); |
| 121 | + SERIAL_PORT_MONITOR.print(speedNb); |
| 122 | + SERIAL_PORT_MONITOR.println(" speed to test."); |
| 123 | + start_time=millis(); |
| 124 | +} |
| 125 | + |
| 126 | +void loop() { |
| 127 | + uint32_t mask = 0; |
| 128 | + if (configCur == configNb) { |
| 129 | + SERIAL_PORT_MONITOR.println("SerialLoop test done.\nResults:"); |
| 130 | + SERIAL_PORT_MONITOR.print("OK: "); |
| 131 | + SERIAL_PORT_MONITOR.println(nbTestOK); |
| 132 | + SERIAL_PORT_MONITOR.print("KO: "); |
| 133 | + SERIAL_PORT_MONITOR.println(nbTestKO); |
| 134 | + SERIAL_PORT_MONITOR.print("Test duration (ms): "); |
| 135 | + SERIAL_PORT_MONITOR.print(millis() - start_time); |
| 136 | + |
| 137 | + while(1); // End test |
| 138 | + } |
| 139 | + |
| 140 | + SERIAL_PORT_MONITOR.println("########################"); |
| 141 | + SERIAL_PORT_MONITOR.print("Config: "); |
| 142 | + SERIAL_PORT_MONITOR.print(serialConfig[configCur].name); |
| 143 | + SERIAL_PORT_MONITOR.print(" (0x"); |
| 144 | + SERIAL_PORT_MONITOR.print(serialConfig[configCur].config, HEX); |
| 145 | + SERIAL_PORT_MONITOR.println(")"); |
| 146 | + for (uint32_t s=0; s<speedNb; s++) { |
| 147 | + SERIAL_PORT_MONITOR.print("Test at "); |
| 148 | + SERIAL_PORT_MONITOR.print(serialSpeed[s]); |
| 149 | + SERIAL_PORT_MONITOR.println(" baud"); |
| 150 | + SERIAL_PORT_TESTED.begin(serialSpeed[s],serialConfig[configCur].config); |
| 151 | + mask = dataMask(serialConfig[configCur].config); |
| 152 | + for (uint32_t i=0; i<=(0xFF&mask); i++) |
| 153 | + { |
| 154 | + test_uart(i&mask); |
| 155 | + } |
| 156 | + SERIAL_PORT_TESTED.end(); |
| 157 | + } |
| 158 | + SERIAL_PORT_MONITOR.println("End."); |
| 159 | + configCur++; |
| 160 | +} |
| 161 | + |
0 commit comments