/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name          : hdlc.h
* Author             : jrl
* Date First Issued  : 23/03/2005
* Description        : Function prototypes, memory mapping and register structures
*			for the HDLC interface
********************************************************************************
* History:
*  23/03/05 : Created
*******************************************************************************/

#ifndef __HDLC_H
#define __HDLC_H

#include "71x_lib.h"

//u8 RecvBuffer[100];

typedef volatile struct
{
  vu16 PARH;
  vu16 EMPTY1;
  vu16 PARL;
  vu16 EMPTY2;
  vu16 PAMH;
  vu16 EMPTY3;
  vu16 PAML;
  vu16 EMPTY4;
  vu16 GA1;
  vu16 EMPTY5;
  vu16 GA0;
  vu16 EMPTY6;
  vu16 GAM1;
  vu16 EMPTY7;
  vu16 GAM0;
  vu16 EMPTY8;
  vu16 PRES;
  vu16 EMPTY9;
  vu16 POSS;
  vu16 EMPTY10;
  vu16 TCTL;
  vu16 EMPTY11;
  vu16 RCTL;
  vu16 EMPTY12;
  vu16 BRR;
  vu16 EMPTY13;
  vu16 PRSR;
  vu16 EMPTY14;
  vu16 PSR;
  vu16 EMPTY15;
  vu16 FSBR;
  vu16 EMPTY16;
  vu16 TFBCR;
  vu16 EMPTY17;
  vu16 RFBCR;
  vu16 EMPTY18;
  vu16 PCR;
  vu16 EMPTY19;
  vu16 ISR;
  vu16 EMPTY20;
  vu16 IMR;
} HDLC_TypeDef;

#define HDLC_BASE      (APB1_BASE + 0xE000)
#define HDLC_RECEIVEBUFFER_BASE_LO  (HDLC_BASE + 0x0800)
#define HDLC_TRANSMITBUFFER_BASE_LO  (HDLC_BASE + 0x0880)
#define HDLC_RECEIVEBUFFER_BASE_HI  (HDLC_BASE + 0x0810)
#define HDLC_TRANSMITBUFFER_BASE_HI  (HDLC_BASE + 0x0890)

#define HDLC_RBUFLO HDLC_RECEIVEBUFFER_BASE_LO*
#define HDLC_RBUFHI HDLC_RECEIVEBUFFER_BASE_HI*
#define HDLC_TXBUFLO HDLC_TRANSMITBUFFER_BASE_LO*
#define HDLC_TXBUFHI HDLC_TRANSMITBUFFER_BASE_HI*


//#ifndef DEBUG
  #define HDLC ((HDLC_TypeDef *)HDLC_BASE)
//#else   /* DEBUG */
//  EXT HDLC_TypeDef *HDLC;

//#endif  /* DEBUG */


typedef enum
{
  HDLC_NPOSB_mask 	= 0x000F,
  HDLC_NPREB_mask 	= 0x00F0,
  HDLC_POSE  		= 0x0100,         
  HDLC_PREE  		= 0x0200,
  HDLC_TCOD_mask 	= 0x0C00,         
  HDLC_TCRCI 		= 0x1000,        
  HDLC_ITF   		= 0x2000,         
  HDLC_SOC   		= 0x4000,
  HDLC_HTEN  		= 0x8000          
} HDLC_TCTL_Flags;

typedef enum
{
  HDLC_NRZ  = (0<<10),
  HDLC_NRZI= (1<<10),
  HDLC_FM0= (2<<10),
  HDLC_MANCH= (3<<10)
} HDLC_TCOD_ENC;


typedef enum
{
  HDLC_R_NRZ,
  HDLC_R_NRZI,
  HDLC_R_FM0,
  HDLC_R_MANCH
} HDLC_RCOD_ENC;


typedef enum
{
  HDLC_RCOD_mask = 0x0003,
  HDLC_RCRCI  	= 0x0004,
  HDLC_SIC  	= 0x0008,         
  HDLC_LBEN  	= 0x0010,         
  HDLC_AEN 	= 0x0020,        
  HDLC_GA0E   	= 0x0040,    
  HDLC_GA1E  	= 0x0080,         
  HDLC_GA2E  	= 0x0100,         
  HDLC_BAE 	= 0x0200,        
  HDLC_PAE   	= 0x0400,         
  HDLC_DPLLE   	= 0x0800,
  HDLC_RCME  	= 0x1000          
} HDLC_RCTL_Flags;

