/****************************************************************************/
/* Copyright 1990 to 1994 MBARI                                             */
/****************************************************************************/
/* Summary  : Serial communication declarations for Microcontroller Board   */
/* Filename : serial.h                                                      */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV                                                   */
/* Version  : 1.0                                                           */
/* Created  : 11/21/90                                                      */
/* Modified : 08/23/94                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/.cvsroot/micro/h/serial.h,v 1.1.1.1 1997/05/02 17:15:41 pean Exp $
 * $Log: serial.h,v $
 * Revision 1.1.1.1  1997/05/02 17:15:41  pean
 * Initial release of the microcontroller software after Tiburon
 * Moolpool Dive to test IView, Lapboxes, modified Power can
 * GF/5V using bus capacitance mode.
 *
 * Revision 1.1  92/05/14  09:17:53  09:17:53  pean (Andrew Pearce 408-647-3746)
 * Initial revision
 *
*/
/****************************************************************************/

#ifndef SERIAL_H
#define SERIAL_H


#define BAUD_RATE        38400          /* Serial Port Baud Rate            */
#define SERIAL_BUF_SIZE    512          /* Size of Serial Receive Buffer    */
#define FRAME_HDR_SIZE       8          /* Frame overhead in bytes          */

#define DATA_FRAME        0x7e          /* Data Frame Type                  */
#define SRQ_FRAME         0x7d          /* Service Request Frame Type       */
#define SRQ_ACK_FRAME     0x7c          /* SRQ Frame Acknowledge            */
#define KEEP_ALIVE_FRAME  0x7b          /* Keep Alive Frame                 */
#define LINK_RESET_FRAME  0x7a          /* Link Reset Frame                 */

#define DLE 0x10                        /* Delete Character for Framing     */
#define STX 0x02                        /* Start of text character          */
#define ETX 0x03                        /* End of text character            */

#define FRAME_RX_TIME   50              /* 500 msec frame receive timer     */
#define RX_IDLE_TIME   150              /* This is approx 3 character times */
                                        /* at 38400 baud and 10MHz CPU clock*/

#define FRAME_TYPE(hdr)          (hdr & 0x7f)
#define FRAME_SEQ_NO(hdr)       ((hdr & 0x80) >> 7)

#define SET_FRAME_HEADER(type, seq) (Byte) (((seq << 7) & 0x80) | (type & 0x7f))

typedef struct
{
    Byte    hdr;
    Word    length;                         /* Frame Length                 */
    Byte    *buffer;                        /* Pointer to frame data        */
    Byte    *start;                         /* Pointer to frame start       */
} serialFrame;

typedef struct SRQFrame SRQFrame;           /* Make SRQ Frame a data type   */
                                            /* for list handling below      */

struct  SRQFrame                            /* SRQ Frame for SRQ work Q     */
{
    SRQFrame *lst_next;
    SRQFrame *lst_prev;

    serialFrame frame;
    Nat16       ackCount;
};
                                            /* Transmit State Machine states*/
enum txState {IDLE, SOF_DLE, SOF_STX, F_TYPE, DATA, CKSUM, EOF_DLE, EOF_ETX};

#ifdef __STDC__                             /* ANSI C function prototypes   */

Void initDataLinkLayer( Void );

Void dataLinkDisconnectHook( Void (*disconnectFunc) () );

Void setLinkRxTimeout( Nat16 rxTimeout );

Nat16 getLinkRxTimeout( Void );

Void initSerialPort( Void );

Int16 transmitFrame(serialFrame *frame);

Void receiveByte( Void );

Byte* getFrameFromRing(Int16 *frameLen);

#endif

#endif
