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

📄 usbdriver.c

📁 SmartARM2400系列开发板全套资料
💻 C
📖 第 1 页 / 共 3 页
字号:
			 INT16U  timeout:   超时等待时间, 等于0表示无限等待 			 INT16U  timeout:   timeout	of transmitting,0 indicates limitless waiting		 
** 输   出: 0: 发送成功	 > 0: 发送失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort1(INT32U len, INT8U *sendbuff, INT16U timeout)
{

#if DMA_ENGINE_EN
	return (USB_WritePortDMA(3, &Ctrl_Usb[1], sendbuff, len, timeout));		/* DMA方式 */
#else
	return (USB_WritePort(3, 64, 2, &Ctrl_Usb[1], sendbuff, len, timeout));	/* 非DMA方式 */
#endif

}	/* 通过物理端点 3 发送数据 */


/*******************************************************************************************************************************
** 函数名称: INT8U WritePort2()									Name:		 INT8U WritePort2()	
** 功能描述: 用端口 2 发送len个字节								Function:	 Send len Bytes via Port2
** 输   入: INT32U  len: 发送的字节数							Input:		 INT32U  len:   	numbers will be send
						  (取值范围为0x00000001 ~ 0xFFFFFFFF)			    			    	(range: 0x00000001 ~ 0xFFFFFFFF)	
			 INT8U   sendbuff:  发送缓冲区指针								 INT8U   sendbuff:  send buffer
			 INT16U  timeout:   超时等待时间, 等于0表示无限等待 			 INT16U  timeout:   timeout	of transmitting,0 indicates limitless waiting		 
** 输   出: 0: 发送成功	 > 0: 发送失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort2(INT32U len,INT8U *sendbuff,INT16U timeout)
{

#if DMA_ENGINE_EN
	return (USB_WritePortDMA(5, &Ctrl_Usb[3], sendbuff, len, timeout));		 /* DMA方式 */	
#else
	return (USB_WritePort(5, 64, 2, &Ctrl_Usb[3], sendbuff, len, timeout));	 /* 非DMA方式 */	
#endif

}	/* 通过物理端点 5 发送数据 */



/***********************************************************************************************************************
** 函数名称: USB_RW_Param()										Name:		USB_RW_Param()	
** 功能描述: 填写USB接收或发送控制块有关参数					Function:	fill the CTRL_USB structure
** 输	 入: CTRL_USB *pUsb   : USB接收或发送控制块指针			Input:		CTRL_USB *pUsb: the CTRL_USB structure
			 INT8U *buff	  : 接收或发送缓冲区指针						INT8U *recbuff: the reception/transmittion buffer
** 输	 出: 0   调用成功    > 0 调用失败(错误码)				Output:	 	0:  sucessfully	 >0: fail (it is error code)
***********************************************************************************************************************/
INT8U USB_RW_Param(CTRL_USB *pUsb, INT8U *pbuff)
{
	if (bEPPflags.bits.configuration == 0)						
		return USB_ERR_NO_CONFIG;							/* USB总线未配置完成	the USB bus is not confirgurated */

	if (pbuff == NULL)
		return USB_ERR_BUFF_INVALID;						/* 缓冲区指针无效错误	the pointer of buff is invalid   */
	
	if (pUsb->bEpUsed == 1)
		return USB_ERR_ENDP_OCCUPY;							/* 端点已被其它任务占用 the endpoint have been used */  
		
	pUsb->bEpUsed = 1;										/* 本任务占用该端点     the task use the endpoint */
					
	return USB_NO_ERR;
}


/***********************************************************************************************************************
** 函数名称: USB_WaitEpReady()									Name:		USB_WaitEpReady()	
** 功能描述: 等待端点就绪										Function:	Waiting for the endpoint is ready
** 输	 入: CTRL_USB *pUsb : USB接收或发送控制块指针			Input:		CTRL_USB *pUsb: the CTRL_USB structure
			 INT16U timeout	: 超时时间										INT8U *recbuff: timeout
** 输	 出: 0   调用成功    > 0 调用失败(错误码)				Output:	 	0:  sucessfully	 >0: fail (it is error code)
***********************************************************************************************************************/
#if (!DMA_ENGINE_EN)
INT8U USB_WaitEpReady(CTRL_USB *pUsb, INT16U timeout)
{
	INT8U err;

	OS_ENTER_CRITICAL();
	if (pUsb->bEpReady == 1)									   
	{													   /* 端点已就绪   	                the endpoint is ready */
		pUsb->bEpReady = 0;
		OS_EXIT_CRITICAL();	
		err = USB_NO_ERR;								   /* 无须等待						need not waiting */
	}
	else
	{
		pUsb->bTaskWaiting = 1;							   /* 置位,表示任务进入等待的状态   set the bit, indicate the task enter the waiting status */
		OSSemPend(pUsb->Ep_Sem, timeout, &err); 		   /* 等待端点就绪(进入等待状态)   	wait for endpoint is ready */
		OS_EXIT_CRITICAL();
	}
	
	return err;
}
#endif


/*************************************************************************************************************************
** 函数名称: USB_ReadPort()									  Name:		  USB_ReadPort()
** 功能描述: 读端点(非DMA方式)								  Function:	  read data from endpoint(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 *recbuff:  接收缓冲区								  INT8U *recbuff: the reception buffer
			 INT16U	timeout:  超时等待时间, 等于0表示无限等待             INT16U timeout: timeout of receiving,0 indicates limitless waiting
** 输	 出: 0:	读成功       > 0 读失败(错误码)				  Output:	  0:  sucessfully	 >0: fail (it is error code)
**************************************************************************************************************************/
#if (!DMA_ENGINE_EN)
INT8U USB_ReadPort(INT8U endp, INT32U eppsize, INT8U buffnums, CTRL_USB *pUsb, INT32U len, INT8U *recbuff, INT16U timeout)
{
	INT8U err,i;
	INT32U reccnt = 0, tmp;	
	
	OS_ENTER_CRITICAL();
	err = USB_RW_Param(pUsb, recbuff);						   /* 检查参数正确性  				  check the parameter */
	if (err != USB_NO_ERR)
	{
		OS_EXIT_CRITICAL();
		OSTimeDly(1);
		return err;											   /* 返回错误码     				  return error code   */
	}
	OS_EXIT_CRITICAL();	
	
	while(1)
	{	
		err = USB_WaitEpReady(pUsb, timeout);				   /* 等待端点收到数据				  waiting for the endpoint receiving data */	

		/********** 下面从端点缓冲区中读取数据 *********/
		if (err == OS_NO_ERR)
		{
			OS_ENTER_CRITICAL();
			for(i = 0; i < buffnums; i++)
    		{
    			if ((USB_SelectEndpoint(endp) & 0x60) == 0)
					break;									   /* 缓冲区为空 					  endpoint buffer is empty */
								
    			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;

    if ((USB_SelectEndpoint(endp) & 0x02) == 0x02)  	/* 端点已被禁止     the endpoint has been stalled */
	    return USB_ENDP_STALLED;

	OS_ENTER_CRITICAL();
	err = USB_RW_Param(pUsb, sendbuff);							/* 检查参数正确性  	check the parameter */
	if (err != USB_NO_ERR)
	{
		OS_EXIT_CRITICAL();
		OSTimeDly(1);
		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

⌨️ 快捷键说明

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