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

📄 usbdriver.c

📁 周立功 214x pack 配套光盘
💻 C
📖 第 1 页 / 共 2 页
字号:
    			tmp = len - reccnt;
				if (tmp >= eppsize)							   /* 根据接收的长度从缓冲区中读取数据read data from endpoint buffer according the len value */		
					tmp = USB_ReadEndpoint(endp, eppsize, recbuff + reccnt);
				else
					tmp = USB_ReadEndpoint(endp, tmp, recbuff + reccnt);
					
				reccnt = reccnt + tmp;
				if (reccnt >= len)
				{											   /* 接收成功 						  receive sucessfully */
					OS_EXIT_CRITICAL();
					USB_RW_Result(endp, buffnums, pUsb, 1, 0);
					return USB_NO_ERR;
				}
			}

			pUsb->bTaskWaiting = 0;							    /* 任务未进入等待接收状态         tha Task has not enter waiting receiving status */
			OS_EXIT_CRITICAL();
		}
		else
		{
			USB_RW_Result(endp, buffnums, pUsb, 1, 1);			/* 超时退出 					  exit when timeout*/
			return USB_ERR_WR_TIMEOUT;
		}
	}
}
#endif

/*************************************************************************************************************************
** 函数名称: USB_WritePort()								  Name:		  USB_WritePort()
** 功能描述: 向USB主机发送数据(非DMA方式)					  Function:	  send data to USB Host(Not DMA)
** 输   入: INT8U	   endp:  物理端点索引号 	  			  Input:	  INT8U	    endp:  the physical endpoint number
			 INT32U eppsize:  端点缓冲区大小							  INT32U eppsize:  the size of endpoint buffer
  		     INT8U buffnums:  该端点的缓冲区个数						  INT8U buffnums:  numbers of endpoint buffer
			 CTRL_USB *pUsb:  接收/发送数据控制块                         CTRL_USB *pUsb:  the control block of receiving/sending 
			 INT32U     len:  接收字节个数								  INT32U     len:  the numbers(Bytes) that will be received
			 INT8U *sendbuff: 接收缓冲区								  INT8U *sendbuff: the transmision buffer
			 INT16U	timeout:  超时等待时间, 等于0表示无限等待             INT16U timeout:  timeout of transmision,0 indicates limitless waiting
** 输	 出: 0:	读成功       > 0 读失败(错误码)				  Output:	  0:  sucessfully	 >0: fail (it is error code)
**************************************************************************************************************************/
#if (!DMA_ENGINE_EN)
INT8U USB_WritePort(INT8U endp, INT32U eppsize, INT8U buffnums, CTRL_USB *pUsb, INT8U *sendbuff, INT32U len, INT16U timeout)
{
	INT8U err,i;
	INT32U sendcnt = 0, tmp;
	
	OS_ENTER_CRITICAL();
	err = USB_RW_Param(pUsb, sendbuff);							/* 检查参数正确性  	check the parameter */
	if (err != USB_NO_ERR)
	{
		OS_EXIT_CRITICAL();
		return err;
	}
	OS_EXIT_CRITICAL();
		
	while(1)
	{	
		OS_ENTER_CRITICAL();
		pUsb->bTaskWaiting = 0;									/* 任务未进入等待发送状态               the task has not enter waiting for transmitting status*/
		for(i = 0; i < buffnums; i++)
    	{
			if ((USB_SelectEndpoint(endp) & 0x60) == 0x60)  	/* 端点缓冲区满     the endpoint buffer is full */
				break;
    	    	
    		tmp = len - sendcnt;								
			if (tmp >= eppsize)									/* 根据未发送的长度向缓冲中写入数据 */
				tmp = USB_WriteEndpoint(endp, eppsize, sendbuff + sendcnt);
			else
				tmp = USB_WriteEndpoint(endp, tmp, sendbuff + sendcnt);
					
			sendcnt = sendcnt + tmp;
			if (sendcnt >= len)									
			{													/* 全部数据都已写入端点缓冲区中         all data has been written into endpoint buffer */
				OS_EXIT_CRITICAL();
				goto USB_WRITE_END;
			}
		}
		OS_EXIT_CRITICAL();
		
		err = USB_WaitEpReady(pUsb, timeout);					/* 等待端点缓冲区中的数据发送到USB主机  waiting for the data in endpoint buffer has been transmitted to USB Host*/		
		if (err != USB_NO_ERR)
		{
			 USB_RW_Result(endp, buffnums, pUsb, 0, 1);
			 return USB_ERR_WR_TIMEOUT;							/* 超时退出                             timeout and exit */
		}
	}
	
	/** 等待最后发送缓冲区中的数据全部发到USB主机 **/
USB_WRITE_END:

	err = USB_WaitEpReady(pUsb, timeout);						/* 等待端点缓冲区中的数据发送到USB主机  waiting for the data in endpoint buffer has been transmitted to USB Host*/	
	USB_RW_Result(endp, buffnums, pUsb, 0, 0);	
	if (err == OS_TIMEOUT)
	    return USB_ERR_WR_TIMEOUT;								/* 超时退出  							timeout and exit*/
	else
		return USB_NO_ERR;										/* 发送成功  							transmit sucessfully */
}
#endif

