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

📄 usb.c

📁 COLDFIRE 5275板极初始化程序
💻 C
📖 第 1 页 / 共 2 页
字号:
					 	ep[0].buffer.length = wLength;			
				}
				else if(bDesIndex == 2)
				{
					ep[0].buffer.start = &String2Desc;
					if(wLength == 255)
						ep[0].buffer.length = SD2LEN;
					else
					 	ep[0].buffer.length = wLength;	
				}			
				
			}
			else if(bDesType == INTERFACE)
			{
				/* Need current configuration */
				ep[0].buffer.start  = usb_get_desc(cConfiguration, bDesIndex, -1, -1); 
				cInterface = bDesIndex;
				ep[0].buffer.length = wLength;
			}
			else if(bDesType == ENDPOINT)
			{
				/* Need current configuration and interface */
				ep[0].buffer.start  = usb_get_desc(cConfiguration, cInterface, -1, bDesIndex); 
				ep[0].buffer.length = wLength;
			}
			
			/* Set Buffer position */
			ep[0].buffer.position = 0;
			
			bytes_left = ep[0].buffer.length - ep[0].buffer.position;
			
			iPacket =0;
			
			/* Start by filling FIFO till FF or no bytes left*/
			while((!(MCF_USB_EP0ISR&MCF_USB_EPnISR_FF))&&(bytes_left > 0))
			{
				/* iPacket is the byte count */
				
				/* if last byte in packet, write end of Frame */
				if((iPacket == 7)||(bytes_left==1))
					MCF_USB_EP0FCR |= MCF_USB_EPnFCR_WFR;
			
				/* if this is the last byte in packet and no more bytes are left, set the ZLPS */
				if((iPacket == 7)&&(bytes_left==1))
					MCF_USB_EP0SR |= MCF_USB_EPnSR_ZLPS;

				/* Write Byte to FIFO */
				MCF_USB_EP0FDR_B3 = *((uint8*)(ep[0].buffer.start+ep[0].buffer.position));

				/* Update buffer position */
				ep[0].buffer.position = ep[0].buffer.position + 1;
				
				/* update # of bytes left */			
				bytes_left = ep[0].buffer.length - ep[0].buffer.position;
			
				/* update packet marker */			
				iPacket++;
				
				/* reset packet marker */
				if(iPacket == 8)
					iPacket=0;
			
			}

			/* Set CO bit after first transaction */
			if(ep[0].buffer.length ==64)
				MCF_USB_CR |= MCF_USB_CR_CO;
									
			break;
		case SET_DESCRIPTOR:
			break;
		case GET_CONFIGURATION:
			break;
		case SET_CONFIGURATION:
			break;
		case GET_INTERFACE:
			break;
		case SET_INTERFACE:
			break;
		case SYNCH_FRAME:
			break;
					
		default:
			break;
	}

}
/********************************************************************/
void 
usb_in_service(uint32 epnum, uint32 event)
{
	uint32 free_space;
	uint32 bytes_left;

	if(usb_isr_debug_print != 0)
	{
		
		if (event & MCF_USB_EPnISR_EOT)
			printf("Received EOT for EP #%d\n",epnum);
	}
	
	if ((event & MCF_USB_EPnISR_EOT)&&(epnum ==0))
	{
		/* Clear EOT */
		MCF_USB_EPnISR(epnum) = MCF_USB_EPnISR_EOT;
			
		if (ep[epnum].buffer.start)
		{
			if (ep[epnum].buffer.free)
				free(ep[epnum].buffer.start);

			/* Call the Tx Handler */
			usb_ep_tx_done(epnum);
		}
		ep[epnum].buffer.start = 0;
		ep[epnum].buffer.length = 0;
		ep[epnum].buffer.position = 0;
		ep[epnum].buffer.free = 0;
	}
	
	if ((event & MCF_USB_EPnISR_FL)&&(epnum ==0))
	{
	
		bytes_left = ep[0].buffer.length - ep[0].buffer.position;
		
		/* Kick off by filling FIFO till Alarm goes away or no bytes left*/
		while((MCF_USB_EP0FSR&MCF_USB_EPnFSR_ALARM)&&(bytes_left > 0))
		{
			/* iPacket is the byte count */

			/* if last byte in packet, write end of Frame */
			if((iPacket == 7)||(bytes_left==1))
				MCF_USB_EP0FCR |= MCF_USB_EPnFCR_WFR;
		
			/* if this is the last byte in packet and no more bytes are left, set the ZLPS */
			if((iPacket == 7)&&(bytes_left==1))
				MCF_USB_EP0SR |= MCF_USB_EPnSR_ZLPS;
		
			/* Write Byte to FIFO */
			MCF_USB_EP0FDR_B3 = *((uint8*)(ep[0].buffer.start+ep[0].buffer.position));
			
			/* Update buffer position */
			ep[0].buffer.position = ep[0].buffer.position + 1;
			
			/* update # of bytes left */			
			bytes_left = ep[0].buffer.length - ep[0].buffer.position;
		
			/* update packet marker */			
			iPacket++;
			
			/* reset packet marker */
			if(iPacket == 8)
				iPacket=0;
		
		}
	}
}
/********************************************************************/
void
usb_ep_tx_done(uint32 epnum)
{
	/* Check if this a Vendor/Class specific transfer */
	if (epnum == 0)
		usb_devreq_done(0);
}
/********************************************************************/
void
usb_devreq_done(uint32 error)
{
	if (error)
	{
		/* This sends a single STALL as the handshake */
		MCF_USB_CR |= MCF_USB_CR_CO | MCF_USB_CR_CE;	
	}

	else
	{
		MCF_USB_CR |= MCF_USB_CR_CO;	
	}
	
	
	/* wait for CO to clear */
	while(MCF_USB_CR & MCF_USB_CR_CO);

	return;
}
/********************************************************************/

