⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.c

📁 USBN9603的DEMO程序
💻 C
📖 第 1 页 / 共 3 页
字号:
			if (isoState.command.isoCommand == ISOCH_GET_DATA)
			{
				/*----------------------------------------------
				*  number of bytes received in the last request 
				-----------------------------------------------*/
				*((dword *)&feedback[0]) = isoState.dataSize - isoState.restToRead ;
				*((dword *)&feedback[4]) = isoState.numOfErrors;
				device_buffers.zero_data=1;
				send_control_data(feedback,8);
			}
			break;
		case READ_USB_REG:
			regAddress = REQ_VALUE(req).as_bytes.lsb;
			feedback[0] = read_usb(regAddress);
			send_control_data(feedback,1);
			break;
		case WRITE_USB_REG:
			regAddress = REQ_VALUE(req).as_bytes.lsb;
			regVal = REQ_INDEX(req).as_bytes.lsb;
			write_usb(regAddress,regVal);
			zero_length_data_response(ENDPOINT_0);
			break;
		case STOP_REQUEST:
			interruptState.interruptNum = 0;
			interruptState.restToSend = 0;
			interruptState.interruptCommand = INTERRUPT_NO_COMMAND;
			WATCHDOG_STOP;
			bulkState.command.bulkCommand = BULK_NO_COMMAND;
			bulkState.restToRead = 0;
			bulkState.restToWrite = 0;
			bulkState.dataSize = 0;
			isoState.command.isoCommand = ISOCH_NO_COMMAND;
			isoState.dataSize = 0;
			isoState.restToRead = 0;
			isoState.restToWrite = 0;
			local_event_table = 0;
			event_table = 0;
			zero_length_data_response(ENDPOINT_0);
			break;
		default :
			break;
	}
}


/*----------------------------------------------------------------------------------------------
*	Prototype
*		void BulkReceiveEvent()
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		Read new packet from endpoint num 2 (bulk receive) according to the current bulk command
----------------------------------------------------------------------------------------------*/
__inline__ void BulkReceiveEvent()
{
	byte curBuff;
	byte readCount = 0;
	byte byteToWrite = 0;
	/* ------------------------------------------------
	* Is it bulk immediate response back command?  
	* ------------------------------------------------*/
	if (bulkState.command.bulkCommand == BULK_SEND_IMM_BACK)
	{
		/*--------------------------------------------
		* Check whether current buffer ready ( empty ) 
		* --------------------------------------------*/
		_disable_();	
		curBuff = bulkState.bufferForGetData;
		if (bulkState.locBuff[curBuff].getReady)
		{
			 /* --------------------------------------
			  * Buffer is ready.
			  * Read new packet to this buffer 
			  ----------------------------------------*/
			clear_event(EVT_USB_BULK_RX);
			clear_local_event(EVT_USB_BULK_RX);
			//USB_fast_read(readCount,bulkState.locBuff[curBuff].buffer,ENDPOINT_2);
			readCount = USB_Receive_Data(bulkState.locBuff[curBuff].buffer,ENDPOINT_2);
			bulkState.restToRead -= readCount;
			bulkState.locBuff[curBuff].getReady = FALSE;
			/* --------------------------------------
			*  Buffer is ready to be send back 
			* --------------------------------------*/
			bulkState.locBuff[curBuff].sendReady = TRUE;
			/* --------------------------------------
			* Go to the next buffer 
			* --------------------------------------*/	
			bulkState.bufferForGetData = bulkState.locBuff[curBuff].nextBuf;
		}
		else
		{	/* --------------------------------------
			 *	Buffer is not ready.
			 * wait for the next interrupt 
			 * -------------------------------------*/
			 clear_event(EVT_USB_BULK_RX);
		}
		_enable_();					
	}		
	/* ------------------------------------------------
	* If it is 
	*		- bulk get data command  or
	*        - bulk delayed response command
	* ------------------------------------------------*/
	else
	{ /* ---------------
	  *  read new packet 
	  *-----------------*/
		readCount = min(TX_BULK_EP_FIFO_SIZE,bulkState.restToRead);
		clear_event(EVT_USB_BULK_RX);
		clear_local_event(EVT_USB_BULK_RX);
	   /* --------------------------------------
	   * If it is bulk delayed response command	
	   * --------------------------------------*/		
		if (bulkState.command.bulkCommand == BULK_SEND_AFTER_LAST_BACK)
		{   /* ----------------------------------
			* Read packet into the local buffer 
			* ----------------------------------*/
			USB_fast_read(readCount,readPtr,ENDPOINT_2);
			readPtr += readCount;
		}
		else
			/* --------------------------------------------------------------------------
			* Read and check the packet's content.
			* Each byte of the packet should be equal to a one determined by the host.
			*---------------------------------------------------------------------------*/
			bulkState.numOfErrors += USB_read_compare(readCount,bulkState.data,ENDPOINT_2);

		bulkState.restToRead -= readCount;
		/* Is all the data read?  */
		if ( bulkState.restToRead == 0 )
		{
		   /* --------------------------------------
		   * If it is bulk delayed response command	
		   * --------------------------------------*/					
			if ( bulkState.command.bulkCommand == BULK_SEND_AFTER_LAST_BACK )			{
				/*--------------------------------------------------
				* Put the pointer to the begging of the local buffer 
				*-------------------------------------------------*/
				readPtr = buffer; 
				/*-----------------------------------
				* Start sending data back to th host 
				*------------------------------------*/
				byteToWrite = min(bulkState.restToWrite,TX_BULK_EP_FIFO_SIZE);
				USB_Transmit_Data(byteToWrite,writePtr,ENDPOINT_1);
				bulkState.restToWrite -= byteToWrite;
				writePtr += byteToWrite;
			}
		}
	}
}			


