FreeCalypso > hg > fc-sim-tools
annotate serial/serport.c @ 102:c2f18d7b0a1e
doc/Admin-write-commands: document write-hplmn-timer command
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 14 May 2022 23:20:38 +0000 |
parents | fbedb67d234f |
children |
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 |
53
fbedb67d234f
serial: fix parity for inverse coding convention
Mychaela Falconia <falcon@freecalypso.org>
parents:
39
diff
changeset
|
32 set_serial_params(bps, parity) |
38
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 |
53
fbedb67d234f
serial: fix parity for inverse coding convention
Mychaela Falconia <falcon@freecalypso.org>
parents:
39
diff
changeset
|
36 target_termios.c_iflag = IGNBRK|IGNPAR; |
38
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; |
53
fbedb67d234f
serial: fix parity for inverse coding convention
Mychaela Falconia <falcon@freecalypso.org>
parents:
39
diff
changeset
|
39 if (parity == 1) |
fbedb67d234f
serial: fix parity for inverse coding convention
Mychaela Falconia <falcon@freecalypso.org>
parents:
39
diff
changeset
|
40 target_termios.c_cflag |= PARODD; |
38
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 target_termios.c_lflag = 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 target_termios.c_cc[VMIN] = 1; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 target_termios.c_cc[VTIME] = 0; |
53
fbedb67d234f
serial: fix parity for inverse coding convention
Mychaela Falconia <falcon@freecalypso.org>
parents:
39
diff
changeset
|
44 target_termios.c_ispeed = bps; |
fbedb67d234f
serial: fix parity for inverse coding convention
Mychaela Falconia <falcon@freecalypso.org>
parents:
39
diff
changeset
|
45 target_termios.c_ospeed = bps; |
38
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (ioctl(target_fd, TCSETSF2, &target_termios) < 0) { |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 perror("TCSETSF2"); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 exit(1); |
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 return 0; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 set_serial_nonblock(state) |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 int 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 ioctl(target_fd, FIONBIO, &state); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
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 serial_card_reset() |
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 int mctl_arg = TIOCM_DTR | TIOCM_RTS; |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 ioctl(target_fd, TIOCMBIS, &mctl_arg); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 usleep(20000); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 ioctl(target_fd, TCFLSH, TCIFLUSH); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 ioctl(target_fd, TIOCMBIC, &mctl_arg); |
1d96f3b4f155
serial: started with fcsim-serial-atr
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |