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

📄 mpc5200usbtool.c

📁 MPC5200 BSP 支持ATA,USB, I2C,扩展网口
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Include files */#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ioLib.h"#include "ctype.h"#include "usb/usbPlatform.h"		    #include "usb/tools/cmdParser.h"	    #include "usb/usbPciLib.h"		    #include "usb/usbdLib.h"		    #include "config.h"#ifdef INCLUDE_USB_MPC5200		#include "usbHcdMpc5200Lib.h"#endif#include "usb/usbHid.h" 		    #ifdef INCLUDE_USB_KEYBOARD    #include "drv/usb/usbKeyboardLib.h" 	#endif#ifdef INCLUDE_USB_MOUSE    #include "drv/usb/usbMouseLib.h"		#endif#ifdef INCLUDE_USB_MS 		#include "iosLib.h"    #include "dosFsLib.h"    #include "usb/usbdLib.h"    #include "usb/usbQueueLib.h"    #include "dcacheCbio.h"		#include "dpartCbio.h"		#include "syslib.h"		#include "usrFdiskPartLib.h"		#include "cbiolib.h"#endif#ifdef INCLUDE_USB_MS_BULKONLY		#ifdef INCLUDE_USB_MS_BULKONLY_SCSI    		#include "usbMpc5200DevLib.h"		#endif		#ifdef INCLUDE_USB_MS_BULKONLY_SFF8070				#include "usbSFFBulkDevLib.h"		#endif#endif#ifdef INCLUDE_USB_MS_CBI		#include "usbCbiUfiDevLib.h"#endif/* defines */#define PGM_NAME	"usbTool"#define PGM_DESCR	"USB Application Program"#define PGM_VERSION	"01f"#define PGM_COPYRIGHT	"Copyright (c) 2007, Zhao Housework.\n"/* Prompt definition */#define PROMPT		"usb>"		#define INDENT		2		/* misc defines */#define INT_PIPE_SRVC_INTERVAL	    20	#define CTRL_Z			    26	#define MAX_MFG_STR_LEN 	    30#define MAX_PROD_STR_LEN	    30#define TX_BFR_SIZE		    0x1000#define AUDIO_BFR_SIZE		    0x8000#define GENERIC_USB_BFR		256/* locals */#ifdef	INCLUDE_USBLOCAL BOOL initialized = FALSE;LOCAL USBD_CLIENT_HANDLE usbdClientHandle = NULL;LOCAL GENERIC_HANDLE uhciAttachToken = NULL;LOCAL GENERIC_HANDLE ohciAttachToken = NULL;LOCAL GENERIC_HANDLE Mpc5200AttachToken = NULL;LOCAL BOOL enterPressed;#endif#ifdef INCLUDE_USBLOCAL UINT16 cmdUsbInit    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout			    )    {    UINT16 usbdVersion;    char usbdMfg [USBD_NAME_LEN+1];    UINT16 s;       if (initialized)	{	fprintf (fout, "Already initialized.\n");	return RET_CONTINUE;	}      s = usbdInitialize ();    fprintf (fout, "usbdInitialize() returned %d\n", s);    if (s == OK)	{		s = usbdClientRegister (PGM_NAME, &usbdClientHandle);	fprintf (fout, "usbdClientRegister() returned %d\n", s);	if (s == OK)	    {		    fprintf (fout, "usbdClientHandle = 0x%x\n", (UINT32) usbdClientHandle);	  if ((s = usbdVersionGet (&usbdVersion, usbdMfg)) != OK)		{		fprintf (fout, "usbdVersionGet() returned %d\n", s);		}	  if (s == OK)		initialized = TRUE;	    }	}        if (s != OK) 	{	fprintf (fout, "initialization failed\n");	}    return RET_CONTINUE;    }LOCAL UINT16 cmdUsbDown    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout			    )    {    UINT16 s;    initialized = FALSE;    uhciAttachToken = ohciAttachToken = Mpc5200AttachToken = NULL;       if (usbdClientHandle == NULL)	{	fprintf (fout, "usbdClientHandle == NULL.  not registered\n");	}    else	{	s = usbdClientUnregister (usbdClientHandle);	usbdClientHandle = NULL;	fprintf (fout, "usbdClientUnregister() returned %d\n", s);	}       s = usbdShutdown ();    fprintf (fout, "usbdShutdown() returned %d\n", s);    return RET_CONTINUE;    }LOCAL VOID getHcdType    (    char **ppCmd,		    FILE *fout, 		    HCD_EXEC_FUNC *execFunc,	    GENERIC_HANDLE **ppHandle,	    pUINT8 pPciClass,		    pUINT8 pPciSubclass,	    pUINT8 pPciPgmIf,		    char **ppHcdName		    )    {    char hcdName [32];        *ppCmd = GetNextToken (*ppCmd, hcdName, sizeof (hcdName)); 		if (KeywordMatch (hcdName, "Mpc5200", 7) == 0)		{#ifdef INCLUDE_USB_MPC5200		*execFunc = usbHcdMpc5200Exec;		*ppHandle = &Mpc5200AttachToken;		*pPciClass = 0;		*pPciSubclass = 0;		*pPciPgmIf = 0;		*ppHcdName = "Mpc5200";#else     fprintf (fout, "Mpc5200 Component Not Included.\n");     *execFunc = NULL;#endif		}    else		{		fprintf (fout, "Must specify HCD type as 'MPC5200'.\n");		*execFunc = NULL;		}   return;}LOCAL UINT16 cmdAttach    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout			    )    {    HCD_EXEC_FUNC execFunc;    GENERIC_HANDLE *pToken;    UINT8 pciClass;    UINT8 pciSubclass;    UINT8 pciPgmIf;    char *hcdName;    PCI_CFG_HEADER pciCfgHdr;    UINT16 s;        getHcdType (ppCmd, fout, &execFunc, &pToken, &pciClass, &pciSubclass,	&pciPgmIf, &hcdName);    if (execFunc != NULL)	{	if (*pToken != NULL)	    {	    fprintf (fout, "%s already attached.\n", hcdName);	    return RET_CONTINUE;	    }	if (strcmp(hcdName, "Mpc5200") == 0)	  {	  	pciCfgHdr.baseReg [0] = 0xf0001000;	  	pciCfgHdr.intLine = 6;	    s = usbdHcdAttach (execFunc, &pciCfgHdr, pToken);	  }	else	{    	fprintf (fout, "No %s host controller found.\n", hcdName);	    return RET_CONTINUE;	}	fprintf (fout, "usbdHcdAttach() returned %d\n", s);	if (s == OK)	    {	    fprintf (fout, "AttachToken = 0x%x\n", (UINT32) *pToken);	    }	}    return RET_CONTINUE;    }LOCAL UINT16 cmdDetach    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout			    )    {    HCD_EXEC_FUNC execFunc;    GENERIC_HANDLE *pToken;    UINT8 pciClass;    UINT8 pciSubclass;    UINT8 pciPgmIf;    char *hcdName;    UINT16 s;    getHcdType (ppCmd, fout, &execFunc, &pToken, &pciClass, &pciSubclass,	&pciPgmIf, &hcdName);    if (execFunc != NULL)	{		if (*pToken == NULL)	    {	    fprintf (fout, "%s not attached.\n", hcdName);	    }	else	    {	    fprintf (fout, "Detaching %s HCD.\n", hcdName);	    s = usbdHcdDetach (*pToken);	    *pToken = NULL;	    fprintf (fout, "usbdHcdDetach() returned %d\n", s);	    }	}    return RET_CONTINUE;    }    LOCAL VOID showUsbString    (    USBD_CLIENT_HANDLE clientHandle,    USBD_NODE_ID nodeId,    FILE *fout,    UINT8 stringId,    UINT16 maxLen    )    {    UINT8 bfr [USB_MAX_DESCR_LEN];    pUSB_STRING_DESCR pString = (pUSB_STRING_DESCR) bfr;    UINT16 actLen;    UINT16 i;    char c;    UINT16 languageId=0;       int    lang_len;    int    lang_count;    UINT8 *lang_ptr;        if (usbdDescriptorGet (clientHandle, nodeId,	USB_RT_STANDARD | USB_RT_DEVICE, USB_DESCR_STRING, 0, 0,	sizeof (bfr), (pUINT8) &bfr, &actLen) != OK)	{	fprintf (fout, "<<can't read string>>");	return;	}    lang_len   = pString->length - sizeof(pString->length) - sizeof(pString->descriptorType);    lang_count = lang_len/sizeof(languageId);    lang_ptr   = pString->string;    for ( i=0; i<lang_len && lang_ptr ; i++,lang_ptr+=2 )    {      if ( lang_ptr[0]==0x09 && lang_ptr[1]==0x04 )      {        languageId = (lang_ptr[1]<<8) | lang_ptr[0];        break;      }     }     if ( languageId==0 && lang_len>0 && pString->string )    {      languageId = (pString->string[1]<<8) | pString->string[0];    }     if (usbdDescriptorGet (clientHandle, nodeId,	USB_RT_STANDARD | USB_RT_DEVICE, USB_DESCR_STRING, stringId, languageId,	sizeof (bfr), (pUINT8) &bfr, &actLen) != OK)	{	fprintf (fout, "<<can't read string>>");	return;	}    for (i = 0; i + 2 < actLen && i < maxLen * 2; i += 2)	{	c = pString->string [i];	fprintf (fout, "%c", (isprint ((int) c)) ? c : '_');	}    if (i + 2 < actLen)	fprintf (fout, "...");    }LOCAL VOID showMfgModel    (    USBD_CLIENT_HANDLE clientHandle,    USBD_NODE_ID nodeId,    FILE *fout    )    {    USB_DEVICE_DESCR devDescr;    UINT16 actLen;    if (usbdDescriptorGet (clientHandle, nodeId,	USB_RT_STANDARD | USB_RT_DEVICE, USB_DESCR_DEVICE, 0, 0,	sizeof (devDescr), (pUINT8) &devDescr, &actLen) != OK ||	actLen < sizeof (devDescr))	return;    if (devDescr.manufacturerIndex != 0 || devDescr.productIndex != 0)	fprintf (fout, " = ");    if (devDescr.manufacturerIndex != 0)	{	showUsbString (clientHandle, nodeId, fout, devDescr.manufacturerIndex,	    MAX_MFG_STR_LEN);	fprintf (fout, "/");	}    if (devDescr.productIndex != 0)	{	showUsbString (clientHandle, nodeId, fout, devDescr.productIndex,	    MAX_PROD_STR_LEN);	}    }LOCAL UINT16 HubEnumerate    (    USBD_CLIENT_HANDLE clientHandle,	    USBD_NODE_ID hubId, 		    FILE *fout,    UINT16 indent    )    {    UINT16 portCount;			    UINT16 portIndex;			    UINT16 nodeType;			    USBD_NODE_ID nodeId;		    UINT16 s;     fprintf (fout, "%*shub 0x%x", indent, "", (UINT32) hubId);    showMfgModel (clientHandle, hubId, fout);    fprintf (fout, "\n");    if ((s = usbdHubPortCountGet (clientHandle, hubId, &portCount)) 	!= OK)	{	fprintf (fout, "usbdHubPortCountGet() returned %d\n", s);	return ERROR;	}    fprintf (fout, "%*sport count = %d\n", indent+INDENT, "", portCount);      for (portIndex = 0; portIndex < portCount; portIndex++) 	{	if ((s = usbdNodeIdGet (clientHandle, hubId, portIndex, &nodeType, 	    &nodeId)) != OK)	    {	    fprintf (fout, "%*susbdNodeIdGet() returned %d\n", indent+INDENT, "", 		s);	    return ERROR;	    }	switch (nodeType)	    {	    case USB_NODETYPE_NONE:			fprintf (fout, "%*sport %d not connected\n", indent+INDENT, "", 		    portIndex);		break;	    case USB_NODETYPE_HUB:			fprintf (fout, "%*sport %d is hub 0x%x\n", indent+INDENT, "",		    portIndex, (UINT32) nodeId);		if (HubEnumerate (clientHandle, nodeId, fout, indent+INDENT) 		    != OK)		    return ERROR;		break;	    case USB_NODETYPE_DEVICE:			fprintf (fout, "%*sport %d is device 0x%x", indent+INDENT, "",		    portIndex, (UINT32) nodeId);		showMfgModel (clientHandle, nodeId, fout);		fprintf (fout, "\n");		break;	    default:					fprintf (fout, "%*snode type not recognized for node 0x%x\n",		    indent+INDENT, "", (UINT32) nodeId);		break;	    }	}    return OK;    }LOCAL UINT16 USBEnumerate    (    USBD_CLIENT_HANDLE clientHandle,	    FILE *fout    )    {    UINT16 busCount;			    UINT16 busIndex;			    USBD_NODE_ID rootId;		    UINT16 s;    if ((s = usbdBusCountGet (clientHandle, &busCount)) != OK)	{	fprintf (fout, "usbdBusCountGet() returned %d\n", s);	return ERROR;	}    fprintf (fout, "bus count = %d\n", busCount);    for (busIndex = 0; busIndex < busCount; busIndex++) 	{	if ((s = usbdRootNodeIdGet (clientHandle, busIndex, &rootId))	    != OK)	    {	    fprintf (fout, "usbdRootNodeIdGet() returned %d\n", s);	    return ERROR;	    }	fprintf (fout, "enumerating bus %d\n", busIndex);		if (HubEnumerate (clientHandle, rootId, fout, INDENT) != OK)	    return ERROR;	}    return OK;    }LOCAL UINT16 cmdUsbEnum    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout			    )    {    USBEnumerate (usbdClientHandle, fout);    return RET_CONTINUE;    }LOCAL UINT16 cmdUsbStats    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout			    )    {    long nodeId;    USBD_STATS stats;    UINT16 s;      *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);    if (nodeId == -1)	{	fprintf (fout, "Must specify a node id.\n");	return RET_CONTINUE;	}    if ((s = usbdStatisticsGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, &stats, sizeof (stats)))	!= OK)	fprintf (fout, "usbdStatisticsGet() returned %d\n", s);    else	{	fprintf (fout, "totalTransfersIn    = %ld\n", (long) stats.totalTransfersIn);	fprintf (fout, "totalTransfersout   = %ld\n", (long) stats.totalTransfersOut);	fprintf (fout, "totalReceiveErrors  = %ld\n", (long) stats.totalReceiveErrors);	fprintf (fout, "totalTransmitErrors = %ld\n", (long) stats.totalTransmitErrors);	}    return RET_CONTINUE;    }LOCAL UINT16 cmdGetConfig    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout		    )    {    long nodeId;    UINT16 configuration;    UINT16 s;    *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);    if (nodeId == -1)	{	fprintf (fout, "Must specify a node id.\n");	return RET_CONTINUE;	}    if ((s = usbdConfigurationGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, &configuration))	!= OK)	fprintf (fout, "usbdConfigurationGet() returned %d\n", s);    else	fprintf (fout, "current configuration = 0x%x\n", configuration);    return RET_CONTINUE;    }LOCAL UINT16 cmdSetConfig    (    pVOID Param,		    char **ppCmd,		    FILE *fin,			    FILE *fout		    )    {    long nodeId;    long configuration;    UINT16 s;     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);    *ppCmd = GetHexToken (*ppCmd, &configuration, -1);    if (nodeId == -1 || configuration == -1)	{	fprintf (fout, "Must specify a node id and configuration value.\n");	return RET_CONTINUE;	}

⌨️ 快捷键说明

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