__interrupt__
void 
usb_ep0_handler (void)
{
	uint32 event;

	/* usb_ep0_handler */
	event = MCF_USB_EP0ISR & (~MCF_USB_EP0IMR);

	if(event & MCF_USB_EPnISR_DR)
		usb_handle_device_request();
	if(event & MCF_USB_EPnISR_EOT)
		usb_in_service(0, event);
	
	if(event & MCF_USB_EPnISR_FL)
	{
		usb_in_service(0, event);	
	}
		
	/* a simple debug print */
	if(usb_isr_debug_print != 0)
	{
		if(event & MCF_USB_EPnISR_FF)
			printf("-EP0 FIFO Full\n");
		if(event & MCF_USB_EPnISR_FM)
			printf("-EP0 FIFO Empty\n");	
		if(event & MCF_USB_EPnISR_FE)
			printf("-EP0 FIFO Error\n");	
		if(event & MCF_USB_EPnISR_FH)
			printf("-EP0 FIFO High level alarm\n");	
		if(event & MCF_USB_EPnISR_FL)
			printf("-EP0 FIFO Low level alarm\n");	
		if(event & MCF_USB_EPnISR_MDR)
			printf("-EP0 FIFO Multiple setup packets pending\n");	
		if(event & MCF_USB_EPnISR_EOT)
			printf("-EP0 FIFO End of Transfer\n");	
		if(event & MCF_USB_EPnISR_DR)
			printf("-EP0 FIFO Device Request\n");	
		if(event & MCF_USB_EPnISR_EOF)
			printf("-EP0 FIFO End of Frame\n");	
	}

	MCF_USB_EP0ISR = event;	

}