/*----------------------------------------------------------------------------------------------
*	Prototype
*		void BulkTransmitEvent()
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		Send new packet to endpoint num 1 (bulk transmit) according to the current bulk command
----------------------------------------------------------------------------------------------*/
__inline__ void BulkTransmitEvent()
{
	byte curBuff = 0;
	byte byteToWrite = 0;
	/*----------------------------------------
	* Is the is more date to be sent?
	*----------------------------------------*/
	if (bulkState.restToWrite > 0)
	{
	   /* ------------------------------------------------
	   * Is it bulk immediate response back command?  
	   * ------------------------------------------------*/
		if (bulkState.command.bulkCommand == BULK_SEND_IMM_BACK)
		{
			/*--------------------------------------------
			* Check whether current buffer ready ( full ) 
			* --------------------------------------------*/
			_disable_();	
			curBuff = bulkState.bufferForSendData;
			if (bulkState.locBuff[curBuff].sendReady)
			{
				 /* --------------------------------------
				  * Buffer is ready.
				  * Send new packet from this buffer 
				  ----------------------------------------*/
				byteToWrite = min(bulkState.restToWrite,TX_BULK_EP_FIFO_SIZE);
				clear_event(EVT_USB_BULK_TX);
				clear_local_event(EVT_USB_BULK_TX);
				USB_Transmit_Data(byteToWrite,bulkState.locBuff[curBuff].buffer,ENDPOINT_1);
				bulkState.restToWrite -= byteToWrite;
				bulkState.locBuff[curBuff].sendReady = FALSE;
				/* ---------------------------------
				* Buffer ready to receive new data 
				* --------------------------------*/
				bulkState.locBuff[curBuff].getReady = TRUE;
				/*-------------------------
				* Go to the next buffer 
				* ------------------------*/	
				bulkState.bufferForSendData = bulkState.locBuff[curBuff].nextBuf;	
			}
			else
			{	/* --------------------------------------
				 *	Buffer is not ready.
				 * wait for the next interrupt 
				 * -------------------------------------*/
				 clear_event(EVT_USB_BULK_TX);
			}	
			_enable_();	
		}
	   /* ------------------------------------------------
	   * Is it
	   *		- bulk send data command  or
	   *        - bulk delayed response command
	   * ------------------------------------------------*/
		else				
		{ /* ---------------
		  *  send new packet 
		  *-----------------*/
			byteToWrite = (bulkState.restToWrite < TX_BULK_EP_FIFO_SIZE) ?
						bulkState.restToWrite : TX_BULK_EP_FIFO_SIZE ;
			bulkState.restToWrite -= byteToWrite;
			clear_event(EVT_USB_BULK_TX);
			clear_local_event(EVT_USB_BULK_TX);
			/* --------------------------------------
			 * If it is bulk delayed response command	
			* --------------------------------------*/	
			if ( bulkState.command.bulkCommand == BULK_SEND_AFTER_LAST_BACK )			
			{  /* ---------------------------------------
				* Send next packet from the local buffer 
				* -------------------------------------*/
				USB_Transmit_Data(byteToWrite,writePtr,ENDPOINT_1);
				writePtr += byteToWrite;
			}
			else
				/* ------------------------------------------------------------------------------
				* Send a new packet, each byte of which is equal to a one determined by the host.
				*-------------------------------------------------------------------------------*/
				USB_write_data(byteToWrite,bulkState.data,ENDPOINT_1);
		}
	}
	/*---------------------------------
	* There is no more data to send
	*--------------------------------*/
	else
	{

		/*------------------------------------
		* Send zero length packet to 
		* mark end of the data, if needed
		* -----------------------------------*/	
		if (bulkState.zeroPacketNeed)
		{ 
			usbn9604_tx_enable(ENDPOINT_1);
			bulkState.zeroPacketNeed = FALSE;
		}
		clear_event(EVT_USB_BULK_TX);
		clear_local_event(EVT_USB_BULK_TX);
		/*--------------------------------------------------
		* Put the pointer to the begging of the local buffer 
		*-------------------------------------------------*/
		writePtr = buffer;
		bulkState.command.bulkCommand = BULK_NO_COMMAND;
		bulkState.numOfErrors = 0;
		bulkState.data = 0; 
	}				
}

