File: serial.c

package info (click to toggle)
pm3 1.1.13-11
  • links: PTS
  • area: main
  • in suites: potato
  • size: 174,164 kB
  • ctags: 133,819
  • sloc: ansic: 982,617; modula3: 548,483; cpp: 57,119; exp: 21,673; sh: 17,053; lisp: 13,693; makefile: 13,492; asm: 11,795; yacc: 8,575; sed: 1,100; objc: 476; csh: 254; awk: 223; pascal: 95; fortran: 5
file content (607 lines) | stat: -rw-r--r-- 16,568 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
static char _[] = "@(#)serial.c	5.21 93/10/26 09:47:06, Srini, AMD.";
/******************************************************************************
 * Copyright 1991 Advanced Micro Devices, Inc.
 *
 * This software is the property of Advanced Micro Devices, Inc  (AMD)  which
 * specifically  grants the user the right to modify, use and distribute this
 * software provided this notice is not removed or altered.  All other rights
 * are reserved by AMD.
 *
 * AMD MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS
 * SOFTWARE.  IN NO EVENT SHALL AMD BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL
 * DAMAGES IN CONNECTION WITH OR ARISING FROM THE FURNISHING, PERFORMANCE, OR
 * USE OF THIS SOFTWARE.
 *
 * So that all may benefit from your experience, please report  any  problems
 * or  suggestions about this software to the 29K Technical Support Center at
 * 800-29-29-AMD (800-292-9263) in the USA, or 0800-89-1131  in  the  UK,  or
 * 0031-11-1129 in Japan, toll free.  The direct dial number is 512-462-4118.
 *
 * Advanced Micro Devices, Inc.
 * 29K Support Products
 * Mail Stop 573
 * 5900 E. Ben White Blvd.
 * Austin, TX 78741
 * 800-292-9263
 *****************************************************************************
 *      Engineer: Srini Subramanian.
 *****************************************************************************
 * This module contains the functions to initialize, read, and write to the
 * serial ports (COM1, COM2,...) on a PC.
 *****************************************************************************
 */

#include <stdio.h>
#include <conio.h>
#include <bios.h>
#include <dos.h>  
#include <string.h>
#include "types.h"
#include "memspcs.h"
#include "messages.h"
#include "mtip.h"
#include "tdfunc.h"

/* Serial Port Defs */
 /*
 * Divisors for different baud rates to be used to initialize DLA
 * register.
 */
#define	_DIV_COM_110	1047
#define	_DIV_COM_150	768
#define	_DIV_COM_300	384
#define	_DIV_COM_600	192
#define	_DIV_COM_1200	96
#define	_DIV_COM_2400	48
#define	_DIV_COM_4800	24
#define	_DIV_COM_9600	12
#define	_DIV_COM_19200	6
#define	_DIV_COM_38400	3
#define	_DIV_COM_115200	1

#define	LCR_DLAB	0x80

#define	DLA_LOW_OFFSET	0x0


/*
** Definitions
*/

#define BUF_SIZE               2048

/*
** This data structure is used by the interrupt driven
** serial I/O.
*/

struct serial_io_t {
   int error;                      /* Error code */
   unsigned int    port;           /* Port number */
   unsigned int    port_code;      /* Port code (for bios calls) */
   unsigned int    int_number;     /* Port interrupt number      */
   unsigned int    int_mask;       /* Port interrupt mask        */
   unsigned int    baud;           /* Port baud rate             */
   unsigned int    old_vector_ds;  /* Interrupt vector (old)     */
   unsigned int    old_vector_dx;
   volatile
   unsigned char  *start;          /* Start of ring buffer       */
   volatile
   unsigned char  *end;            /* End of ring buffer         */
   };

static unsigned char   serial_io_buffer[BUF_SIZE];

/* These definitions are from bios.h */
#define CHAR_SIZE         _COM_CHR8
#define STOP_BITS        _COM_STOP1
#define PARITY        _COM_NOPARITY

/*
** Serial port definitions
*/

#define INTR_MASK    0x21    /* 8259 Interrupt Mask Port */
#define INTR_EOI     0x20    /* 8259 EOI Port */

#define COM1         0x3f8   /* COM1 Port Base */
#define COM1_CODE    0x00    /* COM1 Port Code */
#define COM1_INT     0x0c    /* COM1 Interrupt Number */
#define COM1_MASK    0x10    /* COM1 Interrupt Mask (IRQ4) */

#define COM2         0x2f8   /* COM2 Port Base */
#define COM2_CODE    0x01    /* COM2 Port Code */
#define COM2_INT     0x0b    /* COM2 Interrupt Number */
#define COM2_MASK    0x08    /* COM2 Interrupt Mask (IRQ3) */

