comparison rvinterf/old/before-rvinterf/openport.c @ 0:e7502631a0f9

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