FreeCalypso > hg > fc-sim-tools
annotate serial/serport.c @ 46:e2ef4b8e4136
main tools: display spenh info when serial back end is used
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 21 Mar 2021 02:08:02 +0000 |
parents | 61a8ac93764f |
children | fbedb67d234f |
rev | line source |
---|---|
39
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
1 /* |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
2 * This module implements the guts of OS-specific serial port handling |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
3 * for our fc-sim-tools serial back end. The present version is very |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
4 * Linux-specific, using Linux-specific <asm/...> header files, |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
5 * struct termios2 and BOTHER to request arbitrary serial baud rates |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
6 * outside of POSIX Bxxx rigid set. |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
7 */ |
61a8ac93764f
serial/serport.c: add important explanatory comment
Mychaela Falconia <falcon@freecalypso.org>
parents:
38
diff
changeset
|
8 |
38
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <sys/types.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <sys/file.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <sys/ioctl.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <asm/ioctls.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <asm/termbits.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <stdio.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include <stdlib.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include <unistd.h> |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 int target_fd; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 open_serial_port(ttyport) |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 char *ttyport; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 target_fd = open(ttyport, O_RDWR|O_NONBLOCK); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 if (target_fd < 0) { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 perror(ttyport); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 exit(1); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 } |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 ioctl(target_fd, TIOCEXCL); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 return 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 set_serial_params(br) |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 struct termios2 target_termios; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 target_termios.c_iflag = IGNBRK; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 target_termios.c_oflag = 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 target_termios.c_cflag = BOTHER|CLOCAL|HUPCL|CREAD|CS8|CSTOPB|PARENB; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 target_termios.c_lflag = 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 target_termios.c_cc[VMIN] = 1; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 target_termios.c_cc[VTIME] = 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 target_termios.c_ispeed = br; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 target_termios.c_ospeed = br; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (ioctl(target_fd, TCSETSF2, &target_termios) < 0) { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 perror("TCSETSF2"); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 exit(1); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 return 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 } |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 set_serial_nonblock(state) |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 int state; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 ioctl(target_fd, FIONBIO, &state); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 serial_card_reset() |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 int mctl_arg = TIOCM_DTR | TIOCM_RTS; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 ioctl(target_fd, TIOCMBIS, &mctl_arg); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 usleep(20000); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 ioctl(target_fd, TCFLSH, TCIFLUSH); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 ioctl(target_fd, TIOCMBIC, &mctl_arg); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |