lathoub / Arduino-USBMIDI

Allows a microcontroller, with native USB capabilities, to appear as a MIDI device over USB to a connected computer
MIT License
148 stars 11 forks source link

ESP32 S2 and S3 support?? #18

Open jhsa opened 1 year ago

jhsa commented 1 year ago

Would it be possible to make this library work with the ESP32 S2 and S3? Both support native USB. Thanks

lathoub commented 1 year ago

I don't have the hardware, it crossed my mind, but hoping other would contribute - you? (What underlying lib from S2 or S3 can we use?)

jhsa commented 1 year ago

I believe this one can do it as well. But I do prefer the way you library handles the MIDI, based on the arduino midi library which is quite intuitive for a beginner like me. About the hardware, are you in Europe?

https://github.com/adafruit/Adafruit_TinyUSB_Arduino

lathoub commented 1 year ago

I’ll have a look at the Tiny-USB API (Yes, Europe based (Belgium))

jhsa commented 1 year ago

Cool, I am in Germany. Is there a way we can send Private messages here?

jhsa commented 1 year ago

The ESP32 S2 can do WiFi and native USB but it cannot do BLE Midi unfortunately. The ESP32 S3 should be able to do all of them. This board seems to be quite new and it is still a bit expensive. I am still waiting for it. I do have a couple ESP32 S2 Mino though.

DonWT commented 11 months ago

Is there anything new known about USB MIDI on the ESP32-S3?

I want to build a MIDI instrument that can switch between BLE MIDI and USB MIDI. I am hoping the S3 can do this.

lathoub commented 11 months ago

I did found this and looks promising (albeit USB only), but i never tried - can you try?

DonWT commented 11 months ago

I don't have an S3 right now but I will order one and give it a try.

jhsa commented 11 months ago

I am successfully using Serial, USB, BLE, and RTP Midi on an S3. All in the same project. For the USB I am using the library I have linked above. "TinyUSB" I do love the ESP32-S3 board by the way. Very versatile.

lathoub commented 11 months ago

Cool! Can you post the code on how to use TinyUSB with this lib?

jhsa commented 11 months ago

Cool! Can you post the code on how to use TinyUSB with this lib?

Well, I believe I use the "TinyUSB" library instead of this one, not "with" this one. Also, I think I am using the "TinyUSB" library because I couldn't make this one work at the time. Don't know if anything changed lately. But I can post how I use it. The following is just an example:

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>

// USB MIDI object
Adafruit_USBD_MIDI usb_midi;

// Create a new instance of the Arduino MIDI Library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MidiUsb);

// Create a new instance of the Arduino MIDI Library for serial
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, DIN_MIDI);

void setup() {
//************ USB MIDI init ***************
  TinyUSBDevice.setManufacturerDescriptor("My_Manufacturer");
  TinyUSBDevice.setProductDescriptor("iL9_USB");

  MidiUsb.begin(MIDI_CHANNEL_OMNI);

  MidiUsb.setHandleProgramChange(USB_ProgramChange);
  MidiUsb.setHandleControlChange(USB_ControlChange);
}

void USB_ProgramChange(byte channel, byte program) {
// Do whatever needs to be done when receiving Program Change.
}

void USB_ControlChange(byte channel, byte controller, byte value)
{
// Do whatever needs to be done when receiving Control Change.
}

void loop() {

MidiUsb.read();

}

And to send MIDI via USB:

MidiUsb.sendControlChange(Control, Value, Channel);  // To send Control change messages

MidiUsb.sendProgramChange(Program , Channel);    // To send Program change messages

The picture shows the Boards configuration I use to make it work.

ESP32-S3_IDE Configuration

lathoub commented 11 months ago

Looks like TinyUSB exposes a serial interface (rather than a USB device). Good to know Thx for the research

DonWT commented 10 months ago

I am successfully using Serial, USB, BLE, and RTP Midi on an S3. All in the same project. For the USB I am using the library I have linked above. "TinyUSB" I do love the ESP32-S3 board by the way. Very versatile.

Would you please tell me which board you are using.

