annotate rvinterf/old/openport.c @ 563:a418c48046ad

L1: l1_init.c compiles
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 08 Aug 2014 02:28:45 +0000
parents f42854da4563
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
126
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module takes care of opening the serial port and setting the
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * proper "raw" termios modes, including the baud rate.
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 */
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/types.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/file.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/ioctl.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <termios.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdio.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <stdlib.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <string.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <strings.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <unistd.h>
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 int target_fd;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 char *baudrate_name = "115200";
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 static struct baudrate {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 char *name;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 speed_t termios_code;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 } baud_rate_table[] = {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 {"19200", B19200},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 {"38400", B38400},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 {"57600", B57600},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 {"115200", B115200},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 {"203125", B230400},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 {"406250", B460800},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 {"812500", B921600},
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 /* table search terminator */
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 {NULL, B0}
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 };
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 static speed_t
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 get_baud_rate()
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 struct baudrate *br;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 for (br = baud_rate_table; br->name; br++)
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 if (!strcmp(br->name, baudrate_name))
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 break;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 if (!br->name) {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n",
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 baudrate_name);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 exit(1);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 }
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 return br->termios_code;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 }
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 open_target_serial(ttydev)
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 char *ttydev;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 struct termios target_termios;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 speed_t br_code;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 br_code = get_baud_rate();
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 target_fd = open(ttydev, O_RDWR|O_NONBLOCK);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 if (target_fd < 0) {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 perror(ttydev);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 exit(1);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 }
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 target_termios.c_iflag = IGNBRK;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 target_termios.c_oflag = 0;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 target_termios.c_lflag = 0;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 target_termios.c_cc[VMIN] = 1;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 target_termios.c_cc[VTIME] = 0;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 cfsetispeed(&target_termios, br_code);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 cfsetospeed(&target_termios, br_code);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 perror("initial tcsetattr on target");
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 exit(1);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 }
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 return 0;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 }
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 set_serial_nonblock(state)
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 int state;
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 {
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 ioctl(target_fd, FIONBIO, &state);
811b138f1bed rvtdump utility written, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 }