/*******************************************************************************
* File Name: USBUART_1_drv.c
* Version 0.0
*
*  Description:
*    Endpoint 0 Driver for the USBFS Component.
*
*   Note:
*
********************************************************************************
* Copyright 2008-2009, Cypress Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions, 
* disclaimers, and limitations in the end user license agreement accompanying 
* the software package with which this file was provided.
********************************************************************************/



#include "cydevice.h"
#include "cyfitter.h"
#include "USBUART_1.h"

/*******************************************************************************
* Forward references for the EP0 ISR
********************************************************************************/
void  USBUART_1_HandleSetup(void);
void  USBUART_1_HandleIN(void);
void  USBUART_1_HandleOUT(void);
uint8 USBUART_1_InitControlRead(void);
void  USBUART_1_ControlReadDataStage(void);
void  USBUART_1_ControlReadStatusStage(void);
void  USBUART_1_ControlReadPrematureStatus(void);
uint8 USBUART_1_InitControlWrite(void);
void  USBUART_1_ControlWriteDataStage(void);
void  USBUART_1_ControlWriteStatusStage(void);
void  USBUART_1_ControlWritePrematureStatus(void);
uint8 USBUART_1_InitNoDataControlTransfer(void);
void  USBUART_1_NoDataControlStatusStage(void);
void USBUART_1_InitializeStatusBlock(void);
void USBUART_1_UpdateStatusBlock(uint8 bCompletionCode);

/*******************************************************************************
* Request Handlers
********************************************************************************/
uint8 USBUART_1_HandleStandardRqst(void);
uint8 USBUART_1_DispatchClassRqst(void);
uint8 USBUART_1_HandleVendorRqst(void);

/*******************************************************************************
* External data references
********************************************************************************/

/*******************************************************************************
* Global data allocation
********************************************************************************/
T_USBUART_1_EP_CTL_BLOCK USBUART_1_EP[9];
uint8 USBUART_1_bEPHalt;
uint8 USBUART_1_bConfiguration;
uint8 USBUART_1_bInterfaceSetting[9];
uint8 USBUART_1_bDeviceAddress;
uint8 USBUART_1_bDeviceStatus;
uint8 USBUART_1_bDevice;
/*******************************************************************************
* Local data allocation
********************************************************************************/
uint8 USBUART_1_bEP0Toggle;
uint8 USBUART_1_bLastPacketSize;
uint8 USBUART_1_bTransferState;
T_USBUART_1_TD CurrentTD;
uint8 USBUART_1_bEP0Mode;
uint8 USBUART_1_bEP0Count;
uint16 USBUART_1_TransferByteCount;