/**************************************************************************************************************************************
** 函数名称: USB_RW_Result()									Name:		USB_RW_Result()
** 功能描述: USB 读写端点后处理									Function:	process it after the read/write port
** 输	 入: INT8U endp		  : 端点索引号						Input:		INT8U endp		 :  the index of endpoint
			 INT8U buffnums	  : 该端点缓冲区个数							INT8U buffnums	 :  numbers of endpoint buffer
			 CTRL_USB *pUsb   : USB接收或发送控制块指针						CTRL_USB *pUsb   :	the pointer of CTRL_USB struct
			 INT8U bread	  : 当前任务状态(1:接收 0:发送)					INT8U bread	     :  status of current task(1:receive 0:transmit)
			 INT8U berr		  : 数据收发有错误								INT8U berr		 :	there is error during receving or sending	 
** 输	 出: 无													Output:		NULL
***************************************************************************************************************************************/
#if (!DMA_ENGINE_EN)
void USB_RW_Result(INT8U endp, INT8U buffnums, CTRL_USB *pUsb, INT8U bread, INT8U berr)
{	
	INT8U i,endpstatus;
	
	OS_ENTER_CRITICAL();
	if (berr == 1)
	{
		for (i = 0; i < buffnums; i++)
		{
			endpstatus = USB_SelectEndpoint(endp);
			if ((endpstatus & 0x60) != 0)					/* 接收/发送缓冲区不空  receiving/transmission buffer is not empty */
			{												
				if (bread == 1)
		   		{											/* 当前为接收状态		receiving currently */			
					USB_SelectEndpoint(endp);
					USB_ClearBuffer();						/* 清空OUT 端点缓冲区	clear OUT endpoint buffer */
				}
				else
			   		USB_SetEndpointStatus(endp, 0);			/* 解禁 IN 端点			unstall IN endpoint */
			}
		}		
	}
	
	pUsb->bEpUsed = 0;										/* 标识本任务不占用该端点   flag the task not use the endpoint */	
	pUsb->bTaskWaiting = 0;									/* 标识任务未进入等待状态   flag no task does not enter waiting status */
	OS_EXIT_CRITICAL();
}
#endif

/***************************************************************************************************************
** 函数名称 : USB_ConfigEndpoint()						Name	 : USB_ConfigEndpoint()
** 功能描述 : 配置所有端点的最大包大小	  			    Function : config the maxpacket size of all endpoint
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void USB_ConfigEndpoint(void)
{
	USB_ConfigMaxPaketSize(0,EP0_PACKET_SIZE);
	USB_ConfigMaxPaketSize(1,EP0_PACKET_SIZE);

	USB_ConfigMaxPaketSize(2,EP1_PACKET_SIZE);
	USB_ConfigMaxPaketSize(3,EP1_PACKET_SIZE);
	
	USB_ConfigMaxPaketSize(4,EP2_PACKET_SIZE);
	USB_ConfigMaxPaketSize(5,EP2_PACKET_SIZE);
}

/***************************************************************************************************************
** 函数名称 : Usb_Exception()							Name	 : Usb_Exception()
** 功能描述 : USB 中断服务程序			  			    Function : USB Interrupt Service Program
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void Usb_Exception(void) 
{
	INT32U usb_ints,dev_ints,devstatus;
	
	OS_ENTER_CRITICAL();
		
	/****** USB status interrupt ******/
	dev_ints = USBDevIntSt;								/* 读设备中断状态寄存器  read device interrupt status register */
	if (dev_ints & DEVINT_STATUS_DEVSTAT)			
	{
		devstatus = USB_GetDevStatus();
		if ((devstatus & 0x10) != 0)
			USB_BusReset();								/* 总线复位处理   		 process the bus reset      */

		if ((devstatus & 0x08) != 0)
			USB_SuspendChange();						/* 挂起改变处理   		 process the suspend change */

		if ((devstatus & 0x02) != 0)
			USB_ConnectChange();						/* 连接改变处理   		 process the connect change */
		
		USBDevIntClr = DevStatusInterrupt;
		goto USB_ISR_EXIT;								/* 退出中断服务程序      exit the interrupt service */
	}	
	
	/****** data transmission interrupt ******/
	usb_ints = USBIntSt & 0x7;
	if (usb_ints != 0)
	{
		if (usb_ints & USBINT_STATUS_HP)
			Usb_HPService();					   		/* 高优先级中断处理    process High priority Interrupt */
		
		if (usb_ints & USBINT_STATUS_LP)
			Usb_LPService();							/* 低优先级中断处理    process Slow priority Interrupt */

#if DMA_ENGINE_EN		
		if (usb_ints & USBINT_STATUS_DMA)
			USB_DMAService();							/* DMA中断处理         process DMA Interrupt */
#endif
	}

