hc
2023-05-26 a23f51ed7a39e452c1037343a84d7db1ca2c5bd7
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "common.h"
 
int writeport(int *fd, unsigned char *chars,int len) {
 
        //printf("*fd = %d\n",getbaud(*fd));
        int n = write(*fd, chars,len);
        if (n < 0) {
                fputs("write failed!\n", stderr);
               printf("\n written %d bytes to port fail:%d\n",len, n);
                return n;
        }
        printf("\n written %d bytes to port \n",n);
        return n;
}
 
int readport(int *fd, unsigned char *result) {
        int iIn=0;
   iIn = read(*fd, result, bufsize);
   if (iIn < 0) {
        if (errno == EAGAIN) {
                printf("SERIAL EAGAIN ERROR\n");
                return 0;
        } else {
              printf("SERIAL read error %d %s\n", errno, strerror(errno));
                return 0;
        }
    }
   //printf("\n read %d bytes from port",iIn); 
 
   /* If we got an expected response trailer, break. */
   if(iIn ==1 && result[0] == 0x00 ) {
       //printf("\n iIn ==1 and 0x00 character \n"); 
       return 0;
   }
 
        return iIn;
}
 
 
int getbaud(int fd) {
        struct termios termAttr;
        int Speed = -1;
        speed_t baudRate;
        tcgetattr(fd, &termAttr);
        /* Get the input speed.                              */
        baudRate = cfgetispeed(&termAttr);
   switch (baudRate) {
       case B300:    Speed = 300; break;
       case B600:    Speed = 600; break;
       case B1200:   Speed = 1200; break;
       case B1800:   Speed = 1800; break;
       case B2400:   Speed = 2400; break;
       case B4800:   Speed = 4800; break;
       case B9600:   Speed = 9600; break;
       case B19200:  Speed = 19200; break;
       case B38400:  Speed = 38400; break;
       case B57600:  Speed = 57600; break;
       case B115200: Speed = 115200; break;
       case B230400: Speed = 230400; break;
       case B460800: Speed = 460800; break;
       case B500000: Speed = 500000; break;
       case B576000: Speed = 576000; break;
       case B921600: Speed = 921600; break;
       case B1000000:Speed = 1000000; break;
       case B1152000:Speed = 1152000; break;
       case B1500000:Speed = 1500000; break;
       case B2000000:Speed = 2000000; break;
       case B2500000:Speed = 2500000; break;
       case B3000000:Speed = 3000000; break;
       case B3500000:Speed = 3500000; break;
       case B4000000:Speed = 4000000; break;
    }
    return Speed;
}
 
int initport(int fd,long baudrate,int flow_ctrl) 
{
   struct termios options;
        memset(&options, 0 , sizeof(options));
        if(ERROR == tcgetattr(fd, &options)) {
                perror("tcgetattr: ");
                return ERROR;
        }
   printf("buadrate:%d\n", baudrate);
   options.c_cflag &= ~CBAUD;
 
   switch (baudrate) {
       case 300:    cfsetispeed(&options,B300); 
               cfsetospeed(&options,B300);
               options.c_cflag |= B300;
               break;
       case 600:    cfsetispeed(&options,B600);
               cfsetospeed(&options,B600);
               options.c_cflag |= B600;
               break;
       case 1200:    cfsetispeed(&options,B1200);
               cfsetospeed(&options,B1200);
               options.c_cflag |= B1200;
               break;
       case 1800:    cfsetispeed(&options,B1800);
               cfsetospeed(&options,B1800);
               options.c_cflag |= B1800;
               break;
       case 2400:    cfsetispeed(&options,B2400);
               cfsetospeed(&options,B2400);
               options.c_cflag |= B2400;
               break;
       case 4800:    cfsetispeed(&options,B4800);
               cfsetospeed(&options,B4800);
               options.c_cflag |= B4800;
               break;
       case 9600:  
       default:    cfsetispeed(&options,B9600);
               cfsetospeed(&options,B9600);
               options.c_cflag |= B9600;
               break;
       case 19200:    cfsetispeed(&options,B19200);
               cfsetospeed(&options,B19200);
               options.c_cflag |= B19200;
               break;
       case 38400:    cfsetispeed(&options,B38400);
               cfsetospeed(&options,B38400);
               options.c_cflag |= B38400;
               break;
                case 57600:     cfsetispeed(&options,B57600);
                                cfsetospeed(&options,B57600);
                                options.c_cflag |= B57600 | CBAUDEX;
                                break;    
       case 115200:    cfsetispeed(&options,B115200);
               cfsetospeed(&options,B115200);
               options.c_cflag |= B115200 | CBAUDEX;
               break;
       case 230400:    cfsetispeed(&options,B230400);
               cfsetospeed(&options,B230400);
               options.c_cflag |= B230400 | CBAUDEX;
               break;
       case 460800:    cfsetispeed(&options,B460800);
               cfsetospeed(&options,B460800);
               options.c_cflag |= B460800 | CBAUDEX;
               break;
       case 500000:    cfsetispeed(&options,B500000);
               cfsetospeed(&options,B500000);
               options.c_cflag |= B500000 | CBAUDEX;
               break;
       case 576000:    cfsetispeed(&options,B576000);
               cfsetospeed(&options,B576000);
               options.c_cflag |= B576000 | CBAUDEX;
               break;
       case 921600:    cfsetispeed(&options,B921600);
               cfsetospeed(&options,B921600);
               options.c_cflag |= B921600 | CBAUDEX;
               break;
       case 1000000:    cfsetispeed(&options,B1000000);
               cfsetospeed(&options,B1000000);
               options.c_cflag |= B1000000 | CBAUDEX;
               break;
       case 1152000:    cfsetispeed(&options,B1152000);
               cfsetospeed(&options,B1152000);
               options.c_cflag |= B1152000 | CBAUDEX;
               break;
       case 1500000:    cfsetispeed(&options,B1500000);
               cfsetospeed(&options,B1500000);
               options.c_cflag |= B1500000 | CBAUDEX;
               break;
       case 2000000:    cfsetispeed(&options,B2000000);
               cfsetospeed(&options,B2000000);
               options.c_cflag |= B2000000 | CBAUDEX;
               break;
       case 2500000:    cfsetispeed(&options,B2500000);
               cfsetospeed(&options,B2500000);
               options.c_cflag |= B2500000 | CBAUDEX;
               break;
       case 3000000:    cfsetispeed(&options,B3000000);
               cfsetospeed(&options,B3000000);
               options.c_cflag |= B3000000 | CBAUDEX;
               break;
       case 3500000:    cfsetispeed(&options,B3500000);
               cfsetospeed(&options,B3500000);
               options.c_cflag |= B3500000 | CBAUDEX;
               break;
       case 4000000:    cfsetispeed(&options,B4000000);
               cfsetospeed(&options,B4000000);
               options.c_cflag |= B4000000 | CBAUDEX;
               break;
   }
        
   /* Enable or Disable Hardware flow control. */
    if(!flow_ctrl) {
       options.c_cflag &= ~CRTSCTS;
       printf("\n Flow control disabled \n");
   }
   else {
       options.c_cflag |= CRTSCTS;
       printf("\n Flow control enabled \n");
   }
 
        /* No Parity, 8N1. */
        options.c_cflag &= ~PARENB; // No parity bit
        options.c_cflag &= ~CSTOPB; // 1 stop bit
        options.c_cflag &= ~CSIZE;  // character size 8
        options.c_cflag |=  CS8 ;
 
        /* Hardware Control Options - Set local mode and Enable receiver to receive characters */
        options.c_cflag     |= (CLOCAL | CREAD );
   
        /* Terminal Control options */
        /* - Disable signals. Disable canonical input processing. Disable echo. */
        options.c_lflag     &= ~(ICANON | IEXTEN | ECHO | ISIG); /* Line options - Raw input */
        
       options.c_iflag     &= ~(ICRNL | INPCK | ISTRIP | BRKINT | IXON | IXOFF | IXANY);
        /* Output processing - Disable post processing of output. */
        options.c_oflag     &= ~OPOST;      /* Output options - Raw output */
        /* Control Characters - Min. no. of characters */
        options.c_cc[VMIN]  = 0;
        /* Character/Packet timeouts. */
        options.c_cc[VTIME] = 3;
 
   if(ERROR == tcflush(fd,TCOFLUSH) ) {
                  printf("tcflush failed");
                  return ERROR;
        }
 
   if(ERROR == tcsetattr(fd, TCSANOW, &options))  {
                printf("Error: Couldn't configure Serial port " );
                return ERROR;
        }
        
/*
   printf("\n\n");
   system("stty -F /dev/ttyS0 -a");
   printf("\n\n"); 
*/
   return SUCCESS;
}
 
