usb_hid_host.c

来自「Freescale MCF5445evb 参考测试代码」· C语言 代码 · 共 534 行 · 第 1/2 页

C
534
字号
	/* data for Set Address command */	buf0[0] = 0x00050000 | ((0x7F & device_address) <<8);	buf0[1] = 0x00000000;	    usb_qtd1 = usb_qtd_init(0x8, 0, SETUP_PID, buf0);    usb_qtd2 = usb_qtd_init(0x0, 1, IN_PID, 0);    usb_qtd1->next_qtd = (uint32)usb_qtd2;        	/* Point the QH to the linked list of qTDs */	usb_qh_ep0->next_qtd = (uint32)usb_qtd1;    	/* Wait for transaction to complete */	while (!((MCF_USB_USBSTS&MCF_USB_USBSTS_UI) |			(MCF_USB_USBSTS&MCF_USB_USBSTS_UEI)));	    /* Check for errors */  	if( MCF_USB_USBSTS & MCF_USB_USBSTS_UEI)    {    	printf("ERROR!!!\n");	    temp = *(uint32 *)(MCF_USB_ASYNCLISTADDR + 0x18);        printf("qTD status = 0x%08x\n",temp);	     }     else     {     	printf("Set address command complete!!\n\n");		#ifdef DEBUG_PRINT	        	printf("USBSTS = 0x%08x\n",MCF_USB_USBSTS);    	#endif    }	/* Clear the USB interrupt status bit */	MCF_USB_USBSTS |= (MCF_USB_USBSTS_UI | MCF_USB_USBSTS_UEI);		/* Change the device address in the QH to the new value */	usb_qh_ep0->ep_char |= USB_QH_EP_CHAR_DEV_ADDR(device_address);	    /* Return memory for descriptors to the heap */    free((void *)usb_qtd1->malloc_ptr);    free((void *)usb_qtd2->malloc_ptr);}/********************************************************************/void get_dev_desc(USB_QH * usb_qh_ep0, uint8 * device_descriptor, uint32 device_address){	USB_QTD * usb_qtd1, *usb_qtd2, *usb_qtd3;	uint32 i, temp;	uint32 buf0[MAX_USB_BUFFER_SIZE];		/* data for Get Descriptor command */	buf0[0] = 0x80060001;	buf0[1] = 0x00001200;		    usb_qtd1 = usb_qtd_init(0x8, 0, SETUP_PID, buf0);    usb_qtd2 = usb_qtd_init(0x40, 0, IN_PID, (uint32*) device_descriptor);    usb_qtd3 = usb_qtd_init(0x0, 1, OUT_PID, 0);    usb_qtd1->next_qtd = (uint32)usb_qtd2;    usb_qtd2->next_qtd = (uint32)usb_qtd3;       	/* Point the QH to the linked list of qTDs */	usb_qh_ep0->next_qtd = (uint32)usb_qtd1;        	    MCF_USB_ASYNCLISTADDR = (uint32) usb_qh_ep0;    		/* Wait for transaction to complete */	while (!((MCF_USB_USBSTS&MCF_USB_USBSTS_UI) |			(MCF_USB_USBSTS&MCF_USB_USBSTS_UEI)));	    /* Check for errors */  	if( MCF_USB_USBSTS & MCF_USB_USBSTS_UEI)    {    	printf("ERROR!!!\n");	    temp = *(uint32 *)(MCF_USB_ASYNCLISTADDR + 0x18);        printf("qTD status = 0x%08x\n",temp);	        for(i=0; i<5; i++)        	device_descriptor[i] = 0;     }     else     {     	printf("Device descriptor has been read!!\n\n");		#ifdef DEBUG_PRINT	        	printf("USBSTS = 0x%08x\n",MCF_USB_USBSTS);    	#endif    }	#ifdef DEBUG_PRINT			for( i=0; i<0x12; i++)        	printf("device_descriptor[%02x] = 0x%02x\n",i,device_descriptor[i]);		#endif    	/* Clear the USB interrupt status bit */	MCF_USB_USBSTS |= (MCF_USB_USBSTS_UI | MCF_USB_USBSTS_UEI);		/* Set the max packet size in the QH to the max packet size in the device descriptor */	usb_qh_ep0->ep_char = ((usb_qh_ep0->ep_char & ~USB_QH_EP_CHAR_MAX_PACKET(0xFFF))								| USB_QH_EP_CHAR_MAX_PACKET(device_descriptor[0x7]));	    /* Return memory for descriptors to the heap */    free((void *)usb_qtd1->malloc_ptr);    free((void *)usb_qtd2->malloc_ptr);    free((void *)usb_qtd3->malloc_ptr);       }/********************************************************************/void get_config_desc(USB_QH * usb_qh_ep0, uint8 *cfg_desc, int length){	USB_QTD * usb_qtd1, *usb_qtd2, *usb_qtd3;	uint32 i, temp;	uint32 buf0[MAX_USB_BUFFER_SIZE];		/* data for Get Configuration Descriptor command */	buf0[0] = 0x80060002;	buf0[1] = ((length & 0x00FF) << 8) | ((length & 0xFF00) >> 8);		    usb_qtd1 = usb_qtd_init(0x8, 0, SETUP_PID, buf0);    usb_qtd2 = usb_qtd_init(length, 0, IN_PID, (uint32 *)cfg_desc);    usb_qtd3 = usb_qtd_init(0x0, 1, OUT_PID, 0);    usb_qtd1->next_qtd = (uint32)usb_qtd2;    usb_qtd2->next_qtd = (uint32)usb_qtd3;        	/* Point the QH to the linked list of qTDs */	usb_qh_ep0->next_qtd = (uint32)usb_qtd1;        		/* Wait for transaction to complete */	while (!((MCF_USB_USBSTS&MCF_USB_USBSTS_UI) |			(MCF_USB_USBSTS&MCF_USB_USBSTS_UEI)));	    /* Check for errors */  	if( MCF_USB_USBSTS & MCF_USB_USBSTS_UEI)    {    	printf("ERROR!!!\n");	    temp = *(uint32 *)(MCF_USB_ASYNCLISTADDR + 0x18);        printf("qTD status = 0x%08x\n",temp);	        for(i=0; i<3; i++)        	config_descriptor[i] = 0;     }     else     {		#ifdef DEBUG_PRINT		     	printf("Configuration descriptor has been read!!\n\n");        	printf("USBSTS = 0x%08x\n",MCF_USB_USBSTS);    	#endif    }	/* Clear the USB interrupt status bit */	MCF_USB_USBSTS |= (MCF_USB_USBSTS_UI | MCF_USB_USBSTS_UEI);	    /* Return memory for descriptors to the heap */    free((void *)usb_qtd1->malloc_ptr);    free((void *)usb_qtd2->malloc_ptr);    free((void *)usb_qtd3->malloc_ptr);       }/********************************************************************/void set_configuration(USB_QH * usb_qh_ep0, uint32 config_value, uint32 device_address){	USB_QTD * usb_qtd1, *usb_qtd2;	uint32 temp;	uint32 buf0[MAX_USB_BUFFER_SIZE];	/* data for Set Address command */	buf0[0] = 0x00090000 | ((0x7F & config_value) <<8);	buf0[1] = 0x00000000;	    usb_qtd1 = usb_qtd_init(0x8, 0, SETUP_PID, buf0);    usb_qtd2 = usb_qtd_init(0x0, 1, IN_PID, 0);    usb_qtd1->next_qtd = (uint32)usb_qtd2;        	/* Point the QH to the linked list of qTDs */	usb_qh_ep0->next_qtd = (uint32)usb_qtd1;        /* Enable async schedule *///	MCF_USB_USBCMD |= MCF_USB_USBCMD_ASE;		/* Wait for asynchronous schedule to enable *///	while (!(MCF_USB_USBSTS & MCF_USB_USBSTS_AS));	/* Wait for transaction to complete */	while (!((MCF_USB_USBSTS&MCF_USB_USBSTS_UI) |			(MCF_USB_USBSTS&MCF_USB_USBSTS_UEI)));	    /* Check for errors */  	if( MCF_USB_USBSTS & MCF_USB_USBSTS_UEI)    {    	printf("ERROR!!!\n");	    temp = *(uint32 *)(MCF_USB_ASYNCLISTADDR + 0x18);        printf("qTD status = 0x%08x\n",temp);	     }     else     {     	printf("Set configuration command complete!!\n\n");		#ifdef DEBUG_PRINT	        	printf("USBSTS = 0x%08x\n",MCF_USB_USBSTS);    	#endif    }	/* Clear the USB interrupt status bit */	MCF_USB_USBSTS |= (MCF_USB_USBSTS_UI | MCF_USB_USBSTS_UEI);	    /* Return memory for descriptors to the heap */    free((void *)usb_qtd1->malloc_ptr);    free((void *)usb_qtd2->malloc_ptr);}/********************************************************************/void get_report_desc(USB_QH * usb_qh_ep0, uint8 * report_descriptor, uint32 device_address){	USB_QTD * usb_qtd1, *usb_qtd2, *usb_qtd3;	uint32 i, temp;	uint32 buf0[MAX_USB_BUFFER_SIZE];		/* data for Get Configuration Descriptor command */	buf0[0] = 0x81060022;	buf0[1] = 0x00007400;		    usb_qtd1 = usb_qtd_init(0x8, 0, SETUP_PID, buf0);    usb_qtd2 = usb_qtd_init(0x74, 0, IN_PID, (uint32 *)report_descriptor);    usb_qtd3 = usb_qtd_init(0x0, 1, OUT_PID, 0);    usb_qtd1->next_qtd = (uint32)usb_qtd2;    usb_qtd2->next_qtd = (uint32)usb_qtd3;        	/* Point the QH to the linked list of qTDs */	usb_qh_ep0->next_qtd = (uint32)usb_qtd1;        	    /* Enable async schedule *///	MCF_USB_USBCMD |= MCF_USB_USBCMD_ASE;		/* Wait for asynchronous schedule to enable *///	while (!(MCF_USB_USBSTS & MCF_USB_USBSTS_AS));	/* Wait for transaction to complete */	while (!((MCF_USB_USBSTS&MCF_USB_USBSTS_UI) |			(MCF_USB_USBSTS&MCF_USB_USBSTS_UEI)));	    /* Check for errors */  	if( MCF_USB_USBSTS & MCF_USB_USBSTS_UEI)    {    	printf("ERROR!!!\n");	    temp = *(uint32 *)(MCF_USB_ASYNCLISTADDR + 0x18);        printf("qTD status = 0x%08x\n",temp);	        for(i=0; i<3; i++)        	report_descriptor[i] = 0;    }    else    {		#ifdef DEBUG_PRINT		     	printf("Report descriptor has been read!!\n\n");        	printf("USBSTS = 0x%08x\n",MCF_USB_USBSTS);    	#endif    }	#ifdef DEBUG_PRINT			i=0;		while (!((report_descriptor[i-1] == 0xC0) & (report_descriptor[i-2] == 0xC0)))         {        	printf("report_descriptor[%02x] = 0x%02x\n",i,report_descriptor[i]);	        	i++;        }	#endif    	/* Clear the USB interrupt status bit */	MCF_USB_USBSTS |= (MCF_USB_USBSTS_UI | MCF_USB_USBSTS_UEI);	    /* Return memory for descriptors to the heap */    free((void *)usb_qtd1->malloc_ptr);    free((void *)usb_qtd2->malloc_ptr);    free((void *)usb_qtd3->malloc_ptr);       }

⌨️ 快捷键说明

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