#define MSR_OFFSET   0x6     /* Modem Status Register offset */
#define LSR_OFFSET   0x5     /* Line status Register offset */
#define MCR_OFFSET   0x4     /* Modem Control Register offset */
#define LCR_OFFSET   0x3     /* Line Control Register offest */
#define	IID_OFFSET   0x2     /* Interrupt pending register */
#define IER_OFFSET   0x1     /* Interrupt Enable Register offest */

/* Bits in Line Status Register (LSR) */
#define AC1   0x80    /* Always clear */
#define TSRE  0x40    /* Transmitter Shift Register Empty */
#define THRE  0x20    /* Transmitter Holding Register Empty */
#define BI    0x10    /* Break Interrupt */
#define FE    0x08    /* Framing Error */
#define PE    0x04    /* Parity Error */
#define OE    0x02    /* Overrun Error */
#define DR    0x01    /* Data Ready */

/* Bits in Modem Control Register */
#define CD    0x80
#define RI    0x40
#define DSR   0x20
#define CTS   0x10
#define OUT2  0x08
#define RTS   0x02
#define DTR   0x01

#define MAX_BLOCK  	1000	

/*  function prototypes */

void   endian_cvt PARAMS((union msg_t *, int));
void   tip_convert32 PARAMS((BYTE *));
INT32	init_parport (char *);

void   interrupt far serial_int PARAMS((void));
void	(interrupt far *OldVector)();
int    get_byte_serial PARAMS((void));

extern	int	BlockCount;
extern	int	lpt_initialize;
/* globals */

struct serial_io_t serial_io;

INT32  in_msg_length=0;
INT32  in_byte_count=0;

/*
** Serial Port functions
*/

/*
** This function is used to initialize the communication
** channel.  First the serial_io data structure is
** initialized.  Then the new interrupt vector is installed.
** Finally, the port is initialized, with DTR, RTS and OUT2
** set.
**
*/

INT32 write_memory_serial (ignore1, ignore2, ignore3, ignore4, ignore5, ignore6)
	INT32 ignore1;
	ADDR32 ignore2;
	BYTE *ignore3;
	INT32 ignore4; 
	INT32 ignore5;
	INT32 ignore6; 
{ 
	return(-1); }

INT32 read_memory_serial (ignore1, ignore2, ignore3, ignore4, ignore5, ignore6)
	INT32 ignore1;
	ADDR32 ignore2;
	BYTE *ignore3;
	INT32 ignore4;
	INT32 ignore5;
	INT32 ignore6;
{ return(-1); }

INT32 fill_memory_serial() { return(-1); }

INT32
init_comm_serial(ignore1, ignore2)
INT32 ignore1;
INT32 ignore2;
   {
   unsigned result;
   unsigned config;
   unsigned   int comm_status;

   /* Initialize serial_io */
   serial_io.error = FALSE;

   /* Set up port number */
   if ((strcmp(tip_config.comm_port, "com1") == 0) ||
       (strcmp(tip_config.comm_port, "com1:") == 0)) {
      serial_io.port = COM1;
      serial_io.port_code = COM1_CODE;
      serial_io.int_number = COM1_INT;
      serial_io.int_mask = COM1_MASK;
      }
   else
   if ((strcmp(tip_config.comm_port, "com2") == 0) ||
       (strcmp(tip_config.comm_port, "com2:") == 0)) {
      serial_io.port = COM2;
      serial_io.port_code = COM2_CODE;
      serial_io.int_number = COM2_INT;
      serial_io.int_mask = COM2_MASK;
      }
   else
      return((INT32) -1);

    /* Check status */
    comm_status = inp(serial_io.port+LSR_OFFSET);
#if 0
    /* reset any communication errors */
    outp(serial_io.port+LSR_OFFSET, 
		       (unsigned int) (comm_status & ~(FE|PE|OE)));
#endif
			       

   /* Get baud rate (Note: MS-DOS only goes to 9600) */
   outp (serial_io.port+LCR_OFFSET, LCR_DLAB);

   if (strcmp(tip_config.baud_rate, "110") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_110);
   else
   if (strcmp(tip_config.baud_rate, "150") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_150);
   else
   if (strcmp(tip_config.baud_rate, "300") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_300);
   else
   if (strcmp(tip_config.baud_rate, "600") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_600);
   else
   if (strcmp(tip_config.baud_rate, "1200") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_1200);
   else
   if (strcmp(tip_config.baud_rate, "2400") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_2400);
   else
   if (strcmp(tip_config.baud_rate, "4800") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_4800);
   else
   if (strcmp(tip_config.baud_rate, "9600") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_9600);
   else
   if (strcmp(tip_config.baud_rate, "19200") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_19200);
   else
   if (strcmp(tip_config.baud_rate, "38400") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_38400);
   else
   if (strcmp(tip_config.baud_rate, "115200") == 0)
      outpw (serial_io.port+DLA_LOW_OFFSET, _DIV_COM_115200);
   else
      return((INT32) -1);  /* EMBAUD); */

   /* Set LCR */
   outp (serial_io.port+LCR_OFFSET, 
		(unsigned int) (_COM_CHR8|_COM_STOP1|_COM_NOPARITY));

   /* Save old interrupt vector */
   OldVector = _dos_getvect (serial_io.int_number);

   /* Initialize ring buffer */
   serial_io.start = serial_io_buffer;
   serial_io.end = serial_io_buffer;

   /* Install interrupt vector */
   /* Note:  the interrupt handler should be in the same code */
   /*        segment as this function.  We will use CS for    */
   /*        the segment offset value.                        */

   _dos_setvect(serial_io.int_number, serial_int); /* new handler */

   /* Turn on DTR, RTS and OUT2 */
   result = outp((serial_io.port+MCR_OFFSET), (DTR | RTS | OUT2));

   /* Enable interrupt on serial port controller */
   result = outp((serial_io.port+IER_OFFSET), 0x01);

   /* Set interrupt mask on 8259 */
   config = inp(INTR_MASK);  /* Get current 8259 mask */
   result = outp(INTR_MASK, (config & ~serial_io.int_mask));

   /* Set global message indices */
   in_msg_length = 0;
   in_byte_count = 0;

   /* initialize parallel port */
   if (lpt_initialize)
      return (init_parport(tip_config.par_port));

   return((INT32) 0);
   }  /* end init_comm_serial() */

