Beispiel #1
0
/*FUNCTION*----------------------------------------------------------------
*
* Function Name  : main (Main_Task if using MQX)
* Returned Value : none
* Comments       :
*     Execution starts here
*
*END*--------------------------------------------------------------------*/
void Main_Task ( uint32_t param )
{ /* Body */
    USB_STATUS           status = USB_OK;

    /* _usb_otg_init needs to be done with interrupts disabled */
    _int_disable();
    _int_install_unexpected_isr();
    _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER);
    /*
    ** 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 (
    USBCFG_DEFAULT_HOST_CONTROLLER,   /* Use value in header file */
    &host_handle);             /* Returned pointer */

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

    /*
    ** 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);
    }
    _int_enable();

    printf("\n MQX USB Audio Speaker Host Demo\n");
    fflush(stdout);
    /* Creat lwevents*/
    if (MQX_OK !=_lwevent_create(&USB_Keyboard_Event, LWEVENT_AUTO_CLEAR)){
        printf("\n_lwevent_create USB_Keyboard_Event failed.\n");
    }
    if (MQX_OK !=_lwevent_create(&USB_Audio_FU_Request, LWEVENT_AUTO_CLEAR)){
        printf("\n_lwevent_create USB_Audio_FU_Request failed.\n");
    }
    if (MQX_OK !=_lwevent_create(&SD_Card_Event, LWEVENT_AUTO_CLEAR)){
        printf("\n_lwevent_create SD_Card_Event failed.\n");
    }
    _task_create(0, AUDIO_TASK, (uint32_t) host_handle);
    _task_create(0, HID_KEYB_TASK, (uint32_t) host_handle);
    _task_create(0, SDCARD_TASK, (uint32_t) host_handle);
    _task_create(0, SHELL_TASK, (uint32_t) host_handle);
    /* The main task has done its job, so exit */
} /* Endbody */
Beispiel #2
0
void Main_Task ( uint_32 param )
{ /* Body */
   USB_STATUS           status = USB_OK;
   
   /* _usb_otg_init needs to be done with interrupts disabled */
   _int_disable();

   _int_install_unexpected_isr();
   if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) {
      printf("\n Driver installation failed");
      _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 (USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle);

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

   /*
   ** 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);
   }
          
   _int_enable();

   printf("\nMQX USB HID Keyboard + Mouse Demo");
   fflush(stdout);

   _task_create(0, MOUSE_TASK, (uint_32) host_handle);
   _task_create(0, KEYBOARD_TASK, (uint_32) host_handle);

   /* The main task has done its job, so exit */
} /* Endbody */
Beispiel #3
0
/**************************************************************************
  Global variables
 **************************************************************************/