typedef enum
{
  HDLC_CKSource,
  HDLC_HTXCKSource
} HDLC_TCKS;

typedef enum
{
  HDLC_CK,
  HDLC_HTXCK,
  HDLC_HRXCK,
  HDLC_NA
} HDLC_RCLKS;

typedef enum
{
  HDLC_TX_LOW_HALF_BUFFER_EMPTY,
  HDLC_TX_HIGH_HALF_BUFFER_EMPTY
} HDLC_XMIT_HALF_BUFF_READY;

typedef enum
{
  HDLC_RECV_LOW_HALF_BUFFER_FULL,
  HDLC_RECV_HIGH_HALF_BUFFER_FULL
} HDLC_RECV_HALF_BUFF_READY;


typedef enum
{
  HDLC_NOISE,
  HDLC_IDLE,
  HDLC_FLAGS,
  HDLC_FRAME
} HDLC_RECEIVE_LINE_STATUS;

typedef enum
{
  HDLC_RBC = 0x0080,
  HDLC_RDO = 0x0040,
  HDLC_CRC = 0x0020,
  HDLC_RAB = 0x0010
} HDLC_FSBR_Flags;

typedef enum
{
  HDLC_TEN = 0x0002,
  HDLC_REN = 0x0001
} HDLC_PCR_Flags;

typedef enum
{
 HDLC_TMC = 0x0040,
 HDLC_TBE = 0x0020,
 HDLC_TDU = 0x0010,
 HDLC_RBF = 0x0008,
 HDLC_RFO = 0x0004,
 HDLC_RMC = 0x0002,
 HDLC_RME = 0x0001
} HDLC_ISR_Flags;

typedef enum
{
 HDLC_TMCM = 0x0040,
 HDLC_TBEM = 0x0020,
 HDLC_TDUM = 0x0010,
 HDLC_RBFM = 0x0008,
 HDLC_RFOM = 0x0004,
 HDLC_RMCM = 0x0002,
 HDLC_RMEM = 0x0001
} HDLC_IMR_Flags;


/*******************************************************************************
* Function Name      : HDLC_Init
* Description        : Initialize all the HDLC Registers to reset state
* Input              : None
* Return             : None.
*******************************************************************************/

void HDLC_Init(void);

/*******************************************************************************
* Function Name      : HDLC_Init_Xmit
* Description        : Initialize the HDLC Transmission Parameters
* Input              : any combination of HDLC_TCTL_Flags
* Return             : None.
*******************************************************************************/
void HDLC_Init_Xmit(HDLC_TCTL_Flags Flags);
 
/*******************************************************************************
* Function Name      : HDLC_Init_Recv
* Description        : Initialize the HDLC Receive Parameters
* Input              : any combination of HDLC_RCTL_Flags
* Return             : None.
*******************************************************************************/
void HDLC_Init_Recv(HDLC_RCTL_Flags Flags);
 
/*******************************************************************************
* Function Name      : HDLC_PAddressConfigH
* Description        : Writes to the Private Address Register High
* Input              : 16 bit Private Address High value
* Return             : None.
*******************************************************************************/
void HDLC_PAddressConfigH(u16 Private_Address_High);
 
/*******************************************************************************
* Function Name      : HDLC_PAddressConfigL
* Description        : Writes to the Private Address register Low
* Input              : 16 bit Private Address Low value
* Return             : None.
*******************************************************************************/
void HDLC_PAddressConfigL(u16 Private_Address_Low);

 /*******************************************************************************
* Function Name      : HDLC_PAddressMaskConfigH
* Description        : Writes to the Private Address Mask register High
* Input              : 16 bit Private Address Mask High value
* Return             : None.
*******************************************************************************/
void HDLC_PAddressMaskConfigH(u16 Private_Address_Mask_High);
 

/*******************************************************************************
* Function Name      : HDLC_PAddressMaskConfigL
* Description        : Writes to the Private Address Mask register Low
* Input              : 16 bit Private Address Mask Low value
* Return             : None.
*******************************************************************************/
void HDLC_PAddressMaskConfigL(u16 Private_Address_Mask_Low);
 

/*******************************************************************************
* Function Name      : HDLC_BroadcastAddressConfig
* Description        : Writes to the Broadcast Address register
* Input              : 8 bit Broadcast Address
* Return             : None.
*******************************************************************************/
void HDLC_BroadcastAddressConfig(u8 Broadcast_Address);
 

/*******************************************************************************
* Function Name      : HDLC_GroupAddress2Config
* Description        : Writes to the Group Address2 register
* Input              : 8 bit Group Address 2 
* Return             : None.
*******************************************************************************/
void HDLC_GroupAddress2Config(u8 Group_Address2);
 
/*******************************************************************************
* Function Name      : HDLC_GroupAddress1Config
* Description        : Writes to the Group Address1 register
* Input              : 8 bit Group Address 1 
* Return             : None.
*******************************************************************************/
void HDLC_GroupAddress1Config(u8 Group_Address1);
 
/*******************************************************************************
* Function Name      : HDLC_GroupAddress0Config
* Description        : Writes to the Group Address0 register
* Input              : 8 bit Group Address 0
* Return             : None.
*******************************************************************************/
void HDLC_GroupAddress0Config(u8 Group_Address0);
 
/*******************************************************************************
* Function Name      : HDLC_BroadcastAddressMaskConfig
* Description        : Writes to the Broadcast Address Mask register
* Input              : 8 bit Broadcast Address Mask
* Return             : None.
*******************************************************************************/
void HDLC_BroadcastAddressMaskConfig(u8 Broadcast_Address_Mask);
 

/*******************************************************************************
* Function Name      : HDLC_GroupAddressMask2Config
* Description        : Writes to the Group Address Mask 2 register
* Input              : 8 bit Group Address Mask 2 
* Return             : None.
*******************************************************************************/
void HDLC_GroupAddressMask2Config(u8 Group_Address_Mask2);
 
/*******************************************************************************
* Function Name      : HDLC_GroupAddressMask1Config
* Description        : Writes to the Group AddressMask1 register
* Input              : 8 bit Group Address Mask 1 
* Return             : None.
*******************************************************************************/
void HDLC_GroupAddressMask1Config(u8 Group_Address_Mask1);
 
/*******************************************************************************
* Function Name      : HDLC_GroupAddressMask0Config
* Description        : Writes to the Group Address0 register
* Input              : 8 bit Group Address 0
* Return             : None.
*******************************************************************************/
void HDLC_GroupAddressMask0Config(u8 Group_AddressMask0);
 
/*******************************************************************************
* Function Name      : HDLC_PreambleSequenceConfig
* Description        : Writes to the Preamble Sequence register
* Input              : 16 bit Preamble Sequence
* Return             : None.
*******************************************************************************/
void HDLC_PreambleSequenceConfig(u16 PreambleSequence);
 

/*******************************************************************************
* Function Name      : HDLC_PostambleSequenceConfig
* Description        : Writes to the Postamble Sequence register
* Input              : 16 bit Postamble Sequence
* Return             : None.
*******************************************************************************/
void HDLC_PostambleSequenceConfig(u16 PostambleSequence);
 

/*******************************************************************************
* Function Name      : HDLC_XmitClockSourceConfig
* Description        : Writes to the TCKS bit in the BRR register
* Input              : 1 bit Clock source
* Return             : None.
*******************************************************************************/
void HDLC_XmitClockSourceConfig(HDLC_TCKS Tcks);
 
/*******************************************************************************
* Function Name      : HDLC_XmitBaudrateConfig
* Description        : Writes the Baudrate in the BRR register
* Input              : 12 bit Transmission clock divider
* Return             : None.
*******************************************************************************/
void HDLC_XmitBaudrateConfig(u16 Baudrate);
 
/*******************************************************************************
* Function Name      : HDLC_RecvClockSourceConfig
* Description        : Writes to the RCKS bits in the PRSR register
* Input              : 2 bit Clock source
* Return             : None.
*******************************************************************************/
void HDLC_RecvClockSourceConfig(HDLC_RCLKS Rclks);
 
/*******************************************************************************
* Function Name      : HDLC_RecvClockPrescalerConfig
* Description        : Writes to the PRS bits in the PRSR register
* Input              : 8 bit  receive Clock divider
* Return             : None.
*******************************************************************************/
void HDLC_RecvClockPrescalerConfig(u8 Prescale);

/*******************************************************************************
* Function Name      : HDLC_ReceiveLineStatus
* Description        : Reads the RLS Status bits in HDLC_PSR
* Input              : 
* Return             : Status.
*******************************************************************************/
HDLC_RECEIVE_LINE_STATUS HDLC_ReceiveLineStatus(void);
 
/*******************************************************************************
* Function Name      : HDLC_ReceiveHalfBufferStatus
* Description        : Reads the RBR  bit in HDLC_PSR
* Input              : 
* Return             : Status.
*******************************************************************************/
HDLC_RECV_HALF_BUFF_READY HDLC_ReceiveHalfBufferStatus(void);
 

