USB Composite + Roger Clark's Core, Serial over USB

Post here all questions related to LibMaple core if you can't find a relevant section!
jhsa
Posts: 26
Joined: Sat Jan 08, 2022 12:10 am

USB Composite + Roger Clark's Core, Serial over USB

Post by jhsa »

Hey guys, I did search this forums before posting. As the title suggests I am trying to add USB Serial using the USB Composite library to my project that runs on a STM32 Blue Pill development board.
I have tried the "MultiSerial" example but it doesn't seem to do anything.
What I need is basic Serial communication between my project and a computer via USB Serial. Could someone please point me in the right direction?
I already use the USB Midi provided by this library. But need also normal serial for configurations between an App on my PC and the Blue Pill board.
I am a beginner, so please be gentle ;) :)
Thank you...

João
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by ag123 »

For usb rather than using 'libraries', at some point one should go back to 'first principles' to figure out how to implement a usb protocol from scratch.
this would help you understand the codes at least.
the usb protocols are mostly documented at
https://www.usb.org/documents
it partly has to do with 'device descriptors', 'device descriptors' tells the host what to expect of the device.
Normally, there is a hierarchy where you have the device and within each device there can be multiple interfaces, so there could be multiple different device/interface descriptors.
device/interface descriptors are merely a bunch of data, you could literally hard code it in structures and send it when a get_descriptor usb request is received.
but 'descriptors' alone merely describe the interface. so the next thing is you'd need to respond on the relevant usb endpoints that you declared your descriptors for. this is the usb stack that ST provides a driver for as well.

if you have a usb sniffer, e.g. wireshark
https://wiki.wireshark.org/CaptureSetup/USB
you could observe how the comms occur and how the descriptors are declared. the device class specs can be found on
https://www.usb.org/documents
normally, i've tried it and for usb-serial (cdc acm) there are basically 3 bulk in out endpoints used, one endpoint is IN for sending data to the host, one is OUT for receiving data from the host. and a 3rd i think is a control endpoint.
if you need to implement multiple serial connections, more of the sets of similar endpoints is needed.
you would also need an implementation to handle those multiple endpoints. i'm not sure how that is done in usb-composite, you may like ot use these as clues and review the codes.
jhsa
Posts: 26
Joined: Sat Jan 08, 2022 12:10 am

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by jhsa »

As I said, I wouldn't be able to write this from scratch.. Total beginner here, remember? :P
I am already using this library for Midi, so I could perhaps just use it also for normal serial. Need to find out how though. I am trying this, but it doesn't even register.. perhaps some "class" or whatever it is called is missing? :o
Thanks

Code: Select all

#include <USBComposite.h>

const char ManufacturerName[] = "Just Me";
const char DeviceName[] = "My Device";
const char DeviceSerial[] = "00000000000000000001";

USBCompositeSerial CompositeSerial;


uint32_t counter = 0;
void setup()
{

  pinMode(PA11, OUTPUT);
  pinMode(PA12, OUTPUT);
  digitalWrite(PA11, LOW);
  digitalWrite(PA12, LOW);
  delay(10);
  pinMode(PA11, INPUT);
  pinMode(PA12, INPUT);
  
  
   USBComposite.setManufacturerString(ManufacturerName);
  USBComposite.setProductString(DeviceName);
  USBComposite.setSerialString(DeviceSerial);
  //USBComposite.setVendorId(VendorId);
  //USBComposite.setProductId(ProductId);

  USBComposite.setProductId(0x0036);
  CompositeSerial.registerComponent();
  USBComposite.begin();
  delay(2000);
}
void loop()
{
  CompositeSerial.print("Counter: ");
  CompositeSerial.print(counter);
  CompositeSerial.println(" - Hello World!");
  counter++;
  delay(500);
}
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by Bakisha »

Can you check your previous thread and look for my first post there for example? That should give you both midi and usb virtual com port over one usb cable. I used st-link for upload, not stm32duino bootloader.
arpruss
Posts: 83
Joined: Sat Dec 21, 2019 10:06 pm

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by arpruss »

If all you want is a single serial stream over USB, you should just use Serial, instead of USBComposite. USBComposite is overkill for that.
jhsa
Posts: 26
Joined: Sat Jan 08, 2022 12:10 am

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by jhsa »

Bakisha wrote: Mon Jan 17, 2022 4:36 am Can you check your previous thread and look for my first post there for example? That should give you both midi and usb virtual com port over one usb cable. I used st-link for upload, not stm32duino bootloader.
Ok, here is your code from the other thread:

Code: Select all

#include <USBComposite.h>

#define SPEAKER_PIN PA8


