FreeCalypso > hg > freecalypso-sw
annotate loadtools/sercomm.c @ 84:ccc5161848c7
loadtools: support building for GTA0x AP
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Tue, 20 Aug 2013 04:51:15 +0000 |
parents | f1df95eed62c |
children |
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> |
50
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
12 #include <string.h> |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
13 #include <strings.h> |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 #include <unistd.h> |
49
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
15 #include "baudrate.h" |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 char *target_ttydev; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 int target_fd; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 struct termios target_termios; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 |
49
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
21 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
|
22 /* 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
|
23 {"115200", B115200, 0}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
24 {"57600", B57600, 1}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
25 {"38400", B38400, 2}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
26 {"19200", B19200, 4}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
27 /* 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
|
28 {"812500", B921600, -1}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
29 {"406250", B460800, -1}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
30 {"203125", B230400, -1}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
31 /* table search terminator */ |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
32 {NULL, B0, -1}, |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
33 }; |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
34 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
|
35 |
7
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 open_target_serial() |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 if (target_fd < 0) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 perror(target_ttydev); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 exit(1); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 target_termios.c_iflag = IGNBRK; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 target_termios.c_oflag = 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 target_termios.c_lflag = 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 target_termios.c_cc[VMIN] = 1; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 target_termios.c_cc[VTIME] = 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 /* 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
|
50 cfsetispeed(&target_termios, B19200); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 cfsetospeed(&target_termios, B19200); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) { |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 perror("initial tcsetattr on target"); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 exit(1); |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 } |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 return 0; |
aa1f6fe16fef
loadtools building blocks started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 } |
9
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
58 |
50
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
59 struct baudrate * |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
60 find_baudrate_by_name(srch_name) |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
61 char *srch_name; |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
62 { |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
63 struct baudrate *br; |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
64 |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
65 for (br = baud_rate_table; br->name; br++) |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
66 if (!strcmp(br->name, srch_name)) |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
67 break; |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
68 if (br->name) |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
69 return(br); |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
70 else { |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
71 fprintf(stderr, "error: baud rate \"%s\" not known\n", |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
72 srch_name); |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
73 return(NULL); |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
74 } |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
75 } |
f1df95eed62c
loadtools: -b option works in fc-iram
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
49
diff
changeset
|
76 |
49
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
77 switch_baud_rate(br) |
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
78 struct baudrate *br; |
9
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
79 { |
49
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
80 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
|
81 cfsetospeed(&target_termios, br->termios_code); |
9
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
82 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) { |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
83 perror("tcsetattr to switch baud rate"); |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
84 exit(1); |
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
85 } |
49
54392d1ea474
loadtools: first beginnings for the baud rate switching logic
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
9
diff
changeset
|
86 current_baud_rate = br; |
9
fea204bc7674
fc-sertool compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
7
diff
changeset
|
87 } |