annotate loadtools/sercomm.c @ 49:54392d1ea474

loadtools: first beginnings for the baud rate switching logic
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 23 Jun 2013 02:53:16 +0000
parents fea204bc7674
children f1df95eed62c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module handles the establishment of serial communication
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * with the target, i.e., the host-side termios stuff.
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 */
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/types.h>
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/file.h>
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/ioctl.h>
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <termios.h>
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdio.h>
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <stdlib.h>
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <unistd.h>
49
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
13 #include "baudrate.h"
7
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 char *target_ttydev;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 int target_fd;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 struct termios target_termios;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18
49
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
19 struct baudrate baud_rate_table[] = {
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
20 /* the first listed rate will be our default */
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
21 {"115200", B115200, 0},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
22 {"57600", B57600, 1},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
23 {"38400", B38400, 2},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
24 {"19200", B19200, 4},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
25 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
26 {"812500", B921600, -1},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
27 {"406250", B460800, -1},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
28 {"203125", B230400, -1},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
29 /* table search terminator */
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
30 {NULL, B0, -1},
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
31 };
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
32 struct baudrate *current_baud_rate;
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
33
7
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 open_target_serial()
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 {
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK);
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 if (target_fd < 0) {
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 perror(target_ttydev);
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 exit(1);
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 }
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 target_termios.c_iflag = IGNBRK;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 target_termios.c_oflag = 0;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 target_termios.c_lflag = 0;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 target_termios.c_cc[VMIN] = 1;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 target_termios.c_cc[VTIME] = 0;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 /* start at B19200, as that's what we'll need to use initially */
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 cfsetispeed(&target_termios, B19200);
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 cfsetospeed(&target_termios, B19200);
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 perror("initial tcsetattr on target");
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 exit(1);
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 }
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 return 0;
aa1f6fe16fef loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 }
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
56
49
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
57 switch_baud_rate(br)
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
58 struct baudrate *br;
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
59 {
49
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
60 cfsetispeed(&target_termios, br->termios_code);
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
61 cfsetospeed(&target_termios, br->termios_code);
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
62 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
63 perror("tcsetattr to switch baud rate");
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
64 exit(1);
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
65 }
49
54392d1ea474 loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 9
diff changeset
66 current_baud_rate = br;
9
fea204bc7674 fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 7
diff changeset
67 }