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

📄 usrusbkbdinit.c

📁 cpc-1631的BSP包for VxWorks操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
		{
		printf("usbKbdDevFind() returned ERROR\n");
		USB_KBD_MUTEX_GIVE;
		return;
		}
	    printf("USB Keyboard attached as %s\n", kbdName);
	    }
	}
    else if (attachCode == USB_KBD_REMOVE)
	{
	/* find the related device */

	if (usbKbdDevFind (pChan, &pUsbKbdDev) != OK)
	    {
	    printf ("usbKbdDevFind could not find channel 0x%d", (UINT32)pChan);
		USB_KBD_MUTEX_GIVE;	    
	    return;
	    }	

	/* delete the device */
	usbKbdDevDelete (pUsbKbdDev);

	if (usbKeyboardSioChanUnlock (pChan) != OK)
	    {
	    printf("usbKeyboardSioChanUnlock () returned ERROR\n");
		USB_KBD_MUTEX_GIVE;	    
	    return;
	    }
	sprintf (kbdName, "%s%d", USB_KBD_NAME, kbdCount-1);
	printf ("USB Keyboard %s removed\n", kbdName);

        /* now we can decrement the counter */
        kbdCount--;

	}
	USB_KBD_MUTEX_GIVE;
    }

/*******************************************************************************
*
* usbKbdOpen - open a usbKbdDrv serial device.
*
* Increments a counter that holds the number of open paths to device. 
*
* RETURNS: the pointer to USB_KBD_DEV
*
* ERRNO: none
*
* \NOMANUAL
*/

LOCAL int usbKbdOpen
    (
    USB_KBD_DEV	* pUsbKbdDev,  /* keyboard device to read from */
    char     *	name,		/* device name */
    int		flags,		/* flags */
    int        	mode		/* mode selected */
    )
    {
    USB_KBD_MUTEX_TAKE(WAIT_FOREVER);

    pUsbKbdDev->numOpen++;  /* increment number of open paths */
    
    sioIoctl (pUsbKbdDev->pSioChan, SIO_OPEN, NULL);
	USB_KBD_MUTEX_GIVE;
    return ((int) pUsbKbdDev);
    }

/*******************************************************************************
*
* usbKbdDrvUnInit - shuts down an I/O USB keyboard driver
*
* This is supplied to for the user, but it should be noted that iosDrvRemove()
* may cause unpredictable results.
*
* RETURNS: OK or ERROR
*
* ERRNO: none
*/

STATUS usbKbdDrvUnInit (void)
    {
    if (!usbKbdDrvNum)
	return (OK);

    /* remove the driver */

    if (iosDrvRemove (usbKbdDrvNum, TRUE) != OK)
	{
	printf("iosDrvRemove () returned ERROR\n");
	return (ERROR);
	}

    usbKbdDrvNum = 0;

    /* delete the mutex semaphores */
 
    if (semDelete (usbKbdMutex) == ERROR)
	{
	printf("semDelete (usbKbdMutex) returned ERROR\n");
	return (ERROR);
	}

    if (semDelete (usbKbdListMutex) == ERROR)
	{
	printf("semDelete (usbKbdListMutex) returned ERROR\n");
	return (ERROR);
	}

    /* unregister */
 
    if (usbKeyboardDynamicAttachUnRegister (usbKbdDrvAttachCallback, 
					 NULL) 
					!= OK)
	{
	printf("usbKeyboardDynamicAttachUnRegister () returned ERROR\n");
	return (ERROR);
	}
 
    /* shutdown */

    if (usbKeyboardDevShutdown () != OK)
	{
	printf("usbKeyboardDynamicAttachUnRegister () returned ERROR\n");
	return (ERROR);
	}

    return (OK);
    }

/*******************************************************************************
*
* usbKbdClose - close a usbKbdDrv serial device.
*
* Decrements the counter of open paths to device and alerts the driver 
* with an ioctl call when the count reaches zero. This scheme is used to
* implement the HUPCL(hang up on last close).      
*
* RETURNS: the pointer to USB_KBD_DEV
*
* ERRNO: none
*
* \NOMANUAL
*/

LOCAL int usbKbdClose
    (
    USB_KBD_DEV	* pUsbKbdDev          /* keyboard device to read from */
    )
    {

    /* if there are no open channels */
	USB_KBD_MUTEX_TAKE(WAIT_FOREVER);
    if (!(--pUsbKbdDev->numOpen))
	{
	sioIoctl (pUsbKbdDev->pSioChan, SIO_HUP, NULL);
	}
	USB_KBD_MUTEX_GIVE;
    return ((int) pUsbKbdDev);
    }

/*******************************************************************************
*
* usbKbdIoctl - issue device control commands
*
* This routine issues device control commands to an USB keyboard device.
*
* RETURNS: depends on the function invoked.
*
* ERRNO: none
*
* \NOMANUAL
*/

LOCAL int usbKbdIoctl
    (
    USB_KBD_DEV	* pUsbKbdDev,	/* keyboard device to read from */
    int		request,	/* request code */
    void *	arg		/* some argument */
    )
    {
    int x = 0;
	USB_KBD_MUTEX_TAKE(WAIT_FOREVER);
    x = sioIoctl (pUsbKbdDev->pSioChan, request, arg);
    USB_KBD_MUTEX_GIVE;
    return x;
    }

/*******************************************************************************
*
* usbKbdRead - read from the USB keyboard
*
* This routines is a no-op for a read from the USB keyboard.
*
* RETURNS: OK, always.
*/
#if (USB_KEYBOARD_POLLING == TRUE)
LOCAL int usbKbdReadPoll
    (
    USB_KBD_DEV	* pUsbKbdDev,           /* keyboard device to read from */
    char *buffer, 
    UINT32 nBytes    
    )
    {
    int status = OK;
	USB_KBD_MUTEX_TAKE(WAIT_FOREVER);
    status = pUsbKbdDev->pSioChan->pDrvFuncs->pollInput (pUsbKbdDev->pSioChan, buffer);
    USB_KBD_MUTEX_GIVE;
    if (status == EAGAIN)
    	return 0;
    if (status == 0)
    	return 1;
    return -1;
    }
#else
/*******************************************************************************
*
* usbKbdReadInt - read from the USB keyboard
*
* This routines is a no-op for a read from the USB keyboard.
*
* RETURNS: OK, always.
*/


LOCAL int usbKbdReadInt
    (
    USB_KBD_DEV	* pUsbKbdDev,           /* keyboard device to read from */
    char *buffer, 
    UINT32 nBytes    
    )
    {

	int nRngBytes;
	int n = 0;
	int totalProc = 0;
	char aChar;
	int rcvValue = 0;


	USB_KBD_MUTEX_TAKE(WAIT_FOREVER);


	while ( (totalProc < nBytes) && ((rcvValue = msgQReceive(pUsbKbdDev->buff,buffer,sizeof(char),NO_WAIT)) == (sizeof(char)))) {
		totalProc++;
		buffer++;
	}
	USB_KBD_MUTEX_GIVE;
	if (totalProc > 0)
		return totalProc;
	if (msgQReceive(pUsbKbdDev->buff,buffer,sizeof(char),WAIT_FOREVER) != (sizeof(char)))
		return ERROR;
	else
		return 1;
}


LOCAL STATUS usbKbdTxCallback  (void *callbackParam,  char aChar)  {

    USB_KBD_DEV	* pDev = (USB_KBD_DEV *) callbackParam; /* keyboard device */
	msgQSend (pDev->buff,&aChar,sizeof(char),NO_WAIT,MSG_PRI_NORMAL);
	return OK;
    }
#endif
/*******************************************************************************
*
* usbKbdWrite - write to the USB keyboard
*
* 
* USB keyboards don't like to be written to.  We don't support this feature.
*
* RETURNS: ENOSYS
*
* ERRNO: none
*
* \NOMANUAL
*/

LOCAL int usbKbdWrite
    (
    USB_KBD_DEV	* pUsbKbdDev,		/* keyboard device to read from */
    char	* buffer,		/* buffer of data to write  */
    UINT32	  nBytes		/* number of bytes in buffer */
    )
    {

    return (ENOSYS);
    }


