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

📄 d12driver.c

📁 基于ARM7的U盘程序开发代码,可以
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               广州周立功单片机发展有限公司
**                                     研    究    所
**                                        产品一部 
**
**                                 http://www.zlgmcu.com
**
** 文   件   名: D12Driver.c
** 最后修改日期: 2004年11月15日
** 描        述: USB驱动程序软件包: PDIUSBD12 设备端驱动程序 应用层
** 版	     本: V5.0
**********************************************************************************************************/

#include "datatype.h"
#include "console.h"
#include "D12Config.h"
#include "D12CI.h"
#include "D12HAL.h"
#include "Chap_9.h"
#include "Descriptor.h"
#include "D12Driver.h"
#include "Protozlg.h"
#include "BulkOnly.h"

INT8U SIG;
INT8U GenEpBuf[EP1_PACKET_SIZE];		//端点1接收缓冲区	
INT8U EpBuf[EP2_PACKET_SIZE];			//端点2接收缓冲区


extern void Delay(U32 time);


/*********************************************************************
** 函数名称: UsbInit()
** 功能描述: 设置D12与硬件的连接, 初始化D12, 复位D12
** 输   入: 无
** 输   出: 0: 初始化成功    1: 初始化失败  
**********************************************************************/
U8 UsbInit()
{
	bEPPflags.value = 0;					//初始化USB事件标志变量
	//UsbSendDescF = 0;

	if(D12_ReadChipID()!=0x1210 )
		return 1;

	D12_SetDMA(MyD12DmaCfg);
	if(D12_GetDMA()!=MyD12DmaCfg)
		return 1;

	D12_SetMode(MyD12EpCfgOff, D12Pll24M);
	Delay(20000);
	D12_SetMode(MyD12EpCfgOn, D12Pll24M);

 	return 0;								//初始化成功
}



/*******************************************************************
** 函数名称: d12_busreset()
** 功能描述: USB总线复位处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void d12_busreset(void)
{
	//在此添加总线复位处理代码
	//printf("bus reset!\n");
}

/*******************************************************************
** 函数名称: dma_eot()
** 功能描述: DMA结束处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void dma_eot(void)
{
	//在此添加DMA结束处理代码
}

/*******************************************************************
** 函数名称: d12_suspend()
** 功能描述: 总线挂起改变处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void d12_suspend(void)
{
	//在此添加总线挂起改变代码
	//printf("d12 suspend.\n");
}

/*******************************************************************
** 函数名称: ep1_txdone()
** 功能描述: 端点1 IN(发送)处理处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void ep1_txdone(void)
{
	D12_ReadLastTransactionStatus(3);		//清中断标志位
}

/*******************************************************************
** 函数名称: ep1_txdone()
** 功能描述: 端点1 OUT(接收)处理处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void ep1_rxdone(void)
{
	unsigned char len;

	D12_ReadLastTransactionStatus(2);		 //清中断标志位
	len = D12_ReadEndpoint(2,sizeof(GenEpBuf),GenEpBuf);	//读取数据
	if(len != 0)
		bEPPflags.bits.ep1_rxdone = 1;		//标志接收到数据
}

/*******************************************************************
** 函数名称: ep2_txdone()
** 功能描述: 端点2 IN(发送)处理处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void ep2_txdone(void)
{
	D12_ReadLastTransactionStatus(5);		//清中断标志位
	//printf("send over!\n");
	//可添加用户代码(进行检测端点号5IN令牌的操作)
	
//	if(SIG) {
//		SIG = 0;
//		D12_WriteEndpoint(5, sizeof(CSW), (INT8U*)&csw1);
//	}	
	
	if(byte_cnt.word>0)
	{
		if(byte_cnt.word<64)
		{
			D12_WriteEndpoint(5, byte_cnt.byte.low, EpBuf);
			byte_cnt.word=0;
		}
		else
		{
			byte_cnt.word=byte_cnt.word-64;
			D12_WriteEndpoint(5, 64, EpBuf);
		}
	}
	
	if ((D12_ReadEndpointStatus(5) & 0x60) != 0x60)
	{
		if(byte_cnt.word>0)
		{
			if(byte_cnt.word<64)
			{
				D12_WriteEndpoint(5, byte_cnt.byte.low, EpBuf);
				byte_cnt.word=0;
			}
			else
			{
				byte_cnt.word=byte_cnt.word-64;
				D12_WriteEndpoint(5, 64, EpBuf);
			}
		}
	}
	
}

/*******************************************************************
** 函数名称: ep2_rxdone()
** 功能描述: 端点2 OUT(接收)处理处理函数
** 输	 入: 无
** 输	 出: 无
********************************************************************/
void ep2_rxdone(void)
{
	INT8U i,len;
	//CSW  csw_ret;

	D12_ReadLastTransactionStatus(4);			//复位中断寄存器
	len = D12_ReadEndpoint(4, 64, EpBuf);		//读取端点2接收数据
	if (len != 0)
		bEPPflags.bits.ep2_rxdone = 1;			//标志端点2接收到数据

//处理接收到的数据
	if (bEPPflags.bits.ep2_rxdone) {  //端点2收到数据
		DISABLE();
		bEPPflags.bits.ep2_rxdone = 0;
		ENABLE();
	
		//printf("Endpoint2 received data.\n");  
		for(i=0;i<sizeof(CBW);i++)
		{
		 *((unsigned char*)&cbw1+i) = EpBuf[i];
		}

	if( cbw1.dCBWSignature == 0x43425355 )
	{
		//printf("CBW OK!\n");

		csw1.dCSWSignature = 0x53425355;
		csw1.dCSWTag = cbw1.dCBWTag;
		if (((cbw1.bCBWCBLength & 0x1F) == 0x06) ||  //command of 6 bytes
			  ((cbw1.bCBWCBLength & 0x1F) == 0x0A) ||  //command of 10 bytes
			  ((cbw1.bCBWCBLength & 0x1F) == 0x0C))    //command of 12 bytes
		{
				pufi1 = (PUFI)(cbw1.CBWCB);
				exec_UFI();					
		}//if UFI
			
	}//if Signature	 
	
		
	}
}

