This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Detecting Pen Drive Insertion on LPC1768

Hello

I am using LPC1768 for one of the project and i need to detect if the USB device is plugged in. How is it possible that i detect something like a pen drive being inserted.

I will be using the controller to interact to the PC as well as with a pendrive and i dont intend to use OTG. I need to have a option which will be able to detect the connection type.

Regards
Prakash

Parents
  • Hello Tsuneo,

    I made the hardware change as you suggest for Direct connection option.

    The code is as follows.

      if(IS_USB_CABLE_CONNECTED())
      {
        if(!(device_and_host_switch_flag == DEVICE))
        {
          LPC_USB->OTGStCtrl = 0;
          enumerated_flag = DEVICE;
          USB_Init();       // USB Initialization
          USB_Connect(TRUE);        // USB Connect
          while(!USB_Configuration);        // wait until USB is configured
          device_and_host_switch_flag = DEVICE;
        }
      }
      else if(is_usb_stick_connected())
      {
        if(!(device_and_host_switch_flag == HOST))
        {
          enumerated_flag = HOST;
          Host_Init();      /* Initialize the lpc17xx host
          device_and_host_switch_flag = HOST;
          /* Do data transfer */
          LPC_GPIO0->FIOSET  = BIT4 | BIT5; /*switch on trasistors connected to D+ and D- /*
          rc = Host_EnumDev(); /* Enumerate the device connected*/
          if (rc == OK)
          {
            /* Initialize the mass storage and scsi interfaces */
            rc = MS_Init( &blkSize, &numBlks, inquiryResult );
            if (rc == OK)
            {
              rc = FAT_Init(); /* Initialize the FAT16 file system */
              if (rc == OK)
              {
                Main_Copy();        //Copy MSREAD.TXT to MSWRITE.TXT
              }
            }
          }
          LPC_GPIO0->FIOCLR = BIT4 | BIT5;/*switch off trasistors connected to D+ and D-  */
        }
      }
      else
      {
        device_and_host_switch_flag = USB_NONE;
      }
    

    The USB Device is enumerated fine for above code. But when I remove the USB cable and connect the USB stick host fails to enumerate.

    If swap the LPC_GPIO0->FIOSET = BIT4 | BIT5; /* and
    LPC_GPIO0->FIOCLR = BIT4 | BIT5; /* */ then HOST is enumerate fine but during USB_Init(); the code gets abort.

    After detecting the USB stick, software should turn on the both trasistors before transferring data and after completion of data transfer software should turn off the both trasistors right?

    -Prakash

Reply
  • Hello Tsuneo,

    I made the hardware change as you suggest for Direct connection option.

    The code is as follows.

      if(IS_USB_CABLE_CONNECTED())
      {
        if(!(device_and_host_switch_flag == DEVICE))
        {
          LPC_USB->OTGStCtrl = 0;
          enumerated_flag = DEVICE;
          USB_Init();       // USB Initialization
          USB_Connect(TRUE);        // USB Connect
          while(!USB_Configuration);        // wait until USB is configured
          device_and_host_switch_flag = DEVICE;
        }
      }
      else if(is_usb_stick_connected())
      {
        if(!(device_and_host_switch_flag == HOST))
        {
          enumerated_flag = HOST;
          Host_Init();      /* Initialize the lpc17xx host
          device_and_host_switch_flag = HOST;
          /* Do data transfer */
          LPC_GPIO0->FIOSET  = BIT4 | BIT5; /*switch on trasistors connected to D+ and D- /*
          rc = Host_EnumDev(); /* Enumerate the device connected*/
          if (rc == OK)
          {
            /* Initialize the mass storage and scsi interfaces */
            rc = MS_Init( &blkSize, &numBlks, inquiryResult );
            if (rc == OK)
            {
              rc = FAT_Init(); /* Initialize the FAT16 file system */
              if (rc == OK)
              {
                Main_Copy();        //Copy MSREAD.TXT to MSWRITE.TXT
              }
            }
          }
          LPC_GPIO0->FIOCLR = BIT4 | BIT5;/*switch off trasistors connected to D+ and D-  */
        }
      }
      else
      {
        device_and_host_switch_flag = USB_NONE;
      }
    

    The USB Device is enumerated fine for above code. But when I remove the USB cable and connect the USB stick host fails to enumerate.

    If swap the LPC_GPIO0->FIOSET = BIT4 | BIT5; /* and
    LPC_GPIO0->FIOCLR = BIT4 | BIT5; /* */ then HOST is enumerate fine but during USB_Init(); the code gets abort.

    After detecting the USB stick, software should turn on the both trasistors before transferring data and after completion of data transfer software should turn off the both trasistors right?

    -Prakash

Children
No data