/* ------------------------------------------------------------------------*/
/* Project:       20051 DRIVER                                             */
/* Filename:      ARC51.H                                                  */
/* Description:   Main interface routine to low level arcnet routines      */
/* Version:       1.5                                                      */
/* Author:        M. Justice;MK;JF                                         */
/* History:       Original 09/15/92                                        */
/*		  mod for 8051 5/7/97	BNL				   */
/* ------------------------------------------------------------------------*/

/* architecture related definitions */
#define map_reg             (*(unsigned char *)0x01ffff) 
#define REGISTER_PAGE       0x01FF00
#define r51_read(reg)                  (*(unsigned char *) (REGISTER_PAGE + reg))
#define r51_write(reg, value)   (*(unsigned char *) (REGISTER_PAGE +reg) = value )
#define idata_mem                       ((unsigned char *)(0x000000))
#define xdata_mem                       ((unsigned char *)(0x010000))

/* read diagnostic, write command register 1 */
#define   TENT_ID     0x04    /* set if tent ID == 1st dest ID ctr of token */ 
#define   EXC_NAK     0x08    /* excessive nak - set if 128 naks have occurred */
#define   TOKEN       0x10    /* token has been sighted */    
#define   RCVACT      0x20    /* data transitions detected on the RXIN pin of device */
#define   DUPID       0x40    /* set if a duplicate node ID is detected */
#define   MYRECON     0x80    /* set when lost token timer times out */

/* write command register */
#define   CLR_TX_INT  0x00    /* used in command chaining operation */
#define   OFFSET      0x20    /* 256 byte offset to specified page */
#define   CLR_RI      0x08    /* clear rx int - used in command chaining op */
#define   DISABLETX   0x01    /* Disable Transmitter */

/* cancel any pending tx command, will set TA to 1 when com20020 next receives the token */
#define   DISABLERX   0x02    /* Disable Receiver. */

/* Cancel any pending rx command.  RI set to 1 the next time the token is received */
#define   ENABLETX    0x03    /* Enable Transmitter */
#define   ENABLERX    0x04    /* Enable Receiver */
#define   BROADCAST   0x80    /* Accept Broadcasts */
#define   SHORTPACKET 0x05    /* Accept Short Packets Only */
#define   LONGPACKET  0x0D    /* Accept Long Packet and Short Packets */
#define   CLEARRECON  0x16    /* Resets the RECON Status Bit */
#define   CLEARPOR    0x0E    /* Resets the POR Status Bit */
#define   CLEARFLAGS  0x1E    /* Resets the POR and RECON Status Bits */

/* read/write address pointer high register 2 */
#define   AUTOINC     0x40    /* 1 (autoinc adr ptr), 1 (no autoinc) */
#define   RDDATA      0x80    /* 0 (next access is a write), 1 (read) */
#define   WRDATA      0x7f    /* 0 (next access is a write), 1 (read) */

/* read/write configuration register 6
   sub address bits 0 & 1 (determines the significance of register 7 */
#define   SUBADR_MASK 0x03
#define   TENT_ID_REG 0x00    /* used to build a node network map */
#define   NODE_ID_REG 0x01    /* set or read ID */
#define   SETUP_REG   0x02    /* set mem access and network clock speed */
#define   NEXT_ID_REG 0x03    /* this is the node we are passing token to */
#define   SUB_AD      0xfc    /* template to preserve all but bits 0  1 */

/* meaning of extended timeout bits 3 & 4 */
#define   ET_MASK     0x18    /* mask to isolate bits 4,3 */
#define   US1130      0x00    /* fastest response, idle and recon times */
#define   US563       0x08
#define   US285       0x10
#define   US78        0x18    /* slowest "" */

#define   BACKPLANE   0x04    /* set to access backplane directly */
#define   TXEN        0x20    /* set to enable device to join network */
#define   CCHEN       0x40    /* set to enable command chaining */
#define   RESET       0x80    /* set to exec software reset */

/* read/write tentid, nodeid, setup register 7 */
#define   SLOW_ARB    0x01    /* set for slow mem access - default = 0 */

/* clock prescaler values */
#define   CLK_MASK    0x06    /* mask to isolate clock prescaler bits 1 & 2 */
#define   CLK2500     0x00    /* fastest (2500 Kbps) network speed */
#define   CLK1250     0x02    
#define   CLK625      0x04
#define   CLK312      0x06    /* slowest (312 Kbps) network speed */

#define   TA          0x01    /* Transmitter Available */
#define   TMA         0x02    /* Transmitted Message Acknowledged */
#define   RECON       0x04    /* Reconfiguration Flag */
#define   TEST        0x08    /* Test Flag */
#define   POR         0x10    /* Poer On Reset Indication */
#define   ETS1        0x20    /* Extended Timeout Status 1 */
#define   ETS2        0x40    /* Extended Timeout Status 2 */
#define   RIH          0x80    /* Receiever Inhibited */
#define   TA2         0x20    /* TA if chaining */
#define   RI2         0x40    /* RI if chaining */

