Search Results



peter paul ==> ALDL coms

From owner-diy_efi-archive  Mon May 26 23:05:02 1997
Received: by coulomb.eng.ohio-state.edu (940816.SGI.8.6.9/940406.SGI)
	for diy_efi-outgoing id XAA15986; Mon, 26 May 1997 23:00:39 GMT
Return-Path: <owner-diy_efi>
Received: from edam.direct.ca by coulomb.eng.ohio-state.edu via ESMTP (940816.SGI.8.6.9/940406.SGI)
	for <diy_efi@coulomb.eng.ohio-state.edu> id TAA15853; Mon, 26 May 1997 19:00:25 -0400
Received: from pfenske (van-as-09c03.direct.ca [204.174.245.35]) by edam.direct.ca (8.8.3/8.8.0) with SMTP id QAA17084 for <diy_efi@coulomb.eng.ohio-state.edu>; Mon, 26 May 1997 16:00:08 -0700 (PDT)
Date: Mon, 26 May 1997 16:00:08 -0700 (PDT)
Message-Id: <199705262300.QAA17084@edam.direct.ca>
X-Sender: pfenske@direct.ca
X-Mailer: Windows Eudora Pro Version 2.1.2
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
To: diy_efi
From: peter paul fenske <pfenske@direct.ca>
Subject: ALDL coms
Sender: owner-diy_efi
Precedence: bulk
Reply-To: diy_efi

Hi Gang

here is the updated com software. This will let you command
the ecm and also save the contents to disk.

Enjoy:peter

/**********************************************************
 GCAR1.C                       

***********************************************************/

#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include "comdefs.h"               /* register definitions */
#include "alt_key.h"
#include "ascii.h"

#define BUF_SIZE 200               /* Size of Rx and Tx buffers  */

#define FALSE 0
#define TRUE  !FALSE

#define FORMAT  0x03               /* 8N1 format in LCR    */
#define BAUD  8192

void interrupt com_svc (__CPPARGS);    /* RX data ISR     */
void interrupt (*old_svc) (__CPPARGS); /* old int. vector */

void init_com (int port, char format, int baud);
                                  /* initialize COM port  */
void restore  (int port);         /* restore COM vector   */
void put_com  (char c);           /* transmit data        */
int  get_com  (void);             /* receive data         */
void do_command(void);            /* process <Alt> Keys   */
long atox(char *cmdline);
int  tohex(char letter);

/* The following are treated as global variables and are  */
/* declared outside the main program.                     */

FILE *stream;
char text_buff[140];
int  file_status = FALSE;

int rx_buff[BUF_SIZE];           /* receive data buffer  */
char tx_buff[BUF_SIZE];           /* transmit data buffer  */
int  rx_in_ptr, rx_out_ptr;       /* write & read pointers*/
int  tx_in_ptr, tx_out_ptr;       /* write & read pointers*/
int retransmit_out_ptr, retransmit_in_ptr;
int  transmitting, tx_out_ptr_check;
int  carrier_sense;

int run;                          /* program run/quit flag */
int com_base;                     /* COM port base address */
int displacement;                 /* flow control          */

void main (void)
{
  int c;
  int port;                       /* COM port number 1-4   */

  file_status = FALSE;

  init_com( port=1, FORMAT, BAUD ); /* initialize COM port */

  printf("\n\n                  GM Car Monitor:\n\n\
<alt> t Transmits  <alt> x Exits  <alt> l Loads Hex data\n");

  printf("\nEnter filename to save data  (or <Enter> for no file)\n\
Filename: ");

  gets(text_buff);

  if( isgraph(*text_buff ))
   if ((stream = fopen(text_buff, "wt")) == NULL)
   {
	  printf("Cannot open text file.\n");
   }
   else
	  file_status = TRUE;

  printf("Press <alt> l to load the Hex data Transmit string\nMonitor mode...");

  run = TRUE;                     /* program run/quit flag */
  while( run )                    /* Run till we get bored */
  {
    c = get_com ();               /* check for character;  */
    if (c != -1)                  /* if one was received,  */
    {
      printf("%.2X  ",(unsigned char)c );   /* print it              */
      if(file_status)
        fprintf(stream,"%.2X  ",(unsigned char)c );   /* print it to file     */

    }
    if (kbhit ())                 /* if a key has been hit */
    {
      c = getch ();               /* read char from kybd   */
      if (c == NUL)               /* if it's a NUL,        */
        do_command();             /* 2nd byte as command   */
    }
  }

  if( file_status )
   fclose(stream); /* close file */

  restore(port);
}

