FreeCalypso > hg > fc-sim-tools
annotate serial/xmit.c @ 41:5ee00413b8af
serial: beginning of fcsim-serial-be
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 20 Mar 2021 20:53:51 +0000 |
parents | |
children |
rev | line source |
---|---|
41
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements the function for sending byte strings |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * to the SIM and collecting the UART echo. |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/time.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/errno.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <string.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <strings.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <unistd.h> |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 extern int target_fd; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 extern int inverse_coding; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 send_bytes_to_sim(data, nbytes) |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 u_char *data; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 unsigned nbytes; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 { |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 u_char buf1[255], buf2[255]; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 fd_set fds; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 struct timeval tv; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 unsigned rcvd; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 int cc; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 bcopy(data, buf1, nbytes); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (inverse_coding) |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 invert_bytes(buf1, nbytes); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 write(target_fd, buf1, nbytes); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 for (rcvd = 0; rcvd < nbytes; ) { |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 FD_ZERO(&fds); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 FD_SET(target_fd, &fds); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 tv.tv_sec = 1; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 tv.tv_usec = 0; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 cc = select(target_fd+1, &fds, NULL, NULL, &tv); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 if (cc < 0) { |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (errno == EINTR) |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 continue; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 perror("select"); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 return(-1); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (cc < 1) { |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 fprintf(stderr, |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 "error: timeout waiting for echo of Tx bytes\n"); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(-1); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 cc = read(target_fd, buf2 + rcvd, nbytes - rcvd); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (cc <= 0) { |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 perror("read after successful select"); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return(-1); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 rcvd += cc; |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 if (bcmp(buf1, buf2, nbytes)) { |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 fprintf(stderr, "error: UART echo mismatch\n"); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 return(-1); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } else |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return(0); |
5ee00413b8af
serial: beginning of fcsim-serial-be
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |