6 void (interrupt EN_FAR
*(old_vect23
))(void);
7 void (interrupt EN_FAR
*(old_vect1b
))(void);
9 /* Trap break etc to allow program to terminate on PC */
10 void interrupt EN_FAR
en_ctrl_brk()
14 void interrupt EN_FAR
en_ctrl_c() {
18 int ser_read(int fd
,char *buf
,int bufsize
)
23 t
= com_getc(&port_handle
);
24 while (t
!= -1 && bufcnt
< bufsize
) {
26 t
= com_getc(&port_handle
);
32 int ser_write(int fd
,char *buf
,int bufsize
)
36 while (bufsize
-- > 0) {
37 while (com_putc(*buf
,&port_handle
) != 0);
45 while (!terminate
&& port_handle
.tx_cnt
!= 0)
48 com_clear_que(RX
| TX
, &port_handle
);
50 printf("Parity err %d framing err %d overrun err %d dropped %d\n",
51 port_handle
.parity_errs
,port_handle
.framing_errs
,
52 port_handle
.overrun_errs
,port_handle
.rx_overflow
);
53 com_port_destroy(&port_handle
);
55 setvect(0x23, old_vect23
); /* restore ^C, ^Break */
56 setvect(0x1b, old_vect1b
);
59 int init_comm(char *cport
,long baud
,int two_stop
)
61 printf("Port: %s, Baud rate: %i\n", port
, baud
);
65 if (sscanf(cport
,"%x,%x",&base
,&irq
) == 2) {
67 com_config(COM1
,base
,irq
);
69 if (strlen(cport
) != 4 || strnicmp(cport
,"COM",3) != 0 ||
70 (cport
[3] > '4' || cport
[3] < '1')) {
71 printf("Illegal port %s, must be com1 to com4\n",cport
);
76 case '1': port
= COM1
; break;
77 case '2': port
= COM2
; break;
78 case '3': port
= COM3
; break;
79 case '4': port
= COM4
; break;
81 printf("Illegal port %s, must be com1 to com4\n",cport
);
85 if( com_port_create(port
, baud
,'N',8,1 + two_stop
, 4096, 4096,
88 printf("Cannot create COM port");
94 /*--------- take over 0x23 and 0x1B to suppress CTRL-C and Break ---------*/
95 old_vect23
= getvect(0x23);
96 old_vect1b
= getvect(0x1b);
97 setvect(0x23, en_ctrl_c
);
98 setvect(0x1b, en_ctrl_brk
);
101 com_clear_que(RX
| TX
, &port_handle
);
103 if (port_handle
.uart_type
== ID_16550
) {
104 fifo_enable(&port_handle
);
105 com_fifo_trigger(RX_TRIG_4
,&port_handle
);
112 int getchar_nonblock(void) {
122 #define ser_read(a,b,c) read(a,b,c)
123 #define ser_write(a,b,c) write(a,b,c)
126 int init_comm(char *, long, int);
129 int init_comm(port
,baud
,two_stop
)
135 printf("Port: %s, Baud rate: %i\n", port
, baud
);
137 struct termios tios
; /* Serial port TERMIO structure */
140 port_fd
= open(port
,O_RDWR
,0);
142 fprintf (stderr
,"init_comm: Open failed on port '%s': ",port
);
147 /* Set to 8 bit selected baud no parity with no special processing */
148 if (tcgetattr(port_fd
,&tios
) < 0) {
149 perror("init_comm: tcgetattr failed");
152 tios
.c_cflag
= CS8
| CREAD
| HUPCL
| CLOCAL
| baud
;
154 tios
.c_cflag
|= CSTOPB
;
159 tios
.c_cc
[VTIME
] = 1;
161 if (cfsetispeed(&tios
,baud
) != 0)
162 printf("init_comm: set ispeed failed\n");
163 if (cfsetospeed(&tios
,baud
) != 0)
164 printf("init_comm: set ospeed failed\n");
166 if (tcsetattr(port_fd
,TCSANOW
,&tios
) < 0) {
167 perror("init_comm: tcsetattr failed");
170 tcflush(port_fd
,TCIOFLUSH
);
174 struct termios orig_tios
; /* Serial port TERMIO structure */
176 void restore_termio() {
177 if (tcsetattr(STDIN_FILENO
,TCSANOW
,&orig_tios
) < 0) {
178 perror("restore_termio: tcsetattr failed");
181 printf("Terminal settings restored\n");
186 void set_input_nonblock() {
187 struct termios tios
; /* Serial port TERMIO structure */
189 signal(SIGINT
, restore_termio
);
190 if (tcgetattr(STDIN_FILENO
,&orig_tios
) < 0) {
191 perror("set_input_nonblock: tcgetattr failed");
194 if (tcgetattr(STDIN_FILENO
,&tios
) < 0) {
195 perror("tcgetattr failed");
199 tios
.c_cc
[VTIME
] = 0;
200 tios
.c_lflag
&= ~(ICANON
| ECHO
);
201 if (tcsetattr(STDIN_FILENO
,TCSANOW
,&tios
) < 0) {
202 perror("set_input_nonblock: tcsetattr failed");
207 int getchar_nonblock(void) {
210 set_input_nonblock();