/*******************************************************************************
* Function Name      : HDLC_XmitHalfBufferStatus
* Description        : Reads the TBR  bit in HDLC_PSR
* Input              : 
* Return             : Status.
*******************************************************************************/
HDLC_XMIT_HALF_BUFF_READY HDLC_XmitHalfBufferStatus(void);
 
/*******************************************************************************
* Function Name      : HDLC_ReceiveFrameStatusByte
* Description        : Reads the Status  bits in HDLC_FSBR
* Input              : 
* Return             : FSBR Status bits.
******************************************************************************/

u8 HDLC_ReceiveFrameStatusByte(void);
  
/*******************************************************************************
* Function Name      : HDLC_XmitFrameByteCountRead
* Description        : Reads the xmit frame byte count HDLC_TFBCR
* Input              : 
* Return             : xmit frame byte count
******************************************************************************/

u16 HDLC_XmitFrameByteCountRead (void);
  	
/*******************************************************************************
* Function Name      : HDLC_XmitFrameByteCountWrite
* Description        : Writes the xmit frame byte count to HDLC_TFBCR
* Input              : bytecount
* Return             : 
******************************************************************************/

void HDLC_XmitFrameByteCountWrite (u16 bytecount);
  	
/*******************************************************************************
* Function Name      : HDLC_RecvFrameByteCountRead
* Description        : Reads the recv frame byte count HDLC_RFBCR
* Input              : 
* Return             : recv frame byte count
******************************************************************************/

u16 HDLC_RecvFrameByteCountRead (void);
 	
/*******************************************************************************
* Function Name      : HDLC_Xmit_Control
* Description        : Enable/Disable Transmitter
* Input              : ENABLE DISABLE
* Return             : 
******************************************************************************/

void HDLC_Xmit_Control (FunctionalState NewState);
  
/*******************************************************************************
* Function Name      : HDLC_Recv_Control
* Description        : Enable/Disable Receiver
* Input              : ENABLE DISABLE
* Return             : 
******************************************************************************/

void HDLC_Recv_Control (FunctionalState NewState);
  	
/*******************************************************************************
* Function Name      : HDLC_ReadInterruptStatus
* Description        : Read Interrupt Status
* Input              : One of the HDLC_ISR_Flags
* Return             : 1 or 0 = set or reset
******************************************************************************/

u8 HDLC_ReadInterruptStatus(HDLC_ISR_Flags Flags);  

/*******************************************************************************
* Function Name      : HDLC_ReadPCRStatus
* Description        : Read Interrupt Status
* Input              : One of the HDLC_ISR_Flags
* Return             : 1 or 0 = set or reset
******************************************************************************/

u8 HDLC_ReadPCRStatus(HDLC_PCR_Flags Flags);

/*******************************************************************************
* Function Name      : HDLC_ClearInterruptStatus
* Description        : Clear Interrupt Status bit
* Input              : One of the HDLC_ISR_Flags
* Return             : 
******************************************************************************/

void HDLC_ClearInterruptStatus(HDLC_ISR_Flags Flags);
 
/*******************************************************************************
* Function Name      : HDLC_InterruptMaskBit
* Description        : Enable/Disable Interrupt Mask bit
* Input              : One of the HDLC_IMR_Flags
* Return             : 
******************************************************************************/

void HDLC_InterruptMaskBit(HDLC_IMR_Flags Flags, FunctionalState NewState );
  

/*******************************************************************************
* Function Name      : HDLC_ITConfig
* Description        : Enable/Disable Interrupt Mask bit 1=Interrupt enabled
* Input              : One of the HDLC_IMR_Flags
* Return             : 
******************************************************************************/

void HDLC_ITConfig(HDLC_IMR_Flags Flags, FunctionalState NewState );


/*******************************************************************************
* Function Name      : HDLC_Recv_Frame
* Description        : Retreive Recv buffer
* Input              : p to frame data array, number of bytes to transmit
* Return             : 0=success
*******************************************************************************/

u8 HDLC_Recv_Frame (u8 *pBufr);


/*******************************************************************************
* Function Name      : HDLC_Xmit_Frame
* Description        : Load Xmit buffer and start xmit
* Input              : p to frame data array, number of bytes to transmit
* Return             : 0=success
*******************************************************************************/

u8 HDLC_Xmit_Frame (u8 *pBufr, u16 bytecount);

#endif /* __HDLC_H */

