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

📄 parser.c

📁 usbn9603等时传输的firmware
💻 C
📖 第 1 页 / 共 2 页
字号:
 /*----------------------------------------------------------------------------
 *  Copyright (c) 200 by National Semiconductor Corporation
 *  National Semiconductor Corporation
 *
 *  All rights reserved
 *
 *<<<-------------------------------------------------------------------------
 *  File Contents:
 *	parser.c 
 *
 *  Project: USB Demo firmware
 *  Author : Yan Nosovitsky
 *  Date   : Oct 2001
 *----------------------------------------------------------------------->>>*/

#define parser_c
#include "..\include\all.h"
#include "..\usb\usb.h"
#include <string.h>
#include <stdlib.h> 
#include "command_api.h"
#include "parser.h"


byte 			buffer[2*1024];
byte 			*readPtr = buffer;
byte 			*writePtr = buffer;
BulkState 		bulkState ;
InterruptState 	interruptState;
byte 			feedback[8];

extern Device_buffers_t device_buffers;


void event_wait(void)
{
    _di();
    while (event_table == 0)     
		__asm__("eiwait");       
    _ei();
}

/*----------------------------------------------------------------------------------------------
*	Prototype
*		int main()
*
*	Parameters
*		None
*
*	Returns
*		Success
*
*	Description
*		Polls on the event table (event_table) and perform appropriate request.
----------------------------------------------------------------------------------------------*/

int main()
{
	byte *buffPtr = buffer;
	byte readCount = 0;
	byte byteToRead = 0;
	byte writeCount = 0;
	byte byteToWrite = 0;
	byte index = 1;
	byte curBuff;

	while(TRUE)
	{	
		event_wait();
		
		while (event_table&EVT_MASK)
		{ 
		/* -------------------------------------------------------------
		* Check whether new packet received from the host 
		* --------------------------------------------------------------*/
			if (event_table & EVT_USB_BULK_RX) 
			{
			   /* ------------------------------------------------
			   * Is it bulk immediate response back command?  
			   * ------------------------------------------------*/
				if (bulkState.bulkCommand == BULK_SEND_IMM_BACK)
				{
					/*--------------------------------------------
					* Check whether current buffer ready ( empty ) 
					* --------------------------------------------*/
					curBuff = bulkState.bufferForGetData;
					if (bulkState.bulkBuff[curBuff].getReady)
					{
						 /* --------------------------------------
						  * Buffer is ready.
						  * Read new packet to this buffer 
						  ----------------------------------------*/
						readCount = min(TX_BULK_EP_FIFO_SIZE,bulkState.restToRead);
						clear_event(EVT_USB_BULK_RX);
						USB_bulk_fast_read(readCount,bulkState.bulkBuff[curBuff].buffer);
						bulkState.restToRead -= readCount;
						bulkState.bulkBuff[curBuff].getReady = FALSE;
						/* --------------------------------------
						*  Buffer is ready to be send back 
						* --------------------------------------*/
						bulkState.bulkBuff[curBuff].sendReady = TRUE;
						/* --------------------------------------
						* Go to the next buffer 
						* --------------------------------------*/	
						bulkState.bufferForGetData = bulkState.bulkBuff[curBuff].nextBuf;
					}
					else
					{	/* --------------------------------------
						 *	Buffer is not ready.
						 * wait for the next interrupt 
						 * -------------------------------------*/
					}				
				}		
			   /* ------------------------------------------------
			   * 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); 
				   /* --------------------------------------
				   * If it is bulk delayed response command	
				   * --------------------------------------*/		
					if (bulkState.bulkCommand == BULK_SEND_AFTER_LAST_BACK)
					{   /* ----------------------------------
						* Read packet into the local buffer 
						* ----------------------------------*/
						USB_bulk_fast_read(readCount,readPtr);
						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_bulk_read_compare(readCount,bulkState.data);

					bulkState.restToRead -= readCount;
					/* Is all the data read?  */
					if ( bulkState.restToRead == 0 )
					{
					   /* --------------------------------------
					   * If it is bulk delayed response command	
					   * --------------------------------------*/					
						if ( bulkState.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_Send_Data(byteToWrite,writePtr);
							bulkState.restToWrite -= byteToWrite;
							writePtr += byteToWrite;
						}
					}
				}
			}
			/* -------------------------------------------------------------
			* Check whether the host is ready to get next packet
			* --------------------------------------------------------------*/
			if (event_table & EVT_USB_BULK_TX)
			{
				/*----------------------------------------
				* Is the is more date to be sent?
				*----------------------------------------*/
				if (bulkState.restToWrite > 0)
				{
				   /* ------------------------------------------------
				   * Is it bulk immediate response back command?  
				   * ------------------------------------------------*/
					if (bulkState.bulkCommand == BULK_SEND_IMM_BACK)
					{
						/*--------------------------------------------
						* Check whether current buffer ready ( full ) 
						* --------------------------------------------*/
						curBuff = bulkState.bufferForSendData;
						if (bulkState.bulkBuff[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);
							USB_Send_Data(byteToWrite,bulkState.bulkBuff[curBuff].buffer);
							bulkState.restToWrite -= byteToWrite;
							bulkState.bulkBuff[curBuff].sendReady = FALSE;
							/* ---------------------------------
							* Buffer ready to receive new data 
							* --------------------------------*/
							bulkState.bulkBuff[curBuff].getReady = TRUE;
							/*-------------------------
							* Go to the next buffer 
							* ------------------------*/	
							bulkState.bufferForSendData = bulkState.bulkBuff[curBuff].nextBuf;	
						}
						else
						{	/* --------------------------------------
							 *	Buffer is not ready.
							 * wait for the next interrupt 
							 * -------------------------------------*/
						}	

					}
				   /* ------------------------------------------------
				   * 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);
						/* --------------------------------------
						 * If it is bulk delayed response command	
						* --------------------------------------*/	
						if ( bulkState.bulkCommand == BULK_SEND_AFTER_LAST_BACK )
						{  /* ---------------------------------------
							* Send next packet from the local buffer 

⌨️ 快捷键说明

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