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

📄 enumeration_example.c

📁 AT91SAM9261的USB设备驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
// Interrupt Service Routines
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief  Handler for the USB controller interrupt
//!
//!         Defers the call to the USB_Handler function.
//------------------------------------------------------------------------------
void ISR_Driver(void)
{
    USB_Handler(&sUsb);
}

//------------------------------------------------------------------------------
//! \brief  Handler for the VBus state change interrupt
//!
//!         This method calls the USB_Attach function to perform the necessary
//!         operations.
//------------------------------------------------------------------------------
#if !defined(USB_BUS_POWERED)
void ISR_VBus(void)
{
    USB_Attach(&sUsb);

    // Acknowledge the interrupt
    AT91F_PIO_GetInterruptStatus(AT91C_PIO_VBUS);
}
#endif // !defined(USB_BUS_POWERED)

// Callbacks
//------------------------------------------------------------------------------
extern void DRIVER_AsmUARTInterruptHandler(void);
extern void DRIVER_AsmVBUSInterruptHandler(void);

//------------------------------------------------------------------------------
//! \brief  Callback invoked during the initialization of the USB driver
//!
//!         Configures and enables USB controller and VBus monitoring interrupts
//! \param  pUsb    Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Init(const S_usb *pUsb)
{
    // Configure and enable the USB controller interrupt
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          USB_GetDriverID(pUsb),
                          AT91C_AIC_PRIOR_HIGHEST,
                          0, //AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
                          DRIVER_AsmUARTInterruptHandler);

    AT91F_AIC_EnableIt(AT91C_BASE_AIC, USB_GetDriverID(pUsb));

#ifndef USB_BUS_POWERED
    // Configure VBus monitoring
    BRD_ConfigureVBus(USB_GetDriverInterface(pUsb));

    // Configure and enable the Vbus detection interrupt
    AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
                          AT91C_ID_VBUS,
                          AT91C_AIC_PRIOR_LOWEST,
                          0, //AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
                          ISR_VBus);

    AT91F_PIO_InterruptEnable(AT91C_PIO_VBUS, AT91C_VBUS);
    AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_VBUS);
#else
    // Power up the USB controller
    USB_Attach(pUsb);
#endif
}
//------------------------------------------------------------------------------
//! \brief  Callback invoked when the device becomes suspended
//!
//!         Disables LEDs (if they are used) and then puts the device into
//!         low-power mode. When traces are used, the device does not enter
//!         low-power mode to avoid losing some outputs.
//! \param  pUsb    Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Suspend(const S_usb *pUsb)
{


#if defined(NOTRACES)
    DEV_Suspend();
#endif
}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when the device leaves the suspended state
//!
//!         The device is first returned to a normal operating mode and LEDs are
//!         re-enabled. When traces are used, the device does not enter
//!         low-power mode to avoid losing some outputs.
//! \param  pUsb    Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_Resume(const S_usb *pUsb)
{
#if defined(NOTRACES)
    DEV_Resume();
#endif

}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when a new SETUP request is received
//!
//!         The new request if forwarded to the standard request handler,
//!         which performs the enumeration of the device.
//! \param  pUsb   Pointer to a S_usb instance
//------------------------------------------------------------------------------
static void CBK_NewRequest(const S_usb *pUsb)
{
    MY_REQUEST_HANDLE(&sClass);//STD_RequestHandler(&sClass);
}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when a Reset request is received
//!
//! \param  pUsb   Pointer to a S_usb instance
//------------------------------------------------------------------------------
//static void CBK_Reset(const S_usb *pUsb)
//{
    // Put your reset handler here
//}

//------------------------------------------------------------------------------
//! \brief  Callback invoked when a SOF is received
//!
//! \param  pUsb   Pointer to a S_usb instance
//------------------------------------------------------------------------------
#define DST_ADDR 0x0
#define SRC_ADDR 0x20001000
void CopyEVT(void)
{

//*(int*)0x18=0xe51fff20;
	int i;
	for(i=0;i<9;i++)
	{
		*(int *)(DST_ADDR+i*4) = *(int *)(SRC_ADDR+i*4);
	}
}
char temp[PACKET_LENGTH];
unsigned int g_menuzzz = 0;
char Store[1000000];