/*
** This function is used to send bytes over the serial line.
** If the bytes are successfully sent, a zero is returned.  
** If the bytes are not sent, a -1 is returned.
*/

INT32
send_bfr_serial(bfr_ptr, length, port_base, comm_err)
   BYTE   *bfr_ptr;
   INT32  length;
   INT32  port_base;
   INT32  *comm_err;
   {
   int        retries;
   INT32      byte_count = 0;
   unsigned   int comm_status;
   unsigned   int result;

   /* Send message */
   retries = 0;
   do {

      /* check user interrupt */
      SIGINT_POLL
      /* Check if data ready */
      comm_status = inp(serial_io.port+LSR_OFFSET);

      /* Check for communication errors */
      if ((comm_status & (FE | PE | OE)) != 0) {
	  *comm_err = 1;
	  return (-1);
      }

      /* If Transmitter Holding Register Empty (THRE) */
      /* send out data */
      if ((comm_status & THRE) != 0) {
         result = outp(serial_io.port, bfr_ptr[byte_count]);
         byte_count = byte_count + 1;
         retries = 0;
         } else {
            retries = retries + 1;
            if (retries >= 20000)   
               return (-1);   /* EMNOSEND); */
            }

      } while (byte_count < length );  

   return(0);
   }  /* end send_bfr_serial() */

/*
** This function is used to receive bytes over a serial line.
**
** If block equals NONBLOCK then the function returns as soon
** there are no bytes remaining in the UART.           
** If block equals BLOCK then the function waits until all
** bytes are gotten before returning.
** 
** If all bytes requested are gotten, 0 is returned, else -1.
*/

INT32
recv_bfr_serial(bfr_ptr, length, block, port_base, comm_err)
   BYTE   *bfr_ptr;
   INT32  length;
   INT32  block;
   INT32  port_base;
   INT32  *comm_err;
   {
   int        comm_status;
   int        c;
   int        result;  
   int        bytes_free;

   int      block_count = 0;

   /* Loop as long as characters keep coming */
   for (;;) {

      /* Check for communication errors */
      comm_status = inp(serial_io.port+LSR_OFFSET);
      if ((comm_status & (FE | PE | OE)) != 0)
	  {
	  *comm_err = 1;
	  return (-1);
	  }

      /* Check for buffer overflow */
      if (serial_io.error == TRUE)
 	  {
	  *comm_err = 1;
	  return (-1);
 	  }

      /* Do flow control.  If the buffer is 9/10 full, */
      /* deassert DTR and RTS.  If the buffer becomes */
      /* 1/10 full, reassert DTR and RTS.              */
      bytes_free = (int) (serial_io.start - serial_io.end);
      if (bytes_free <= 0)
         bytes_free = BUF_SIZE + bytes_free;

      comm_status = inp(serial_io.port+MCR_OFFSET);
      if (bytes_free <= (BUF_SIZE/10))
         result = outp((serial_io.port+MCR_OFFSET),
                       (comm_status & ~DTR & ~RTS));

      if (bytes_free >= ((9*BUF_SIZE)/10))
         result = outp((serial_io.port+MCR_OFFSET),
                       (comm_status | DTR | RTS));

      /* Get character */
      c = get_byte_serial();

      /* return if no char & not blocking */
      if ((c == -1) && (block == NONBLOCK))
         return (-1);  

      /* return if no char, blocking, and past block count */
      if ((c == -1) && (block == BLOCK) && (block_count++ > BlockCount))
         return (-1);  

      /* Save byte in bfr_ptr buffer */
      if (c != -1) {
      	  bfr_ptr[in_byte_count] = (BYTE) c;
	  block_count = 0;
      	  in_byte_count = in_byte_count + 1;
	  }

      /* Message received ? */
      if (in_byte_count == length) {
         in_byte_count = 0;
         return(0);
         }
      }  	/* end for(;;) */
   }  		/* end recv_bfr_serial() */