void put_com(char c)              /* Fill Tx Buffer       */
{
  int tx_ptr;

    tx_ptr = (tx_in_ptr+1) % BUF_SIZE;    /* point to next spot in */
    if (tx_ptr != tx_out_ptr)             /* buffer; if available */
    {
      tx_buff[tx_ptr] = c;                /* save received data   */
      tx_in_ptr = tx_ptr;                 /* and update rx input ptr */
    }
}

int get_com(void)                        /* Receive character    */
{
  if(rx_out_ptr == rx_in_ptr)
    return (-1);
  else
  {
    displacement--;
    rx_out_ptr = (rx_out_ptr+1) % BUF_SIZE;    /* point to next spot in */
    return (rx_buff[rx_out_ptr]);
  }
}

void do_command(void)
{
  switch (getch())                    /* select command */
  {
   case ALTX:                   /* <alt> x exits program */
    run = FALSE;
    break;

   case ALTT:                   /* <alt> t transmits data */
    printf("--  " );
    if(file_status)
      fprintf(stream,"--  " );   /* print it to file     */

    carrier_sense=1;
    delay(10);
    if(carrier_sense)
    {
      outp( (com_base+MCR), (inp( com_base+MCR ) & ~RTS) ); /* enable Tx */
      tx_out_ptr = retransmit_out_ptr;       /* retrieve transmit pointers  */
      tx_in_ptr  = retransmit_in_ptr;        /* for retransmitting     */
      transmitting=TRUE;
      tx_out_ptr_check=tx_out_ptr;
      if(inp(com_base+LSR) & THRE)
        if( tx_out_ptr != tx_in_ptr )
        {
          tx_out_ptr = (tx_out_ptr+1) % BUF_SIZE;    /* point to next spot */
          outp( com_base+THR, tx_buff[tx_out_ptr] ); /* and Tx */
        }
    }
    break;
   case ALTL:                   /* <alt> l loads hex string */
    printf("\n\n         Load Hex String");
    printf("\nEnter each Hex byte followed by <Enter>");
    printf("\npress <Enter> twice on last byte\n");
    for(;;)
    {
      gets(text_buff);
      if( tohex(*text_buff) < 0 )
        break;
      put_com((char)atox(text_buff));
    }
    retransmit_out_ptr = tx_out_ptr;       /* save transmit pointers  */
    retransmit_in_ptr  = tx_in_ptr;        /* for retransmitting     */
    printf("Press <alt> t to transmit string\n");

    break;
  }
}

/**********************************************************
  INIT_COM - initialize COMx port and interrupt vector
***********************************************************/
void init_com (int port, char format, int baud)
{
  char c;
  int com_int, irq_msk;
  unsigned dbaud;

  dbaud = (unsigned)(115200L/baud); /* rate divisor*/

  switch (port)                   /* port-specific defns. */
  {
    case 1:
      com_base = COM1;            /* port base address    */
      com_int  = INT_C;           /* hardware int number  */
      irq_msk  = IRQ4M;           /* PIC interrupt mask   */
      break;
    case 2:
      com_base = COM2;            /* port base address    */
      com_int  = INT_B;           /* hardware int number  */
      irq_msk  = IRQ3M;           /* PIC interrupt mask   */
      break;
    case 3:
      com_base = COM3;            /* port base address    */
      com_int  = INT_C;           /* hardware int number  */
      irq_msk  = IRQ4M;           /* PIC interrupt mask   */
      break;
    case 4:
      com_base = COM4;            /* port base address    */
      com_int  = INT_B;           /* hardware int number  */
      irq_msk  = IRQ3M;           /* PIC interrupt mask   */
      break;
    default:
      break;
  };

  asm CLI;                        /* disable interrupts   */

  outp (com_base+LCR, DLAB);      /* access divisor latch */
  outp (com_base+DLL, (char)dbaud); /* set UART bit rate   */
  outp (com_base+DLM, (char)(dbaud >> 8));
  outp (com_base+LCR, format);    /* set word length, etc.*/

  old_svc = getvect (com_int);    /* save old COMx vector */
  setvect (com_int, com_svc);     /* vector -> com_svc () */

  rx_in_ptr = rx_out_ptr = 0;           /* init buffer pointers */
  tx_in_ptr = tx_out_ptr = 0;           /* init buffer pointers */
  retransmit_out_ptr = retransmit_in_ptr = 0;
  transmitting = 0;
  carrier_sense = 0;
  displacement = 0;

  c = inp (PIC_IMR);
  outp (PIC_IMR, c & ~irq_msk);           /* enable interrupt on  */
  outp (com_base+IER, ERBFI | ETBEI);     /* Data Ready & THRE  */
  c = inp (com_base+RBR);                 /* clear RX data reg    */
  outp (com_base+MCR, OUT2|DTR|RTS);      /* gate 8250 INT to 8259 */
  asm STI;                                /* enable interrupts    */
}