int unit_test_usb_host(void) {

    static _usb_host_handle host_handle;
    USB_STATUS  error;

    USB_lock();

    _int_install_unexpected_isr();



    if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) {
        printf("\n Driver installation failed");
        _task_block();
    }

    error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle);
    if (error == USB_OK) {
        error = _usb_host_driver_info_register(host_handle, (pointer)DriverInfoTable);
        if (error == USB_OK) {
            error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME, NULL);
        }
    }

    USB_unlock();

    if (error != USB_OK) {
        _task_block();
    }

    if (MQX_OK != _lwevent_create(&usb_event, LWEVENT_AUTO_CLEAR)) {
        return -1;
    }

    /* prepare events to be auto or manual */
    _lwevent_set_auto_clear(&usb_event, 0xFFFFFFFF);
    /* pre-set events */
    _lwevent_clear(&usb_event, 0xFFFFFFFF);

    _lwevent_wait_ticks(&usb_event, 0x1, FALSE, 0);

    return 0;
}
Beispiel #4
0
void Main_task(uint_32 initial_data)
{
    USB_STATUS status = USB_OK;
   
    _int_disable();

    _int_install_unexpected_isr();
    
    _usb_host_driver_install(0, (pointer)&_bsp_usb_host_callback_table);

    status = _usb_host_init
        (HOST_CONTROLLER_NUMBER,   /* Use value in header file */
        MAX_FRAME_SIZE,            /* Frame size per USB spec  */
        &host_handle);             /* Returned pointer */
    if (status != USB_OK) 
    {
        printf("USB Host Initialization failed. STATUS: %x\n", status);
        _int_enable();
        _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("Driver Registration failed. STATUS: %x\n", status);
        _int_enable();
        _task_block();
    }

    _int_enable();

    printf("\nMQX USB\nWaiting for USB device to be attached...\n");
    fflush(stdout);
   
    while (1) 
       {};
}
Beispiel #5
0
void usbh_init()
{
    USB_STATUS status = USB_OK;
   
    _int_disable();
    _usb_host_driver_install(0, (pointer)&_bsp_usb_host_callback_table);

    status = _usb_host_init
        (HOST_CONTROLLER_NUMBER,   /* Use value in header file */
        MAX_FRAME_SIZE,            /* Frame size per USB spec  */
        &host_handle);             /* Returned pointer */
    if (status != USB_OK) 
    {
        printf("USB Host Initialization failed. STATUS: %x\n", status);
        fflush(stdout);
        exit(1);
    }

    /*
    ** 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("Driver Registration failed. STATUS: %x\n", status);
        fflush(stdout);
        exit(1);
    }

    _int_enable();

    printf("\nMQX USB\nWaiting for USB device to be attached...\n");
    fflush(stdout);

    /* If a device will be attached, handler in usbh_<class>_handler.c file
    ** will catch that event
    */
}   
Beispiel #6
0
void Main_Task ( uint_32 param )
{ /* Body */
   USB_STATUS           status = USB_OK;
   uint_32              i = 0;
   _usb_host_handle     host_handle;
   boolean              ch_avail;
   
   if (NULL == (device_registered = _mem_alloc(sizeof(*device_registered))))
   {
       printf("\nMemory allocation failed");
      _task_block();
   }
   if (USB_OK != _lwevent_create(device_registered, LWEVENT_AUTO_CLEAR))
   {
       printf("\nlwevent create failed");
      _task_block();
   }
   if (MQX_OK != _lwevent_create(&acm_device.acm_event, LWEVENT_AUTO_CLEAR)) {
       printf("\nlwevent create failed");
      _task_block();
   }
   if (MQX_OK != _lwevent_create(&data_device.data_event, LWEVENT_AUTO_CLEAR)) {
       printf("\nlwevent create failed");
      _task_block();
   }
   
   /* _usb_otg_init needs to be done with interrupts disabled */
   _int_disable();

   _int_install_unexpected_isr();
   if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) {
      printf("\n Driver installation failed");
      _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 (USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle);

   if (status != USB_OK) 
   {
      printf("\nUSB Host Initialization failed. STATUS: %x", status);
      _int_enable();
      _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);
      _int_enable();
      _task_block();
   }
          
   _int_enable();
   
   if (NULL == (f_uart = fopen("ittyb:", (pointer) (IO_SERIAL_XON_XOFF | IO_SERIAL_TRANSLATION) ))) {
      printf("\nError opening ittyb:", status);
      _task_block(); /* internal error occured */
   }

   printf("\nInitialization passed. Plug-in CDC device to USB port first.\n");
   printf("Use ttyb: as the in/out port for CDC device data.\n");
   printf("This example requires that the CDC device uses HW flow.\n");
   printf("If your device does not support HW flow, then set \n");
   printf("CDC_EXAMPLE_USE_HW_FLOW in cdc_serial.h to zero and rebuild example project.\n");

   while (1) {
      uart2usb_num = usb2uart_num = 0; /* reset number of bytes in buffers */
      
      _lwevent_wait_ticks(device_registered, 0x01, TRUE, 0);
   
      if (NULL == (f_usb = fopen(device_name, (pointer) &usb_open_param))) {
          printf("\nInternal error occured");
          _task_block(); /* internal error occured */
      }
      
      while (1) {
         /* due to the fact that uart driver blocks task, we will check if char is available and then we read it */
         do {
            if (IO_OK != ioctl(f_uart, IO_IOCTL_CHAR_AVAIL, &ch_avail)) {
                printf("\nUnexpected error occured");
                _task_block(); /* unexpected error occured */
            }
            if (ch_avail == TRUE) {
               /* read data from UART */
               num_done = fread(uart2usb + uart2usb_num, sizeof(uart2usb[0]), 1, f_uart); /* read max. 1 character from UART */
               if (IO_ERROR == num_done)
               {
                  printf("\nUnexpected error occured");
                  _task_block(); /* unexpected error occured */
               }
               uart2usb_num += num_done;
            }
         } while ( (ch_avail == TRUE) && (uart2usb_num < (sizeof(uart2usb) / sizeof(uart2usb[0]))) );

         /* write them to USB */
         if (uart2usb_num) 
         {
            num_done = fwrite(uart2usb, sizeof(uart2usb[0]), uart2usb_num, f_usb);
            if (IO_ERROR == num_done) 
            {
               if (ferror(f_usb) != USBERR_NO_INTERFACE)
               {
                  printf("\nUnexpected error occured");
                  _task_block(); /* unexpected error occured */
               }
               break; /* device was detached */
            }
            for (i = num_done; i < uart2usb_num; i++) /* move buffer data, remove the written ones */
               uart2usb[i - num_done] = uart2usb[i];
            uart2usb_num -= num_done;
         }

         /* read data from USB */
         num_done = fread(usb2uart + usb2uart_num, 1, sizeof(uart2usb) / sizeof(uart2usb[0]) - usb2uart_num, f_usb); /* read characters from USB */
         if (IO_ERROR == num_done) 
         {
            if (ferror(f_usb) != USBERR_NO_INTERFACE)
            {
               printf("\nUnexpected error occured");
               _task_block(); /* unexpected error occured */
            }
            break; /* device was detached */
         }
         usb2uart_num += num_done;
         /* write them to UART */
         if (usb2uart_num) 
         {
            num_done = fwrite(usb2uart, sizeof(usb2uart[0]), usb2uart_num, f_uart);
            if (IO_ERROR == num_done)
            {
               printf("\nUnexpected error occured");
               _task_block(); /* unexpected error occured */
            }
            for (i = num_done; i < usb2uart_num; i++) /* move buffer data, remove the read ones */
               usb2uart[i - num_done] = usb2uart[i];
            usb2uart_num -= num_done;
         }
      }
   }
   _lwevent_destroy(&data_device.data_event);
   _lwevent_destroy(&acm_device.acm_event);
} /* Endbody */
Beispiel #7
0
void USB_task(uint_32 param)
{ 
   _usb_host_handle     host_handle;
   USB_STATUS           error;
   pointer              usb_fs_handle = NULL;

#if DEMO_USE_POOLS && defined(DEMO_MFS_POOL_ADDR) && defined(DEMO_MFS_POOL_SIZE)
   _MFS_pool_id = _mem_create_pool((pointer)DEMO_MFS_POOL_ADDR, DEMO_MFS_POOL_SIZE);
#endif

   _lwsem_create(&USB_Stick,0);
   _lwevent_create(&USB_Event,0);

   USB_lock();
   _int_install_unexpected_isr();
   if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) {
      printf("\n Driver installation failed");
      _task_block();
   }

   error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle);
   if (error == USB_OK) {
      error = _usb_host_driver_info_register(host_handle, (pointer)ClassDriverInfoTable);
      if (error == USB_OK) {
         error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,NULL);
      }
   }

   USB_unlock();

   if (error == USB_OK) {
      
      for ( ; ; ) {
         // Wait for insertion or removal event
         _lwevent_wait_ticks(&USB_Event,USB_EVENT,FALSE,0);

         if ( device.STATE== USB_DEVICE_ATTACHED) {

            if (device.SUPPORTED)  {
               error = _usb_hostdev_select_interface(device.DEV_HANDLE,
               device.INTF_HANDLE, (pointer)&device.CLASS_INTF);
               if(error == USB_OK) {
                  device.STATE = USB_DEVICE_INTERFACED;

                  USB_handle = (pointer)&device.CLASS_INTF;

                  // Install the file system
                  usb_fs_handle = usb_filesystem_install( USB_handle, "USB:", "PM_C1:", "c:" );
                  if (usb_fs_handle) {
                     // signal the application
                     _lwsem_post(&USB_Stick);
                  }
               }
            } else {
                device.STATE = USB_DEVICE_INTERFACED;
            }
         } else if ( device.STATE==USB_DEVICE_DETACHED) {
            _lwsem_wait(&USB_Stick);
            // remove the file system
           usb_filesystem_uninstall(usb_fs_handle);
         }

         // clear the event
         _lwevent_clear(&USB_Event,USB_EVENT);
      }
   }
}
Beispiel #8
0
void USB_task(uint_32 param)
{ 
    _usb_host_handle     host_handle;
    USB_STATUS           error;
    pointer              usb_fs_handle = NULL;
    usb_msg_t            msg;
    /* Store mounting point used. A: is the first one, bit #0 assigned, Z: is the last one, bit #25 assigned */
    uint_32              fs_mountp = 0;
   
#if DEMOCFG_USE_POOLS && defined(DEMOCFG_MFS_POOL_ADDR) && defined(DEMOCFG_MFS_POOL_SIZE)
    _MFS_pool_id = _mem_create_pool((pointer)DEMOCFG_MFS_POOL_ADDR, DEMOCFG_MFS_POOL_SIZE);
#endif

    /* This event will inform other tasks that the filesystem on USB was successfully installed */
    _lwsem_create(&USB_Stick, 0);
    
    if (MQX_OK != _lwmsgq_init(usb_taskq, 20, USB_TASKQ_GRANM)) {
        // lwmsgq_init failed
        _task_block();
    }

    USB_lock();
    _int_install_unexpected_isr();
   if (MQX_OK != _usb_host_driver_install(USBCFG_DEFAULT_HOST_CONTROLLER)) {
      printf("\n Driver installation failed");
      _task_block();
   }

    error = _usb_host_init(USBCFG_DEFAULT_HOST_CONTROLLER, &host_handle);
    if (error == USB_OK) {
        error = _usb_host_driver_info_register(host_handle, (pointer)ClassDriverInfoTable);
        if (error == USB_OK) {
            error = _usb_host_register_service(host_handle, USB_SERVICE_HOST_RESUME,NULL);
        }
    }

    USB_unlock();

    if (error != USB_OK) {
        _task_block();
    }
      
    for (;;) {
        /* Wait for event sent as a message */
        _lwmsgq_receive(&usb_taskq, (_mqx_max_type *) &msg, LWMSGQ_RECEIVE_BLOCK_ON_EMPTY, 0, 0);
         
        //if (device.STATE == USB_DEVICE_ATTACHED) {
        if (msg.body == USB_EVENT_ATTACH) {
          /* This event is not so important, because it does not inform about successfull USB stack enumeration */
        } else if (msg.body == USB_EVENT_INTF && fs_mountp != 0x3FFC)  { /* if mountpoints c: to z: are already used */

            // Install the file system, use device->ccs as a handle
            usb_fs_handle = usb_filesystem_install( (pointer) msg.ccs, "USB:", "PM_C1:", "c:");
                  
            if (usb_fs_handle) {
                DEVICE_STRUCT_PTR dsp = (DEVICE_STRUCT_PTR) msg.ccs;
                dsp->mount = 'c';

                // Mark file system as mounted
                fs_mountp |= 1 << (dsp->mount - 'a');
                // Unlock the USB_Stick = signal to the application as available
                _lwsem_post(&USB_Stick);
            }
        } else if (msg.body == USB_EVENT_DETACH) {
            DEVICE_STRUCT_PTR dsp = (DEVICE_STRUCT_PTR) msg.ccs;

            if (dsp->mount >= 'a' && dsp->mount <= 'z') {
                // Lock the USB_Stick = mark as unavailable
                _lwsem_wait(&USB_Stick);

                // Remove the file system 
                usb_filesystem_uninstall(usb_fs_handle);
                // Mark file system as unmounted
                fs_mountp &= ~(1 << (dsp->mount - 'a'));
            }

            /* Here, the device finishes its lifetime */            
            _mem_free(dsp);
        }
    }
}
Beispiel #9
0
void Main_Task ( uint_32 param )
{ /* Body */
 
   USB_STATUS           status = USB_OK;

   _int_disable();

   _int_install_unexpected_isr();
   status = _usb_host_driver_install(HOST_CONTROLLER_NUMBER, (pointer)&_bsp_usb_host_callback_table);
   if (status) {
      _int_enable();
      printf("ERROR: %ls", status);
      exit(1);
   } /* Endif */

   status = _usb_host_init
      (HOST_CONTROLLER_NUMBER,    /* Use value in header file */
       MAX_FRAME_SIZE,            /* Frame size per USB spec  */
       &host_handle);             /* Returned pointer */

   if (status != USB_OK) {
      _int_enable();
      printf("\nUSB Host Initialization failed. STATUS: %x", status);
      fflush(stdout);
      goto Error_Cleanup;
   } /* Endif */

   /*
   ** 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) {
      _int_enable();
      printf("\nDriver Registration failed. STATUS: %x", status);
      fflush(stdout);
      goto Error_Cleanup;
   }

   _int_enable();

   pCmd = (COMMAND_OBJECT_PTR) USB_mem_alloc_zero(sizeof(COMMAND_OBJECT_STRUCT));
   
   if (pCmd == NULL)
   {
      printf ("\nUnable to allocate Command Object");
      fflush (stdout);
      goto Error_Cleanup;
   }

   pCmd->CBW_PTR = (CBW_STRUCT_PTR) USB_mem_alloc_zero(sizeof(CBW_STRUCT));
   
   if (pCmd->CBW_PTR == NULL)
   {
      printf ("\nUnable to allocate Command Block Wrapper");
      fflush (stdout);
      goto Error_Cleanup;
   }

   pCmd->CSW_PTR = (CSW_STRUCT_PTR) USB_mem_alloc_zero(sizeof(CSW_STRUCT));
   
   if (pCmd->CSW_PTR == NULL)
   {
      printf ("\nUnable to allocate Command Status Wrapper");
      fflush (stdout);
      goto Error_Cleanup;
   }

   buff_in = (uchar_ptr)USB_mem_alloc_uncached(0x400C);
   
   if (buff_in == NULL)
   {
      printf ("\nUnable to allocate Input Buffer");
      fflush (stdout);
      goto Error_Cleanup;
   }
   _mem_zero(buff_in, 0x400C);

   buff_out = (uchar_ptr) USB_mem_alloc_uncached(0x0F);
   
   if (buff_out == NULL)
   {
      printf ("\nUnable to allocate Output Buffer");
      fflush (stdout);
      goto Error_Cleanup;
   }
   _mem_zero(buff_out, 0x0F);

   printf("\nPlease insert Mass Storage Device.\n");
   /*----------------------------------------------------**
   ** Infinite loop, waiting for events requiring action **
   **----------------------------------------------------*/
   for ( ; ; ) {
      switch ( mass_device.dev_state ) {
         case USB_DEVICE_IDLE:
            break ;
         case USB_DEVICE_ATTACHED:
            printf( "Mass Storage Device Attached\n" );
            fflush(stdout);
            mass_device.dev_state = USB_DEVICE_SET_INTERFACE_STARTED;
            status = _usb_hostdev_select_interface(mass_device.dev_handle,
                 mass_device.intf_handle, (pointer)&mass_device.class_intf);
            break ;
         case USB_DEVICE_SET_INTERFACE_STARTED:
            break;
         case USB_DEVICE_INTERFACED:
            usb_host_mass_test_storage();
            mass_device.dev_state = USB_DEVICE_OTHER;
            break ;
         case USB_DEVICE_DETACHED:
            printf ( "\nMass Storage Device Detached\n" );
            fflush(stdout);
            mass_device.dev_state = USB_DEVICE_IDLE;
            break;
         case USB_DEVICE_OTHER:
            break ;
         default:
            printf ( "Unknown Mass Storage Device State = %d\n",\
               mass_device.dev_state );
            fflush(stdout);
            break ;
      } /* Endswitch */
   } /* Endfor */
   
   Error_Cleanup:
   {
      if (pCmd != NULL)
      {
         if (pCmd->CSW_PTR != NULL)
         {
            USB_mem_free(pCmd->CSW_PTR);
         }
         
         if (pCmd->CBW_PTR != NULL)
         {
            USB_mem_free(pCmd->CBW_PTR);
         }
         
         USB_mem_free(pCmd);
      }
      
      if (buff_in != NULL)
      {
         USB_mem_free(buff_in);
      }
      
      if (buff_out != NULL)
      {
         USB_mem_free(buff_out);
      }
      
      if (host_handle != NULL)
      {
         _usb_host_shutdown (host_handle);
      }
   }

} /* Endbody */
Beispiel #10
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;
        }
    }
}