📄 parser.c
字号:
* Each byte of the packet should be equal to a one determined by the host.
*---------------------------------------------------------------------------*/
isoState.numOfErrors += USB_read_compare(readCount,isoState.data,ENDPOINT_4);
isoState.restToRead -= readCount;
/* Is all the data read? */
if ( isoState.restToRead == 0 )
{
}
}
}
/*----------------------------------------------------------------------------------------------
* Prototype
* void IsoTransmitEvent()
*
* Parameters
* None
*
* Returns
* None
*
* Description
* Send new packet to endpoint num 3 (isochronous transmit) according to the current isochronous command
----------------------------------------------------------------------------------------------*/
__inline__ void IsoTransmitEvent()
{
byte curBuff = 0;
byte byteToWrite = 0;
int i=0;
volatile byte sleep = 0;
/*----------------------------------------
* Is the is more date to be sent?
*----------------------------------------*/
if (isoState.restToWrite > 0)
{
/* ------------------------------------------------
* Is it bulk immediate response isochronous command?
* ------------------------------------------------*/
if (isoState.command.isoCommand == ISOCH_SEND_IMM_BACK)
{
/*--------------------------------------------
* Check whether current buffer ready ( full )
* --------------------------------------------*/
curBuff = isoState.bufferForSendData;
if (isoState.locBuff[curBuff].sendReady)
{
/* --------------------------------------
* Buffer is ready.
* Send new packet from this buffer
----------------------------------------*/
byteToWrite = min(isoState.restToWrite,TX_ISO_EP_FIFO_SIZE);
clear_event(EVT_USB_ISO_TX);
clear_local_event(EVT_USB_ISO_TX);
for (i = 0; i < 100; i++)
sleep++;
USB_Transmit_Data(byteToWrite,isoState.locBuff[curBuff].buffer,ENDPOINT_3);
isoState.restToWrite -= byteToWrite;
isoState.locBuff[curBuff].sendReady = FALSE;
/* ---------------------------------
* Buffer ready to receive new data
* --------------------------------*/
isoState.locBuff[curBuff].getReady = TRUE;
/*-------------------------
* Go to the next buffer
* ------------------------*/
isoState.bufferForSendData = isoState.locBuff[curBuff].nextBuf;
}
else
{
if (isoState.restToRead == 0)
{
/*-------------------------------------------------
* Probably we lost some packets
* Send dummy buffers to the host in order to finish
* current operation
*--------------------------------------------------*/
byteToWrite = min(isoState.restToWrite,TX_ISO_EP_FIFO_SIZE);
clear_event(EVT_USB_ISO_TX);
clear_local_event(EVT_USB_ISO_TX);
USB_write_data(byteToWrite,0,ENDPOINT_3);
isoState.restToWrite -= byteToWrite;
}
else
{
/* --------------------------------------
* Buffer is not ready.
* wait for the next interrupt
* -------------------------------------*/
clear_event(EVT_USB_ISO_TX);
}
}
}
/* ------------------------------------------------
* Is it
* - isochronous send data command
* ------------------------------------------------*/
else
{ /* ---------------
* send new packet
*-----------------*/
byteToWrite = min(isoState.restToWrite, TX_ISO_EP_FIFO_SIZE);
isoState.restToWrite -= byteToWrite;
clear_event(EVT_USB_ISO_TX);
clear_local_event(EVT_USB_ISO_TX);
/* ------------------------------------------------------------------------------
* Send a new packet, each byte of which is equal to a one determined by the host.
*-------------------------------------------------------------------------------*/
USB_write_data(byteToWrite,isoState.data,ENDPOINT_3);
}
}
/*---------------------------------
* There is no more data to send
*--------------------------------*/
else
{
/*------------------------------------
* Send zero length packet to
* mark end of the data, if needed
* -----------------------------------*/
if (isoState.zeroPacketNeed)
{
usbn9604_tx_enable(ENDPOINT_3);
isoState.zeroPacketNeed = FALSE;
}
clear_event(EVT_USB_ISO_TX);
clear_local_event(EVT_USB_ISO_TX);
/*--------------------------------------------------
* Put the pointer to the begging of the local buffer
*-------------------------------------------------*/
writePtr = buffer;
isoState.command.isoCommand = ISOCH_NO_COMMAND;
isoState.numOfErrors = 0;
isoState.data = 0;
}
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! The functions described below are used for specific demo test only. Don't use this functions
! for any read/write fifo operation.
! Use instead the USB_Receive_Data()/USB_Transmit_Data() functions described in usbdrv.c file
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*----------------------------------------------------------------------------------------------
* Prototype
* void USB_write_data (int data_size, byte data, endpoint_t ep_num )
*
* Parameters
* data - constant byte to be sent
* data_size - size of data (in bytes) to be sent
* ep_num - endpoint number
*
* Returns
* None
*
* Description
* Fill corresponding endpoint with the same "data" byte
*
----------------------------------------------------------------------------------------------*/
extern const int usbn9604_rx_endpoint_addr[];
extern const int usbn9604_tx_endpoint_addr[];
__inline__ void USB_write_data (int data_size, byte data, endpoint_t ep_num)
{
int i;
_disable_();
USBADDR = usbn9604_tx_endpoint_addr[ep_num];
for (i=0;i<data_size;i++)
USBDATA = data;
//enable data transmittion
usbn9604_tx_enable(ep_num);
_enable_();
}
/*----------------------------------------------------------------------------------------------
* Prototype
* void USB_read_compare (int data_size, byte data, endpoint_t ep_num )
*
* Parameters
* data - constant byte to be read
* data_size - size of data (in bytes) to be read
* ep_num - endpoint number
*
* Returns
* Number of errors
*
* Description
* Read from corresponding endpoint data and copmapre each byte to the "data" byte
*
----------------------------------------------------------------------------------------------*/
#pragma set_options("-funroll-loops")
__inline__ int USB_read_compare(byte data_size, byte data, endpoint_t ep_num )
{
int i;
register byte bytes_count = data_size;
register byte dataToCompare = data;
byte numOfErrors = 0;
_disable_();
USBADDR = usbn9604_rx_endpoint_addr[ep_num];
for(i=0;i<bytes_count;i++)
if ( USBDATA!= dataToCompare)
numOfErrors++;
FLUSH_RXEP(ep_num);
ENABLE_RX(ep_num);
_enable_();
return numOfErrors;
}
/*----------------------------------------------------------------------------------------------
* Prototype
* void USB_fast_read (int data_size, byte* data_ptr, endpoint_t ep_num )
*
* Parameters
* data_size - size of data (in bytes) to be read
* data_ptr - pointer to the local buffer
* ep_num - endpoint number
*
* Returns
* Number of errors
*
* Description
* Read from corresponding endpoint defined size of data into the local buffer without checking
* FIFO counter.
*
----------------------------------------------------------------------------------------------*/
#pragma set_options("-funroll-loops")
__inline__ void USB_fast_read(byte data_size, byte* data_ptr, endpoint_t ep_num)
{
int i;
register byte bytes_count = data_size;
_disable_();
USBADDR = usbn9604_rx_endpoint_addr[ep_num];
for(i=0;i<bytes_count;i++)
*(data_ptr ++) = USBDATA;
FLUSH_RXEP(ep_num);
ENABLE_RX(ep_num);
_enable_();
}
/*----------------------------------------------------------------------------------------------
* Prototype
* void pingPong ()
*
* Parameters
* None
*
* Returns
* None
*
* Description
* This function implements Ping-Pong buffering algorithm for receive. This algorithm is used for
* BULK_GET_DATA test. In this case two same-direction endpoints ( 2 and 6) allocated for the same
* pipe Bulk OUT.
* In this algorithm, the second FIFO is filled with new packet from the host while the contents of the first are
* read by the firmware. Therefore, the ping-pong buffering is an effective way to increase the data transfer rate.
* In order to assure that the first FIFO will be read before the second FIFO is filled, the firmware
* waits to the next interrupt using polling mode ( read RXEV register). In this way Firmware does not spend
* time on the conext switch of interrupt handler.
* Note that in this case this function takes 100% CPU.
----------------------------------------------------------------------------------------------*/
void pingPong()
{
int i;
volatile byte rx_event;
volatile byte rxstat;
byte data = bulkState.data;
byte readCount;
clear_event(EVT_PING_PONG);
clear_local_event(EVT_PING_PONG);
while (bulkState.restToRead )
{
/*--------------------
* Check Receive events
*---------------------*/
if (rx_event = RX_EVENTS)
{
if (rx_event& RX_FIFO1)
{
/*------------------
* endpoint 2
* New packet arrived
*-------------------*/
PCDOUT &= 0x0;
/*---------------------------------------
* Disable this endpoint.
* Next packet will enter into endpoint 6
*---------------------------------------*/
DISABLE_EP(ENDPOINT_2);
/*---------------------
* Read status register
*---------------------*/
rxstat = EP_RXSTATUS(ENDPOINT_2);
/*--------------------------------------------------------
* Check errors and toggel bit.
* Assure that arrived packet is data0 ( Toggle bit is 0)
*--------------------------------------------------------*/
if (!(rxstat & RX_ERR)&&!(rxstat & RX_TOGL))
{ /*---------------------
* Read data from FIFO
*--------------------*/
readCount = min(TX_BULK_EP_FIFO_SIZE,bulkState.restToRead);
_disable_();
USBADDR = RXD1;
for(i=0;i<readCount;i++)
if (data != USBDATA)
bulkState.numOfErrors++;
_enable_();
bulkState.restToRead -= readCount;
/*--------------------
* update next toggle
*------------------*/
endpoint_stat[ENDPOINT_2]->toggle_bit = 1;
}
else
{
/*-------------------
* receive media error
*--------------------*/
printf("Error 2\n");
}
/*-----------------------------
* Flush and re-enable the FIFO
*-----------------------------*/
FLUSH_RXEP(ENDPOINT_2);
ENABLE_RX(ENDPOINT_2);
PCDOUT |= 0x1;
continue;
}
if (rx_event&RX_FIFO3)
{
/*------------------
* endpoint 6
* New packet arrived
*-------------------*/
PCDOUT &= 0x0;
/*---------------------------------------
* Enable the endpoint 2.
* Next packet will enter into endpoint 2
*---------------------------------------*/
ENABLE_EP(ENDPOINT_2);
/*---------------------
* Read status register
*---------------------*/
rxstat = EP_RXSTATUS(ENDPOINT_6);
/*--------------------------------------------------------
* Check errors and toggel bit.
* Assure that arrived packet is data1 ( Toggle bit is 1)
*--------------------------------------------------------*/
if (!(rxstat & RX_ERR)&&(rxstat & RX_TOGL))
{ /*---------------------
* Read data from FIFO
*--------------------*/
readCount = min(TX_BULK_EP_FIFO_SIZE,bulkState.restToRead);
USBADDR = RXD3;
_disable_();
for(i=0;i<readCount;i++)
if (data != USBDATA)
bulkState.numOfErrors++;
_enable_();
bulkState.restToRead -= readCount;
/*-------------------
* update next toggle
*-------------------*/
endpoint_stat[ENDPOINT_2]->toggle_bit = 0;
}
else
{
/*-------------------
* receive media error
*--------------------*/
printf("Error 6\n");
}
/*-----------------------------
* Flush and re-enable the FIFO
*-----------------------------*/
FLUSH_RXEP(ENDPOINT_6);
ENABLE_RX(ENDPOINT_6);
PCDOUT |= 0x1;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -