/*
 * Copyright (c) 2012, Texas Instruments Incorporated
 * All rights reserved.
 */
#include <inc/hw_memmap.h>
#include <inc/hw_types.h>
#include <inc/hw_ints.h>
#include <inc/hw_gpio.h>
#include <inc/hw_sysctl.h>

#include <driverlib/gpio.h>
#include <driverlib/sysctl.h>
#include <driverlib/i2c.h>
#include <driverlib/ssi.h>
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/hal/Timer.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/bios/include/sem.h>
/* MCUSDK Header files */
// #include <ti/drivers/EMAC.h>
#include <ti/drivers/GPIO.h>
// #include <ti/drivers/I2C.h>
// #include <ti/drivers/SDSPI.h>
 #include <ti/drivers/UART.h>
// #include <ti/drivers/USBMSCHFatFs.h>

/* Board Header file */
#include "Board.h"
/* User Header file */
#include "type_def.h"
/*sub function*/
SEM_Handle Sem;
Task_Handle echo;
Timer_Handle TimerHd_Counter;
//UART_Handle uart;
//UART_Params uartParams;
Error_Block eb;
Task_Params taskParams;
#define MAX 16
UChar a[MAX],pt,n,mb_tx_data;
UChar input[MAX];
void uart1Isr(void)
{
	UWORD i;
	unsigned long ulStatus;
	ulStatus = UARTIntStatus(UART1_BASE, false); // false:返回原始的中断状态。 读取当前中断状态

	if(ulStatus & UART_INT_TX)//判断中断标志
	{
		/*发送数据中断*/
		if(pt+14<=n)//尚未发送的字节不少于14字节
		{
			for(i=0;i<14;i++)
				mb_tx_data =  a[pt++];//再发送14字节
		}
		else
		{
			while(pt<n)
			{
				mb_tx_data =  a[pt++];//发送剩余字节
			}
		}
		if(pt==n)
		{
			UARTIntDisable(UART1_BASE, UART_INT_TX);//关闭发送中断
		}
	}
	else
		if(ulStatus & UART_INT_RX)
	{
			/*接收数据中断*/
			for(i=0;i<8;i++)
		    	while(!UARTCharsAvail(UART1_BASE))
		    	{
		    	}
			a[pt++] = (int)UARTCharGetNonBlocking(UART1_BASE);
	}
		else if(ulStatus & UART_INT_RT)//Receive Timeout Interrupt
		{
			a[pt++] = (int)UARTCharGetNonBlocking(UART1_BASE);
			SEM_post(Sem);//发出接收结束的信号量
		}
}
/*
 * UartTask
 */
Void UartTask(UArg a0, UArg a1)
{
	UChar mb_tx_data;//传送数据暂存器
	UWORD i;
#if 1
    /* Enable Receive UART and its interrupt. */
	//	Out_T_RW = 0;
    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2,0);//T_RW = 0,Receive Enable;
    UARTIntRegister(UART1_BASE, uart1Isr);
    UARTIntClear(UART1_BASE, (UART_INT_TX | UART_INT_TX | UART_INT_RT));
    UARTIntDisable(UART1_BASE, (UART_INT_TX | UART_INT_RT));
    UARTIntEnable(UART1_BASE, UART_INT_RX);
    UARTEnable(UART1_BASE);
    /* Set the FIFO level to 7/8 empty and 7/8 full. */
    UARTFIFOLevelSet(UART1_BASE, UART_FIFO_TX1_8, UART_FIFO_RX7_8);
    UARTFIFOEnable(UART1_BASE);

        pt=0;
        n=0;
        while(1)
        {
            while(1)
            {
            	SEM_pend(Sem, 50);//等待接收结束的信号量
            	if(pt==0)continue;//尚未接收到数据继续等待
            	if(n==pt)break;//接收大数据且稳定不变，则结束等待
            	n=pt;//记忆当前接收到的字节数目，继续等待。
            }
        	n=pt;
        	pt=0;
            /* Set the FIFO level to 7/8 empty and 7/8 full. */
            UARTFIFOLevelSet(UART1_BASE, UART_FIFO_TX1_8, UART_FIFO_RX7_8);
            UARTFIFOEnable(UART1_BASE);

                UARTIntClear(UART1_BASE, (UART_INT_TX | UART_INT_RT));
                UARTIntEnable(UART1_BASE, (UART_INT_TX | UART_INT_RT));
                UARTEnable(UART1_BASE);
                if(n>=14)//帧长度达到14字节以上
                     {
                     	for(i=0;i<14;i++)		//先发送14字节
                     	{
                             mb_tx_data =  a[pt++];
                             UARTCharPutNonBlocking(UART1_BASE,mb_tx_data);
                     	}
                         /* Enable Send UART and its interrupt. */
                     	//	Out_T_RW = 1;
                         GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2,~0);//T_RW = 1,Transmit Enable;
                SEM_pend(Sem, 0);//等待发送结束的信号量
            }
            else for(i=0;i<n;i++)
            	mb_tx_data =  a[pt++];//帧长度不到14一次发完，不需要中断和信号量
        }
#endif
}
/*
 *  ======== main ========
 */
Int main(Void)
{       
    /* Call board init functions. */
    Board_initGeneral();
    Board_initGPIO();
    // Board_initEMAC();
    // Board_initI2C();
    // Board_initSDSPI();
    Board_initUART();
    // Board_initUSB(Board_USBDEVICE);
    // Board_initUSBMSCHFatFs();
    
    System_printf("Starting the example\nSystem provider is set to SysMin, halt the target and use ROV to view output.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();    
    /* Create the task */
     Error_init(&eb);
     Task_Params_init(&taskParams);
     taskParams.instance->name = "echo";
     taskParams.priority = 5;//Task&Swi较高的number对应较高的优先级。
     echo = Task_create(UartTask, &taskParams, &eb);
     if (echo == NULL) {
         System_printf("Task was not created\n");
         System_abort("Aborting...\n");
     }
    /* Turn on user LED */
    GPIO_write(Board_LED, Board_LED_ON);
    
    /* Start BIOS. Will not return from this call. */
    BIOS_start();
    return(0);//huangfang
}