/*
** This function is used to reset the communication
** channel.  This is used when resyncing the host and
** target and when exiting the monitor.
*/

INT32
reset_comm_serial(ignore1, ignore2)
INT32	ignore1;
INT32	ignore2;
   {
   unsigned   int status;

#define	CLEAR_STAT	(int) 1

  do {
    /* Clear LSR */
    inp(serial_io.port+LSR_OFFSET);
    /* Clear RX reg */
    inp (serial_io.port);
    /* Clear MSR */
    inp (serial_io.port+MSR_OFFSET);
    /* interrupt pending ? */
    status = inp(serial_io.port+IID_OFFSET);
  } while (status != CLEAR_STAT);

#if 0
    /* reset any communication errors */
    outp(serial_io.port+LSR_OFFSET, 
		       (unsigned int) (comm_status & ~(FE|PE|OE)));
#endif
			       
   /* Initialize serial_io */
   serial_io.error = FALSE;

   /* Initialize ring buffer */
   serial_io.start = serial_io_buffer;
   serial_io.end = serial_io_buffer;

   /* Set global message indices */
   in_msg_length = 0;
   in_byte_count = 0;

   return((INT32) 0);
   }  /* end reset_comm_serial() */


INT32
exit_comm_serial(ignore1, ignore2)
INT32	ignore1;
INT32	ignore2;
   {
   /* Initialize serial_io */
   serial_io.error = FALSE;

   /* Initialize ring buffer */
   serial_io.start = serial_io_buffer;
   serial_io.end = serial_io_buffer;

   /* Set global message indices */
   in_msg_length = 0;
   in_byte_count = 0;

   /* install old handler back */
   _dos_setvect(serial_io.int_number, OldVector);

   return((INT32) 0);
   }  /* end reset_comm_serial() */

/*
** This function is usually used to "kick-start" the target.
** This is nesessary when targets are shared memory boards.
** With serial communications, this function does nothing.
*/

void
go_serial(ignore1, ignore2)
INT32 ignore1;
INT32 ignore2;
   {
   return;
   }  /* end go_serial() */



/*
** This function is used to get a byte from the the
** serial_io_buffer.  The data in this buffer is written
** by the interrupt handler.
**
** If no data is available, a -1 is returned.  Otherwise
** a character is returned.
*/

int
get_byte_serial()
   {
   int result=-1;

      /* Turn interrupts off while reading buffer */
     _disable();

   /* No bytes available */
   if (serial_io.start == serial_io.end)
      result = -1;
   else {

      /* Return character */
      result = (int) *serial_io.start;
      serial_io.start++;
      /* Check for wrap around */
      if (serial_io.start >= (serial_io_buffer+BUF_SIZE)) {
         serial_io.start = serial_io_buffer;         
      }

    }
      /* Turn interrupts back on */
      _enable();

   return (result);
   }  /* end get_byte_serial() */



/*
** This function is the interrupt handler which buffers
** incoming characters.
**
** Note:  The "interrupt" keyword is not well documented.
**        It produces a procedure which returns with an
**        "iret" instead of the usual "ret".
*/

void interrupt serial_int()
   {
   int c;

   /* Get character */
   c = inp(serial_io.port);

   *serial_io.end = (unsigned char) c;
   serial_io.end++;
   /* Check for wrap around */
   if (serial_io.end >= (serial_io_buffer+BUF_SIZE))
      serial_io.end = serial_io_buffer;         

   /* Has the buffer overflowed? */
   if (serial_io.start == serial_io.end)
      serial_io.error = TRUE;

   /* Send EOI to 8259 */
   (void) outp(INTR_EOI, 0x20);

   }  /* end serial_int() */