#define   TRUE        1
#define   FALSE       0
#define   VOID        void
#define   USIGN8      unsigned char
#define   USIGN16     unsigned int
#define   USIGN32     unsigned long

#define   E_OK                0       /* operation successful */
#define   E_BAD_STATUS        1       /* bad initialization status */
#define   E_NOT_INITED        3       /* arcnet was never initialized */
#define   E_BAD_COMMAND       4       /* invalid command value */
#define   E_BAD_PARAMETER     5       /* invalid parameter value */
#define   E_BAD_DATA          6       /* invalid data value */
#define   E_NO_PACKET         7       /* no packet available */
#define   E_NO_TOKEN          8       /* no token seen */
#define   E_BAD_PACKET_SIZE   9       /* bad packet size for packet type */
#define   E_TX_BUSY          10       /* transmitter busy */
#define   E_ACK              11       /* write acked */
#define   E_ABORT            12       /* write aborted */
#define   E_DUPID            13       /* dupicate ID - initialization aborted */
#define   E_ONE_NODE         14       /* only one node on the network */
#define   E_NODE_USED        15       /* node address is used or net full */

/* arcware commands */
#define   SETUP              0
#define   INIT               1
#define   READ               2
#define   WRITE              3
#define   STATUS             4
#define   EXIT               5

#define   MAX_COMMAND_NUMBER 5

/* arcware parameters */
#define   NODEID             0
#define   ACCEPTBROADCAST    1
#define   INBUFS             2
#define   OUTBUFS            3
#define   ACCEPTLONG         4
#define   BACKPLANEMODE      5
#define   SPEED              6
#define   TIMEOUT            7
#define   CHAINING           8
#define   IOPORT_LSB        10
#define   IOPORT_MSB        11
#define   INTLEVEL          12
#define   NOWAIT            13
#define   NOCHECK           14

#define   MAX_PARAMETER_NUMBER 14

#define   DELAY(x)    {for (j=0; j<x; j++) for (i=0; i<255; i++);}




#define DELAYUS(x) _opc(0x78); _opc((x * 65) / 100); _opc(0xd8); _opc(0xfe);
/*
On the COM20051, 8051 uC runs at 16MHz (Tosc = 6.25E-08 sec)
Each 'djnz' instruction takes 24 oscillator periods (Tdjnz = 1.5E-06sec).

The timing loop l1 is specified at 1.5E-06 seconds times the prescaler x.
Therefore the 2/3 of x is used.  This macro is more precise at the upper end
Notice:
	x = 0, x = 1 result in maximum, 380us delay
	Lower limit: x = 2 -> 3.7 us
		     x = 10 -> 11 us
				 x - 100 -> 100 us
	Upper limit: 380 microseconds
 
Mnemonics for DELAYMS(x):

	   mov  r0, x
l1:        djnz r0, l1
*/

#define DELAYMS(x) _opc(0x78); _opc(x); _opc(0x79); _opc(0xde); _opc(0xd9); _opc(0xfe); _opc(0x79); _opc(0xde); _opc(0xd9); _opc(0xfe); _opc(0x79); _opc(0xdf); _opc(0xd9); _opc(0xfe); _opc(0xd8); _opc(0xf2);
/*
On the COM20051, 8051 uC runs at 16MHz (Tosc = 6.25E-08 sec)
Each 'djnz' instruction takes 24 oscillator periods (Tdjnz = 1.5E-06sec).

Each subloop (l1, l2, l3) is specified at 333E-06sec, together resulting
in a 1E-03sec

Notice:
	 the limit for this macro is 255 milliseconds.
 
Mnemonics for DELAYMS(x):

	   mov  r0, x
l4:        mov  r1, #222;  222 = 0xde
l1:        djnz r1, l1
	   mov  r1, #222;  222 = 0xde
l2:        djnz r1, l2
	   mov  r1, #223;  223 = 0xdf
l3:        djnz r1, l3
	   djnz r0, l4
*/

#define CLEAR_UART SCON.0 = 0; SCON.2 = 0; dummy[0] = 0;
/*
clear both RI and R8 bits of the SCON Register in 8051
clear the dummy[0] - usual holder for the serial input
*/


USIGN8 arc51_init(void);
USIGN8 arc51_read(USIGN8 *data_ptr);
USIGN8 arc51_write(USIGN8 *data_ptr);
USIGN8 arc51_status(void);
USIGN8 INP(USIGN8  add);
VOID   arc51_exit(void);
VOID   arc51_write_data(USIGN8 page, USIGN8 offset, USIGN8 count, USIGN8 shortlong, USIGN8 *user_buffer);
VOID   arc51_get_check_status(void);
VOID   arc51_interrupt(void);
VOID   OUTP(USIGN8 add,USIGN8 val);
