Best Way to use USB

What could be included in further releases, or for the forum.
Post Reply
jbaumann
Posts: 10
Joined: Sun Jul 15, 2018 6:08 pm

Best Way to use USB

Post by jbaumann » Tue Jul 17, 2018 8:44 pm

Hi,

it seems that with the STM32 environment there are lots of different possibilities to use the USB port. But what I am missing is a simple overview which way to utilize USB has which advantages and disadvantages(ease of use vs. low overhead, functionality etc.), or even a comprehensive overview of how to access USB with different libraries (with examples would be fantastic).

Putting this on the Wiki could be a good guide for everyone I think :-)

Cheers, Joe

jbaumann
Posts: 10
Joined: Sun Jul 15, 2018 6:08 pm

Re: Best Way to use USB

Post by jbaumann » Thu Jul 19, 2018 3:25 pm

Ok, then I'll start with what I have found out so far. I'm using the BlinknCount example...

With -DSERIAL_USB or the STM32Duino Bootloader

Maps Serial to USB (using the Maple core)

Code: Select all

int n = 0;
void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
	Serial.begin(9600);
	while (!Serial) {
		digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
		delay(100);         // fast blink
	}
	Serial.println("Blink LED & count Demo");
}
void loop() {
	digitalWrite(LED_BUILTIN, HIGH);   // set the LED on
	delay(500);              // wait for a second
	digitalWrite(LED_BUILTIN, LOW);    // set the LED off
	Serial.print("Serial Loop #: ");
	n++;
	Serial.println(n);
	delay(500);              // wait
}
With USBComposite library written by arpruss

Code: Select all

#include 
int n = 0;
extern USBCompositeSerial CompositeSerial;
void setup() {
	USBComposite.clear(); // clear any plugins previously registered
	CompositeSerial.registerComponent();
	USBComposite.begin();
	while (!CompositeSerial) {
		digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
		delay(100);         // fast blink
	}
	pinMode(LED_BUILTIN, OUTPUT);
	CompositeSerial.println("Blink LED & count Demo");
}
void loop() {
	digitalWrite(LED_BUILTIN, HIGH);   // set the LED on
	delay(500);              // wait for a second
	digitalWrite(LED_BUILTIN, LOW);    // set the LED off
	CompositeSerial.print("Serial Loop #: ");
	n++;
	CompositeSerial.println(n);
	delay(500);              // wait
}

alw1746
Posts: 5
Joined: Sat Jul 28, 2018 7:10 am
Location: Sydney, Australia

Re: Best Way to use USB

Post by alw1746 » Mon Oct 08, 2018 4:31 am

I use the following to create a simple serial port with a #undef to revert to standard serial if necessary.

Code: Select all

#include 
#define Serial uSerial     //replace with #undef Serial if using standard Serial
#ifdef Serial
  USBCompositeSerial Serial;
#endif
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
  Serial.println("Before HI");
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  Serial.println("Before LO");
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
However,I notice the virtual com port in Windows works the first time (in Serial Monitor or putty), but errors out on subsequent uploads

Code: Select all

Error while setting serial port parameters: 9,600 N 8 1
when you connect to it. The only way to recover is power cycle the STM32 board. Has anyone else come across this?

User avatar
mrburnette
Posts: 3001
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Best Way to use USB

Post by mrburnette » Mon Oct 08, 2018 2:17 pm

Joe:

As you develop this thread, if you want to convert to a WiKi, PM Roger Clark for a WiKi UID.

IMO, the thread is adequate unless it gets too long and has too many "interruptions." However, you may wish to have members interact with you to develop your concepts/ideas or to validate same. Wiki's are ideal for instructional or educational materials but in the end, a long thread could be easily translated to a WiKi topic.

Please remember that we do have a USB HID library, so that covers many uses beyond the basic/default use of programmingt the board and configuring a serial console under the IDE.


Ray

ag123
Posts: 1322
Joined: Thu Jul 21, 2016 4:24 pm

Re: Best Way to use USB

Post by ag123 » Mon Oct 08, 2018 7:05 pm

i've left this piece in code snippets which turns a BP/MM into a usb-usart dongle
viewtopic.php?f=18&t=2752

arpruss
Posts: 282
Joined: Sat Sep 30, 2017 3:34 am

Re: Best Way to use USB

Post by arpruss » Thu Oct 18, 2018 3:14 am

alw1746 wrote:
Mon Oct 08, 2018 4:31 am
However,I notice the virtual com port in Windows works the first time (in Serial Monitor or putty), but errors out on subsequent uploads
when you connect to it. The only way to recover is power cycle the STM32 board. Has anyone else come across this?
I tried your code and it worked just fine on my Win10 laptop in Serial Monitor.

Post Reply