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

📄 m5373evb_usb_device_mouse_demo.c

📁 Freescale ColdFire MCF537x 家族的参考代码
💻 C
字号:
/* * File:		main.c * Purpose:		Main process * */#include "common.h"#include "usb.h"#include "m5373evb.h"void latch_init(void);void poll_for_vbus(void);uint32 check_mif(void);int hid_device_enum(uint8*,	uint8*, uint8* );uint32 ep_list_addr;#define USB_MODULE		USB_OTG/* X, Y displacement info */#define MAX_DISP	300#define DISPLACE	2/* Defines for different setup packets */#define GET_DEV_DESCRIPTOR 0x80060001#define GET_CONFIG_DESCRIPTOR 0x80060002#define SET_CONFIGURATION 0x00090100#define SET_IDLE 0x210A0000#define GET_REPORT_DESCRIPTOR 0x81060022/********************************************************************/void main (void){	uint32 i;	USB_DTD *usb_dtd1, *usb_dtd2, *usb_dtd3, *usb_dtd4;	uint32 mouse_desc[4];	static int x = 0;	static int y = 0;	int8 x_disp, y_disp;		/* declarations for HID device descriptors */	uint8 device_descriptor[MAX_USB_DESC_SIZE];	uint8 config_descriptor[MAX_USB_DESC_SIZE];	uint8 report_descriptor[MAX_USB_DESC_SIZE];		/* Initialize device descriptor values */	device_descriptor[0] = 0x12;	device_descriptor[1] = 0x01;	device_descriptor[2] = 0x10;	device_descriptor[3] = 0x01;	device_descriptor[4] = 0x00;	device_descriptor[5] = 0x00;	device_descriptor[6] = 0x00;	device_descriptor[7] = 0x40;	device_descriptor[8] = 0x2A;	device_descriptor[9] = 0x06;	device_descriptor[10] = 0x01;	device_descriptor[11] = 0x00;	device_descriptor[12] = 0x00;	device_descriptor[13] = 0x00;	device_descriptor[14] = 0x00;	device_descriptor[15] = 0x00;	device_descriptor[16] = 0x00;	device_descriptor[17] = 0x01;	/* Initialize configuration descriptor values */	/* For HID class the configuration descriptor data also includes	 * the interface and HID descriptor values.	 */	config_descriptor[0] = 0x09;	config_descriptor[1] = 0x02;	config_descriptor[2] = 0x22;	config_descriptor[3] = 0x00;	config_descriptor[4] = 0x01;	config_descriptor[5] = 0x01;	config_descriptor[6] = 0x00;	config_descriptor[7] = 0xA0;	config_descriptor[8] = 0x32;	config_descriptor[9] = 0x09;	config_descriptor[10] = 0x04;	config_descriptor[11] = 0x00;	config_descriptor[12] = 0x00;	config_descriptor[13] = 0x01;	config_descriptor[14] = 0x03;	config_descriptor[15] = 0x01;	config_descriptor[16] = 0x02;	config_descriptor[17] = 0x00;	config_descriptor[18] = 0x09;	config_descriptor[19] = 0x21;	config_descriptor[20] = 0x10;	config_descriptor[21] = 0x01;	config_descriptor[22] = 0x00;	config_descriptor[23] = 0x01;	config_descriptor[24] = 0x22;	config_descriptor[25] = 0x34;	config_descriptor[26] = 0x00;	config_descriptor[27] = 0x07;	config_descriptor[28] = 0x05;	config_descriptor[29] = 0x81;	config_descriptor[30] = 0x03;	config_descriptor[31] = 0x04;	config_descriptor[32] = 0x00;	config_descriptor[33] = 0x0A;			/* Initialize report descriptor values */	report_descriptor[0] = 0x05;	report_descriptor[1] = 0x01;	report_descriptor[2] = 0x09;	report_descriptor[3] = 0x02;	report_descriptor[4] = 0xA1;	report_descriptor[5] = 0x01;	report_descriptor[6] = 0x09;	report_descriptor[7] = 0x01;	report_descriptor[8] = 0xA1;	report_descriptor[9] = 0x00;	report_descriptor[10] = 0x05;	report_descriptor[11] = 0x09;	report_descriptor[12] = 0x19;	report_descriptor[13] = 0x01;	report_descriptor[14] = 0x29;	report_descriptor[15] = 0x05;	report_descriptor[16] = 0x15;	report_descriptor[17] = 0x00;	report_descriptor[18] = 0x25;	report_descriptor[19] = 0x01;	report_descriptor[20] = 0x95;	report_descriptor[21] = 0x05;	report_descriptor[22] = 0x75;	report_descriptor[23] = 0x01;	report_descriptor[24] = 0x81;	report_descriptor[25] = 0x02;	report_descriptor[26] = 0x95;	report_descriptor[27] = 0x01;	report_descriptor[28] = 0x75;	report_descriptor[29] = 0x03;	report_descriptor[30] = 0x81;	report_descriptor[31] = 0x01;	report_descriptor[32] = 0x05;	report_descriptor[33] = 0x01;	report_descriptor[34] = 0x09;	report_descriptor[35] = 0x30;	report_descriptor[36] = 0x09;	report_descriptor[37] = 0x31;	report_descriptor[38] = 0x09;	report_descriptor[39] = 0x38;	report_descriptor[40] = 0x15;	report_descriptor[41] = 0x81;	report_descriptor[42] = 0x25;	report_descriptor[43] = 0x7F;	report_descriptor[44] = 0x75;	report_descriptor[45] = 0x08;	report_descriptor[46] = 0x95;	report_descriptor[47] = 0x03;	report_descriptor[48] = 0x81;	report_descriptor[49] = 0x06;	report_descriptor[50] = 0xC0;	report_descriptor[51] = 0xC0;	    /* Global interrupt enable */	mcf5xxx_irq_enable();	printf("MCF532x Initialization Complete!\n");	printf("Starting USB device mode test.\n\n");			/* Initialize the latch on the M5329EVB */	latch_init();		printf("\nWaiting for cable attach.\n\n");	/* Poll latch on EVB waiting for VBUS detection */	poll_for_vbus();		printf("\nCable attach detected.\n\n");									/* Perform basic device init including setting up EP0 OUT */	ep_list_addr = usb_device_init();	if (!(hid_device_enum(device_descriptor, config_descriptor, report_descriptor )))    	printf("\nERROR during USB device enumeration.\n");    		/* Enable EP1 */	  	MCF_USB0_EPCR1 = ( 0  						| MCF_USB_EPCR_TXE  						| MCF_USB_EPCR_TXT_INT  						| MCF_USB_EPCR_RXT_BULK );		/* Initialize IN endpoint queue head for EP1 */	usb_ep_qh_init(ep_list_addr, EP_QH1_IN, 0, 0x8, 0, 0xDEAD0001);		/* Initialize dTDs for sending mouse data. We will calculate four	 * mouse positions at a time, so there are four corresponding dTDs.	 */	usb_dtd1 = usb_dtd_init(0x4, 0, 0, &mouse_desc[0]);	usb_dtd2 = usb_dtd_init(0x4, 0, 0, &mouse_desc[1]);	usb_dtd3 = usb_dtd_init(0x4, 0, 0, &mouse_desc[2]);	usb_dtd4 = usb_dtd_init(0x4, 1, 0, &mouse_desc[3]);		/* Point dTDs to each other to create a linked list */	usb_dtd1->next_dtd = (uint32) usb_dtd2;	usb_dtd2->next_dtd = (uint32) usb_dtd3;	usb_dtd3->next_dtd = (uint32) usb_dtd4;	while(1)	{		/* Calculate the next 4 mouse displacement values to fill the 4 qTDs */		for ( i=0; i<4; i++)		{			/* Send mouse pointer displacement data */			if ((x < MAX_DISP) && (y == 0))			{				x++;				x_disp = DISPLACE;				y_disp = 0;			}			else if ((x == MAX_DISP) && (y < MAX_DISP))			{				y++;				x_disp = 0;				y_disp = DISPLACE;			}			else if ((y == MAX_DISP) && (x > 0))			{				x--;				x_disp = -DISPLACE;				y_disp = 0;			}			else /* ((x == 0) && (y > 0)) */			{				y--;				x_disp = 0;				y_disp = -DISPLACE;			}						mouse_desc[i] = (0								| ((x_disp&0xFF)<<16)								| ((y_disp&0xFF)<<8) );		}		/* Send the data */		/* re-Initialize dTD tokens*/		usb_dtd1->dtd_token |= USB_DTD_TOKEN_TOTAL_BYTES(4)								| USB_DTD_TOKEN_STAT_ACTIVE;		usb_dtd2->dtd_token |= USB_DTD_TOKEN_TOTAL_BYTES(4)								| USB_DTD_TOKEN_STAT_ACTIVE;		usb_dtd3->dtd_token |= USB_DTD_TOKEN_TOTAL_BYTES(4)								| USB_DTD_TOKEN_STAT_ACTIVE;		usb_dtd4->dtd_token |= USB_DTD_TOKEN_TOTAL_BYTES(4)								| USB_DTD_TOKEN_STAT_ACTIVE;										/* re-initialize dTD buffer pointers */		usb_dtd1->dtd_buf0 = (uint32)&mouse_desc[0];		usb_dtd2->dtd_buf0 = (uint32)&mouse_desc[1];		usb_dtd3->dtd_buf0 = (uint32)&mouse_desc[2];		usb_dtd4->dtd_buf0 = (uint32)&mouse_desc[3];				/* Point QH to new dTDs */		((USB_EP_QH*)(ep_list_addr + EP_QH1_IN))->next_dtd = (uint32)usb_dtd1;			    /* Prime Tx buffer for EP1 */	    MCF_USB0_EPPRIME |= MCF_USB_EPPRIME_PETB(2);				/* Wait for IN to complete *///    	while ((*(uint32 *)(ep_list_addr+0xCC)) & 0x80);//		while (!(MCF_USB0_EPCOMPLETE & 0x00020000));	    while (!(MCF_USB0_USBSTS & MCF_USB_USBSTS_UI ));		/* Clear interrupt */		MCF_USB0_USBSTS |= MCF_USB_USBSTS_UI; //		MCF_USB0_EPCOMPLETE |= MCF_USB_EPCOMPLETE_ETCE(2);   		}}/********************************************************************/voidlatch_init (void){	/* Initialization for the latch on the fire engine (U6) */		/* Initialize TIN3 as a GPIO output to enable the write	   half of the latch */	MCF_GPIO_PAR_TIMER = 0x00;	MCF_GPIO_PDDR_TIMER = 0x08;	MCF_GPIO_PCLRR_TIMER = 0x0;	 	/* Initialize latch to drive signals to inactive states */	*((uint16 *)(LATCH_ADDRESS)) = 0x00FE;		}/********************************************************************/voidpoll_for_vbus (void){	while (!(MCF_EPORT_EPPDR & MCF_EPORT_EPPDR_EPPD6));	}/********************************************************************/uint32check_mif(void){	int i;		for (i = 0; i < 10000; i++)	{		if (MCF_I2C_I2SR & 0x02)	/* check MIF */			return 1;	}	return 0;}/********************************************************************/int hid_device_enum(uint8* device_descriptor, uint8* config_descriptor, 				uint8* report_descriptor ){	uint32 done; 	uint32 * setup1, * setup2;	uint32 device_addr, config_len;	printf("\nWaiting for reset.\n");  	while (!( MCF_USB0_USBSTS & MCF_USB_USBSTS_URI));  			  	  	printf("\nReset detected!!!\n");  		/* Handle USB bus reset */  	usb_bus_reset();  			/* waiting for USB connected status indication */	get_port_speed(USB_MODULE);		done = 0;		/* Allocate space to store the setup packet information. */	setup1 = (uint32*)malloc(4);	setup2 = (uint32*)malloc(4);		/* The first setup packet should be a request for the first 8 bytes of	 * the descriptor. We handle this one separately since the full device	 * descriptor will be returned for all other GET DESCRIPTOR requests.	 */	 	/* Get SETUP packet */   	get_setup_packet(ep_list_addr, setup1, setup2);	/* Verify that SETUP packet is a GET DESCRIPTOR */	if (*(uint32 *)setup1 != 0x80060001)	{	    printf("\nERR!!! Expected GET DESCRIPTOR command not received!\n");	    	    return 0;    }        usb_device_send_control_packet(ep_list_addr, 0, device_descriptor, 0x8);	 			while (!done)	{		/* Get SETUP packet */   		get_setup_packet(ep_list_addr, setup1, setup2);				switch (*(uint32 *)setup1)		{			case GET_DEV_DESCRIPTOR:			    usb_device_send_control_packet(ep_list_addr, 0, device_descriptor, 0x12);			    break;			    			case GET_CONFIG_DESCRIPTOR:				/* Determine the length of the config descriptor the host is requesting */				config_len = ((*(uint32 *)setup2 & 0xFF00)>>8);				/* If requested length exceeds the actual, use the actual length of descriptor */				if (config_len > 0x22)						config_len = 0x22;		   				   		usb_device_send_control_packet(ep_list_addr, 0, config_descriptor, config_len);				break;							case SET_CONFIGURATION:				usb_device_send_zero_len_packet(ep_list_addr, 0);						/* For a USB mouse there is only one configuration, so this function	 			 * doesn't do anything with the configuration values sent by the host.	 			 */	 			break;	 						case SET_IDLE:				usb_device_send_zero_len_packet(ep_list_addr, 0);				break;							case GET_REPORT_DESCRIPTOR:    			usb_device_send_control_packet(ep_list_addr, 0, report_descriptor, 0x34);	 				 			/* The report descriptor should always be the last thing sent, so 	 			 * at this point we are done with the enumeration. 	 			 */	 			done = 1; 	 			break;	 				 		default:	 			/* Test to see if command is a SET ADDRESS */				if ((*(uint32 *)setup1 & 0xFFFF0000) == 0x00050000)				{					device_addr = ((*(uint32 *)setup1 & 0xFF00)<<17);					usb_device_send_zero_len_packet(ep_list_addr, 0);						/* Set the new device address */					MCF_USB_DEVICEADDR(USB_MODULE) = device_addr;					break;				}				else					printf("ERR!!! Unsupported command.\n\n");				break;	 				 				 	} /* end switch */		 		} /* end while */				return 1;}/********************************************************************/

⌨️ 快捷键说明

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