Oleg Le

LPC1768 usb OTG + external hid keyboard  - problem

Discussion created by Oleg Le on Jan 16, 2017
Latest reply on Sep 7, 2017 by 承 王

Hello, Freinds!
The problem with enumerating HID keyboard on the LPC1768. I connect the keyboard to the USB LPC1768 host. I am using the code library from Keil. At power LCP1768 start working with the keyboard correctly. But if I extract the keyboard and connect keyboard again - its doesnt work. Maybe its impossible to hot connection of keyboard to MCU?
My code below:

void USBH_Thread (void const *arg) {
char con = 0; /* Connection status of keyboard */
char con_ex = 40; /* Previous connection status + initial time in 100 ms intervals for initial display */
uint8_t out[64]; /* Output to keyboard LEDs */
out[0]=1;
USBH_Initialize (0); /* Initialize USB Host 0 */

while (1) {
   con = USBH_HID_GetDeviceStatus(0) == usbOK; /* Get kbd connection status */

   if ((con ^ con_ex) & 1) { /* If connection status changed */
   if (con) {
      USBH_HID_Write (0,(uint8_t *)&out,1);/* Turn on NUM LED */
      printf ("\nKeyboard connected\n");
   } else {
      printf ("\nKeyboard disconnected ...\n");
   }
   con_ex = con;
   } else if (con_ex > 1) {
   con_ex -= 2; /* Decrement initial time */
   if ((con_ex <= 1) && (!con)) { 
      printf ("\nNo keyboard connected ... \n");
      con_ex = con;
   } else {
      osDelay(200);
   }
   }
   osDelay(100);
   }
}
osThreadDef(USBH_Thread, osPriorityNormal, 1, NULL);

Outcomes