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

📄 usbdrv.c

📁 杭州立宇泰豪华型44B0开发板
💻 C
📖 第 1 页 / 共 4 页
字号:
		* --------------------------------------------*/
		//_disable_();	
		curBuff = bulkState.bufferForGetData;
		if (bulkState.locBuff[curBuff].getReady)
		{
			 /* --------------------------------------
			  * Buffer is ready.
			  * Read new packet to this buffer 
			  ----------------------------------------*/
			
			//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;
			BulkTransmitEvent();
		}
		else
		{	/* --------------------------------------
			 *	Buffer is not ready.
			 * wait for the next interrupt 
			 * -------------------------------------*/
			 
		}
		//_enable_();					
	}		
	/* ------------------------------------------------
	* If it is 
	*		- bulk get data command  or
	*        - bulk delayed response command
	* ------------------------------------------------*/
	else
	{ /* ---------------
	  *  read new packet 
	  *-----------------*/
		readCount = mini(TX_BULK_EP_FIFO_SIZE,bulkState.restToRead);
		
	   /* --------------------------------------
	   * If it is bulk delayed response command	
	   * --------------------------------------*/		
		if ((bulkState.command.bulkCommand == BULK_SEND_AFTER_LAST_BACK)||
			(bulkState.command.bulkCommand == BULK_TRANS_FILENAME))
		{   /* ----------------------------------
			* Read packet into the local buffer 
			* ----------------------------------*/
			USB_fast_read(readCount,readPtr,ENDPOINT_2);
			readPtr += readCount;
		}
		else if(bulkState.command.bulkCommand == BULK_TRANS_FILE){
				USB_fast_read(readCount,readPtr,ENDPOINT_2);
				readPtr += readCount;
				if(readPtr>=(buffer+2048)){//已经是最后
					readPtr=buffer;
					for(i=0;i<2048;i++){
				        *((unsigned char *)(RAM_ADDRESS+pointer))=*readPtr++;
						pointer++;
					}
					readPtr=buffer;
				}
			}
					
			
		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 = mini(bulkState.restToWrite,TX_BULK_EP_FIFO_SIZE);
				USB_Transmit_Data(byteToWrite,writePtr,ENDPOINT_1);
				bulkState.restToWrite -= byteToWrite;
				writePtr += byteToWrite;
			}
			else if(bulkState.command.bulkCommand == BULK_TRANS_FILENAME){
				readPtr = buffer;
				Uart_Printf("\nReceive a file named:");
				for(i=0;i<64;i++)
					Uart_Printf("%02x-",*readPtr++);
				Uart_Printf("\n");
				readPtr = buffer;
				FileStat.fn_dlflg=1;

				i=0;
				while((char)(*readPtr)!='.'){
					FileStat.file_name[i]=(char)(*readPtr);
					readPtr++;
					i++;
					}
				FileStat.file_name[i]='\0';
				Uart_Printf("\nFileName:%s",FileStat.file_name);
				readPtr++;

				i=0;
				while((char)(*readPtr)!=' '){
					FileStat.suffix[i]=(char)(*readPtr);
					readPtr++;
					i++;
					}
				FileStat.suffix[i]='\0';
                Uart_Printf("\nFileSuffix:%s",FileStat.suffix);
				
				readPtr=buffer;
				readPtr+=60;
				FileStat.file_len=(unsigned int)((*readPtr++)+((*readPtr++)<<8)+((*readPtr++)<<16)+((*readPtr++)<<24));
				Uart_Printf("\nFileLen:%d",FileStat.file_len);
				readPtr=buffer;
				}
			else if(bulkState.command.bulkCommand == BULK_TRANS_FILE){

                   if(readPtr!=buffer){
				   	   j=readPtr-buffer;
				   	   readPtr=buffer;
					   for(i=0;i<j;i++){
				           *((unsigned char *)(RAM_ADDRESS+pointer))=*readPtr++;
						   pointer++;
					    	}
                   	}
					readPtr=buffer;
				                
				if(FileStat.suffix[0]=='t'){// txt文件
				    Uart_Printf("\n\nTextFile: ");
				    *((unsigned char *)(RAM_ADDRESS+pointer))='\0';
					Uart_Printf("\n");
					for(i=0;i<pointer;i++)
				   	  Uart_Printf("%c",*((char *)(RAM_ADDRESS+i)));//打印该文本文件
					}
				else if(FileStat.suffix[0]=='b'){//bin file
                          
                                     Uart_Printf("\nDo not Support.");
			
					}
				pointer=0;
				}
		}
	}
}			

/*----------------------------------------------------------------------------------------------
*	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.
*		
----------------------------------------------------------------------------------------------*/

/*__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_();
}

/*__inline*/void USB_word_read(byte data_size,two_bytes *data_ptr,endpoint_t ep_num)
{
    int i;
    byte lbyte;
	byte hbyte;
	register byte words_count = data_size>>1;
	//_disable_();	
	USBADDR = usbn9604_rx_endpoint_addr[ep_num];
	for(i=0;i<words_count;i++){
		lbyte=USBDATA;
	    hbyte=USBDATA;
		*data_ptr=lbyte+(hbyte<<8);
		data_ptr+=2;
		}
	if((data_size%2)!=0)
		*data_ptr=USBDATA;
	FLUSH_RXEP(ep_num);			
	ENABLE_RX(ep_num);
	//_enable_();
}
		

/*----------------------------------------------------------------------------------------------
*	Prototype
*		void Bulk_Parser(USB_request_t *req)
*
*	Parameters
*		req -	pointer to structure representing host request
*
*	Returns
*		none
*
*	Description
*		Parses an imcoming bulk command
----------------------------------------------------------------------------------------------*/