jhsa commented 10 months ago

I am successfully using Serial, USB, BLE, and RTP Midi on an S3. All in the same project. For the USB I am using the library I have linked above. "TinyUSB" I do love the ESP32-S3 board by the way. Very versatile.

Would you please tell me which board you are using.

ESP32-S3 N16R8 It has two USB type C connectors.

https://www.aliexpress.com/item/1005004629274672.html?spm=a2g0o.order_list.order_list_main.355.23bb1802Wew09f

DonWT commented 10 months ago

Would you please tell me which board you are using.

ESP32-S3 N16R8 It has two USB type C connectors.

https://www.aliexpress.com/item/1005004629274672.html?spm=a2g0o.order_list.order_list_main.355.23bb1802Wew09f

Thank you.

DonWT commented 10 months ago

Here are my findings so far.

I am using a very similar board to jhsa - a cheap clone of the Espressif ESP32-S3-DevKitC-1 (note not the current version 1.1): https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/hw-reference/esp32s3/user-guide-devkitc-1-v1.0.html

Arduino 2.2.1 with the following board settings:

image
Basically, the board has to be in USG-OTG mode.

Connected to the USB-OTG port on the board (labelled USB underneath the board), nothing on the USB to Serial port (labelled COM).

image

(Working with this board requires a lot of button presses so I brought out the Boot and Reset buttons to make it easier to use. I added a direct connection to D+ D- to see if that made any difference and it did not).

What works:

1) The Adafruit TinyUSB midi_test at https://github.com/adafruit/Adafruit_TinyUSB_Arduino/blob/master/examples/MIDI/midi_test/midi_test.ino with one modification: I needed to add a Delay(1000) call after the Serial.begin(115200), without this the sketch just hangs.

2) This minimal sketch:

include

include

include

// USB MIDI object Adafruit_USBD_MIDI usb_midi;

// Create a new instance of the Arduino MIDI Library, // and attach usb_midi as the transport. MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);

void setup() { Serial.begin(115200); delay(1000); MIDI.begin(); }

