Exemplo n.º 1
0
/*FUNCTION*----------------------------------------------------------------
 *
 * Function Name  : Keyboard_Task
 * Returned Value : none
 * Comments       :
 *     Execution starts here
 *
 *END*--------------------------------------------------------------------*/
void Keyboard_Task(void* param)
{
    usb_status status = USB_OK;
    static uint8_t i = 0;
    usb_host_handle kbd_host_handle = (usb_host_handle) param;
    uint8_t j = 0;

    // Wait for insertion or removal event
    OS_Event_wait(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED | USB_EVEN_OUTPUT | USB_EVENT_STATE_CHANGE, FALSE, 0);
    if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_STATE_CHANGE))
    {
        OS_Event_clear(kbd_usb_event, USB_EVENT_STATE_CHANGE);
        update_state();
    }
    if (OS_Event_check_bit(kbd_usb_event, USB_EVEN_OUTPUT))
    {
        OS_Event_clear(kbd_usb_event, USB_EVEN_OUTPUT);
        if (detach == 1)
        {
            /*clear the keyboard flag value*/
            for (j = 8 - 1; j >= 2; j--)
            {
                flag[j] = 0;
            }

        }
        for (j = 2; j < 8; j++)
        {
            if ((buffer_timer[j] == flag[j]) && ((g_current_time - flag_time[j]) > KB_CONTINUE_PRESS))
            {
                if (flag[j] > 1)
                {
                    usb_host_hid_keyboard_output_keyvalue(flag[j]);
                }
            }
        }
        if (!OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED))
        {
            return;
        }
    }
    if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL))
    {
        OS_Event_clear(kbd_usb_event, USB_EVENT_CTRL);
    }

    switch(kbd_hid_device.DEV_STATE)
    {
    case USB_DEVICE_IDLE:
        break;

    case USB_DEVICE_ATTACHED:
        USB_PRINTF("\r\nKeyboard device attached\r\n");
        kbd_hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED;
        status = usb_host_open_dev_interface(kbd_host_handle, kbd_hid_device.DEV_HANDLE, kbd_hid_device.INTF_HANDLE, (usb_class_handle*) &kbd_hid_device.CLASS_HANDLE);
        if (status != USB_OK)
        {
            USB_PRINTF("\r\nError in _usb_hostdev_open_interface: %x\r\n", status);
            return;
        }
        break;

    case USB_DEVICE_INTERFACE_OPENED:
        kbd_hid_get_report_descriptor();
        break;

    case USB_DEVICE_GET_REPORT_DESCRIPTOR_DONE:
        kbd_hid_get_buffer();
        USB_PRINTF("Keyboard interfaced, setting protocol...\r\n");
        /* now we will set the USB Hid standard boot protocol */
        kbd_hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL;

        kbd_hid_com->class_ptr = kbd_hid_device.CLASS_HANDLE;
        kbd_hid_com->callback_fn = usb_host_hid_keyboard_ctrl_callback;
        kbd_hid_com->callback_param = 0;

        status = usb_class_hid_set_protocol(kbd_hid_com, USB_PROTOCOL_HID_KEYBOARD);

        if (status != USB_OK)
        {
            USB_PRINTF("\r\nError in usb_class_hid_set_protocol: %x\r\n", status);
        }
        break;

    case USB_DEVICE_SET_INTERFACE_STARTED:
        break;

    case USB_DEVICE_INUSE:
        //USB_PRINTF("%d\n", data_received);
        i++;
        if (kbd_data_received)
        {
            /******************************************************************
             Initiate a transfer request on the interrupt pipe
             ******************************************************************/
            kbd_hid_com->class_ptr = kbd_hid_device.CLASS_HANDLE;
            kbd_hid_com->callback_fn = usb_host_hid_keyboard_recv_callback;
            kbd_hid_com->callback_param = 0;
            if (i == 4)
            {
                sleep_test = TRUE;
            }

            status = usb_class_hid_recv_data(kbd_hid_com, kbd_buffer, kbd_size);
            if (sleep_test)
            {
                sleep_test = FALSE;
            }
            if (status != USB_OK)
            {
                USB_PRINTF("\r\nError in usb_class_hid_recv_data: %x", status);
            }
            else
            {
                //USB_PRINTF("try to get recv data\n");
                kbd_data_received = 0;
            }

        }
        /* Wait until we get the data from keyboard. */

        if (OS_Event_wait(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED, FALSE, 0) == OS_EVENT_OK)
        {
            if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA))
            {
                //USB_PRINTF("get data\n");
                OS_Event_clear(kbd_usb_event, USB_EVENT_DATA);
                kbd_data_received = 1;
                if (kbd_hid_device.DEV_STATE == USB_DEVICE_INUSE)
                {
                    process_kbd_buffer((uint8_t *) kbd_buffer);
                }
                status = usb_class_hid_recv_data(kbd_hid_com, kbd_buffer, kbd_size);
                if (status != USB_OK)
                {
                    USB_PRINTF("\r\nError in usb_class_hid_recv_data: %x", status);
                }
                else
                {
                    //USB_PRINTF("try to get recv data\n");
                    kbd_data_received = 0;
                }
            }

            if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA_CORRUPTED))
            {
                //USB_PRINTF("get corrupted\n");
                kbd_data_received = 1;
                OS_Event_clear(kbd_usb_event, USB_EVENT_DATA_CORRUPTED);
                status = usb_class_hid_recv_data(kbd_hid_com, kbd_buffer, kbd_size);
                if (status != USB_OK)
                {
                    USB_PRINTF("\r\nError in usb_class_hid_recv_data: %x", status);
                }
                else
                {
                    //USB_PRINTF("try to get recv data\n");
                    kbd_data_received = 0;
                }
            }

            if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL))
            {
                //USB_PRINTF("get control\n");
                //OS_Event_set(USB_Event, USB_EVENT_CTRL);
            }
        }

        break;

    case USB_DEVICE_DETACHED:
        if (kbd_data_received == 1)
        {
            status = usb_host_close_dev_interface(kbd_host_handle, kbd_hid_device.DEV_HANDLE, kbd_hid_device.INTF_HANDLE, kbd_hid_device.CLASS_HANDLE);
            if (status != USB_OK)
            {
                USB_PRINTF("error in _usb_hostdev_close_interface %x\r\n", status);
            }
            kbd_hid_device.INTF_HANDLE = NULL;
            kbd_hid_device.CLASS_HANDLE = NULL;
            USB_PRINTF("Going to idle state\r\n");
            kbd_hid_device.DEV_STATE = USB_DEVICE_IDLE;
            
            if (kbd_reportDescriptor != NULL)
            {
                OS_Mem_free(kbd_reportDescriptor);
                kbd_reportDescriptor = NULL;
            }
            if (kbd_buffer != NULL)
            {
                OS_Mem_free(kbd_buffer);
                kbd_buffer = NULL;
            }

        }
        else
        {
            if (OS_Event_wait(kbd_usb_event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED, FALSE, 0) == OS_EVENT_OK)
            {
                if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA))
                {
                    //USB_PRINTF("get data\n");
                    OS_Event_clear(kbd_usb_event, USB_EVENT_DATA);
                    kbd_data_received = 1;
                    if (kbd_hid_device.DEV_STATE == USB_DEVICE_INUSE)
                    {
                        process_kbd_buffer((uint8_t *) kbd_buffer);
                        OS_Mem_zero(kbd_buffer, kbd_size);
                    }
                }

                if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_DATA_CORRUPTED))
                {
                    //USB_PRINTF("get corrupted\n");
                    kbd_data_received = 1;
                    OS_Event_clear(kbd_usb_event, USB_EVENT_DATA_CORRUPTED);
                }

                if (OS_Event_check_bit(kbd_usb_event, USB_EVENT_CTRL))
                {
                    //USB_PRINTF("get control\n");
                    //OS_Event_set(USB_Event, USB_EVENT_CTRL);
                }
            }
            OS_Event_set(kbd_usb_event, USB_EVENT_CTRL);
        }
        break;
    case USB_DEVICE_OTHER:
        break;
    default:
        break;
    } /* Endswitch */
}
Exemplo n.º 2
0
void Mouse_Task( uint32_t param )
{
    USB_STATUS              status = USB_OK;
    TR_INIT_PARAM_STRUCT    tr;
    HID_COMMAND_PTR         hid_com;
    unsigned char               *buffer;
    PIPE_STRUCT_PTR         pipe;
    uint32_t                 e;
    _usb_host_handle        mouse_host_handle = (_usb_host_handle) param;
   
    hid_com = (HID_COMMAND_PTR) _mem_alloc(sizeof(HID_COMMAND));

    /* event for USB callback signaling */
    _lwevent_create(&USB_Mouse_Event, LWEVENT_AUTO_CLEAR);

    printf("\nMQX USB HID Mouse Demo\nWaiting for USB Mouse to be attached...\n");
    fflush(stdout);
   
    /*
    ** Infinite loop, waiting for events requiring action
    */
    for ( ; ; ) {
    
        // Wait for insertion or removal event
        _lwevent_wait_ticks(&USB_Mouse_Event, USB_EVENT_CTRL, FALSE, 0);
        
        switch ( mouse_hid_device.DEV_STATE ) {
            case USB_DEVICE_IDLE:
                break;
            case USB_DEVICE_ATTACHED:
                printf("\nMouse device attached\n");
                fflush(stdout);
                mouse_hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED;
                status = _usb_hostdev_select_interface(mouse_hid_device.DEV_HANDLE, mouse_hid_device.INTF_HANDLE, (void *)&mouse_hid_device.CLASS_INTF);
                if (status != USB_OK) {
                    printf("\nError in _usb_hostdev_select_interface: %x", status);
                    fflush(stdout);
                    _task_block();
                } /* Endif */
                break;
            case USB_DEVICE_SET_INTERFACE_STARTED:
                break;

            case USB_DEVICE_INTERFACED:
                pipe = _usb_hostdev_find_pipe_handle(mouse_hid_device.DEV_HANDLE, mouse_hid_device.INTF_HANDLE, USB_INTERRUPT_PIPE, USB_RECV);
                if (pipe == NULL) {
                    printf("\nError getting interrupt pipe.");
                    fflush(stdout);
                    _task_block();
                }
                _usb_hostdev_get_buffer(mouse_hid_device.DEV_HANDLE, pipe->MAX_PACKET_SIZE, (void **) &buffer);
                if (buffer == NULL) {
                    printf("\nMemory allocation failed. STATUS: %x", status);
                    fflush(stdout);
                    _task_block();
                }

                printf("Mouse interfaced, setting protocol...\n");
                /* now we will set the USB Hid standard boot protocol */
                mouse_hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL;
            
                hid_com->CLASS_PTR = (CLASS_CALL_STRUCT_PTR)&mouse_hid_device.CLASS_INTF;
                hid_com->CALLBACK_FN = usb_host_hid_mouse_ctrl_callback;
                hid_com->CALLBACK_PARAM = 0;
            
                status = usb_class_hid_set_protocol(hid_com, USB_PROTOCOL_HID_MOUSE);
         
                if (status != USB_STATUS_TRANSFER_QUEUED) {
                      printf("\nError in usb_class_hid_set_protocol: %x", status);
                      fflush(stdout);
                }
                break;
            case USB_DEVICE_INUSE:
                printf("Mouse device ready, try to move the mouse\n");
                while (1) {
                    /******************************************************************
                    Initiate a transfer request on the interrupt pipe
                    ******************************************************************/
                    usb_hostdev_tr_init(&tr, usb_host_hid_mouse_recv_callback, NULL);
                    tr.G.RX_BUFFER = buffer;
                    tr.G.RX_LENGTH = pipe->MAX_PACKET_SIZE;                     
                        
                    status = _usb_host_recv_data(mouse_host_handle, pipe, &tr);
                        
                    if (status != USB_STATUS_TRANSFER_QUEUED) {
                        printf("\nError in _usb_host_recv_data: %x", status);
                        fflush(stdout);
                    }
                    
                    /* Wait untill we get the data from keyboard. */
                    _lwevent_wait_ticks(&USB_Mouse_Event, USB_EVENT_CTRL | USB_EVENT_DATA | USB_EVENT_DATA_CORRUPTED, FALSE, 0);
                        
                    e = _lwevent_get_signalled();
                    if (USB_EVENT_DATA == e) {
                        if(mouse_hid_device.DEV_STATE == USB_DEVICE_INUSE) {
                            process_mouse_buffer((unsigned char *)buffer);
                        }
                    }
                    else if (USB_EVENT_CTRL == e) {
                        /* kick the outer loop again to handle the CTRL event */
                        _lwevent_set(&USB_Mouse_Event, USB_EVENT_CTRL);
                        break;
                    }
                }
                break;
            case USB_DEVICE_DETACHED:
                printf("Going to idle state\n");
                mouse_hid_device.DEV_STATE = USB_DEVICE_IDLE;
                break;
            case USB_DEVICE_OTHER:
                break;
             default:
                printf("Unknown Mouse Device State = %d\n", mouse_hid_device.DEV_STATE);
                fflush(stdout);
                break;
        } /* Endswitch */
    } /* Endfor */
} /* Endbody */
Exemplo n.º 3
0
void Mouse_Task ( uchar *buffer, HID_COMMAND_PTR hid_com  )
{
	USB_STATUS              status = USB_OK;
	static _usb_pipe_handle        pipe;
	static uint_32 check = 0, usb_event_flag = 1;
	static TR_INIT_PARAM_STRUCT    tr;
	uint_32                 result, test_buffer[4]; 
   	
		// Wait for insertion or removal event
        //result = _usb_event_wait_ticks(&USB_Event, USB_EVENT_CTRL, FALSE, 0);
        
        //if (USB_EVENT_NOT_SET == result) return;
      	switch ( hid_device.DEV_STATE ) {
         	case USB_DEVICE_IDLE:
            	break;
         	case USB_DEVICE_ATTACHED:
	            printf("\nMouse device attached");
	            //fflush(stdout);
	            hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED;
	            status = _usb_hostdev_select_interface(hid_device.DEV_HANDLE, hid_device.INTF_HANDLE, (pointer2)&hid_device.CLASS_INTF);
	            if (status != USB_OK) {
               		printf("\nError in _usb_hostdev_select_interface: %x", status);
               		//fflush(stdout);
               		exit(1);
            	} /* Endif */
            	break;
         	case USB_DEVICE_SET_INTERFACE_STARTED:
            	break;
         	case USB_DEVICE_INTERFACED:
         		printf("Mouse interfaced, setting protocol...\n");
         		/* now we will set the USB Hid standard boot protocol */
            	hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL;
            
	            hid_com->CLASS_PTR = (CLASS_CALL_STRUCT_PTR)&hid_device.CLASS_INTF;
	            hid_com->CALLBACK_FN = usb_host_hid_ctrl_callback;
	            hid_com->CALLBACK_PARAM = 0;
         	
	         	status = usb_class_hid_set_protocol(hid_com, USB_PROTOCOL_HID_MOUSE);
         
	         	if (status != USB_STATUS_TRANSFER_QUEUED) {
	                  printf("\nError in usb_class_hid_set_protocol: %x", status);
	                  //fflush(stdout);
	         	}
	            break;
			case USB_DEVICE_INUSE:
			    if(0 == check){
			      check = 1;
            pipe = _usb_hostdev_find_pipe_handle(hid_device.DEV_HANDLE, hid_device.INTF_HANDLE, USB_INTERRUPT_PIPE, USB_RECV);
         		if(pipe){
         			printf("Mouse device ready, try to move the mouse\n");
			      } 
			    }

   			/******************************************************************
              Initiate a transfer request on the interrupt pipe
              ******************************************************************/
              if (usb_event_flag == 1){
     			      usb_hostdev_tr_init(&tr, usb_host_hid_recv_callback, NULL);
              	tr.RX_BUFFER = buffer;
              	tr.RX_LENGTH = HID_BUFFER_SIZE;	                  	
              	
              	status = _usb_host_recv_data(host_handle, pipe, &tr);
              	
              	if (status != USB_STATUS_TRANSFER_QUEUED) {
                 	printf("\nError in _usb_host_recv_data: %x", status);
                 	//fflush(stdout);
                }
              }
            
              /* Wait untill we get the data from keyboard. */
              if(_usb_event_wait_ticks(&USB_Event,USB_EVENT_CTRL | USB_EVENT_DATA, FALSE, 0) == USB_EVENT_SET){
                usb_event_flag = 1;
                _usb_event_clear(&USB_Event, USB_EVENT_CTRL | USB_EVENT_DATA);
              } else {
                usb_event_flag = 0;
              }
              
              /* if not detached in the meanwhile */
              if((hid_device.DEV_STATE == USB_DEVICE_INUSE) && (usb_event_flag == 1)) {	                         
//              if((hid_device.DEV_STATE == USB_DEVICE_INUSE)) {	                         
                  process_mouse_buffer((uchar *)buffer);  
                  //_usb_event_clear(&USB_Event, USB_EVENT_CTRL | USB_EVENT_DATA);      
                                             	                
              }
              //else {
              //    /* kick the outer loop again to handle the CTRL event */
              //    _usb_event_set(&USB_Event, USB_EVENT_CTRL);
              //    break;
              //}
	         		
    	     	break;
         	case USB_DEVICE_DETACHED:
         	  check = 0;
         		printf("Going to idle state\n");
            	hid_device.DEV_STATE = USB_DEVICE_IDLE;
            	break;
         	case USB_DEVICE_OTHER:
	            break;
    	     default:
        	    printf("Unknown Mouse Device State = %d\n", hid_device.DEV_STATE);
            	//fflush(stdout);
            	break;
      	} /* Endswitch */
   
} /* Endbody */
Exemplo n.º 4
0
void Mouse_Task( uint_32 param )
{
    USB_STATUS              status = USB_OK;
    _usb_pipe_handle        pipe;
    TR_INIT_PARAM_STRUCT    tr;
    HID_COMMAND_PTR         hid_com;
    uchar_ptr               buffer;
    _usb_host_handle        mouse_host_handle = (_usb_host_handle) param;
   
    /***************************************************************************
    ** Allocate memory for being able to send commands to HID class driver. Note 
    ** that USB_mem_alloc_uncached() allocates aligned buffers on cache line boundry.
        ** This  is required if program is running with data cache on system.
    ***************************************************************************/
    hid_com = (HID_COMMAND_PTR) USB_mem_alloc_uncached(sizeof(HID_COMMAND));

    /* event for USB callback signaling */
    _lwevent_create(&USB_Mouse_Event, LWEVENT_AUTO_CLEAR);

    /* Allocate buffer to receive data from interrupt. It must be created on uncached heap,
    ** since some USB host controllers use DMA to access those buffers.
    */
    buffer = USB_mem_alloc_uncached(HID_MOUSE_BUFFER_SIZE);
    if (!buffer) {
        printf("\nError allocating buffer!");
        fflush(stdout);
        exit(1);
    } /* Endif */

    printf("\nMQX USB HID Mouse Demo\nWaiting for USB Mouse to be attached...\n");
    fflush(stdout);
   
    /*
    ** Infinite loop, waiting for events requiring action
    */
    for ( ; ; ) {
    
        // Wait for insertion or removal event
        _lwevent_wait_ticks(&USB_Mouse_Event, USB_Mouse_Event_CTRL, FALSE, 0);
        
        switch ( mouse_hid_device.DEV_STATE ) {
            case USB_DEVICE_IDLE:
                break;
            case USB_DEVICE_ATTACHED:
                printf("\nMouse device attached\n");
                fflush(stdout);
                mouse_hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED;
                status = _usb_hostdev_select_interface(mouse_hid_device.DEV_HANDLE, mouse_hid_device.INTF_HANDLE, (pointer)&mouse_hid_device.CLASS_INTF);
                if (status != USB_OK) {
                    printf("\nError in _usb_hostdev_select_interface: %x", status);
                    fflush(stdout);
                    exit(1);
                } /* Endif */
                break;
            case USB_DEVICE_SET_INTERFACE_STARTED:
                break;
            case USB_DEVICE_INTERFACED:
                printf("Mouse interfaced, setting protocol...\n");
                /* now we will set the USB Hid standard boot protocol */
                mouse_hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL;
            
                hid_com->CLASS_PTR = (CLASS_CALL_STRUCT_PTR)&mouse_hid_device.CLASS_INTF;
                hid_com->CALLBACK_FN = usb_host_hid_mouse_ctrl_callback;
                hid_com->CALLBACK_PARAM = 0;
            
                status = usb_class_hid_set_protocol(hid_com, USB_PROTOCOL_HID_MOUSE);
         
                if (status != USB_STATUS_TRANSFER_QUEUED) {
                      printf("\nError in usb_class_hid_set_protocol: %x", status);
                      fflush(stdout);
                }
                break;
            case USB_DEVICE_INUSE:
                pipe = _usb_hostdev_find_pipe_handle(mouse_hid_device.DEV_HANDLE, mouse_hid_device.INTF_HANDLE, USB_INTERRUPT_PIPE, USB_RECV);
                if(pipe){
                    printf("Mouse device ready, try to move the mouse\n");
                
                    while(1){
                        /******************************************************************
                        Initiate a transfer request on the interrupt pipe
                        ******************************************************************/
                        usb_hostdev_tr_init(&tr, usb_host_hid_mouse_recv_callback, NULL);
                        tr.RX_BUFFER = buffer;
                        tr.RX_LENGTH = HID_MOUSE_BUFFER_SIZE;                       
                        
                        status = _usb_host_recv_data(mouse_host_handle, pipe, &tr);
                        
                        if (status != USB_STATUS_TRANSFER_QUEUED) {
                            printf("\nError in _usb_host_recv_data: %x", status);
                            fflush(stdout);
                        }
                    
                        /* Wait untill we get the data from keyboard. */
                        _lwevent_wait_ticks(&USB_Mouse_Event, USB_Mouse_Event_CTRL | USB_Mouse_Event_DATA, FALSE, 0);
                        
                        /* if not detached in the meanwhile */
                        if(mouse_hid_device.DEV_STATE == USB_DEVICE_INUSE) {
                            process_mouse_buffer((uchar *)buffer);
                        }
                        else {
                            /* kick the outer loop again to handle the CTRL event */
                            _lwevent_set(&USB_Mouse_Event, USB_Mouse_Event_CTRL);
                            break;
                        }
                    }
                }
                break;
            case USB_DEVICE_DETACHED:
                printf("Going to idle state\n");
                mouse_hid_device.DEV_STATE = USB_DEVICE_IDLE;
                break;
            case USB_DEVICE_OTHER:
                break;
             default:
                printf("Unknown Mouse Device State = %d\n", mouse_hid_device.DEV_STATE);
                fflush(stdout);
                break;
        } /* Endswitch */
    } /* Endfor */
} /* Endbody */
Exemplo n.º 5
0
void Main_Task(uint_32 param)
{
    USB_STATUS status = USB_OK;
    _usb_pipe_handle pipe;
    TR_INIT_PARAM_STRUCT tr;
    HID_COMMAND hid_com;
    uchar *buffer;

    printf("\n** MQX USB Keyboard To Mouse Demo **\n");
    
    printf("\nDemo will allow you control mouse cursor on your PC using keyboard attached to TWR-SER2 board.");
    printf("\nThis demo requires TWR-SER2 card and correct BSP setting.");
    printf("\nSee FSL_MQX_getting_started.pdf chapter 7 for details.");
    fflush(stdout);

    MouseDev_Init();

    /* Allocate buffer to receive data from interrupt. It must be created on uncached heap,
    ** since some USB host controllers use DMA to access those buffers.
    */
    buffer = USB_mem_alloc_uncached(HID_BUFFER_SIZE);
    if (buffer == NULL) {
        printf("\nMemory allocation failed. STATUS: %x", status);
        fflush(stdout);
        _task_block();
    }
    
    /* _usb_otg_init needs to be done with interrupts disabled */
    _int_disable();

    /* event for USB callback signaling */
    _lwevent_create(&USB_Event, LWEVENT_AUTO_CLEAR);

    _int_install_unexpected_isr();

    /* Register USB interface and its settings from BSP */
    if (MQX_OK != _usb_host_driver_install(&_bsp_usb_host_khci0_if)) {
        printf("\n\nUSB Host Installation failed.");
        fflush(stdout);
        _task_block();
    }

    /*
     ** It means that we are going to act like host, so we initialize the
     ** host stack. This call will allow USB system to allocate memory for
     ** data structures, it uses later (e.g pipes etc.).
     */
    status = _usb_host_init(&_bsp_usb_host_khci0_if, &host_handle);

    if(status != USB_OK) {
        printf("\nUSB Host Initialization failed. STATUS: %x", status);
        fflush(stdout);
        _task_block();
    }

    /*
     ** since we are going to act as the host driver, register the driver
     ** information for wanted class/subclass/protocols
     */
    status = _usb_host_driver_info_register(host_handle, DriverInfoTable);
    if(status != USB_OK) {
        printf("\nDriver Registration failed. STATUS: %x", status);
        fflush(stdout);
        _task_block();
    }

    _int_enable();
    printf("\nHost initialization finished. Attach USB Keyboard to the board.");

    /*
     ** Infinite loop, waiting for events requiring action
     */
    for(;;) {

        // Wait for insertion or removal event
        _lwevent_wait_ticks(&USB_Event, USB_EVENT_CTRL, FALSE, 0);

        switch (hid_device.DEV_STATE) {

        case USB_DEVICE_IDLE:
            break;

        case USB_DEVICE_ATTACHED:
            hid_device.DEV_STATE = USB_DEVICE_SET_INTERFACE_STARTED;

            status = _usb_hostdev_select_interface(hid_device.DEV_HANDLE, hid_device.INTF_HANDLE, (pointer) & hid_device.CLASS_INTF);
            if(status != USB_OK) {
                printf("\nError in _usb_hostdev_select_interface: %x", status);
                fflush(stdout);
                _task_block();
            }
            break;

        case USB_DEVICE_SET_INTERFACE_STARTED:
            break;

        case USB_DEVICE_INTERFACED:
            printf("\nKeyboard device interfaced, setting protocol.");
            /* now we will set the USB Hid standard boot protocol */
            hid_device.DEV_STATE = USB_DEVICE_SETTING_PROTOCOL;

            hid_com.CLASS_PTR = (CLASS_CALL_STRUCT_PTR) & hid_device.CLASS_INTF;
            hid_com.CALLBACK_FN = usb_host_hid_ctrl_callback;
            hid_com.CALLBACK_PARAM = 0;

            /* Force the keyboard to behave as in USB Hid class standard boot protocol */
            status = usb_class_hid_set_protocol(&hid_com, USB_PROTOCOL_HID_KEYBOARD);

            if(status != USB_STATUS_TRANSFER_QUEUED) {
                printf("\nError in usb_class_hid_set_protocol: %x", status);
                fflush(stdout);
            }

            break;

        case USB_DEVICE_INUSE:
            pipe = _usb_hostdev_find_pipe_handle(hid_device.DEV_HANDLE, hid_device.INTF_HANDLE, USB_INTERRUPT_PIPE, USB_RECV);

            if(pipe) {
                printf("\n\nUse W, A, S or D to move the cursor.\nW = UP\nS = DOWN\nA = LEFT\nD = RIGHT\n");

                while(1) {
                  /******************************************************************
                    Initiate a transfer request on the interrupt pipe
                  ******************************************************************/
                    usb_hostdev_tr_init(&tr, usb_host_hid_recv_callback, NULL);
                    tr.G.RX_BUFFER = (uchar *) buffer;
                    tr.G.RX_LENGTH = HID_BUFFER_SIZE;

                    status = _usb_host_recv_data(host_handle, pipe, &tr);

                    if(status != USB_STATUS_TRANSFER_QUEUED) {
                        printf("\nError in _usb_host_recv_data: %x", status);
                        fflush(stdout);
                    }

                    /* Wait untill we get the data from keyboard. */
                    _lwevent_wait_ticks(&USB_Event, USB_EVENT_CTRL | USB_EVENT_DATA, FALSE, 0);

                    /* if not detached in the meanwhile */
                    if(hid_device.DEV_STATE == USB_DEVICE_INUSE) {
                        process_kbd_buffer((uchar *)buffer);
                    }
                    else {
                        /* kick the outer loop again to handle the CTRL event */
                        _lwevent_set(&USB_Event, USB_EVENT_CTRL);
                        break;
                    }

                    /* Slight delay to be nice to other processes. (Note that keyboards have its 
                       own autorepeat delay typically much longer than one would expect. In case 
                       the user holds the key (without any other keys changed), the USB response 
                       from keyboard is delayed (typ by 500ms). This is why the USB host should 
                       handle autorepeat feature by itself (not done in this example) */
                    _time_delay(1);

                    USB_HID_Periodic_Task(); //feed the USB device with the correct setup answers
                }
            }
            break;

        case USB_DEVICE_DETACHED:
            printf("Going to idle state\n");
            hid_device.DEV_STATE = USB_DEVICE_IDLE;
            break;
        }
    }
}