/********************************************************************/
__interrupt__
void 
usb_ep1_handler (void)
{
	uint32 isr;
	
	/* usb_ep1_handler */
	isr = MCF_USB_EP1ISR;

	if(usb_isr_debug_print != 0)
	{	
		if(isr & MCF_USB_EPnISR_FF)
			printf("-EP1 FIFO Full\n");
		if(isr & MCF_USB_EPnISR_FM)
			printf("-EP1 FIFO Empty\n");	
		if(isr & MCF_USB_EPnISR_FE)
			printf("-EP1 FIFO Error\n");	
		if(isr & MCF_USB_EPnISR_FH)
			printf("-EP1 FIFO High level alarm\n");	
		if(isr & MCF_USB_EPnISR_FL)
			printf("-EP1 FIFO Low level alarm\n");	
		if(isr & MCF_USB_EPnISR_MDR)
			printf("-EP1 FIFO Multiple setup packets pending\n");	
		if(isr & MCF_USB_EPnISR_EOT)
			printf("-EP1 FIFO End of Transfer\n");	
		if(isr & MCF_USB_EPnISR_DR)
			printf("-EP1 FIFO Device Request\n");	
		if(isr & MCF_USB_EPnISR_EOF)
			printf("-EP1 FIFO End of Frame\n");	
	} 
	
	/* if EP1 Low level alarm, fill it up with incrementing data bytes */
	
	/* Check if FIFO low */
	if(isr & MCF_USB_EPnISR_FL) 
	{
		/* Fill FIFO till FH, better bandwidth if trigger on FF */	
		while(MCF_USB_EP1FSR&MCF_USB_EPnFSR_ALARM)
		{

			if(iPacketEP1 == 28)
				MCF_USB_EP1FCR |= MCF_USB_EPnFCR_WFR;

			/* build next FIFO write */
			FIFOdata =( 0 | 
					  ((uint32)(INdata)<<24)|
					  ((uint32)(INdata+1)<<16)|
					  ((uint32)(INdata+2)<<8)|
					  ((uint32)(INdata+3)<<0));
			
			MCF_USB_EP1FDR = FIFOdata;
				
			/* INdata now = the next data to be written */
			INdata = INdata +4;		
			
			/* iPacket now = the next packet index to be written */
			iPacketEP1 = iPacketEP1 +4;
			TotalData =	TotalData +4;
			
			if(iPacketEP1 == 32)
				iPacketEP1 = 0;
			
		}
		
	}
	MCF_USB_EP1ISR=isr;	
}
/********************************************************************/
__interrupt__
void 
usb_ep2_handler (void)
{
	uint32 isr;
	uint32 cmd = 0;
	uint32 epsr;
	
	/* usb_ep2_handler */
	isr = MCF_USB_EP2ISR;
	
	
	if(usb_isr_debug_print != 0)
	{	
		if(isr & MCF_USB_EPnISR_FF)
			printf("-EP2 FIFO Full\n");
		if(isr & MCF_USB_EPnISR_FM)
			printf("-EP2 FIFO Empty\n");	
		if(isr & MCF_USB_EPnISR_FE)
			printf("-EP2 FIFO Error\n");	
		if(isr & MCF_USB_EPnISR_FH)
			printf("-EP2 FIFO High level alarm\n");	
		if(isr & MCF_USB_EPnISR_FL)
			printf("-EP2 FIFO Low level alarm\n");	
		if(isr & MCF_USB_EPnISR_MDR)
			printf("-EP2 FIFO Multiple setup packets pending\n");	
		if(isr & MCF_USB_EPnISR_EOT)
			printf("-EP2 FIFO End of Transfer\n");	
		if(isr & MCF_USB_EPnISR_DR)
			printf("-EP2 FIFO Device Request\n");	
		if(isr & MCF_USB_EPnISR_EOF)
			printf("-EP2 FIFO End of Frame\n");	
	}

	MCF_USB_EP2ISR=isr;	
}
/********************************************************************/
__interrupt__
void 
usb_ep3_handler (void)
{
	uint32 isr;
	uint32 cmd = 0;
	uint32 epsr;	
	
	/* usb_ep3_handler */
	isr = MCF_USB_EP3ISR;
	
	epsr = MCF_USB_EP3SR;
	if(((epsr>>16)&0x0000FFFF)>0)
	{			
		/* Read 1st long word for CMD*/
		cmd = MCF_USB_EP3FDR;
		if(cmd == MCF5275_CMD_LED)
			toggle_leds();
	}	
	
	if(usb_isr_debug_print != 0)
	{	
		if(isr & MCF_USB_EPnISR_FF)
			printf("-EP3 FIFO Full\n");
		if(isr & MCF_USB_EPnISR_FM)
			printf("-EP3 FIFO Empty\n");	
		if(isr & MCF_USB_EPnISR_FE)
			printf("-EP3 FIFO Error\n");	
		if(isr & MCF_USB_EPnISR_FH)
			printf("-EP3 FIFO High level alarm\n");	
		if(isr & MCF_USB_EPnISR_FL)
			printf("-EP3 FIFO Low level alarm\n");	
		if(isr & MCF_USB_EPnISR_MDR)
			printf("-EP3 FIFO Multiple setup packets pending\n");	
		if(isr & MCF_USB_EPnISR_EOT)
			printf("-EP3 FIFO End of Transfer\n");	
		if(isr & MCF_USB_EPnISR_DR)
			printf("-EP3 FIFO Device Request\n");	
		if(isr & MCF_USB_EPnISR_EOF)
			printf("-EP3 FIFO End of Frame\n");	
	}

	MCF_USB_EP3ISR=isr;	
}
/********************************************************************/
__interrupt__
void 
usb_handler (void)
{
	uint32 isr;
	uint32 tmp;
	
	/* usb_handler */
	isr = MCF_USB_ISR;

	if(isr & MCF_USB_ISR_RSTP)
	{
		/* uart0_out_char('r'); */
	}
	if(isr & MCF_USB_ISR_CC)
	{

	}
	if(isr & MCF_USB_ISR_RSTR)
	{
		/* uart0_out_char('R'); */
		/* Flush USB FIFOs */
		MCF_USB_EP0SR |= MCF_USB_EPnSR_FL;
		MCF_USB_EP1SR |= MCF_USB_EPnSR_FL;
		MCF_USB_EP2SR |= MCF_USB_EPnSR_FL;
		MCF_USB_EP3SR |= MCF_USB_EPnSR_FL;
	}
	if(isr & MCF_USB_ISR_SUSP)
	{
		/* uart0_out_char('S'); */	
	}
	if(isr & MCF_USB_ISR_RES)
	{
		/* uart0_out_char('s'); */
	}
	if(usb_isr_debug_print != 0)
	{
		
		if(isr & MCF_USB_ISR_MSOF)
			printf("-USB Missed Start of Frame\n");
		if(isr & MCF_USB_ISR_SOF)
			printf("-USB Start of Frame\n");
		if(isr & MCF_USB_ISR_RSTP)
			printf("-USB End of Reset signalling\n");
		if(isr & MCF_USB_ISR_RSTR)
			printf("-USB Reset in progress\n");
		if(isr & MCF_USB_ISR_RES)
			printf("-USB has left suspended state->resume\n");
		if(isr & MCF_USB_ISR_SUSP)
			printf("-USB is suspended\n");
		if(isr & MCF_USB_ISR_FM)
			printf("-USB Match occured between Frame number and frame match register\n");
		if(isr & MCF_USB_ISR_CC)
			printf("-USB Configuration change occured\n");
	}
		
	MCF_USB_ISR=isr;	
}
/********************************************************************/

⌨️ 快捷键说明

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