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

📄 hid.c

📁 嵌入式LINUX9系统应用开发详解中USB编程实例
💻 C
字号:
/*********************************************************************
 *
 * Copyright:
 *	MOTOROLA, INC. All Rights Reserved.  
 *  You are hereby granted a copyright license to use, modify, and
 *  distribute the SOFTWARE so long as this entire notice is
 *  retained without alteration in any modified and/or redistributed
 *  versions, and that such modified versions are clearly identified
 *  as such. No licenses are granted by implication, estoppel or
 *  otherwise under any patents or trademarks of Motorola, Inc. This 
 *  software is provided on an "AS IS" basis and without warranty.
 *
 *  To the maximum extent permitted by applicable law, MOTOROLA 
 *  DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, INCLUDING 
 *  IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
 *  PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH REGARD TO THE 
 *  SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) AND ANY 
 *  ACCOMPANYING WRITTEN MATERIALS.
 * 
 *  To the maximum extent permitted by applicable law, IN NO EVENT
 *  SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING 
 *  WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS 
 *  INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY
 *  LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.   
 * 
 *  Motorola assumes no responsibility for the maintenance and support
 *  of this software
 ********************************************************************/

/*
 * File:		hid.c
 * Purpose:		Device Driver for the HID Class
 */

#include "mcf5272.h"
#include "init.h"
#include "hid.h"

/********************************************************************/

/* Global Endpoint Status Structures */
extern USB_EP_STATE ep[];

/* Global Remote Wakeup Flag */
extern volatile int fRemoteWakeup;

/********************************************************************/
int main (void)
{	
	MCF5272_IMM *imm = mcf5272_get_immp();
	static int8 x = 0;
	static int8 y = 0;
	int8 x_disp, y_disp;
	uint8 buffer[3];
	int i;

	printf("\n\n");
	printf("Running HID Test\nBuilt on %s %s\n",__DATE__, __TIME__);
	#ifdef DEBUG
		printf("DEBUG prints ON\n");
	#else	
		printf("DEBUG prints OFF\n");
	#endif

	usb_init();
	
	/* Interrupts take over from here */
	mcf5272_wr_sr(0x2000);

    while(1)
	{
		/* Send mouse pointer displacement data */
		if ((x < 25) && (y == 0))
		{
			x++;
			x_disp = 4;
			y_disp = 0;
		}
		else if ((x == 25) && (y < 25))
		{
			y++;
			x_disp = 0;
			y_disp = 4;
		}
		else if ((y == 25) && (x > 0))
		{
			x--;
			x_disp = -4;
			y_disp = 0;
		}
		else /* ((x == 0) && (y > 0)) */
		{
			y--;
			x_disp = 0;
			y_disp = -4;
		}
		
		/* Set button info */
		buffer[0] = 0;
		/* Set X displacement */
		buffer[1] = x_disp;
		/* Set Y displacement */
		buffer[2] = y_disp;
		/* Send the data */
		usb_tx_data(1, buffer, sizeof(buffer));

		i = 100000;
		while (i--) 
		{
			if (fRemoteWakeup)
			{
				/* Reset the Remote Wakeup flag */
				fRemoteWakeup = 0;

				/* Wait a while, then RESUME the bus */
				i = 100000;
				while (i--) {};
				MCF5272_WR_USB_EP0CTL(imm, MCF5272_RD_USB_EP0CTL(imm) 
					| MCF5272_USB_EP0CTL_RESUME);
				break;
			}
		}
	}
	return (0);
}

/********************************************************************/
void
usb_ep_halt(uint32 epnum)
{
	#if (DEBUG)
		printf("Endpoint %d has been HALTed\n", epnum);
	#else
		(void) epnum;
	#endif
}

/********************************************************************/
void
usb_ep_unhalt(uint32 epnum)
{
	#if (DEBUG)
		printf("Endpoint %d has been UNHALTed\n", epnum);
	#else
		(void) epnum;
	#endif
}

/********************************************************************/
void
usb_devcfg_notice(uint8 flags, uint8 config, uint32 altsetting)
{
	(void) flags;
	(void) config;
	(void) altsetting;
}

/********************************************************************/
void
usb_ep_tx_done(uint32 epnum)
{
	/* Was this a Vendor/Class specific transfer? */
	if (epnum == 0)
		usb_vendreq_done(0);
}

/********************************************************************/
void
usb_ep_rx_done(uint32 epnum, uint8 status)
{
	/* Was this a Vendor/Class specific transfer? */
	if (epnum == 0)
	{
		usb_vendreq_done(0);
		return;
	}

	/****************************************************************
	 If the data in the EP buffer is needed, it must be copied
	 now because the calling function will free the buffer.
	*****************************************************************/
	switch (status)
	{
		case MALLOC_ERROR:
			#if (DEBUG)
				printf("Could not allocate EP%d OUT buffer.\n",epnum);
			#endif
			return;
		case OVERFLOW_ERROR:
			#if (DEBUG)
				printf("The OUT buffer for EP%d overflowed!\n",epnum);
			#endif
		case SUCCESS:
			#if (DEBUG)
				printf("Unexpected: received %d bytes on EP%d\n",
					ep[epnum].buffer.position, epnum);
			#endif
		default:
			break;
	}
}