/*****************************************************************************
*
* usrUsbKbdInit - initialize the USB keyboard driver
*
* This function initializes the USB keyboard driver and registers for attach 
* callbacks.  
*
* RETURNS: N/A
*
* ERRNO: none
*
* \NOMANUAL
*/

void usrUsbKbdInit (void) {

    /* Check if driver already installed */

    if (usbKbdDrvNum > 0)
	{
	printf ("Keyboard already initilaized.\n");
	return ;
	}

    if (usbKeyboardDevInit () != OK) {
		printf ("usbKeyboardDevInit() returned ERROR\n");
		return;
    }
    if (usbKeyboardDynamicAttachRegister (usbKbdDrvAttachCallback, (void *) NULL) != OK)   {
	    printf ("usbKeyboardDynamicAttachRegister() returned ERROR\n");
	    return;
	}
	
	 /* Create the mutex semaphores */
     usbKbdMutex = semMCreate (SEM_Q_PRIORITY | SEM_DELETE_SAFE |
			      SEM_INVERSION_SAFE);
 
    usbKbdListMutex = semMCreate (SEM_Q_PRIORITY | SEM_DELETE_SAFE |
				  SEM_INVERSION_SAFE);
 	
    /* initialize the linked list */
 
    lstInit (&usbKbdList);

    /* Install the appropriate functions into the driver table */
    	usbKbdDrvNum = iosDrvInstall ((FUNCPTR) NULL, 
					      (FUNCPTR) usbKbdDevDelete, 
   					      (FUNCPTR) usbKbdOpen, 
					      (FUNCPTR) usbKbdClose, 
#if (USB_KEYBOARD_POLLING == TRUE)
	   					  (FUNCPTR) usbKbdReadPoll, 
#else
				    	  (FUNCPTR) usbKbdReadInt, 
#endif					
					      (FUNCPTR) usbKbdWrite, 
					      (FUNCPTR) usbKbdIoctl);

	return;
    }

/*************************************************************************
*
* enterThread - waits for user to press [enter]
*
* This is a thread which wait for the enter to be pressed for the keyboard
* driver to come out of polling mode
*
* RETURNS:  N/A
*
* ERRNO: none
*
* \NOMANUAL
*/

LOCAL void enterThread ( void )

    {
    char bfr [256];

    printf ("Press [enter] to terminate polling.\n");
    gets (bfr);
    enterPressed = TRUE;
    }



/*************************************************************************
*
* playWithKeyboard - prints key presses to the terminal
*
* This is a test-application which prints the key pressed to the terminal.
*
* RETURNS: OK or ERROR
*
* ERRNO: none
* 
*/

STATUS playWithKeyboard ( void )
    {
    int		taskId;
    char	inChar;
    int		fd = 0;


    /* Poll for input or until user presses CTRL-Z on USB keyboard or
     * [enter] on main keyboard. */


    if ((fd = open ("/usbKb/0", 2,0)) == ERROR)
	return (ERROR);


    /* Create thread to watch for keypress */

    enterPressed = FALSE;

     if((taskId = taskSpawn ("tEnter", 
			     5,		/* priority */
			     0,		/* task options */ 
			     0x4000, 	/* 16k stack space */	
			     (FUNCPTR) enterThread, 
			     0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )) ==ERROR)
	{
	printf(" TaskSpawn Error...!\n");
	return ERROR;	
	}

    printf ("Press CTRL-Z to terminate polling.\n");

    while (!enterPressed)
	{
	    if (read (fd, &inChar, 1) != 0)
		{
		printf ("ASCII %3d", inChar);
		if (inChar >= 32)
		    printf (" '%c'", inChar);
		printf ("\n");

		if (inChar == CTRL_Z)
		    {
		    printf ("Stopped by CTRL-Z\n");
		    break;
		    }
		}

	taskDelay (2);
	}

    taskDelete (taskId);

    return OK;
    }

⌨️ 快捷键说明

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