/***********************************************************************
** 函数名称: void Usb_int(void)
** 功能描述: D12中断服务程序
************************************************************************
void Usb_int(void)  interrupt 0
{
	DISABLE();					        //进入D12中断服务程序时关中断
	fn_usb_isr();	
  ENABLE();     							//退出D12中断服务程序时开中断
}
*/

/************************************************************
** 函数名称: disconnect_USB() 
** 功能描述: 断开USB总线
************************************************************/
void disconnect_USB(void)
{
	// Initialize D12 configuration
	D12_SetMode(D12_NOLAZYCLOCK, D12_SETTOONE | D12_CLOCK_12M);
}	//SoftConnect 断开


/************************************************************
** 函数名称: connect_USB()
** 功能描述: 连接USB总线
************************************************************/
void connect_USB(void)
{
	// Initialize D12 configuration
	D12_SetDMA(0x00);
	D12_SetMode(D12_NOLAZYCLOCK|D12_SOFTCONNECT, D12_SETTOONE | D12_CLOCK_12M);
}	//SoftConnect 连接上,并设置为无LazyClock,模式为非同步模式


/************************************************************
** 函数名称: reconnect_USB()
** 功能描述: 重新连接USB总线
************************************************************/
void reconnect_USB(void)
{
	INT16U clk_cnt;

	disconnect_USB();							   //断开芯片与USB总线连接
    for (clk_cnt = 0;clk_cnt<= 0x9000;clk_cnt++); //延时处理
  connect_USB();								   //连接到USB总线
}


/************************************************************
** 函数名称: Get_D12_FirmwareVer()
** 功能描述: 重新连接USB总线
** 输	 入: 无
** 输	 出: 16bit整数,如0x0500表示D12固件软件版本号为5.00
************************************************************/
INT16U Get_D12_FirmwareVer(void)
{
	return 0x0157;					//D12固件软件版本号为1.57
}

/************************************************************
** 函数名称: D12_ReadChipID()
** 功能描述: 读取芯片ID
** 输   入: 无
** 输   出: 芯片的ID号
************************************************************/
INT16U D12_ReadChipID(void)
{
	INT16U tmp;
	
	D12Cmd = 0xfd;
	tmp = D12Dat;
	tmp = tmp<<8|D12Dat;		
	return tmp;	
}


void D12_SetEndpointStatus(U8 Ep,U8 Stat)
{
	D12Cmd = Ep+0x40;	
	D12Dat = Stat;
}

void D12_SetAddressEnable(U8 Addr, U8 bEnable)
{
	if(bEnable) 
		Addr |= 0x80;
	else
		Addr &= 0x7f;	
	D12Cmd = 0xd0;
	D12Dat = Addr;
}

void D12_SetEndpointEnable(U8 Val)
{
	D12Cmd = 0xd8;
	D12Dat = Val;
}

void D12_SetDMA(U8 Dma)
{
	D12Cmd = 0xfb;
	D12Dat = Dma;
}

void D12_SetMode(U8 Val1, U8 Val2)
{
	D12Cmd = 0xf3;
	D12Dat = Val1;
	D12Dat = Val2;
}

void D12_SendResume()
{
	D12Cmd = 0xf6;
}

void D12_AcknowledgeEndpoint(U8 Ep)
{
	D12Cmd = Ep;
	D12Cmd = 0xf1;
	if(!Ep)
		D12Cmd = 0xf2;		
}


U8 D12_GetDMA()
{
	D12Cmd = 0xfb;
	return D12Dat;
}

U8 D12_ReadEndpoint(U8 Ep, U8 cnt, U8 buf[])
{
	U8 tmp,i=0;
	
	D12SelEp(Ep);
	if(D12Dat&1)
	{
		D12Cmd = 0xf0;
		tmp = D12Dat;
		tmp = D12Dat;
		if(cnt>tmp)
			cnt = tmp;
		tmp = cnt;
		while(i < cnt)
		{
			*(buf+i) = D12Dat;
			i++;
		}
		D12Cmd = 0xf2;
		return tmp;	
	}
	else
		return 0;		
}

void D12_WriteEndpoint(U8 Ep, U8 cnt, U8 buf[])
{
	U8 i=0;
	while(1)
	{
		D12SelEp(Ep);
		if(!(D12Dat&1))
			break;
	}
	D12Cmd = 0xf0;
	D12Dat = 0;
	D12Dat = cnt;
	while(i<cnt)
	{
		D12Dat = *(buf+i);
		i++;
	}
	D12Cmd = 0xfa;
}


U16 D12_ReadInterruptRegister()
{
	D12Cmd = 0xf4;
	return D12Dat;
}

INT8U  D12_SelectEndpoint(INT8U bEndp)
{
	D12Cmd = bEndp;
	return D12Dat;
}	

INT8U D12_ReadLastTransactionStatus(INT8U bEndp)
{
  D12Cmd = bEndp+0x40;
  return D12Dat;	
}

INT8U D12_ReadEndpointStatus(INT8U bEndp)
{
	D12Cmd = bEndp+0x80;
	return D12Dat;
}

⌨️ 快捷键说明

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