/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name          : hdlc.c
* Author             : jrl
* Date First Issued  : 03/24/2005
* Description        : Code sources of HDLC functions
********************************************************************************
* History:
*  03/24/2005 : Initial
********************************************************************************/
#include "hdlc.h"
/*******************************************************************************
* Function Name      : HDLC_Init
* Description        : Initialize all the HDLC Registers to reset state
* Input              : None
* Return             : None.
*******************************************************************************/
void HDLC_Init(void)
  { 
	HDLC->PARH = 0;
	HDLC->PARL = 0;	HDLC->PARH = 0;
	HDLC->PAMH = 0;
	HDLC->PAML = 0;
	HDLC->GA1 = 0;
	HDLC->GA0 = 0;
	HDLC->GAM1 = 0;
	HDLC->GAM0 = 0;
	HDLC->PRES = 0;
	HDLC->POSS = 0;
	HDLC->TCTL = 0;
	HDLC->RCTL = 0;
	HDLC->BRR = 0;
	HDLC->PRSR = 0;
	HDLC->TFBCR = 0;
	HDLC->RFBCR = 0;
	HDLC->PCR = 0;
	HDLC->ISR = 0;
	HDLC->IMR = 0;
  }
  


/*******************************************************************************
* 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)
 {
	HDLC->TCTL = 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)
 {
	HDLC->RCTL = 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)
 {
	HDLC->PARH = 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)
 {
	HDLC->PARL = 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)
 {
	HDLC->PAMH = 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)
 {
	HDLC->PAML = 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)
 {
	HDLC->GA1 = ((HDLC->GA1) & 0x00FF) | (Broadcast_Address <<8);
 }


/*******************************************************************************
* 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)
 {
	HDLC->GA1 = ((HDLC->GA1) & 0xFF00) | 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)
 {
	HDLC->GA0 = ((HDLC->GA0) & 0x00FF) | (Group_Address1 << 8);
 }

/*******************************************************************************
* 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)
 {
	HDLC->GA0 = ((HDLC->GA0) & 0xFF00) | 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)
 {
	HDLC->GAM1 = ((HDLC->GAM1) & 0x00FF) | (Broadcast_Address_Mask <<8);
 }

/*******************************************************************************
* 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)
 {
	HDLC->GAM1 = ((HDLC->GAM1) & 0xFF00) | 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)
 {
	HDLC->GAM0 = ((HDLC->GAM0) & 0x00FF) | (Group_Address_Mask1 << 8);
 }

/*******************************************************************************
* 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)
 {
	HDLC->GAM0 = ((HDLC->GAM0) & 0xFF00) | 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)
 {
	HDLC->PRES = PreambleSequence;
 }


/*******************************************************************************
* Function Name      : HDLC_PostambleSequenceConfig
* Description        : Writes to the Postamble Sequence register
* Input              : 16 bit Postamble Sequence
* Return             : None.
*******************************************************************************/
void HDLC_PostambleSequenceConfig(u16 PostambleSequence)
 {
	HDLC->POSS = 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)
 {
	HDLC->BRR = (HDLC->BRR & 0x0FFF) | (Tcks <<12);
 }

/*******************************************************************************
* Function Name      : HDLC_XmitBaudrateConfig
* Description        : Writes the Baudrate in the BRR register
* Input              : 12 bit Transmission Baudrate clock divider
* Return             : None.
*******************************************************************************/
void HDLC_XmitBaudrateConfig(u16 Baudrate)
 {
	HDLC->BRR = (HDLC->BRR & 0xF000) | (Baudrate & 0x0FFF);
 }

