annotate miscutil/openport.c @ 502:cff42ff57c60

osx.c: osx_free_mem() done
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 01 Jul 2014 18:34:53 +0000
parents 3822f3b198d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
434
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * Serial port opening code for fc-serterm
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/file.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/ioctl.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <termios.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <stdio.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <string.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <strings.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <unistd.h>
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 extern int target_fd;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 static struct baudrate {
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 char *name;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 speed_t termios_code;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 } baud_rate_table[] = {
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 {"19200", B19200},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 {"38400", B38400},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 {"57600", B57600},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 {"115200", B115200},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 {"203125", B230400},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 {"406250", B460800},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 {"812500", B921600},
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 /* table search terminator */
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 {NULL, B0}
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 };
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 open_target_serial(ttydev, baudname)
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 char *ttydev, *baudname;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 {
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 struct termios target_termios;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 struct baudrate *br;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 for (br = baud_rate_table; br->name; br++)
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 if (!strcmp(br->name, baudname))
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 break;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 if (!br->name) {
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n",
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 baudname);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 exit(1);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 }
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 target_fd = open(ttydev, O_RDWR|O_NONBLOCK);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 if (target_fd < 0) {
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 perror(ttydev);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 exit(1);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 }
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 target_termios.c_iflag = IGNBRK;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 target_termios.c_oflag = 0;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 target_termios.c_lflag = 0;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 target_termios.c_cc[VMIN] = 1;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 target_termios.c_cc[VTIME] = 0;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 cfsetispeed(&target_termios, br->termios_code);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 cfsetospeed(&target_termios, br->termios_code);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 perror("initial tcsetattr on target");
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 exit(1);
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 }
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 return 0;
3822f3b198d4 fc-serterm: written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 }