Mike Fiore / Mbed 2 deprecated mts_serial_test

Dependencies:   mbed

Committer:
mfiore
Date:
Mon Jul 07 15:55:45 2014 +0000
Revision:
0:2c7ce7d33901
test program to investigate serial issues

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:2c7ce7d33901 1 #include "mbed.h"
mfiore 0:2c7ce7d33901 2 #include "MTSSerialFlowControl.h"
mfiore 0:2c7ce7d33901 3 #include <string>
mfiore 0:2c7ce7d33901 4
mfiore 0:2c7ce7d33901 5 using namespace mts;
mfiore 0:2c7ce7d33901 6
mfiore 0:2c7ce7d33901 7 char pattern[] = "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 \r\n"
mfiore 0:2c7ce7d33901 8 "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 \r\n"
mfiore 0:2c7ce7d33901 9 "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 \r\n"
mfiore 0:2c7ce7d33901 10 "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 \r\n"
mfiore 0:2c7ce7d33901 11 "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 \r\n"
mfiore 0:2c7ce7d33901 12 "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 \r\n"
mfiore 0:2c7ce7d33901 13 "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 \r\n"
mfiore 0:2c7ce7d33901 14 "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 \r\n"
mfiore 0:2c7ce7d33901 15 "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 \r\n"
mfiore 0:2c7ce7d33901 16 "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 \r\n"
mfiore 0:2c7ce7d33901 17 "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 \r\n"
mfiore 0:2c7ce7d33901 18 "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 \r\n"
mfiore 0:2c7ce7d33901 19 "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 \r\n"
mfiore 0:2c7ce7d33901 20 "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 \r\n"
mfiore 0:2c7ce7d33901 21 "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 \r\n"
mfiore 0:2c7ce7d33901 22 "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 \r\n"
mfiore 0:2c7ce7d33901 23 "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 \r\n"
mfiore 0:2c7ce7d33901 24 "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 \r\n"
mfiore 0:2c7ce7d33901 25 "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 \r\n"
mfiore 0:2c7ce7d33901 26 "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 \r\n"
mfiore 0:2c7ce7d33901 27 "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 \r\n"
mfiore 0:2c7ce7d33901 28 "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 \r\n"
mfiore 0:2c7ce7d33901 29 "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 \r\n"
mfiore 0:2c7ce7d33901 30 "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 \r\n"
mfiore 0:2c7ce7d33901 31 "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 \r\n"
mfiore 0:2c7ce7d33901 32 "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 \r\n"
mfiore 0:2c7ce7d33901 33 "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 \r\n"
mfiore 0:2c7ce7d33901 34 "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 \r\n"
mfiore 0:2c7ce7d33901 35 "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 \r\n"
mfiore 0:2c7ce7d33901 36 "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 \r\n"
mfiore 0:2c7ce7d33901 37 "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 \r\n"
mfiore 0:2c7ce7d33901 38 "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 \r\n"
mfiore 0:2c7ce7d33901 39 "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 \r\n"
mfiore 0:2c7ce7d33901 40 "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 \r\n"
mfiore 0:2c7ce7d33901 41 "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 \r\n"
mfiore 0:2c7ce7d33901 42 "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 \r\n"
mfiore 0:2c7ce7d33901 43 "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 \r\n"
mfiore 0:2c7ce7d33901 44 "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 \r\n"
mfiore 0:2c7ce7d33901 45 "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 \r\n"
mfiore 0:2c7ce7d33901 46 "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 \r\n"
mfiore 0:2c7ce7d33901 47 "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 \r\n"
mfiore 0:2c7ce7d33901 48 "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 \r\n"
mfiore 0:2c7ce7d33901 49 "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 \r\n"
mfiore 0:2c7ce7d33901 50 "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 \r\n"
mfiore 0:2c7ce7d33901 51 "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 \r\n"
mfiore 0:2c7ce7d33901 52 "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 \r\n"
mfiore 0:2c7ce7d33901 53 "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 \r\n"
mfiore 0:2c7ce7d33901 54 "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 \r\n"
mfiore 0:2c7ce7d33901 55 "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 \r\n"
mfiore 0:2c7ce7d33901 56 "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 \r\n";
mfiore 0:2c7ce7d33901 57
mfiore 0:2c7ce7d33901 58 int main() {
mfiore 0:2c7ce7d33901 59
mfiore 0:2c7ce7d33901 60 int baud = 115200;
mfiore 0:2c7ce7d33901 61 int mybufsize = 1024;
mfiore 0:2c7ce7d33901 62 char mybuf[mybufsize];
mfiore 0:2c7ce7d33901 63 int readsize = 0;
mfiore 0:2c7ce7d33901 64 string data;
mfiore 0:2c7ce7d33901 65 int index = 0;
mfiore 0:2c7ce7d33901 66 bool loss = false;
mfiore 0:2c7ce7d33901 67
mfiore 0:2c7ce7d33901 68 // STMicro Nucleo F401RE
mfiore 0:2c7ce7d33901 69 //DigitalIn sw(USER_BUTTON);
mfiore 0:2c7ce7d33901 70 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6);
mfiore 0:2c7ce7d33901 71
mfiore 0:2c7ce7d33901 72 // Freescale KL46Z
mfiore 0:2c7ce7d33901 73 DigitalIn sw(PTC12);
mfiore 0:2c7ce7d33901 74 MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6);
mfiore 0:2c7ce7d33901 75
mfiore 0:2c7ce7d33901 76 // Freescale K64F
mfiore 0:2c7ce7d33901 77 //DigitalIn sw(PTA4);
mfiore 0:2c7ce7d33901 78 //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6);
mfiore 0:2c7ce7d33901 79
mfiore 0:2c7ce7d33901 80 io->baud(baud);
mfiore 0:2c7ce7d33901 81
mfiore 0:2c7ce7d33901 82 printf("baud rate at %d\r\n", baud);
mfiore 0:2c7ce7d33901 83
mfiore 0:2c7ce7d33901 84 while (true) {
mfiore 0:2c7ce7d33901 85 printf("reading\r\n");
mfiore 0:2c7ce7d33901 86 data.clear();
mfiore 0:2c7ce7d33901 87 loss = false;
mfiore 0:2c7ce7d33901 88 while (sw == 1) {
mfiore 0:2c7ce7d33901 89 if (io->readable()) {
mfiore 0:2c7ce7d33901 90 readsize = io->read(mybuf, mybufsize, 250);
mfiore 0:2c7ce7d33901 91 if (readsize > 0) {
mfiore 0:2c7ce7d33901 92 data.append(mybuf, readsize);
mfiore 0:2c7ce7d33901 93 printf("read %d total bytes\r\n", data.size());
mfiore 0:2c7ce7d33901 94 } else {
mfiore 0:2c7ce7d33901 95 printf("read failed\r\n");
mfiore 0:2c7ce7d33901 96 }
mfiore 0:2c7ce7d33901 97 }
mfiore 0:2c7ce7d33901 98 }
mfiore 0:2c7ce7d33901 99 wait(1);
mfiore 0:2c7ce7d33901 100
mfiore 0:2c7ce7d33901 101 for (index = 0; index < data.size(); index++) {
mfiore 0:2c7ce7d33901 102 if (pattern[index] != data[index]) {
mfiore 0:2c7ce7d33901 103 printf("data loss at index %d\r\n", index);
mfiore 0:2c7ce7d33901 104 printf("expecting [%02X] found [%02X]\r\n", pattern[index], data[index]);
mfiore 0:2c7ce7d33901 105 loss = true;
mfiore 0:2c7ce7d33901 106 break;
mfiore 0:2c7ce7d33901 107 }
mfiore 0:2c7ce7d33901 108 }
mfiore 0:2c7ce7d33901 109
mfiore 0:2c7ce7d33901 110 if (loss) {
mfiore 0:2c7ce7d33901 111 printf("full pattern [\r\n");
mfiore 0:2c7ce7d33901 112 printf("%s", pattern);
mfiore 0:2c7ce7d33901 113 printf("]\r\n");
mfiore 0:2c7ce7d33901 114 printf("full data [\r\n");
mfiore 0:2c7ce7d33901 115 printf("%s", data.c_str());
mfiore 0:2c7ce7d33901 116 printf("]\r\n");
mfiore 0:2c7ce7d33901 117 printf("\r\n");
mfiore 0:2c7ce7d33901 118
mfiore 0:2c7ce7d33901 119 printf("lost %d bytes\r\n", sizeof(pattern) - data.size());
mfiore 0:2c7ce7d33901 120 } else {
mfiore 0:2c7ce7d33901 121 printf("no data loss\r\n");
mfiore 0:2c7ce7d33901 122 }
mfiore 0:2c7ce7d33901 123
mfiore 0:2c7ce7d33901 124 while (sw == 1);
mfiore 0:2c7ce7d33901 125 wait(1);
mfiore 0:2c7ce7d33901 126 }
mfiore 0:2c7ce7d33901 127 }