/*******************************************************************************
* 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)
 {
	HDLC->PRSR = (HDLC->PRSR & 0xFCFF) | (Rclks <<8);
 }

/*******************************************************************************
* 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)
 {
	HDLC->PRSR = (HDLC->PRSR & 0xFF00) | Prescale;
 }

/*******************************************************************************
* Function Name      : HDLC_ReceiveLineStatus
* Description        : Reads the RLS Status bits in HDLC_PSR
* Input              : 
* Return             : Status.
*******************************************************************************/
HDLC_RECEIVE_LINE_STATUS HDLC_ReceiveLineStatus(void)
 {
	u8 rls;

	HDLC_RECEIVE_LINE_STATUS NewState;

	rls = HDLC->PSR & 0x0003;

	if (rls ==0) 
		NewState = HDLC_NOISE;
	else if (rls == 1)
		 NewState = HDLC_IDLE;
	else if (rls == 2)
		 NewState = HDLC_FLAGS;
	else if (rls == 3)
		 NewState = HDLC_FRAME;	
	return NewState;
 }

/*******************************************************************************
* Function Name      : HDLC_ReceiveHalfBufferStatus
* Description        : Reads the RBR  bit in HDLC_PSR
* Input              : 
* Return             : Status.
*******************************************************************************/
HDLC_RECV_HALF_BUFF_READY HDLC_ReceiveHalfBufferStatus(void)
 {
	u8 rbr;

	HDLC_RECV_HALF_BUFF_READY NewState;

	rbr = HDLC->PSR & 0x0008;

	if (rbr ==0) 
		NewState = HDLC_RECV_LOW_HALF_BUFFER_FULL;
	else if (rbr == 8)
		 NewState = HDLC_RECV_HIGH_HALF_BUFFER_FULL;
		
	return NewState;
 }


/*******************************************************************************
* Function Name      : HDLC_XmitHalfBufferStatus
* Description        : Reads the TBR  bit in HDLC_PSR
* Input              : 
* Return             : Status.
*******************************************************************************/
HDLC_XMIT_HALF_BUFF_READY HDLC_XmitHalfBufferStatus(void)
 {
	u8 tbr;

	HDLC_XMIT_HALF_BUFF_READY NewState;

	tbr = HDLC->PSR & 0x0004;

	if (tbr ==0) 
		NewState = HDLC_TX_LOW_HALF_BUFFER_EMPTY;
	else if (tbr == 4)
		 NewState = HDLC_TX_HIGH_HALF_BUFFER_EMPTY;
		
	return NewState;
 }

/*******************************************************************************
* Function Name      : HDLC_ReceiveFrameStatusByte
* Description        : Reads the Status  bits in HDLC_FSBR
* Input              : 
* Return             : FSBR Status bits.
******************************************************************************/

u8 HDLC_ReceiveFrameStatusByte(void)
  {
	u8 status;
	status = (HDLC->FSBR) >>4;

	return status;
  }

/*******************************************************************************
* Function Name      : HDLC_XmitFrameByteCountRead
* Description        : Reads the xmit frame byte count HDLC_TFBCR
* Input              : 
* Return             : xmit frame byte count
******************************************************************************/

u16 HDLC_XmitFrameByteCountRead (void)
  {
	return (HDLC->TFBCR);
  }
	
/*******************************************************************************
* Function Name      : HDLC_XmitFrameByteCountWrite
* Description        : Writes the xmit frame byte count to HDLC_TFBCR
* Input              : bytecount
* Return             : 
******************************************************************************/

void HDLC_XmitFrameByteCountWrite (u16 bytecount)
  {
	HDLC->TFBCR = bytecount;
  }
	
/*******************************************************************************
* Function Name      : HDLC_RecvFrameByteCountRead
* Description        : Reads the recv frame byte count HDLC_RFBCR
* Input              : 
* Return             : recv frame byte count
******************************************************************************/

u16 HDLC_RecvFrameByteCountRead (void)
  {
	return (HDLC->RFBCR);
  }
	
/*******************************************************************************
* Function Name      : HDLC_Xmit_Control
* Description        : Enable/Disable Transmitter
* Input              : ENABLE DISABLE
* Return             : 
******************************************************************************/

void HDLC_Xmit_Control (FunctionalState NewState)
  {
	
	if (NewState == DISABLE)
	 {
		HDLC->PCR = (HDLC->PCR) & 0xFFFD;
	  }
	else 
	  {
		HDLC->PCR = (HDLC->PCR) | 0x2;
	  }
  }
	

