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

📄 d13driver.c

📁 基于菲利普USB接口芯片ISP1161A1从口的USB设备开发
💻 C
📖 第 1 页 / 共 3 页
字号:
		}
		else if(i_st &D13REG_INTSRC_SUSPEND){ 
		    usb_suspend();							//ISP1181B挂起中断处理				deal with the suspend interrupt of
		    bEPPflags.bits.suspend = 1;				//									ISP1181B					
		}
		else if(i_st & D13REG_INTSRC_EOT)
			dma_eot();								//DMA传输结束处理					deal with the end of DMA transfer
		else if(i_st & D13REG_INTSRC_RESUME){
			D13_LockDevice(0xAA37);					//解锁设备,重新使能其它寄存器		unlock the device,enable all register 
			//添加恢复系统所有元件正常功能代码
		}										  	
		else {
			if(i_st & D13REG_INTSRC_EP0IN)	
				ep0_txdone();						 //控制端点发送数据处理				deal with the transmit of control endpoint IN	
			if(i_st & D13REG_INTSRC_EP0OUT)     
				ep0_rxdone();						 //控制端点接收数据处理				deal with the receive of control endpoint OUT
			if(i_st & D13REG_INTSRC_EP01)
				USB_WriteISR(&UsbSend_EP01,EPINDEX4EP01,16,1);		//端点 1 发送数据处理	deal with the transmit of endpoint 1
			if(i_st & D13REG_INTSRC_EP02)			
				USB_ReadISR(&UsbRec_EP02,EPINDEX4EP02,16,1);		//端点 2 接收数据处理	deal with the receive of endpoint 2	
			if(i_st & D13REG_INTSRC_EP03)
				USB_WriteISR(&UsbSend_EP03,EPINDEX4EP03,64,2);		//端点 3 发送数据处理	deal with the transmit of endpoint 3
			if(i_st & D13REG_INTSRC_EP04)			
				USB_ReadISR(&UsbRec_EP04,EPINDEX4EP04,64,2);		//端点 4 接收数据处理	deal with the receive of endpoint 4	
			if(i_st & D13REG_INTSRC_EP05)
				USB_WriteISR(&UsbSend_EP05,EPINDEX4EP05,64,2);		//端点 5 发送数据处理	deal with the transmit of endpoint 5
			if(i_st & D13REG_INTSRC_EP06)			
				USB_ReadISR(&UsbRec_EP06,EPINDEX4EP06,64,2);		//端点 6 接收数据处理	deal with the receive of endpoint 6			
		}
	}
	//add for ARM
	CLR_INTD13();									//清除 ISP1181B 的中断标志			clear the interrupt flag of ISP1181B
	CLR_INT();										//通知中断结束						info ARM the end of the interrupt
   	
   	OS_EXIT_CRITICAL();							    //退出中断服务程序时开中断			enable all interrupt when interrupt rountine exit

}							

/***********************************************************************************************************************
** 函数名称: void TaskSetup(void *pdata)			Name:	    void TaskSetup(void *pdata)
** 功能描述: 控制传输处理							Function:   deal with control transfer
** 输	 入: void *pdata  任务参数					Input:		void *pdata:   parameter of the task
** 输	 出: 无										Output:	    NULL
** 注	 意: 该任务的优先级应高于其它任务,才能在	Note:		the prior of the task must be higher than other,so 
			 任何情况下传输SETUP包								it will be sucessful when tranfering SETUP packet										 
************************************************************************************************************************/
void  TaskSetup(void *pdata)
{
#if OS_CRITICAL_METHOD == 3       /* Allocate storage for CPU status register */
	OS_CPU_SR  cpu_sr;
#endif
	INT8U err;
	
	pdata = pdata;                /* Prevent compiler warning */
	for (;;)
	{
		OSSemPend(pSetup_Event,0,&err);		//等待Setup包		 wait for SETUP packet
		if (err == OS_NO_ERR){				//接收到Setup包 	 have received SETUP packet
			OS_ENTER_CRITICAL();			//关中断			 disable all interrupt
			control_handler();				//进行控制传输处理	 deal with control transfer
			OS_EXIT_CRITICAL();			    //开中断			 enable all interrupt
		}
	}
}
	
/************************************************************************************************************************
** 函数名称:  USB_ReadISR()										Name:		USB_ReadISR()
** 功能描述:  USB 端点接收中断服务程序							Function:	the reception interrupt service of USB endpoint 
** 输   入:  CTRL_USB *pUsb:  USB接收与发送控制结构体指针		Input:		CTRL_USB *pUsb: the CTRL_USB structure
			  INT8U		endp:  端点索引号									INT8U	  endp: the index of endpoint
			  INT32U eppsize:  端点最大信息包大小							INT32U eppsize: max size of the endpoint
			  INT8U buffnums:  该端点缓冲区个数								INT8U buffnums: numbers of endpoint buffer
** 输   出:  无												Output:		NULL
*************************************************************************************************************************/
void USB_ReadISR(CTRL_USB *pUsb,INT8U endp,INT32U eppsize,INT8U buffnums)
{
	INT8U *pBuff,endpstatus,i;
	INT32U len;
	
	D13_GetEndpointStatusWInteruptClear(endp);		   //清空该端点的中断标志     clear the flag of the endpoint interrupt register
	
	if (pUsb->Grp != 0){						   	   //有任务在等待接收         exist task waiting for reception
		if (pUsb->Sem == 0){					   	   //没有任务占用该端点       no task use the endpoint
			pUsb->Prio = USB_GetHighPrio(pUsb);        //取等待任务列表中优先级最高的任务的优先级 get the highest prior of wating table
			pUsb->Cnt  = 0;				   			   //接收计数清0              counter clear to zero
			pUsb->Sem  = 1; 				   		   //标志有任务独占了该端点   flag that task use the endpoint
		}
		pBuff = pUsb->pBuff[pUsb->Prio];		  	   //取得接收指针 			  get the pointer of reception
		
		for (i = 0; i < buffnums; i++){											
			len = pUsb->Max[pUsb->Prio] - pUsb->Cnt;   //计算未接收字节数         calculate the numbers of bytes that will be received
			if (len > 0){
				if (len >= eppsize)											
					len = (INT8U)D13_ReadEndpoint(endp,(INT8U)eppsize,pBuff + pUsb->Cnt);//接收整个缓冲区 receive a endpoint buffer
				else
					len = (INT8U)D13_ReadEndpoint(endp,(INT8U)len,pBuff + pUsb->Cnt); 	 //接收len个字节  receive len bytes
				pUsb->Cnt = pUsb->Cnt + len;		    //计数器计数 counter counting 
			}		

			if (pUsb->Cnt >= pUsb->Max[pUsb->Prio]){	//如果接收完毕 				      if the reception is finished
 				pUsb->Max[pUsb->Prio] = 0;				//接收完成标志置0,表示正确接收    set zero indicate reception is correct
	   	  		pUsb->Sem  = 0;							//释放该端点接收资源		      release of the resourece of endpoint reception
	   	  		USB_DelPrio(pUsb,pUsb->Prio);			//将该任务从等待队列中删除	      delete the task from waiting table
	  			OSTimeDlyResume(pUsb->Prio);		    //使该接收任务就绪			      resume the reception task
	  			break;
	  	 	}
			
			endpstatus = D13_GetEndpointStatusWOInteruptClear(endp); //读该端点状态寄存器 			  read endpoint status register
			if ((endpstatus & 0x60) == 0)  
				break;												 //如果缓冲为空,立即跳出循环	  if buffer is null,break
			else if(i == 0)
				D13_GetEndpointStatusWInteruptClear(endp); 			
		}//end of for()
	}//end of if (pUsb->Grp != 0)
	else{
	    for (i = 0; i < buffnums; i++){								 //没有任务接收数据,清空该端点接收缓冲区
			D13_ClearBuffer(endp);									 //清空接收缓冲区
			endpstatus = D13_GetEndpointStatusWOInteruptClear(endp); //读该端点状态寄存器 			  read endpoint status register
			if ((endpstatus & 0x60) == 0)  
				break;												 //如果缓冲为空,无须清空		  if buffer is null,break
			else if(i == 0)
				D13_GetEndpointStatusWInteruptClear(endp);
		}//end of for()
	}
}										//no task is ready to receive data,clear the endpoint buffer


/************************************************************************************************************************
** 函数名称:  USB_WriteISR()									Name:		USB_WriteISR()
** 功能描述:  USB 端点发送中断服务程序							Function:	the transmittion interrupt service of USB endpoint
** 输   入:  CTRL_USB *pUsb:  USB接收与发送控制结构体指针		Input:		CTRL_USB *pUsb: the CTRL_USB structure
			  INT8U		endp:  端点索引号									INT8U	  endp: the index of endpoint
			  INT32U eppsize:  端点最大信息包大小							INT32U eppsize: max size of the endpoint
			  INT8U buffnums:  该端点缓冲区个数								INT8U buffnums: numbers of endpoint buffer
** 输   出:  无												Output:		NULL
*************************************************************************************************************************/
void USB_WriteISR(CTRL_USB *pUsb,INT8U endp,INT32U eppsize,INT8U buffnums)
{
	INT32U len;
	INT8U *pBuff,i,endpstatus,status;
	
	endpstatus = D13_GetEndpointStatusWInteruptClear(endp);	//清空对应端点的中断标志位
	if (pUsb->Grp != 0){
		if (pUsb->Cnt >= pUsb->Max[pUsb->Prio]){													//独占任务全部数据写入缓冲区  the task of transmittion have write all data into the buffer
			endpstatus = D13_GetEndpointStatusWOInteruptClear(endp) & 0x60;
			if (endpstatus == 0){							//缓冲区中的数据全部发送完毕  all the data in buffer is all send
				pUsb->Sem  = 0;								//释放该端点的发送资源		  release the transmittion resource of the endpoint
				pUsb->Max[pUsb->Prio] = 0;					//置正确发送标志为0			  flag trasmittion is correct
				USB_DelPrio(pUsb,pUsb->Prio);				//将该任务从等待队列中删除	  delete the task from waiting table
				OSTimeDlyResume(pUsb->Prio);	    		//使该任务就绪				  resume the task
			}
		}
	}
	else
  		return;
  	
  	if (pUsb->Grp != 0){									//有任务在等待发送数据		  exist task waiting for transmitting data
    	if (pUsb->Sem == 0){								//该发送端点可以使用		  no task use the endpoint
			pUsb->Prio = USB_GetHighPrio(pUsb);
			pUsb->Cnt  = 0;									//发送计数清0				  clear the counter to zero
			pUsb->Sem  = 1; 								//使该任务独占该发送资源	  flag that task use the endpoint
		}
		pBuff = pUsb->pBuff[pUsb->Prio];					//得到发送缓冲区指针		  get the pointer of send buffer

		for (i = 0; i < buffnums; i++){
			status = (D13_GetEndpointStatusWOInteruptClear(endp) & 0x60);
			if (status == 0x60)  break;						//如果缓冲区全部都满,那么不可以再写入//if buffer is full,break
															
			len = pUsb->Max[pUsb->Prio] - pUsb->Cnt;
			if (len > 0){		   							//有空的缓冲区且等待发送字节数大于0  buffer is empty and len above zero
				if (len >= eppsize)			   				//写满缓冲区				  write an entire buffer
					len = D13_WriteEndpoint(endp,(INT8U)eppsize,pBuff + pUsb->Cnt); 
				else						   				//写len个字节				  write len length bytes into buffer
					len = D13_WriteEndpoint(endp,(INT8U)len,pBuff + pUsb->Cnt);  
				pUsb->Cnt = pUsb->Cnt + len;      			//发送计数器更新计数     	  counter is counting 
			}//end of if (len > 0)
		}//end of for()	
	}//end of if (pUsb->Grp != 0)	
}

/*************************************************************************************************************************
** 函数名称: USB_ReadPort()									  Name:		  USB_ReadPort()
** 功能描述: 读端点											  Function:	  read data from endpoint
** 输   入: 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 CTRL_USB structure
			 INT32U     len:  接收字节个数								  INT32U     len: the numbers(Bytes) that will be received
			 INT8U *recbuff:  接收缓冲区								  INT8U *recbuff: the reception buffer
			 INT16U	timeout:  等待超时时间								  INT16U timeout: waiting timeout
** 输	 出: 0:	读成功       > 0 读失败(错误码)				  Output:	  0:  sucessfully	 >0: fail (it is error code)
**************************************************************************************************************************/
INT8U USB_ReadPort(INT8U endp,INT8U buffnums,CTRL_USB *pUsb,INT32U len,INT8U *recbuff,INT16U timeout)
{
	INT8U err;
	
	err = USB_RW_Param(pUsb,len,recbuff);			//填写USB接收或发送控制块参数	filled CTRL_USB struct with parameter
	if (err != USB_NO_ERR)   return err;	
	
	OS_ENTER_CRITICAL();
	if (pUsb->Max[OSPrioCur] != 0){					//如果还没接收完成				if reception is not finished
		OS_EXIT_CRITICAL();
		OSTimeDly(timeout);							//定义超时等待					define timeout
	}	
	OS_EXIT_CRITICAL();
	
	return (USB_RW_Result(endp,buffnums,pUsb,1,timeout));	//返回接收结果			return the result of reception
}

/*************************************************************************************************************************
** 函数名称: USB_WritePort()								   Name:	  USB_WritePort()
** 功能描述: 写端点											   Function:  write data to endpoint
** 输	 入: INT8U endp		  : 端点索引号					   Input:	  INT8U	    endp: the index of endpoint
			 INT32U eppsize   : 该端点的的信息包大小					  INT32U eppsize: max size of the endpoint
			 INT8U buffnums   : 该端点发送缓冲区个数					  INT8U buffnums: numbers of endpoint buffer
			 CTRL_USB *pUsb   : 该端点的接收或发送控制块指针			  CTRL_USB *pUsb: the CTRL_USB structure
			 INT8U  *sendbuff : 发送数据字节指针						  INT8U *recbuff: the transmittion buffer
			 INT32U   len	  : 发送数据字节长度						  INT32U     len: the numbers(Bytes) that will be transmitted
			 INT16U	timeout	  : 等待超时时间							  INT16U timeout: waiting timeout
** 输	 出: 0   发送成功    > 0 发送失败(错误码)			   Output:	  0:  sucessfully	 >0: fail (it is error code)
**************************************************************************************************************************/
INT8U USB_WritePort(INT8U endp,INT32U eppsize,INT8U buffnums,CTRL_USB *pUsb,
					INT8U *sendbuff,INT32U len,INT16U timeout)
{
	INT8U err,i;
	INT32U length;

	err = USB_RW_Param(pUsb,len,sendbuff);				//填写USB接收或发送控制块参数	filled CTRL_USB struct with parameter

⌨️ 快捷键说明

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