void Bulk_Parser(USB_request_t *req)
{
	byte command;
	byte readybuf[16]={0x08,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0};
	
		/*-----------------------------------------------------------------------------------
		 * Read the command, parse it and update relevant fields in the bulk state structure.
		 *----------------------------------------------------------------------------------*/
		command = REQ_VENDOR(req);
		bulkState.command.bulkCommand = command;
		bulkState.dataSize = ((dword)(REQ_VALUE(req)/*.lsw*/)|(((dword)(REQ_INDEX(req)/*.msw*/)) << 16));
		bulkState.numOfErrors = 0;
		switch (command)
		{
            //Must receive datum firstly, then send them
			case BULK_SEND_IMM_BACK:
				bulkState.restToRead = bulkState.dataSize;
				bulkState.restToWrite = bulkState.dataSize;
				/* It's seems that USB driver don't need zero length packet */ 
				bulkState.zeroPacketNeed = 0 /*!(bulkState.dataSize%TX_BULK_EP_FIFO_SIZE) */; 
				bulkState.bufferForGetData = 0;
				bulkState.bufferForSendData = 0;
				bulkState.locBuff[0].getReady = TRUE;
				bulkState.locBuff[1].getReady = TRUE;
				bulkState.locBuff[0].sendReady = FALSE;
				bulkState.locBuff[1].sendReady = FALSE;
				bulkState.locBuff[0].nextBuf = 1;
				bulkState.locBuff[1].nextBuf = 0;
				DISABLE_TX(ENDPOINT_1);//disable BULK IN  device->host
				ENABLE_EP(ENDPOINT_2); //enable  BULK OUT host->device
				ENABLE_RX_INTS(RX_FIFO1);//enable RX_FIFO1 RX_EV in MAEV
				break;
			//Must receive datum firstly, then send them
			case BULK_SEND_AFTER_LAST_BACK:
				bulkState.restToRead = bulkState.dataSize;
				bulkState.restToWrite = bulkState.dataSize;
				/* It's seems that USB driver don't need zero length packet */ 
				bulkState.zeroPacketNeed = 0 /*!(bulkState.dataSize%TX_BULK_EP_FIFO_SIZE) */;
				DISABLE_TX(ENDPOINT_1);
				ENABLE_EP(ENDPOINT_2);
				ENABLE_RX_INTS(RX_FIFO1);
				break;
			case BULK_GET_DATA:
			/*	bulkState.restToRead = bulkState.dataSize;
				bulkState.data = (REQ_LENGTH(req)&0x00ff);
				*------------------------------------------
				 * enable endpoint 6 for ping-pong transfer 
				 *-----------------------------------------*
				write_usb(EPC_ADDR(ENDPOINT_6), (byte)ENDPOINT_2 | EP_EN) ;
				ENABLE_RX(ENDPOINT_6);
				if (endpoint_stat[ENDPOINT_2]->toggle_bit)
					*------------------------------------------
					 * next packet is data1 (toggle bit is 1).
					 * First packet must enter to the endpoint 6 
					 *-----------------------------------------*
					DISABLE_EP(ENDPOINT_2);
				else
					*------------------------------------------
					 * next packet is data0 (toggle bit is 1).
					 * First packet must enter to the endpoint 2 
					 *-----------------------------------------*
					ENABLE_EP(ENDPOINT_2);
				*-----------------------------------
				* Disable interrupte 
				* This test will work in polling mode
				*-----------------------------------*
				DISABLE_RX_INTS(RX_FIFO1|RX_FIFO3);
				send_event(EVT_PING_PONG);
				*	DMA init 
				DISABLE_RX_INTS(RX_FIFO1);
				USB_init_dma(ENDPOINT_2);
				USB_start_dma(9); */
				break;
			case BULK_SEND_DATA:
				bulkState.restToWrite = bulkState.dataSize;
				bulkState.data = (REQ_LENGTH(req)&0x00ff);
				/* It's seems that USB driver don't need zero length packet */ 
				bulkState.zeroPacketNeed = 0 /*!(bulkState.dataSize%TX_BULK_EP_FIFO_SIZE) */;
				DISABLE_TX(ENDPOINT_1);
				//send_event(EVT_USB_BULK_TX);
				BulkTransmitEvent();
				/* DMA init 
				DISABLE_TX_INTS(TX_FIFO1);
				USB_init_dma(ENDPOINT_1);
				USB_start_dma(9); */
				break;	
			case BULK_TRANS_FILENAME:
				bulkState.restToRead = bulkState.dataSize;
				/* It's seems that USB driver don't need zero length packet */ 
				bulkState.zeroPacketNeed = 0;
				device_buffers.zero_data=1;
				//send_control_data(readybuf, 16);
				/*FLUSHTX0;
	            control_send_buffer.data = readybuf;
	            control_send_buffer.bytes_counter = 8;*/
                FileStat.fn_dlflg=0;
				
				DISABLE_TX(ENDPOINT_1);
				ENABLE_EP(ENDPOINT_2);
				ENABLE_RX_INTS(RX_FIFO1);
				break;
			case BULK_TRANS_FILE:
				bulkState.restToRead = bulkState.dataSize;
				bulkState.zeroPacketNeed = 0;
				device_buffers.zero_data=1;
				DISABLE_TX(ENDPOINT_1);
				ENABLE_EP(ENDPOINT_2);
				ENABLE_RX_INTS(RX_FIFO1);
			default :
				break;
		}
	zero_length_data_response(ENDPOINT_0);
	
}
void Gen_Parser(USB_request_t *req)
{}
/*----------------------------------------------------------------------------------------------
*	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
*		
----------------------------------------------------------------------------------------------*/
/*__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;
}

⌨️ 快捷键说明

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