/*----------------------------------------------------------------------------------------------
*	Prototype
*		void TimerEvent()
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		Send new inerrupt to the host by endpoint num 5 (interrupt transmit) according to 
*		the current interrupt command
----------------------------------------------------------------------------------------------*/
__inline__ void TimerEvent()
{
	byte intNum = 0;
	/*-------------------------------
	* Is it interrupt get command
	*------------------------------*/
	if (interruptState.interruptCommand == INTERRUPT_GET_INT)
	{
		/*-------------------------------------
		* Send new interrupt packet containing 
		* this interrupt serial number
		*--------------------------------------*/
		intNum = interruptState.interruptNum - interruptState.restToSend;
		USB_Transmit_Data(1,&intNum,ENDPOINT_5);
		clear_event(EVT_TIMER_INT);
		clear_local_event(EVT_TIMER_INT);
		interruptState.restToSend--;
		/*-------------------------------
		* Should we send more interrupts? 
		* -------------------------------*/
		if (interruptState.restToSend != 0)
			/*---------------------------------------------------
			* restart the timer for next interrupt generation
			*--------------------------------------------------*/
			TOUCH_WATCHDOG(interruptState.interval);
		else 
		{	/*-----------------------------------------------
			* All the interrupts were sent. Stop the timer
			*-----------------------------------------------*/
			WATCHDOG_STOP;
			interruptState.interruptCommand = INTERRUPT_NO_COMMAND;
		}
	}	
}

/*----------------------------------------------------------------------------------------------
*	Prototype
*		void IsoReceiveEvent()
*
*	Parameters
*		None
*
*	Returns
*		None
*
*	Description
*		Read new packet from endpoint num 4 (isochronous receive) according to the current isochronous command
----------------------------------------------------------------------------------------------*/
__inline__ void IsoReceiveEvent()
{
	byte curBuff;
	byte readCount = 0;
	
	/* ------------------------------------------------
	* Is it bulk immediate response back command?  
	* ------------------------------------------------*/
	if (isoState.command.isoCommand == ISOCH_SEND_IMM_BACK)
	{
		/*--------------------------------------------
		* Check whether current buffer ready ( empty ) 
		* --------------------------------------------*/
		curBuff = isoState.bufferForGetData;
		if (isoState.locBuff[curBuff].getReady)
		{
			 /* --------------------------------------
			  * Buffer is ready.
			  * Read new packet to this buffer 
			  ----------------------------------------*/
			clear_event(EVT_USB_ISO_RX);
			clear_local_event(EVT_USB_ISO_RX);
			
			readCount = USB_Receive_Data(isoState.locBuff[curBuff].buffer,ENDPOINT_4);

			isoState.restToRead -= readCount;
			isoState.locBuff[curBuff].getReady = FALSE;
			/* --------------------------------------
			*  Buffer is ready to be send back 
			* --------------------------------------*/
			isoState.locBuff[curBuff].sendReady = TRUE;
			/* --------------------------------------
			* Go to the next buffer 
			* --------------------------------------*/	
			isoState.bufferForGetData = isoState.locBuff[curBuff].nextBuf;
		}
		else
		{	/* --------------------------------------
			 *	Buffer is not ready but we must flash this 
			 *  packet in order to re-enable endpoint.
			 *  This packet will be lost
			 * -------------------------------------*/
			FLUSH_RXEP(ENDPOINT_4);			
			ENABLE_RX(ENDPOINT_4);
			isoState.restToRead -= min(TX_ISO_EP_FIFO_SIZE,isoState.restToRead);
			clear_event(EVT_USB_ISO_RX);
			clear_local_event(EVT_USB_ISO_RX);
		}				
	}		
	/* ------------------------------------------------
	* If it is 
	*		- isochronous get data command  
	* ------------------------------------------------*/
	else
	{ /* ---------------
	  *  read new packet 
	  *-----------------*/
		readCount = min(TX_ISO_EP_FIFO_SIZE,isoState.restToRead);
		clear_event(EVT_USB_ISO_RX); 
		clear_local_event(EVT_USB_ISO_RX);
		/* --------------------------------------------------------------------------
		* Read and check the packet's content.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -