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

📄 d13driver.c

📁 基于菲利普USB接口芯片ISP1161A1从口的USB设备开发
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzhou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			D13Driver.c
** Last modified Date:	2004-12-10
** Last Version:		V1.0
** Descriptions:		ISP1181B (D13) 设备端驱动程序 应用层
**						Soft Packet of USB Driver: the Driver of Device of ISP1181B(D13)  
												   Application Layer
**------------------------------------------------------------------------------------------------------
** Created by:			Ming Yuan Zheng	
** Created date:		2004-12-10
** Version:				V1.0
** Descriptions:		The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:			
** Modified date:		
** Version:				
** Descriptions:		
**
**------------------------------------------------------------------------------------------------------
** Modified by: 
** Modified date:
** Version:	
** Descriptions: 
**
********************************************************************************************************/

#include "config.h"

#include "D13Config.h"
#include "D13CI.h"
#include "D13HAL.h"
#include "Chap_9.h"
#include "D13Driver.h"

extern EPPFLAGS bEPPflags;

/* Setup包处理任务堆栈  */
/* Stack of the task that deal with control transfer */
OS_STK  TaskSetupStk[128];

/* Setup包处理任务事件指针 */
/* the pointer of Event that deal SETUP packet */
OS_EVENT *pSetup_Event;

/* ISP1181B 各端口对应的 USB 接收或发送控制块 */
/* USB receive or transmit control block of ISP1181B each endpoint */
CTRL_USB UsbSend_EP01;				//端点 1 发送控制块
CTRL_USB UsbRec_EP02;				//端点 2 接收控制块

CTRL_USB UsbSend_EP03;				//端点 3 发送控制块
CTRL_USB UsbRec_EP04;				//端点 4 接收控制块

CTRL_USB UsbSend_EP05;				//端点 5 发送控制块
CTRL_USB UsbRec_EP06;				//端点 6 接收控制块


/*******************************************************************************************************************************
** 函数名称: INT8U WritePort1()									Name:		 INT8U WritePort1()	
** 功能描述: 用端口 1 发送len个字节								Function:	 Send len Bytes via Port1
** 输   入: 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: 发送成功	 > 0: 发送失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort1(INT32U len,INT8U *sendbuff,INT16U timeout)
{
	return (USB_WritePort(EPINDEX4EP01,16,1,&UsbSend_EP01,sendbuff,len,timeout));
}	//往端口1发送数据

/*******************************************************************************************************************************
** 函数名称: 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: 发送成功	 > 0: 发送失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort2(INT32U len,INT8U *sendbuff,INT16U timeout)
{
	return (USB_WritePort(EPINDEX4EP03,64,2,&UsbSend_EP03,sendbuff,len,timeout));
}	//往端点3发送数据

/*******************************************************************************************************************************
** 函数名称: INT8U WritePort3()									Name:		 INT8U WritePort3()	
** 功能描述: 用端口 3 发送len个字节								Function:	 Send len Bytes via Port3
** 输   入: 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: 发送成功	 > 0: 发送失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort3(INT32U len,INT8U *sendbuff,INT16U timeout)
{
	return (USB_WritePort(EPINDEX4EP05,64,2,&UsbSend_EP05,sendbuff,len,timeout));
}	//往端点5发送数据


/*******************************************************************************************************************************
** 函数名称: INT8U ReadPort1()									Name:		 INT8U ReadPort1()	
** 功能描述: 从端口 1 接收len个字节								Function:	 receive len Bytes from Port1
** 输   入: INT32U  len: 要接收的字节数						Input:		 INT32U  len:   	numbers will be receive
						 (取值范围为0x00000001 ~ 0xFFFFFFFF)			    			    	(range: 0x00000001 ~ 0xFFFFFFFF)	
			 INT8U   recbuff:    接收缓冲区指针								 INT8U   sendbuff:  receive buffer
			 INT16U  timeout:    超时等待时间, 必须大于等于0				 INT16U  timeout:   timeout	of receiving		 
** 输   出: 0: 接收成功	 > 0: 接收失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U ReadPort1(INT32U len,INT8U *recbuff,INT16U timeout)
{
	return (USB_ReadPort(EPINDEX4EP02,1,&UsbRec_EP02,len,recbuff,timeout));
}	//从端点2接收数据

/*******************************************************************************************************************************
** 函数名称: INT8U ReadPort2()									Name:		 INT8U ReadPort2()	
** 功能描述: 从端口 2 接收len个字节								Function:	 receive len Bytes from Port2
** 输   入: INT32U  len: 要接收的字节数						Input:		 INT32U  len:   	numbers will be receive
						 (取值范围为0x00000001 ~ 0xFFFFFFFF)			    			    	(range: 0x00000001 ~ 0xFFFFFFFF)	
			 INT8U   recbuff:    接收缓冲区指针								 INT8U   sendbuff:  receive buffer
			 INT16U  timeout:    超时等待时间, 必须大于等于0				 INT16U  timeout:   timeout	of receiving		 
** 输   出: 0: 接收成功	 > 0: 接收失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U ReadPort2(INT32U len,INT8U *recbuff,INT16U timeout)
{
	return (USB_ReadPort(EPINDEX4EP04,2,&UsbRec_EP04,len,recbuff,timeout));
}	//从端点4接收数据

/*******************************************************************************************************************************
** 函数名称: INT8U ReadPort3()									Name:		 INT8U ReadPort3()	
** 功能描述: 从端口 3 接收len个字节								Function:	 receive len Bytes from Port3
** 输   入: INT32U  len: 要接收的字节数						Input:		 INT32U  len:   	numbers will be receive
						 (取值范围为0x00000001 ~ 0xFFFFFFFF)			    			    	(range: 0x00000001 ~ 0xFFFFFFFF)	
			 INT8U   recbuff:    接收缓冲区指针								 INT8U   sendbuff:  receive buffer
			 INT16U  timeout:    超时等待时间, 必须大于等于0				 INT16U  timeout:   timeout	of receiving		 
** 输   出: 0: 接收成功	 > 0: 接收失败(错误码)				Output:		 0:	sucessfully		>0 fail (error code)
*******************************************************************************************************************************/
INT8U ReadPort3(INT32U len,INT8U *recbuff,INT16U timeout)
{
	return (USB_ReadPort(EPINDEX4EP06,2,&UsbRec_EP06,len,recbuff,timeout));
}	//从端点5接收数据


/**********************************************************************************************************************
** 函数名称: Init_D13()								Name:	   Init_D13()
** 功能描述: 设置 D13(ISP1181B) 与MCU的连接接口,	Function:  Configure the interface connected to MCU,reset D13,
			 复位D13,连接上USB总线							   connected to USB bus
** 输   入: 无										Input:	   NULL
** 输   出: 0: 初始化成功    	1: 	初始化失败  	Output:	   0:  Initialize sucessfully	1: Initialize fail
** 注	 意: 请在调用该函数以后再打开D13中断		Note:	   Enable the interrupt of D13 after the function be called
***********************************************************************************************************************/
INT8U Init_D13(void)
{
	INT32U i;
	
	OS_ENTER_CRITICAL();
	
	Init_D13Port();								  		 //初始化MCU与ISP1181B 连接的接口 	 initialize MCU and ISP1181B's Interface 
	Rst_D13();									  		 //复位D13	 					 	 reset D13 chip
	reconnect_USB();        					   		 //重新连接USB总线				  	 reconnect USB bus
	
	i = D13_ReadChipID(); 								 //读芯片ID号  read ChipID
	if (i!= ISP1181B_CHIPID && i != ISP1181A_CHIPID &&
	    i!= ISP1161A_CHIPID)
	{	
		OS_EXIT_CRITICAL(); 
		return 1;								  		 //读芯片ID号不正确,返回失败		 ChipID is error,return 1
	}
	
	bEPPflags.value = 0;						  		 //USB事件标志置0					 USB Event Flag clear to zero
	
  	UsbRec_EP02.Sem = 0;						   		 //初始化 USB 接收或发送控制块
 	UsbRec_EP04.Sem = 0;						 		 //Initialize CTRL_USB structure 
 	UsbRec_EP06.Sem = 0;
 	UsbSend_EP01.Sem = 0;
 	UsbSend_EP03.Sem = 0;
 	UsbSend_EP05.Sem = 0;
 	UsbRec_EP02.Grp  = 0;
	UsbRec_EP04.Grp  = 0;
	UsbRec_EP06.Grp  = 0;
	UsbSend_EP01.Grp = 0;
	UsbSend_EP03.Grp = 0;
	UsbSend_EP05.Grp = 0;	
	
    for (i = 0; i < OS_RDY_TBL_SIZE; i++){
    	UsbRec_EP02.Tbl[i]  = 0;				 		  //清空各端口接收与发送等待任务列表
    	UsbRec_EP04.Tbl[i]  = 0;				  		  //clear the waiting task table of each port
    	UsbRec_EP06.Tbl[i]  = 0;
		UsbSend_EP01.Tbl[i] = 0;
		UsbSend_EP03.Tbl[i] = 0;
		UsbSend_EP05.Tbl[i] = 0;
    }
	
 	pSetup_Event = OSSemCreate(0);				  		  //创建Setup包处理任务信号量
 												  		  //create the semaphore of the task of Handling SETUP packet 		  
 	if (pSetup_Event == NULL)					
 	{
	 	OS_EXIT_CRITICAL(); 
 	 	return 1;								   		  //return 1 when create the semaphore fail
	}	
	
	OS_EXIT_CRITICAL();
 	return 0;							  		   		  //初始化成功	initialize sucessfully
}

/******************************************************************************************************************
** 函数名称: Usb_Exception()					    Name:		Usb_Exception()
** 功能描述: ISP1181B(D13) 中断服务程序			    Function:	the interrupt service routine of ISP1181B(D13)
*******************************************************************************************************************/
void Usb_Exception(void)
{
	INT32U i_st;
			
	OS_ENTER_CRITICAL();						    //关中断							disable all interrupt
		
	D13_LockDevice(0xAA37);
	
	i_st = D13_ReadInterruptRegister();				//读取D13中断寄存器值				read the interrupt register of ISP1181B
	if(i_st != 0){
		if(i_st & D13REG_INTSRC_BUSRESET){
			bus_reset();							//总线复位处理						deal with the bus reset
			bEPPflags.bits.bus_reset = 1;

⌨️ 快捷键说明

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