comparison loadtools/sercomm.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 e7d5ce499693
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This module handles the establishment of serial communication
3 * with the target, i.e., the host-side termios stuff.
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 #include "baudrate.h"
16
17 char *target_ttydev;
18 int target_fd;
19 struct termios target_termios;
20
21 struct baudrate baud_rate_table[] = {
22 /* the first listed rate will be our default */
23 {"115200", B115200, 0},
24 {"57600", B57600, 1},
25 {"38400", B38400, 2},
26 {"19200", B19200, 4},
27 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
28 {"812500", B921600, -1},
29 {"406250", B460800, -1},
30 {"203125", B230400, -1},
31 /* table search terminator */
32 {NULL, B0, -1},
33 };
34 struct baudrate *current_baud_rate;
35
36 open_target_serial()
37 {
38 target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK);
39 if (target_fd < 0) {
40 perror(target_ttydev);
41 exit(1);
42 }
43 target_termios.c_iflag = IGNBRK;
44 target_termios.c_oflag = 0;
45 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
46 target_termios.c_lflag = 0;
47 target_termios.c_cc[VMIN] = 1;
48 target_termios.c_cc[VTIME] = 0;
49 /* start at B19200, as that's what we'll need to use initially */
50 cfsetispeed(&target_termios, B19200);
51 cfsetospeed(&target_termios, B19200);
52 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
53 perror("initial tcsetattr on target");
54 exit(1);
55 }
56 return 0;
57 }
58
59 struct baudrate *
60 find_baudrate_by_name(srch_name)
61 char *srch_name;
62 {
63 struct baudrate *br;
64
65 for (br = baud_rate_table; br->name; br++)
66 if (!strcmp(br->name, srch_name))
67 break;
68 if (br->name)
69 return(br);
70 else {
71 fprintf(stderr, "error: baud rate \"%s\" not known\n",
72 srch_name);
73 return(NULL);
74 }
75 }
76
77 switch_baud_rate(br)
78 struct baudrate *br;
79 {
80 cfsetispeed(&target_termios, br->termios_code);
81 cfsetospeed(&target_termios, br->termios_code);
82 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
83 perror("tcsetattr to switch baud rate");
84 exit(1);
85 }
86 current_baud_rate = br;
87 }