GrumpyOldPizza / arduino-STM32L4

69 stars 60 forks source link

Method to check for USB_VBUS #49

Closed jacky4566 closed 4 years ago

jacky4566 commented 4 years ago

Is there a method to check for USB_VBUS?

I see it is tied to GPIO_PIN_PB2. I tried to read pin 27 but it just returns false.

My intention is to shutdown the device when USB is not connected.

GrumpyOldPizza commented 4 years ago

The USB device is shut down already as long as there is no USB_VBUS detected. So what you are asking for is already implemented.

jacky4566 commented 4 years ago

Right but how do I check for that in my program? Otherwise nothing gets printed because the device is sleeping.


 void setup() {
   Serial.begin(9600);
 }

 void loop() {
   if (/*Check for USB*/) {
     Serial.println(STM32.getTemperature());
     delay(1000);
  } else {
     STM32.stop(1000);
   }
 }
kriswiner commented 4 years ago

VBUS = STM32L0.getVBUS();

On Wed, Oct 9, 2019 at 12:33 PM jacky4566 notifications@github.com wrote:

Right but how do I check for that in my program? Otherwise nothing gets printed because the device is sleeping.

void setup() { Serial.begin(9600); }

void loop() { if (/Check for USB/) { Serial.println(STM32.getTemperature()); delay(1000); } else { STM32.stop(1000); } }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/49?email_source=notifications&email_token=ABTDLKUNYPELL5SAGA7DSI3QNYW2JA5CNFSM4I7DWTRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAZB3SQ#issuecomment-540155338, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKQQCZHQHTDEXFQYEWLQNYW2JANCNFSM4I7DWTRA .

jacky4566 commented 4 years ago

We are talking about the L4 here. That function doesn't exist in STM32.h

jacky4566 commented 4 years ago

I found a workaround by changing the variant.cpp file for my board. Changing Null to GPIOB allows me to read the pin. Is there any disadvantage to doing this?

https://github.com/GrumpyOldPizza/arduino-STM32L4/blob/ac659033eadd50cfe001ba1590a1362b2d87bb76/variants/STM32L433CC-Butterfly/variant.cpp#L87

kriswiner commented 4 years ago

USBConnected = USBDevice.connected(); on L4...

jacky4566 commented 4 years ago

Great that seems to work. This should be in the documentation.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, USBDevice.connected());
}