int serial_openport(long int baudrate) {
    /*int*/ ut.fd = open("/dev/ttyS0", O_RDWR);
    if(ut.fd < 0) {
        printf("open port failed\n");
        return -1;
    }
    fcntl(ut.fd, F_SETFL, 0);
    printf("baudrate:%ld\n",baudrate);
    printf("\n Existing port baud=%d", getbaud(ut.fd));
    tcgetattr(ut.fd, &oldtio);
    initport(ut.fd, baudrate, 0);
    printf("get baudrate :%d \n",getbaud(ut.fd));
    return ut.fd; 
}
 
int serial_send_data(int fd,const unsigned char* data) {
    int i,ret;
    printf("\n");
    for(int i = 0;i<strlen(data);i++)
        printf("%d,",data[i]);
    printf("\n");
    fcntl(fd, F_SETFL, 0);
    if(strlen(data) < 8) {
        ret = writeport(&fd,data,strlen(data));
    } else {
        ret = writeport(&fd,data,8);
    }
    //ioctl(fd, TCSBRK, 1);
  /*  if(tcdrain(fd) < 0)
        printf("\n tcdrain failure \n");
    sleep(3);
    for(i=0;i<5;i++)
        tcsendbreak(fd,5);*/ 
    return ret;
}
 
 
void signalHandler()
{
   printf("\n Closing device %s\n",UART_DEV_NAME);
   gettimeofday(&ut.end_time,NULL);
   timersub(&ut.end_time,&ut.start_time,&ut.diff_time);
   
   if(read_flag && tx_rx == 'r') {
           ut.diff_time.tv_sec -= 5;
           printf("\n Time taken %08ld sec, %08ld usec\n\n ",ut.diff_time.tv_sec,ut.diff_time.tv_usec);
   }
   if(tx_rx == 's') 
           printf("\n Time taken %08ld sec, %08ld usec\n\n ",ut.diff_time.tv_sec,ut.diff_time.tv_usec);
 
   close_port();
}
 
void close_port()
{
        tcsetattr(ut.fd, TCSANOW, &oldtio);
   if(tx_rx == 'r')
           close(fd2);
   else
            close(fd1);
        close(ut.fd);
        exit(1);
}
 
void display_intro()
{
   printf("\n Use the following format to run the HS-UART TEST PROGRAM\n");
   printf(" ts_uart v1.1\n");
   printf(" For sending data: \n ./ts_uart <tx_rx(s/r)> <file_name> <baudrate> <flow_control(0/1)> <max_delay(0-100)> <random_size(0/1)>\n");
   printf(" tx_rx : send data from file (s) or receive data (r) to put in file\n");
   printf(" file_name : file name to send data from or place data in\n");
   printf(" baudrate : baud rate used for TX/RX\n");
   printf(" flow_control : enables (1) or disables (0) Hardware flow control using RTS/CTS lines\n");
   printf(" max_delay : defines delay in seconds between each data burst when sending. Choose 0 for continuous stream.\n");
   printf(" random_size : enables (1) or disables (0) random size data bursts when sending. Choose 0 for max size.\n");
   printf(" max_delay and random_size are useful for sleep/wakeup over UART testing. ONLY meaningful when sending data\n");
   
   printf(" Examples:\n");
   printf(" Sending data (no delays)\n");
   printf(" ts_uart s init.rc 1500000 0 0 0 /dev/ttyS0\n");
   printf(" loop back mode:\n"); 
   printf(" ts_uart m init.rc 1500000 0 0 0 /dev/ttyS0\n");
   printf(" receive, data must be 0x55\n"); 
   printf(" ts_uart r init.rc 1500000 0 0 0 /dev/ttyS0\n");
   
}
 
/* return a random value between 0 and max */ 
int get_rand_value(int max){
   int value = (int) ((float) rand() / (float) RAND_MAX * max); /* map random size within 0 - max range*/
   return value;
}