USB_ISR_EXIT:
	OS_EXIT_CRITICAL();
   	VICVectAddr = 0x00;									/* 通知LPC214x中断结束 Inform LPC214x interrupt end */
}


/***************************************************************************************************************
** 函数名称 : Usb_HPService()							Name	 : Usb_HPService()
** 功能描述 : USB 端点高速中断服务程序	  			    Function : USB endpoint fast Interrupt Service Program
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void Usb_HPService(void)
{
	INT32U ep_st,i;
	
	ep_st = USBEpIntSt;									/* 读端点中断状态寄存器      read endpoint interrupt status register */

	if(ep_st & USB_ENDP00)
		ep0_rxdone();									/* 控制端点接收数据处理 	 process controll endpoint receive data */
	if(ep_st & USB_ENDP01) 
		ep0_txdone();									/* 控制端点发送数据处理 	 process controll endpoint transmit data */

	for (i = 2; i <= 5; i++)
	{
		if ((ep_st & (0x01 << i)) != 0)
			 Usb_EpISR(i);								/* 其它端点接收/发送数据处理 process other endpoint recevie and transmit data */
	}
		
	USBDevIntClr = FASTINTERRUPT;
}


/***************************************************************************************************************
** 函数名称 : Usb_LPService()							Name	 : Usb_LPService()
** 功能描述 : USB 端点慢速中断服务程序	  			    Function : USB endpoint slow Interrupt Service Program
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void Usb_LPService(void)
{
	INT32U ep_st,i;
	
	ep_st = USBEpIntSt;									/* 读端点中断状态寄存器      read endpoint interrupt status register */

	if(ep_st & USB_ENDP00)
		ep0_rxdone();									/* 控制端点接收数据处理 	 process controll endpoint receive data */
	if(ep_st & USB_ENDP01) 
		ep0_txdone();									/* 控制端点发送数据处理 	 process controll endpoint transmit data */

	for (i = 2; i <= 5; i++)
	{
		if ((ep_st & (0x01 << i)) != 0)
			 Usb_EpISR(i);								/* 其它端点接收/发送数据处理 process other endpoint recevie and transmit data */
	}	

	USBDevIntClr = SLOWINTERRUPT;
}

/***************************************************************************************************************
** 函数名称 : Usb_EpISR()								Name	 : Usb_EpISR()
** 功能描述 : 端点接收/发送数据处理		  		    	Function : proecess endpoint receive/transmit
** 输	 入 : INT8U endp: 物理端点号		 			Input	 : INT8U endp: physical endpoint number
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void Usb_EpISR(INT8U endp)
{
	USB_SelectClrIntEndpoint(endp);						/* 选择端点并清除中断  		        select endpoint/clear interrupt */

#if (!DMA_ENGINE_EN)
	if (Ctrl_Usb[endp - 2].bTaskWaiting == 0)			/* 任务未等待进入等待接收/发送状态  there is no task waiting for receiving data */
		Ctrl_Usb[endp - 2].bEpReady = 1;				/* 置位标志 				        set the flag the endpoint is ready */
	else
		OSSemPost(Ctrl_Usb[endp - 2].Ep_Sem);			/* 使等待接收/发送数据的任务就绪    send semaphore to the task waiting for receiving or transmitting data */
#endif
}


/***************************************************************************************************************
** 函数名称 : USB_BusReset()							Name	 : USB_BusReset()
** 功能描述 : USB 总线复位处理				  			Function : process USB bus reset  
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void USB_BusReset(void)
{
	USB_USBDevIntConfig();
	USB_ConfigEndpoint();								/* 重新配置所有端点最大包大小 */
}


/***************************************************************************************************************
** 函数名称 : USB_Suspend()								Name	 : USB_Suspend()
** 功能描述 : USB 总线挂起改变				  			Function : process USB bus suspend change 
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void USB_SuspendChange(void)
{

}


/***************************************************************************************************************
** 函数名称 : USB_ConnectChange()						Name	 : USB_ConnectChange()
** 功能描述 : USB 总线连接改变				  			Function : process USB bus connect change
** 输	 入 : 无							 			Input	 : NULL
** 输	 出 : 无										Output	 : NULL
****************************************************************************************************************/
void USB_ConnectChange(void)
{

}


/*******************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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