/*******************************************************************************
* Function Name: USBUART_1_ep_0_Interrupt
********************************************************************************
* Summary:
*   This Interrupt Service Routine handles Endpoint 0 (Control Pipe) traffic.  It
*   dispactches setup requests and handles the data and status stages.
*   
* Parameters:  
*   None
*******************************************************************************/
CY_ISR(USBUART_1_EP_0_ISR)
{
	uint8 bRegTemp = *USBUART_1_EP0_CR;
		
	if (!bRegTemp & USBUART_1_MODE_ACKD) return;             
	if (bRegTemp & USBUART_1_MODE_SETUP_RCVD) {
		USBUART_1_HandleSetup();
	} 
	else if (bRegTemp & USBUART_1_MODE_IN_RCVD) {
		USBUART_1_HandleIN();	
	}
	else if (bRegTemp & USBUART_1_MODE_OUT_RCVD) {
		USBUART_1_HandleOUT();
	}
	else {
//		ASSERT(0);
	}
	CY_SET_REG8(USBUART_1_EP0_CNT, USBUART_1_bEP0Toggle | USBUART_1_bEP0Count);
	/* Set the Mode Register  */
	CY_SET_REG8(USBUART_1_EP0_CR, USBUART_1_bEP0Mode);
	
   /* PSoC3 ES1, ES2 RTC ISR PATCH  */ 
    #if(CYDEV_CHIP_DIE_EXPECT == CYDEV_CHIP_DIE_LEOPARD)
        #if((CYDEV_CHIP_REV_EXPECT <= CYDEV_CHIP_REV_LEOPARD_ES2) && (USBUART_1_ep_0__ES2_PATCH))      
            USBUART_1_ISR_PATCH();
        #endif
    #endif	
	
}
/*******************************************************************************
* Function Name: USBUART_1_HandleSetup
********************************************************************************
* Summary:
*   This Routine dispatches requests for the four USB request types
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_HandleSetup(void)
{
	uint8 bRequestHandled;
    /* In case the previous transfer did not complete, close it out */
    USBUART_1_UpdateStatusBlock(USBUART_1_XFER_PREMATURE);
    
    switch (CY_GET_REG8(USBUART_1_bmRequestType) & USBUART_1_RQST_TYPE_MASK)
	{
	case USBUART_1_RQST_TYPE_STD:
		bRequestHandled = USBUART_1_HandleStandardRqst();
		break;
	case USBUART_1_RQST_TYPE_CLS:
		bRequestHandled = USBUART_1_DispatchClassRqst();
		break;
	case USBUART_1_RQST_TYPE_VND:
		bRequestHandled = USBUART_1_HandleVendorRqst();
		break;
	default:
		bRequestHandled = USBUART_1_FALSE;
		break;
	}
	if (bRequestHandled == USBUART_1_FALSE){
		USBUART_1_bEP0Mode = USBUART_1_MODE_STALL_IN_OUT;
	}
}
/*******************************************************************************
* Function Name: USBUART_1_HandleIN
********************************************************************************
* Summary:
*   This routine handles EP0 IN transfers.
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_HandleIN(void)
{
	switch (USBUART_1_bTransferState)
	{
	case USBUART_1_TRANS_STATE_IDLE:
		break;
	case USBUART_1_TRANS_STATE_CONTROL_READ:
		USBUART_1_ControlReadDataStage();
		break;
	case USBUART_1_TRANS_STATE_CONTROL_WRITE:
		USBUART_1_ControlReadStatusStage();
		break;
	case USBUART_1_TRANS_STATE_NO_DATA_CONTROL:
		USBUART_1_NoDataControlStatusStage();
		break;
	default:
		break;	
	}
}
/*******************************************************************************
* Function Name: USBUART_1_HandleOUT
********************************************************************************
* Summary:
*   This routine handles EP0 OUT transfers.
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_HandleOUT(void)
{
	switch (USBUART_1_bTransferState)
	{
	case USBUART_1_TRANS_STATE_IDLE:
		break;
	case USBUART_1_TRANS_STATE_CONTROL_READ:
		USBUART_1_ControlReadStatusStage();
		break;
	case USBUART_1_TRANS_STATE_CONTROL_WRITE:
		USBUART_1_ControlWriteDataStage();
		break;
	case USBUART_1_TRANS_STATE_NO_DATA_CONTROL:
        /* Update the completion block */
        USBUART_1_UpdateStatusBlock(USBUART_1_XFER_ERROR);
        /* We expect no more data, so stall INs and OUTs */
        USBUART_1_bEP0Mode = USBUART_1_MODE_STALL_IN_OUT;
		break;
	default:
		break;	
	}
}
/*******************************************************************************
* Function Name: USBUART_1_LoadEP0
********************************************************************************
* Summary:
*   This routine loads the EP0 data registers for OUT transfers.  It uses the
*   CurrentTD (previously initialized by the _InitControlWrite function and
*   updated for each OUT transfer, and the bLastPacketSize) to determine how
*   many uint8s to transfer on the current OUT.
*
*   If the number of uint8s remaining is zero and the last transfer was full, 
*   we need to send a zero length packet.  Otherwise we send the minimum
*   of the control endpoint size (8) or remaining number of uint8s for the
*   transaction.
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_LoadEP0(void)
{
	/* Update the transfer byte count from the last transaction */
    USBUART_1_TransferByteCount += USBUART_1_bLastPacketSize;
    /* Now load the next transaction */
    USBUART_1_bEP0Count = 0;
	while ((CurrentTD.wCount > 0) && (USBUART_1_bEP0Count < 8))
	{
        USBUART_1_EP0_DR0[USBUART_1_bEP0Count] = *CurrentTD.pData++;
		USBUART_1_bEP0Count++;
		CurrentTD.wCount--;
	}

	if ((USBUART_1_bEP0Count > 0) || (USBUART_1_bLastPacketSize == 8))
	{
		/* Update the data toggle */
		USBUART_1_bEP0Toggle ^= USBUART_1_EP0_CNT_DATA_TOGGLE;
		/* Set the Mode Register  */
		USBUART_1_bEP0Mode = USBUART_1_MODE_ACK_IN_STATUS_OUT;
		/* Update the state (or stay the same) */
		USBUART_1_bTransferState = USBUART_1_TRANS_STATE_CONTROL_READ;
	}
	else
	{
		/* Expect Status Stage Out */
		USBUART_1_bEP0Mode = USBUART_1_MODE_STATUS_OUT_ONLY;
		/* Update the state (or stay the same) */
		USBUART_1_bTransferState = USBUART_1_TRANS_STATE_CONTROL_READ;
	}

	/* Save the packet size for next time */
	USBUART_1_bLastPacketSize = USBUART_1_bEP0Count;
}
/*******************************************************************************
* Function Name: USBUART_1_InitControlRead
********************************************************************************
* Summary:
*   Initialize a control read transaction
*   
* Parameters:  
*   None
*******************************************************************************/
uint8 USBUART_1_InitControlRead(void)
{
    uint16 wXferCount;

    /* Set up the state machine */
    USBUART_1_bTransferState = USBUART_1_TRANS_STATE_CONTROL_READ;
    /* Set the toggle, it gets updated in LoadEP */
    USBUART_1_bEP0Toggle = 0;
    /* Initialize the Status Block */
    USBUART_1_InitializeStatusBlock();
    wXferCount = (((uint16)*USBUART_1_wLengthHi << 8) | (*USBUART_1_wLengthLo));

    if (CurrentTD.wCount > wXferCount)
    {
        CurrentTD.wCount = wXferCount;
    }
    USBUART_1_LoadEP0();

    return USBUART_1_TRUE;
}
/*******************************************************************************
* Function Name: USBUART_1_ControlReadDataStage
********************************************************************************
* Summary:
*   Handle the Data Stage of a control read transfer
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_ControlReadDataStage(void)
{
	USBUART_1_LoadEP0();
}
/*******************************************************************************
* Function Name: USBUART_1_ControlReadStatusStage
********************************************************************************
* Summary:
*   Handle the Status Stage of a control read transfer
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_ControlReadStatusStage(void)
{
	/* Update the transfer byte count */
    USBUART_1_TransferByteCount += USBUART_1_bLastPacketSize;
    /* Go Idle */
    USBUART_1_bTransferState = USBUART_1_TRANS_STATE_IDLE;
    /* Update the completion block */
    USBUART_1_UpdateStatusBlock(USBUART_1_XFER_STATUS_ACK);
 	/* We expect no more data, so stall INs and OUTs */
	USBUART_1_bEP0Mode = USBUART_1_MODE_STALL_IN_OUT;
}
/*******************************************************************************
* Function Name: USBUART_1_InitControlWrite
********************************************************************************
* Summary:
*   Initialize a control write transaction
*   
* Parameters:  
*   None
*******************************************************************************/
uint8 USBUART_1_InitControlWrite(void)
{
    uint16 wXferCount;
    
    /* Set up the state machine */
    USBUART_1_bTransferState = USBUART_1_TRANS_STATE_CONTROL_WRITE;
    /* This migh not be necessary */
    USBUART_1_bEP0Toggle = USBUART_1_EP0_CNT_DATA_TOGGLE;;
    /* Initialize the Status Block */
    USBUART_1_InitializeStatusBlock();

    wXferCount = (((uint16)CY_GET_REG8(USBUART_1_wLengthHi) << 8) | (CY_GET_REG8(USBUART_1_wLengthLo)));

    if (CurrentTD.wCount > wXferCount)
    {
        CurrentTD.wCount = wXferCount;
    }
    
	/* Expect Data or Status Stage */
	USBUART_1_bEP0Mode = USBUART_1_MODE_ACK_OUT_STATUS_IN;
    
    return USBUART_1_TRUE;
}
/*******************************************************************************
* Function Name: USBUART_1_ControlWriteDataStage
********************************************************************************
* Summary:
*   Handle the Data Stage of a control write transfer
*       1. Get the data (We assume the destination was validated previously)
*       3. Update the count and data toggle
*       2. Update the mode register for the next transaction
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_ControlWriteDataStage(void)
{
	uint8 *pReg = (uint8 *)USBUART_1_EP0_DR0; 
	USBUART_1_bEP0Count = (CY_GET_REG8(USBUART_1_EP0_CNT ) & 0x0F) - 2;
    
    USBUART_1_TransferByteCount += USBUART_1_bEP0Count;
    
    while ((CurrentTD.wCount > 0) && (USBUART_1_bEP0Count > 0))
	{
        *CurrentTD.pData++ = CY_GET_REG8(pReg++);
        USBUART_1_bEP0Count--;
		CurrentTD.wCount--;
	}
	/* Update the data toggle */
	USBUART_1_bEP0Toggle ^= USBUART_1_EP0_CNT_DATA_TOGGLE;
	/* Expect Data or Status Stage */
	USBUART_1_bEP0Mode = USBUART_1_MODE_ACK_OUT_STATUS_IN;
}
/*******************************************************************************
* Function Name: USBUART_1_ControlWriteStatusStage
********************************************************************************
* Summary:
*   Handle the Status Stage of a control write transfer
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_ControlWriteStatusStage(void)
{
	/* Go Idle */
    USBUART_1_bTransferState = USBUART_1_TRANS_STATE_IDLE;
    /* Update the completion block */
    USBUART_1_UpdateStatusBlock(USBUART_1_XFER_STATUS_ACK);
 	/* We expect no more data, so stall INs and OUTs */
	USBUART_1_bEP0Mode = USBUART_1_MODE_STALL_IN_OUT;
}
/*******************************************************************************
* Function Name: USBUART_1_InitNoDataControlTransfer
********************************************************************************
* Summary:
*   Initialize a no data control transfer
*   
* Parameters:  
*   None
*******************************************************************************/
uint8 USBUART_1_InitNoDataControlTransfer(void)
{
	USBUART_1_bTransferState = USBUART_1_TRANS_STATE_NO_DATA_CONTROL;
	USBUART_1_bEP0Mode = USBUART_1_MODE_STATUS_IN_ONLY;
	return USBUART_1_TRUE;
}
/*******************************************************************************
* Function Name: USBUART_1_NoDataControlStatusStage
********************************************************************************
* Summary:
*   Handle the Status Stage of a no data control transfer.
*
*   SET_ADDRESS is special, since we need to receive the status stage with
*   the old address.
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_NoDataControlStatusStage(void)
{
	/* Change the USB address register if we got a SET_ADDRESS. */
    if (USBUART_1_bDeviceAddress != 0)
    {
        CY_SET_REG8(USBUART_1_CR0, USBUART_1_bDeviceAddress | USBUART_1_CR0_ENABLE);
        USBUART_1_bDeviceAddress = 0;
    }
    	/* Go Idle */
    USBUART_1_bTransferState = USBUART_1_TRANS_STATE_IDLE;
    /* Update the completion block */
    USBUART_1_UpdateStatusBlock(USBUART_1_XFER_STATUS_ACK);
 	/* We expect no more data, so stall INs and OUTs */
	USBUART_1_bEP0Mode = USBUART_1_MODE_STALL_IN_OUT;
}
/*******************************************************************************
* Function Name: USBUART_1_UpdateStatusBlock
********************************************************************************
* Summary:
*   Update the Completion Status Block for a Request.  The block is updated
*   with the completion code the USBUART_1_TransferByteCount.  The
*   StatusBlock Pointer  is set to NULL.
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_UpdateStatusBlock(uint8 bCompletionCode)
{
    if (CurrentTD.pStatusBlock != USBUART_1_NULL)
    {
        CurrentTD.pStatusBlock->bStatus = bCompletionCode;
        CurrentTD.pStatusBlock->wLength = USBUART_1_TransferByteCount;
        CurrentTD.pStatusBlock = USBUART_1_NULL;
    }
}
/*******************************************************************************
* Function Name: USBUART_1_InitializeStatusBlock
********************************************************************************
* Summary:
*   Initialize the Completion Status Block for a Request.  The completion
*   code is set to USB_XFER_IDLE.
*
*   Also, initializes USBUART_1_TransferByteCount.  Save some space,
*   this is the only consumer.
*   
* Parameters:  
*   None
*******************************************************************************/
void USBUART_1_InitializeStatusBlock(void)
{
    USBUART_1_TransferByteCount = 0;
    if (CurrentTD.pStatusBlock != USBUART_1_NULL)
    {
        CurrentTD.pStatusBlock->bStatus = USBUART_1_XFER_IDLE;
        CurrentTD.pStatusBlock->wLength = 0;
    }
}
