Arm Mbed OS support forum

USBMSD check USB connected LPC1768

Hi Everyone,
I hope you can help me with an issue in USB port. I am using LPC1768 with USB API USBMSD and I need to detect when USB is connected to PC in order to make a change of context in my program. I am using MBED Studio IDE with Mbed OS 6.6.0. I am trying to use USBMSD::connect() but I don’t know very well how use this method.

Thanks for your help.
Best Regards

Hello,

How I remember, first what you need is set USBMSD to non blocking mode in its constructor.

USBMSD usb(&sd, false);

And then somewhere (it is depend on application) periodically call the connect method until it is not true.

    while (true){ 
        if(usb.connect()){
            // your code for usb
        }
        thread_sleep_for(500);
    }

Also you can do it in a separate thread instead of the main and so on.

BR, Jan

Hi @JohnnyK thanks for your reply. I implemented your code but i had unexpected behavior. The program enters to “if” statement always, when i have or not the usb cable connected to pc. I have the next thread implemented.

SDBlockDevice sd(p5, p6, p7, p8);
FATFileSystem fs(“fs”);
USBMSD usb(&sd, false);

static DigitalOut led3(LED3);

void USB_thread()
{
while(true)
{
printf(“Thread USB.\r\n”);
if(usb.connect())
{
led3 = 1;
printf(“USB conectado.\r\n”);
}
thread_sleep_for(500);
}
}

I don’t know what I am doing wrong.

Best Regards

Ye, you are right, I was wrong and it’s nonsense. Sorry.

BR, Jan

Don’t worry. I think the best option is to check the VBUS pin on USB port.

Best Regards

I do not know if is something similar is possible with the LPC1768. But with Nucleo-F429ZI is possible to detect connected USB cable via PA_9 pin what is connected to VBUS .

So if it is not possible with the LPC, then you maybe can do it with 5V from USB cable as external interrupt or digital input. For safety by means of a transistor maybe.

For example with Interrupts - click here
#include "mbed.h" // MbedOS 6.5 with Nucleo-F429ZI
#include "SDBlockDevice.h"
#include "USBMSD.h"

DigitalOut led1(LED1);
SDBlockDevice sd(PC_12, PC_11, PC_10, PC_9);
USBMSD usb(&sd, false);
InterruptIn usbVbus(PA_9);
volatile bool isConnected = DigitalIn(PA_9).read();

void riseVbus(){
    isConnected = true;
}

void fallVbus(){
    isConnected = false;
}

int main(){
    printf("Start\n");

    usbVbus.rise(&riseVbus);
    usbVbus.fall(&fallVbus);

    thread_sleep_for(500);
    bool conn = false;
    while(true){
        if(isConnected && !conn){
            printf("USB cable is connected!\n");
            conn = usb.connect();
        }
        if(!isConnected && conn){
            printf("USB cable is not connected!\n");
            conn = false;
            usb.disconnect();
        }
        if(conn){
            usb.process();
        } else{
            thread_sleep_for(500);
            led1 = !led1;
        }
    }
    return 0;
}
For example without Interrupts - click here
#include "mbed.h" // MbedOS 6.5 with Nucleo-F429ZI
#include "SDBlockDevice.h"
#include "USBMSD.h"

DigitalOut led1(LED1);
SDBlockDevice sd(PC_12, PC_11, PC_10, PC_9);
USBMSD usb(&sd, false);
DigitalIn usbVbus(PA_9);

int main(){
    printf("Start\n");

    thread_sleep_for(500);
    bool conn = false;
    while(true){
        if(usbVbus.read() && !conn){
            printf("USB cable is connected!\n");
            conn = usb.connect();
        }
        if(!usbVbus.read() && conn){
            printf("USB cable is not connected!\n");
            conn = false;
            usb.disconnect();
        }
        if(conn){
            usb.process();
        } else{
            thread_sleep_for(500);
            led1 = !led1;
        }
    }
    return 0;
}

BR, Jan

Ah, we wrote post probably same time. I also wrote about the VBUS above.

BR, Jan

1 Like

I finally implemented pin p19 as DigitalIn and everything works well in order to detect when usb cable was connected (without interrupt). Thanks for your reply.

Wanted to implement the same on another target.
I think both of JohnnyK’s examples should be placed in the documentation of the USBMSD API.

1 Like