void loop() { MIDI.sendNoteOn(69, 127, 1); // delay(100); Serial.println("Note on"); delay(1000);
MIDI.sendNoteOff(69, 127, 1); // delay(100); Serial.println("Note off"); delay(1000); }

What does not work:

Using the USB-MIDI Transport.

The same sketch modified to use USB-MIDI Transport:

include

USBMIDI_CREATE_DEFAULT_INSTANCE();

void setup() { Serial.begin(115200); delay(1000); MIDI.begin(); }

void loop() { MIDI.sendNoteOn(69, 127, 1); // delay(100); Serial.println("Note on"); delay(1000);
MIDI.sendNoteOff(69, 127, 1); // delay(100); Serial.println("Note off"); delay(1000); }

does not compile:

WARNING: library MIDIUSB claims to run on avr, sam, samd architecture(s) and may be incompatible with your current board which runs on esp32 architecture(s). In file included from c:\Users\Don\Arduino\libraries\USB-MIDI\src/USB-MIDI.h:26, from C:\Users\Don\Arduino\ESP32-S3 Cheapo board\ESP32_S3_Kontinuum_USBMIDI_Test_using_transport\ESP32_S3_Kontinuum_USBMIDI_Test_using_transport.ino:1: c:\Users\Don\Arduino\libraries\MIDIUSB\src/MIDIUSB.h:18:2: error: #error MIDIUSB can only be used with an USB MCU.

error MIDIUSB can only be used with an USB MCU.

^~~~~ In file included from c:\Users\Don\Arduino\libraries\USB-MIDI\src/USB-MIDI.h:26, from C:\Users\Don\Arduino\ESP32-S3 Cheapo board\ESP32_S3_Kontinuum_USBMIDI_Test_using_transport\ESP32_S3_Kontinuum_USBMIDI_Test_using_transport.ino:1: c:\Users\Don\Arduino\libraries\MIDIUSB\src/MIDIUSB.h:78:2: error: #error "Unsupported architecture"

error "Unsupported architecture"

^~~~~

exit status 1

Compilation error: exit status 1

FWIW, the same sketch using the BLEMIDI Transport does work:

include

include <hardware/BLEMIDI_ESP32.h>

BLEMIDI_CREATE_INSTANCE("MIDI_CONCERTINA", MIDI)

void setup() { Serial.begin(115200); delay(1000); MIDI.begin(); }

void loop() { MIDI.sendNoteOn(69, 127, 1); // delay(100); Serial.println("Note on"); delay(1000);
MIDI.sendNoteOff(69, 127, 1);
// delay(100); Serial.println("Note off"); delay(1000); }

jhsa commented 10 months ago

Try initialize the serial after the Midi.begin(). Do you still need the delay?

DonWT commented 10 months ago

Try initialize the serial after the Midi.begin(). Do you still need the delay?

No, that works without the delay.

jhsa commented 10 months ago

Try initialize the serial after the Midi.begin(). Do you still need the delay?

No, that works without the delay.

Perhaps one of those things that we aren't supposed to understand ;) Midi is also serial, so maybe the library doesn't like the fact that some other serial started before it tries to start its own serial. and I believe that even the baudrate is the same? I am just guessing of course, but at least now we know we don't need the delay :)

DonWT commented 10 months ago

I have found other situations on this and other ESP32 boards where I need to put in a delay after Serial.begin. For example in setup:

Serial.begin(115200); delay(2000); Serial.println("Starting...");

If I don't have the delay then that Serial.println does not happen, in this case a full 2 seconds is needed.

jhsa commented 10 months ago

I have found other situations on this and other ESP32 boards where I need to put in a delay after Serial.begin. For example in setup:

Serial.begin(115200); delay(2000); Serial.println("Starting...");

If I don't have the delay then that Serial.println does not happen, in this case a full 2 seconds is needed.

Yeah, same here, I also sometimes need to add some delays for stuff to work, specially on ESP32 :)

jaca2300 commented 9 months ago

Or maybe just while (!Serial) { }

DonWT commented 9 months ago

Or maybe just while (!Serial) { }

This does not work on ESP32 S3 boards.

ssjimh commented 2 months ago

Is there any outcome or resolution to this thread? It seems to have devolved to discussions of Serial operation.

I'm hoping to send and receive USB Midi on the Espressif ESP32-S3-DevkitC-1 N8R8

DonWT commented 2 months ago

Using the AdaFruit TinyUSB library as outlined above works but I have been having problems with the recent recent versions of TinyUSB library.

As of now I am still using version 2.40, anything later than that does not work for me using a Nano ESP32.

ssjimh commented 2 months ago

Thanks DonWT, examples compile now without indecipherable error messages. (With V2.40 of the library.) Progress using Espressif ESP32-S3-DevkitC-1 board!

DonWT commented 2 months ago

I have experienced two different problems with the Tiny USB library versions after version 2.40.

One is the compile failures that you have seen. These are failures reported when the IDE tries to compile the library itself, not your code. The errors seem to be board dependent, the compile works fine with some boards and not with others.

The other problem is that, at least with my Nano ESP32, the Serial port is not re-established after the midi connection is made. Code is compiled, uploaded and makes the midi connection OK, but any Serial.print statements are ignored. I also have to reset the board into the boot loader before I can upload a new sketch.

Both problems seem to be hardware/board dependent. I have raised the issue on the TinyUSB GitHub but the maintainer only has an Adafruit Feather S3 and he says that he does not see these problems on that board.

Version 2.40 seems to work just fine on the boards that I have and I don't see any compelling reason to want to use a higher version of the TinyUSB library.

Oh, and one other thing. I am using a windows 10 box for development and I have found that the latency between it and my Nano Esp32 running the TinyUSB library is really bad. I can use it for debugging but it would be useless for playing a midi instrument. I don't know if this is because of Windows deficiencies or if it is something to do with the TinyUSB library. The Fourtyseven effects library is not the problem because I can use it for BLE midi connected to an old Android cell phone and that works well, (subjectively) it has low latency.