/*******************************************************************************
* Function Name      : HDLC_Recv_Control
* Description        : Enable/Disable Receiver
* Input              : ENABLE DISABLE
* Return             : 
******************************************************************************/

void HDLC_Recv_Control (FunctionalState NewState)
  {
	
	if (NewState == DISABLE)
	 {
		HDLC->PCR = (HDLC->PCR) & 0xFFFE;
	  }
	else 
	  {
		HDLC->PCR = (HDLC->PCR) | 0x0001;
	  }
  }

/*******************************************************************************
* 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)
  {
 	if ((HDLC->ISR & Flags) == 0)
		return 0;
	else
		return 1;
  }
	
/*******************************************************************************
* Function Name      : HDLC_ReadPCRStatus
* Description        : Read Interrupt Status
* Input              : One of the HDLC_PCR_Flags- HDLC_TEN,HDLC_REN
* Return             : 1 or 0 = set or reset
******************************************************************************/

u8 HDLC_ReadPCRStatus(HDLC_PCR_Flags Flags)
  {
 	if ((HDLC->PCR & Flags) == 0)
		return 0;
	else
		return 1;
  }

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

void HDLC_ClearInterruptStatus(HDLC_ISR_Flags Flags)
  {
 	HDLC->ISR = (HDLC->ISR) & ~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 )
  {
	if (NewState == ENABLE)
 		HDLC->IMR = (HDLC->IMR) | Flags;
	else
		HDLC->IMR = (HDLC->IMR) & ~Flags;
	
  }

/*******************************************************************************
* 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 )
  {
	if (NewState == ENABLE)
 		HDLC->IMR = (HDLC->IMR) | Flags;
	else
		HDLC->IMR = (HDLC->IMR) & ~Flags;
	
  }

/*******************************************************************************
* 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)
  {
	u32 recv_buf_addr = HDLC_RECEIVEBUFFER_BASE_LO;
	u32 addr;
	u8  i=0;
	u16 bytecount;

	if (HDLC_ReadPCRStatus(HDLC_REN)==1)
		return 1;		//Receiver is busy

	if (HDLC_ReadInterruptStatus(HDLC_RBF))
	  {
		HDLC_ClearInterruptStatus(HDLC_RBF);
		if (HDLC_ReceiveHalfBufferStatus()== HDLC_RECV_HIGH_HALF_BUFFER_FULL)
	  	{
			recv_buf_addr = HDLC_RECEIVEBUFFER_BASE_HI;
	  	}
	  }

	bytecount = HDLC_RecvFrameByteCountRead ();	
	
	
	

	for(addr = recv_buf_addr; addr<(recv_buf_addr + (bytecount));(addr+=1))
	  {
		pBufr[i] = (*(u8 *)addr);
		i++;
	  }
	

	
	HDLC_ClearInterruptStatus(HDLC_RBF | HDLC_RMC); //clear recv flags
	return 0;
  }


/*******************************************************************************
* 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)
  {
	u32 xmit_buf_addr = HDLC_TRANSMITBUFFER_BASE_LO;
	u8  i=0;
	
	if (HDLC_ReadPCRStatus(HDLC_TEN)==1)
		return 1;		//Transmitter is busy
	
	HDLC_ClearInterruptStatus(HDLC_TMC | HDLC_TBE | HDLC_TDU); //clear xmit flags

	for(xmit_buf_addr = HDLC_TRANSMITBUFFER_BASE_LO;xmit_buf_addr<(HDLC_TRANSMITBUFFER_BASE_LO + (bytecount));(xmit_buf_addr+=4))
	  {
		(*(u32 *)xmit_buf_addr) = (pBufr[i+3]<<24)|(pBufr[i+2]<<16)|(pBufr[i+1]<<8)|pBufr[i];
		i=i+4;
	  }
	HDLC->PCR = 0x0003;

	//HDLC_Xmit_Control(ENABLE);
	HDLC_XmitFrameByteCountWrite(bytecount);

	
	return 0;
  }