/**********************************************************
  RESTORE - disable COMx interrupt and restore old vector

  You *must* call this function before you exit!
***********************************************************/
void restore (int port)
{
  char c;
  int com_int, irq_msk;

  switch (port)                   /* port-specific defns. */
  {
    case 1:
      com_base = COM1;            /* port base address    */
      com_int  = INT_C;           /* hardware int number  */
      irq_msk  = IRQ4M;           /* PIC interrupt mask   */
      break;
    case 2:
      com_base = COM2;            /* port base address    */
      com_int  = INT_B;           /* hardware int number  */
      irq_msk  = IRQ3M;           /* PIC interrupt mask   */
      break;
    case 3:
      com_base = COM3;            /* port base address    */
      com_int  = INT_C;           /* hardware int number  */
      irq_msk  = IRQ4M;           /* PIC interrupt mask   */
      break;
    case 4:
      com_base = COM4;            /* port base address    */
      com_int  = INT_B;           /* hardware int number  */
      irq_msk  = IRQ3M;           /* PIC interrupt mask   */
      break;
    default:
      break;
  };

  c = inp (PIC_IMR);
  outp (PIC_IMR, c | irq_msk);    /* disable COMx int     */
  outp (com_base+IER, 0x00);
  outp (com_base+MCR, 0x00);      /* all modem outputs off */
  setvect (com_int, old_svc);     /* restore COMx vector  */
}

/**********************************************************
  COM_SVC - receive and transmit data interrupt service routine

  Reads received character from 8250 and places it in the
  receive data buffer if there is room - if there isn't,
  the character is discarded.  Note that wraparound of the
  buffer pointer is handled by incrementing modulo BUF_SIZE.
***********************************************************/
void interrupt com_svc (__CPPARGS)
{
  char c;
  int  rx_ptr, status;

  carrier_sense = 0;

  if( (status=inp(com_base+IIR)) & ID1 )  /* Receive Interrupt? */
  {
    c = inp (com_base+RBR);               /* read received char   */
    rx_ptr = (rx_in_ptr+1) % BUF_SIZE;    /* point to next spot in */
    if (rx_ptr != rx_out_ptr)             /* buffer; if available */
    {
      rx_buff[rx_ptr] = c;                /* save received data   */
      rx_in_ptr = rx_ptr;                 /* and update rx input ptr */
       displacement++;
    }
    if(transmitting)
    {
      tx_out_ptr_check = (tx_out_ptr_check+1) % BUF_SIZE;
      if(c != tx_buff[tx_out_ptr_check])
           tx_out_ptr = tx_in_ptr;     // Kill packet transmission
    }

    if( (inp(com_base+LSR) & (THRE)) == (THRE) ) // if so, disable Tx
    {
      outp( (com_base+MCR), (inp( com_base+MCR ) |  RTS) );
      transmitting = FALSE;
    }
  }
  if( status & ID0 )     /* Transmit Interrupt? */
  {
    if( tx_out_ptr != tx_in_ptr )
   {
      tx_out_ptr = (tx_out_ptr+1) % BUF_SIZE;    /* point to next spot */
      outp( com_base+THR, tx_buff[tx_out_ptr] ); /* and Tx */
   }
  }

//  Flow control may be implemented somewhere else.
//  if( displacement >= (int)(0.8*BUF_SIZE) )
//      put_com(DC3);

//  if( displacement <= (int)(0.2*BUF_SIZE) )
//      put_com(DC1);

  outp (PIC_ICR, EOI);            /* 8259 end of interrupt */
}

long  atox(char *cmdline)     /* see tohex for details	*/
{
	long	x = 0;
	int	y;

	while( (y = tohex(*cmdline++)) >= 0 )
		x = (x << 4) + y;

	return x;
}

int  tohex(char letter)       /* Returns TRUE (number) or ERROR (negative)	*/
{
	if( (letter >= '0') && (letter <= '9') )
		letter = letter - '0';
	else	if( (letter >= 'A') && (letter <= 'F') )
			letter = letter - 'A' + 10;
		else	if( (letter >= 'a') && (letter <= 'f') )
				letter = letter - 'a' + 10;
			else
				letter = -1;

	return (int)letter;
}



John S. Gwynne <jsg@coulomb.eng.ohio-state.edu>