MIDI library, define alternate TX RX pins ?

emmanuel63

Well-known member
Hello,

I am using "old fashioned" MIDI in/out DIN-5 port :
https://www.pjrc.com/teensy/td_libs_MIDI.html

But I have made a mistake when ordering my PCB :
midi IN is connected to pin 30 (instead of 0)
midi OUT is connected to pin 32 (instead of 1)

Is it possible to change RX / TX pin default assignation when using MIDI.h library ?

Emmanuel
 
Pin 30 is an XBAR pin (see Kurt's awesome pinout diagram) so you can reassign it to be the receive input for any of the 8 normal serial port. Details on the hardware serial page.

Pin 32 is a FlexIO pin. So you could try creating a FlexSerial port with pin 32 as transmit. The main limitation with FlexIO is the default clock is too fast for normal baud ranges, like 31250 for MIDI. See the FlexIO_t4 examples for ways to configure slower clock speed. I'm not aware of anyone reporting having used FlexSerial with MIDI_CREATE_INSTANCE(), but at least in theory it should work.

If both of these work, as far as MIDI.h is concerned they will be separate ports. So you'll need to use MIDI_CREATE_INSTANCE to create separate instances for transmit and receive.

The other really bad last resort option would be SoftwareSerial. It's been broken in all Teensyduino releases, but was recently fixed. The working code is only on github today, but will soon be in 1.60-beta1. If you want it now, you'll need to grab the code from github and install it in a way where it overrides the default copy from Teensyduino 1.59. But even after you get the latest SoftwareSerial code, it has a lot of limitations. The worst one is transmitting hogs interrupt time which would be needed by receive, so if you really are using both at the same time you're likely to suffer corrupted incoming MIDI messages if they happen to arrive at the exact moment you're sending anything. Even if that's not a problem, SoftwareSerial is pretty sensitive to other libraries using interrupts. Maybe ok in some projects, but definitely a liability if your project grows (feature creep) and you need to use other libraries.
 
I gave FlexSerial a quick try, and indeed it does work with MIDI.h.

Code:
#include <FlexIO_t4.h>
#include <FlexSerial.h>
#include <MIDI.h>

// Demonstrate using FlexSerial for MIDI OUT, inspired by:
// https://forum.pjrc.com/index.php?threads/75818/

FlexSerial myport(-1, 32); // MIDI OUT on pin 32
MIDI_CREATE_INSTANCE(FlexSerial, myport, mymidi);

void setup() {
  myport.setClock(9795918);
  mymidi.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
  mymidi.sendControlChange(9, 65, 1);
  delay(1000);
}

Here's the MIDI message as seen by my oscilloscope:

file.png
 
If you prefer a pure hardware solution, you can initialize pins 30 & 32 as pinMode(xx, INPUT) in your setup() function. Then add a bodge wire between pin 30 & pin 0, and another between pin 32 and pin 1.

Mark J Culross
KD5RXT
 
Thanks Paul. This is just amazing !
Mark, your solution is great but unfortunately I need pin 0 and 1.
Emmanuel
 
Back
Top