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
 
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:

View attachment 35725

Hi Paul,
Don't want to open another thread on the same subject of getting extra MIDI ports.
It's my first experiment with FlexIO with Teensy 4.1
How did you come with this number?
myport.setClock(9795918);
Is it based on the target CPU clock? Is it the same for T4.1 at nominal frequency of 600MHz?
Are there any limitations on the total number of serial ports (pairs TX+RX)?
I see only certain pins are FlexIO capable, Teensy 4.1: 2-23,26,27,32-41,49,50,52,54.
But is there any difference on which one to choose for TX and RX?


What did Mark mean by "pure hardware solution"?

Thank you, Paul and Kurt.
 
What did Mark mean by "pure hardware solution"?

@Deftaudio: The "pure hardware" reference implied that the bodge wires would carry the signals from the mistaken pins to the intended pins (30 => 0, & 32 =>1), then you could use the normal MIDI calls to make use of the standard MIDI serial port (e.g. using MIDI_CREATE_DEFAULT_INSTANCE(); to create and bind the MIDI interface to the default hardware Serial port), noting that you also must add the pinMode(30, INPUT) & pinMode(32, INPUT) calls in your setup() function to prevent the original pins (30 & 32) from potentially driving against the new pins (0 & 1).

Hope that helps . . .

Mark J Culross
KD5RXT
 
How did you come with this number?

Copied it from another example program. ;)

To be honest, it's been quite a while since I've meddled with FlexIO and really don't recall exactly what went into that clock number.

But I do recall (without looking at the code or reference manual) that the default clock is so fast that 115200 is the slowest standard baud rate you can get without significant error. I also recall the point where baud rates start to be become inaccurate was just above 57600 and close enough that requesting 57600 would usually work anyway, assuming the other side really is 57600 and doesn't have its own error on the slow side. You definitely need to slow the main FlexIO clock down to get 31250 for MIDI.

I also remember trying for days to get FlexIO to use one (or two) of its 16 bit timers for serial baud rate, rather than the usual dual-8 timer. Spent many hours experimenting and watching the pulses on the scope, and re-reading NXP's rather poor documentation and app notes. Never did get it to work. Also tried contacting some people at NXP who didn't have any answers. That was around the time I added the functions to slow the clock using a PLL, which gives a lot more range, but it does tie up one of the PLLs. Fortunately the video PLL is seldom used, at least today...
 
Last edited:
@Deftaudio: The "pure hardware" reference implied that the bodge wires would carry the signals from the mistaken pins to the intended pins (30 => 0, & 32 =>1), then you could use the normal MIDI calls to make use of the standard MIDI serial port (e.g. using MIDI_CREATE_DEFAULT_INSTANCE(); to create and bind the MIDI interface to the default hardware Serial port), noting that you also must add the pinMode(30, INPUT) & pinMode(32, INPUT) calls in your setup() function to prevent the original pins (30 & 32) from potentially driving against the new pins (0 & 1).

Hope that helps . . .

Mark J Culross
KD5RXT
Got that, thank you. I missed that the original topic was on T4, so, yes, you had an extra ports on pads.
 
Copied it from another example program. ;)

To be honest, it's been quite a while since I've meddled with FlexIO and really don't recall exactly what went into that clock number.

But I do recall (without looking at the code or reference manual) that the default clock is so fast that 115200 is the slowest standard baud rate you can get without significant error. I also recall the point where baud rates start to be become inaccurate was just above 57600 and close enough that requesting 57600 would usually work anyway, assuming the other side really is 57600 and doesn't have its own error on the slow side. You definitely need to slow the main FlexIO clock down to get 31250 for MIDI.

I also remember trying for days to get FlexIO to use one (or two) of its 16 bit timers for serial baud rate, rather than the usual dual-8 timer. Spent many hours experimenting and watching the pulses on the scope, and re-reading NXP's rather poor documentation and app notes. Never did get it to work. Also tried contacting some people at NXP who didn't have any answers. That was around the time I added the functions to slow the clock using a PLL, which gives a lot more range, but it does tie up one of the PLLs. Fortunately the video PLL is seldom used, at least today...
Thank you, Paul. No easy solution for T4.1 get more MIDI ports :( Alt/SoftSerial not supported, while FlexIO is not designed for that :)
 
while FlexIO is not designed for that

FlexIO Serial works fine for MIDI at 31250 baud. See msg #5.

In fact, NXP specifically designed FlexIO to be able to implement about a dozen common protocols. Ordinary hardware serial is absolutely one of the many things NXP designed it to be able to do. It is indeed designed for this!
 
Awesome, thank you. Just tried it and have T4.1 running with 14 MIDI out ports (8HW + 6 FlexIO):) Everything is going will do more tests for jitter later.
 
Wow, 14 MIDI out ports. Would really love to see a photo! Maybe we could even show it on the website project blog someday?
 
Of course, Paul, will share this once it's completed.
I believe you saw my other kits based on Teensy already?
Teensy 4.1 based 8x8 MIDI breakout
Teensy 4.0 based 5x5 MIDI breakout
And in Eurorack too :)


Needless to say about Luma-1... But this is Joe Britt's design to interface Z80 and Teensy together :)
 
Last edited:
Hi Paul, Kurt,

I need to find a way to switch a pin between FlexIO serial output and GPIO output in run time.
For FlexIO -> GPIO I just set a pinMode to OUTPUT and it works as expected.
Would you suggest how I can switch from GPIO back to FlexIO?
 
Back
Top