//------------------------------------------------------------------------------
//          Main
//------------------------------------------------------------------------------
#define COMMAND_LENGTH 8
char command[COMMAND_LENGTH];
int GetCommand(char *p)
{
	int i=0;
	while(1)
	{
		i=ReadUSB(temp,PACKET_LENGTH);
		if(i!=0)
		{
			memcpy(p,temp+8,COMMAND_LENGTH);
			return 1;
		}
	}
	
}
int GetData(char *p,int len)
{
	int m=0,i=0,ReadOne;
	while(m<len)
	{		
		i=ReadUSB(temp,PACKET_LENGTH);
		if(i>0)
		{
			if(len-m<PACKET_LENGTH-8)
				ReadOne=len-m;
			else ReadOne=PACKET_LENGTH-8;
			memcpy(p+m,temp+8,ReadOne);
			m+=ReadOne;
		}
	}
	return 1;
}
int SendData(char* p,int len)
{
	int m=0,WriteOne,CurrentPacketNum=0;
	char Head[8];
	while(m<len)
	{
		if(len-m<=PACKET_LENGTH-8)//the last packet
		{
			WriteOne=len-m;
			CurrentPacketNum=-1;
		}
		else //not the last packet
		{
			WriteOne=PACKET_LENGTH-8;
			CurrentPacketNum++;
		}
		//Head[0]=CurrentPacketNum&0x000000ff;
		//Head[1]=CurrentPacketNum&0x0000ff00;
		//Head[2]=CurrentPacketNum&0x00ff0000;
		//Head[3]=CurrentPacketNum&0xff000000;
		memcpy(Head,&CurrentPacketNum,4);
		//Head[4]=WriteOne&0x000000ff;
		//Head[5]=WriteOne&0x0000ff00;
		//Head[6]=WriteOne&0x00ff0000;
		//Head[7]=WriteOne&0xff000000;
		memcpy(Head+4,&WriteOne,4);
		memcpy(temp,Head,8);
		memcpy(temp+8,p+m,WriteOne);
		while(!WriteUSB(temp,PACKET_LENGTH));
		m+=WriteOne;
	}
}
int main()
{
int i,j;
char*p=temp;

//    TRACE_INIT();
//    TRACE_INFO("\n\rMain Enumeration\n\r");
for(i=0;i<PACKET_LENGTH;i++)
temp[i]=0;
CopyEVT();
DEV_Init();
iniBuf();
    // Initialize the USB driver
    USB_Init(&sUsb);

//    TRACE_INFO("Connecting ... ");

    // Wait for the device to be powered before connecting it
    while (!ISSET(USB_GetState(&sUsb), USB_STATE_POWERED));

    USB_Connect(&sUsb);
//    UDP_Disconnect(&sUsb);
//   	USB_Attach(&sUsb);
   	
    while (!ISSET(USB_GetState(&sUsb), USB_STATE_CONFIGURED));

    // Main loop
    while (1) 
    {

		//i=ReadUSB(p,PACKET_LENGTH);
		//p+=4096;
		//if(0)
		//{
			//for(j=10;j<PACKET_LENGTH;j++)
			//temp[j]++;
        	//WriteUSB(temp,PACKET_LENGTH);
        //}
        GetCommand(command);
        if(command[0]==2&&command[2]==2)
        {
        	int m=(command[7]<<24)|(command[6]<<16)|
        		(command[5]<<8)|command[4];
        	GetData(Store,m);
        }
        else if(command[0]==2&&command[2]==0)
        {
        	int m=(command[7]<<24)|(command[6]<<16)|
        		(command[5]<<8)|command[4];
        	SendData(Store,m);
        }
    }
}

⌨️ 快捷键说明

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