class myMidi : public USBMIDI {
    virtual void handleNoteOff(unsigned int channel, unsigned int note, unsigned int velocity) {
      digitalWrite(PB12, HIGH);
            CompositeSerial.print("ON - ");
            CompositeSerial.print(channel, HEX);
            CompositeSerial.print(" ");
            CompositeSerial.print(note, HEX);
            CompositeSerial.print(" ");
            CompositeSerial.println(velocity, HEX);
      noTone(SPEAKER_PIN);
      digitalWrite(PB12, LOW);
    }
    virtual void handleNoteOn(unsigned int channel, unsigned int note, unsigned int velocity) {
      digitalWrite(PB12, HIGH);
      CompositeSerial.print("OFF- ");
      CompositeSerial.print(channel, HEX);
      CompositeSerial.print(" ");
      CompositeSerial.print(note, HEX);
      CompositeSerial.print(" ");
      CompositeSerial.println(velocity, HEX);
      tone(SPEAKER_PIN, (midiNoteFrequency_10ths[note] + 5) / 10);
      digitalWrite(PB12, LOW);
    }

};
myMidi midi;

USBCompositeSerial CompositeSerial;

void setup() {
  pinMode(PB12, OUTPUT);
  USBComposite.setProductId(0x0020);
  pinMode(SPEAKER_PIN, OUTPUT);
  midi.registerComponent();
  CompositeSerial.registerComponent();
  USBComposite.begin();

}
void loop() {
  midi.poll();
}
I can see that to send Serial I could use " CompositeSerial.print( )"
But how to receive serial that is not Midi? And without the Calbacks?
Thanks

João
jhsa
Posts: 26
Joined: Sat Jan 08, 2022 12:10 am

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by jhsa »

arpruss wrote: Fri Jan 21, 2022 3:04 am If all you want is a single serial stream over USB, you should just use Serial, instead of USBComposite. USBComposite is overkill for that.
No, what I really want is to sometimes use normal serial over USB, for configuring settings for example, and in normal operation use the USB for Midi.
I am developing my own DIY guitar effects switcher which also sends and receives MIDI.
I am developing a windows app so I can configure it from my PC.
I opened an issue on the library Github page a couple days ago. here is the link.
Thanks

https://github.com/arpruss/USBComposite ... issues/104
User avatar
Bakisha
Posts: 139
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by Bakisha »

I created some simple example sketch to deal with both midi and virtual COM port. I hope it is helpful for you.

Code: Select all

#include <USBComposite.h>

uint32_t counter = 0;

const char ManufacturerName[] = "Just Me";
const char DeviceName[] = "My Device";
const char DeviceSerial[] = "00000000000000000001";

class myMidi : public USBMIDI {
    virtual void handleNoteOff(unsigned int channel, unsigned int note, unsigned int velocity) {

      CompositeSerial.print("ON - ");
      CompositeSerial.print(channel, HEX);
      CompositeSerial.print(" ");
      CompositeSerial.print(note, HEX);
      CompositeSerial.print(" ");
      CompositeSerial.println(velocity, HEX);

    }
    virtual void handleNoteOn(unsigned int channel, unsigned int note, unsigned int velocity) {

      CompositeSerial.print("OFF- ");
      CompositeSerial.print(channel, HEX);
      CompositeSerial.print(" ");
      CompositeSerial.print(note, HEX);
      CompositeSerial.print(" ");
      CompositeSerial.println(velocity, HEX);

    }

};
myMidi midi;

USBCompositeSerial CompositeSerial;

void setup() {

  pinMode(PA11, OUTPUT);
  pinMode(PA12, OUTPUT);
  digitalWrite(PA11, LOW);
  digitalWrite(PA12, LOW);
  delay(10);
  pinMode(PA11, INPUT);
  pinMode(PA12, INPUT);


  USBComposite.setManufacturerString(ManufacturerName);
  USBComposite.setProductString(DeviceName);
  USBComposite.setSerialString(DeviceSerial);
  //USBComposite.setVendorId(VendorId);
  //USBComposite.setProductId(ProductId);
  midi.registerComponent();
  CompositeSerial.registerComponent();
  USBComposite.begin();

}
void loop() {

  if (CompositeSerial.available() > 0) {
    if (CompositeSerial.read() == 0x0D) { //ascii code for carriage return (CR)
      CompositeSerial.print("Counter: ");
      CompositeSerial.print(counter);
      CompositeSerial.println(" - Hello World!");
      counter++;
    }
  }

  midi.poll();
}
This is the result:
image0001.jpg
image0001.jpg (91.04 KiB) Viewed 4140 times
Uploaded with ST-LINK V2...
jhsa
Posts: 26
Joined: Sat Jan 08, 2022 12:10 am

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by jhsa »

Oh, this is great. I will give it a try tomorrow. Thank you so much..
Could both be used at the same time? Or should I find a way to deactivate one when I use the other.. It can be in Setup as I only have one USB cable I won't use both obviously :)

João
jhsa
Posts: 26
Joined: Sat Jan 08, 2022 12:10 am

Re: USB Composite + Roger Clark's Core, Serial over USB

Post by jhsa »

Funny, it detects the driver for the midi device, but it doesn't give me a COM port for the serial :o

João
Post Reply

Return to “General discussion”