/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : main.c
* Author             : jrl
* Date First Issued  : 4/1/05
* Description        : Template file
***************************************************************

This program will only work with the STR71x devices.

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

#include "UART0_stdio.h"

#include "71x_lib.h"
#include "timers.h"
#include "uart.h"
#include "hdlc.h"

u16 it_val = 0;

static void RTC_ClearCounter (void)
{
  // Wait For Last Task Completion
  RTC_WaitForLastTask();
  // Enter In Configuration Mode
  RTC_EnterCfgMode();
  // Wait For Last Task Completion
  RTC_WaitForLastTask();
  // Clears RTC counter
  RTC->CNTH =0x0000;
  // Wait For Last Task Completion
  RTC_WaitForLastTask();
  RTC->CNTL =0x0000;
  // Exit From Configuration Mode
  RTC_ExitCfgMode ();
  // Wait For Last Task Completion
  RTC_WaitForLastTask();
  // Wait For Last Task Completion
  RTC_WaitForLastTask ();
}


bool Test_HDLC_RAM(u32 bufferbase)
{
  u32 address;
  u32 i = 0xaaaaaaaa;
  //write pattern to Xmit buffer
  for (address = bufferbase; address < (bufferbase+ 0x80); address += 4)
  {
   *(u32 *)address=i;
   i=~i;
  }
  
  //verify pattern is correct
  i=0xaaaaaaaa;
  for (address = bufferbase; address < (bufferbase+ 0x80); address += 4) 
  {
   if (*(u32 *)address != i)
    {
      return FALSE;
    }
    i=~i;
  }
  return TRUE;
}

/*******************************************************************************
* Function Name  : clks_init
* Description    : Initialize device clocks
* Input          : None
* Return         : None
*******************************************************************************/

void clks_init(void)
  {
	RCCU_Div2Config (ENABLE);  		  //OSC input is divide by 2 (4Mhz/2 = 2Mhz)
	RCCU_PLL1Config(RCCU_PLL1_Mul_24, RCCU_Div_1); //RCLK = 48MHz
	RCCU_RCLKSourceConfig (RCCU_PLL1_Output);
	RCCU_MCLKConfig(RCCU_DEFAULT);		//MCLK=48MHz
	RCCU_PCLKConfig(RCCU_Div_2);		//PCLK2=24MHz APB2 periph        TIMER2 on APB2
 	RCCU_FCLKConfig(RCCU_Div_2);		//PCLK1=24MHz APB1 periph (fast) HDLC on APB1

	RCCU_PLL2Config(RCCU_PLL2_Mul_12, RCCU_Div_2);

	//setup timer2 (P0.13) for external HDLC clock connected to HCLK
	TIM_ClockSourceConfig (TIM2, TIM_INTERNAL );
	TIM_PrescalerConfig (TIM2,10 ); //24Mhz/77 = 312KHz
	TIM_PWMOModeConfig (TIM2,0x7,TIM_LOW,0x7,TIM_HIGH); //square wave output
	TIM_CounterConfig(TIM2, TIM_START);
  }

/*******************************************************************************
* Function Name  : EMI_init
* Description    : Initialize EMI banks
* Input          : None
* Return         : None
*******************************************************************************/

void EMI_init (void)
  {
	GPIO_Config(GPIO2, 0x0008, GPIO_AF_PP); // enable alternal function P2.0-3 for all CS
	EMI_Config(3,EMI_ENABLE|EMI_WAITSTATE(7)|EMI_SIZE_8);
	GPIO_Config(GPIO2, 0x000F, GPIO_AF_PP);
	EMI_Config(1,EMI_ENABLE|EMI_WAITSTATE(7)|EMI_SIZE_8);
	EMI_Config(0,EMI_ENABLE|EMI_WAITSTATE(7)|EMI_SIZE_16);
	EMI_Config(2,EMI_ENABLE|EMI_WAITSTATE(7)|EMI_SIZE_16);
  }
 
