FortySevenEffects / arduino_midi_library

MIDI for Arduino
MIT License
1.56k stars 253 forks source link

Not handling usb transport well to send sysex #170

Closed sanotronics closed 3 years ago

sanotronics commented 3 years ago

Hi!

I am sending a 12 byte sysex message with the 5.0.2 version of the library, with an USB instance, and I am not always getting a 12 byte message on the other side: (don't mind the non sysex messages)

sysex

With version 4.4 (the fork which used MIDIUSB to include usb transport) all the messages were always succesfully transmitted and I got no shorter messages.

I think maybe the handling of the code index number might be failing? https://usb.org/sites/default/files/midi10.pdf (page 16)

lathoub commented 3 years ago

Hi @sanotronics

Have you tried running the SysEx example in the 1.1.2 version of the arduino USB-MIDI lib?

I just tried and sending SysEx seems to work normally (MIDI-OX receives SysEx with the correct amount of bytes)

(Using Arduino Leonardo on IDE 1.8.13)

sanotronics commented 3 years ago

I am using an atsamd21g18 (arduino zero, M0) on the 1.8.12 version.

I haven't tried this example, but I've been using the previous version of the Arduino MIDI lib which had a usb transport support, and the same messages worked alright every time.

I think it has something to do with specific lengths of messages. I can test this example you say with a few more message lengths.

This issue is similar but it also happened on the 4.4 version of the MIDI library, for receiving messages instead of sending them https://github.com/FortySevenEffects/arduino_midi_library/issues/168

lathoub commented 3 years ago

I have an old ZERO Pro R3 (from the arduino.org days) and no clue how to even get it to run the USB MIDI code or appear in MIDI-OX as a MIDI device.

Try to run the SysEx example code on your M0 and also tell me how you hook it up

lathoub commented 3 years ago

@sanotronics I tested various sizes of SysEx on an Arduino (over USB) and all passed. I don't have an M0 to test. Can you test the SysEx example code on your M0 and report back?

sanotronics commented 3 years ago

Hi!

I'll give it a go tomorrow!

Will be back with results 💪

sanotronics commented 3 years ago

Hi!

I tested this, on my board (it's based on an M0 design, but it is a custom board).

Using the v5.0.2 version of this library, I get the following messages on the computer sending a 12 byte message of data (without counting headers F0 and F7):

In both images you can see various lengths of messages received, when I am actually sending the same message over and over again. I couldn't detect a pattern, or a regularity to the failures, but see below, as there seems to appear one when increasing message sizes progressively.

If I just go back and switch to the 4.4.0 version of the library (and the minimal necessary changes in the instantiations), all the messages arrive with the proper lengths.

picking up the pattern, it would seem that you get either message_length, message_length -3 or message_length - 6.

at least there is a pattern!

lathoub commented 3 years ago

Please confirm: the above is happening using the SysEx sample code (you are never posting any code in your messages allowing us to reproduce the issue)

sanotronics commented 3 years ago

I copied and pasted the sysex sample code into my device's firmware.

There is some setup I need to do for it to run, but it is the same code running there in the end.

That's why I didn't copy it here. My complete code is this: https://gitlab.com/yaeltex/ytx-controller/-/blob/develop/ytx-controller/setup.ino#L298

After line 298 I just copied the sysex sample code wrapped in a while(1).

I will try to upload the exact same example, but as I said before, only switching to the previous version of the library makes it work ok, without any change in the loop code!

Isn't that enough to say that the new version is doing something the previous version wasn't?

lathoub commented 3 years ago

I tried the sample code on 2 type of boards, all SysEx sizes work fine (MIDI Ox) - trying to get as close to the basic setup as possible.

sanotronics commented 3 years ago

Ok, I tried just with the SysEx.ino code, and I do get the messages alright on my computer.

I found the reason to the malfunction in my code, which is a timer interrupt every ~400 us. Wrapping the sendSysEx function to disable the timer and reenable it after transmitting, made it work.

I am clueless then, as to why with the previous version of the library I'd get transmission to work properly, and not with the new one :/

If I add the code to handle SysEx reception to the SysEx.ino, this issue is still present: https://github.com/FortySevenEffects/arduino_midi_library/issues/168

Thanks @lathoub, sorry for the misunderstanding, but it really seemed something similar to the issue I linked above.