/********************************************************************/
void 
hid_endpoint_isr(uint32 epnum)
{
	/* Interrupt IN service routine */
	MCF5272_IMM *imm = mcf5272_get_immp();
	int event;

	event = MCF5272_RD_USB_EPISR(imm, epnum) 
		  & MCF5272_RD_USB_EPIMR(imm, epnum);

	if (event & ( MCF5272_USB_EPNISR_EOT	
				| MCF5272_USB_EPNISR_EOP
				| MCF5272_USB_EPNISR_FIFO_LVL))
	{
		usb_in_service(epnum,event);
	}
	if (event & MCF5272_USB_EPNISR_HALT)
	{
		MCF5272_WR_USB_EPISR(imm, epnum, MCF5272_USB_EPNISR_HALT);
		#if (DEBUG)
			printf("Endpoint %d has been HALTed\n", epnum);
		#endif
	}
	if (event & MCF5272_USB_EPNISR_UNHALT)
	{
		MCF5272_WR_USB_EPISR(imm, epnum, MCF5272_USB_EPNISR_UNHALT);
		#if (DEBUG)
			printf("Endpoint %d has been UNHALTed\n", epnum);
		#endif
	}
}
/********************************************************************/
void 
usb_vendreq_service(uint8  bmRequestType,
					uint8  bRequest, 
					uint16 wValue,
					uint16 wIndex, 
					uint16 wLength)
{
	static uint8 protocol = 1;	/* Default is Report Protocol */
	static uint8 idle = 0;

	if (! ((bmRequestType & CLASS) && (bmRequestType & RQ_INTERFACE)) )
	{
		printf("Received invalid bmRequestType 0x%02X\n",bmRequestType);
		return;
	}

	switch(bRequest) 
	{
		case GET_REPORT:
			#if (DEBUG)
				printf("usb_vendreq_service() received GET_REPORT request\n");
			#endif
			if (!usb_tx_data(0, hid_get_report_desc(), 
					hid_get_report_desc_size()))
			{
				#if (DEBUG)
					printf("Error in usb_tx_data of REPORT_DESC\n");
				#endif
			}
			break;
		case GET_IDLE: 
			#if (DEBUG)
				printf("usb_vendreq_service() received GET_IDLE request\n");
			#endif
			if (!usb_tx_data(0, &idle, sizeof(idle)))
			{
				#if (DEBUG)
					printf("Error in usb_tx_data during GET_IDLE\n");
				#endif
			}
			break;
		case GET_PROTOCOL:
			#if (DEBUG)
				printf("usb_vendreq_service() received GET_PROTOCOL request\n");
			#endif
			if (!usb_tx_data(0, &protocol, sizeof(protocol)))
			{
				#if (DEBUG)
					printf("Error in usb_tx_data during GET_PROTOCOL\n");
				#endif
			}
			break;
		case SET_REPORT:
			#if (DEBUG)
				printf("usb_vendreq_service() received SET_REPORT request\n");
			#endif
			/* not supported */
			usb_vendreq_done(1);
			break;
		case SET_IDLE:
			#if (DEBUG)
				printf("usb_vendreq_service() received SET_IDLE request\n");
			#endif
			idle = (uint8)(wValue >> 8);
			usb_vendreq_done(0);
			break;
		case SET_PROTOCOL:
			#if (DEBUG)
				printf("usb_vendreq_service() received SET_PROTOCOL request\n");
			#endif
			protocol = (uint8) wValue;
			usb_vendreq_done(0);
			break;
		case GET_DESCRIPTOR:
			switch (wValue >> 8)
			{
				case HID_DESC:
					#if (DEBUG)
						printf("Received GET_DESCRIPTOR(HID_DESC)\n");
					#endif
					if (!usb_tx_data(0,hid_get_hid_desc(),
							hid_get_hid_desc_size()))
					{
						#if (DEBUG)
							printf("Error in usb_tx_data of USB_HID_DESC\n");
						#endif
					}
					break;
				case REPORT_DESC:
					#if (DEBUG)
						printf("Received GET_DESCRIPTOR(REPORT_DESC)\n");
					#endif
					if (!usb_tx_data(0, hid_get_report_desc(), 
							hid_get_report_desc_size()))
					{
						#if (DEBUG)
							printf("Error in usb_tx_data of REPORT_DESC\n");
						#endif
					}
					break;
				case PHYSICAL_DESC:
					#if (DEBUG)
						printf("Received GET_DESCRIPTOR(PHYSICAL_DESC)\n");
					#endif
					/* not supported */
					usb_vendreq_done(1);
					break;
				default:
					#if (DEBUG)
						printf("Received GET_DESCRIPTOR(unknown)\n");
					#endif
					/* not supported */
					usb_vendreq_done(1);
					break;
			}
			break;
		default:
			#if (DEBUG)
				printf("usb_vendreq_service() received unknown request\n");
				printf("  bmRequestType = %#02X\n",bmRequestType);
				printf("       bRequest = %#02X\n",bRequest);
				printf("         wValue = %#04X\n",wValue);
				printf("         wIndex = %#04X\n",wIndex);
				printf("        wLength = %#04X\n",wLength);
			#endif
			/* not supported */
			usb_vendreq_done(1);
			break;
	}
}
/********************************************************************/

⌨️ 快捷键说明

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