void IO_Init(void)
{
	u16 uMask;

	// setup GPIO 1.13 (HDLC HCLK pin) as input Tristate CMOS
	uMask = 1<<13;
	GPIO1->PC0 &=~uMask;
	GPIO1->PC1 |= uMask;
	GPIO1->PC2 &=~uMask;


	// setup GPIO 1.14 (HDLC HRXD pin) as input Tristate CMOS
	uMask = 1<<14;
	GPIO1->PC0 &=~uMask;
	GPIO1->PC1 |= uMask;
	GPIO1->PC2 &=~uMask;

	// setup GPIO 1.15 (HDLC HTXD pin) as output alternate Push-pull
	uMask = 1<<15;
	GPIO1->PC0 |= uMask;
	GPIO1->PC1 |= uMask;
 	GPIO1->PC2 |= uMask;
	// setup GPIO 0.13 (T2 OCMPA pin) as output alternate Push-pull
	uMask = 1<<13;
	GPIO0->PC0 |= uMask;
	GPIO0->PC1 |= uMask;
 	GPIO0->PC2 |= uMask;

}



	
u16 data1 = 0;
u8 XmitBuffer[]={0xaa,0x55,0,0,37,'a','b','c','d','e','f','g','h','i','j',
		  'k','l','m','n','o','p','q','r','s','t',
		'u','v','w','x','y','z','0','1','2','3',
		'4','5','6','7','8','9',0,'g','h','i','j',
		'a','b','c','d','e','f','g','h','i','j',
		'a','b','c','d','e','f','g','h','i','j',
		'a','b','c','d','e','f','g','h','i','j',
		'a','b','c','d','e','f','g','h','i','j'

};
u8 RecvBuffer[100];

	  
//******************************************************************************
int main(void)
{
  #ifdef DEBUG
    debug();
  #endif
  printf("\nHDLC REva demo");

clks_init();
	//need to reconfigure UART after clock config
  UART_Config(UART0,9600,UART_NO_PARITY,UART_1_StopBits,UARTM_8D);
HDLC_Init();		//set all HDLC regs to 0
IO_Init();		//GPIO set to HDLC AF
rtcinit();

printf("\nXmit test ");
if (Test_HDLC_RAM(0xC000E880))
  printf("OK");
else 
  printf("failed");

printf("\nRecv buffer test ");
if (Test_HDLC_RAM(0xC000E800))
  printf("OK");
else 
  printf("failed");
 

 
HDLC_XmitClockSourceConfig(HDLC_CKSource);
HDLC_XmitBaudrateConfig(76);	//APB1 Pclk1/153.6Kbaud = 76
HDLC_Init_Xmit(HDLC_NRZ);

//Set up Receiver
HDLC_Init_Recv(HDLC_PAE | HDLC_R_NRZ);
HDLC_PAddressConfigH(0x0000);
HDLC_PAddressMaskConfigH(0x0000);
//HDLC_PAddressConfigL(0x00AA);          //Rcvr will recognize this single byte address
HDLC_PAddressConfigL(0x0000);          //No filter for address
//HDLC_PAddressMaskConfigL(0x00FF);    //Mask for address filtering
HDLC_PAddressMaskConfigL(0x0000);


HDLC_RecvClockSourceConfig(HDLC_CK);
HDLC_RecvClockPrescalerConfig(76);


*(u32*)HDLC_RECEIVEBUFFER_BASE_LO = 0; //clear so a frame reject prints 0 data.
HDLC_Xmit_Control(DISABLE);


//***************** Test 1 Internal clocking, print receive buffer*********************
//HDLC_Xmit_Frame (XmitBuffer, 42);
HDLC_Xmit_Frame (XmitBuffer, 31);
wait_ms(500);
if(HDLC_ReadInterruptStatus(HDLC_RMC))
  {
	HDLC_Recv_Frame (RecvBuffer);

	printf("\nPCR= %x",HDLC->PCR);
	printf("\nrcvbuffer= %x\n  %s",
		*(u32*)HDLC_RECEIVEBUFFER_BASE_LO, // Print receive buffer contents
		(HDLC_RECEIVEBUFFER_BASE_LO +4));

	printf("\nPCR= %x",HDLC->PCR);
 }
else
  {
	printf("\nLoopback Test failed");
  }


//************************ Test 2 Interrupts ****************************
//RTC count value is put in xmit frame. Interrupt will fire when a good frame is received.
//Received RTC tick value is printed.
// Set the HDLC interrupt channel priority level to 2
EIC_IRQChannelPriorityConfig(HDLC_IRQChannel,2);
// Enable the HDLC IRQ channel interrupts
EIC_IRQChannelConfig(HDLC_IRQChannel,ENABLE);
// HDLC->IMR = 0x0F;  //Enable all received interrupts
HDLC_ITConfig(HDLC_RMCM, ENABLE ); //enable good frame received interrupt

// Enable global IRQ interrupts
EIC_IRQConfig(ENABLE);

RTC_ClearCounter();

while (1)
  {
	
	
	*(u32*)(XmitBuffer+4) = RTC_CounterValue ();
	printf("\nXmit tick = %us", *(u32*)(XmitBuffer+4));
	HDLC_Xmit_Frame (XmitBuffer, 42);
	wait_ms (1000);
	printf("\nRcv tick = %us",*(u32*)(RecvBuffer+4));

  }
  
  
}




