Whykiki
Posts: 3
Joined: Mon May 15, 2017 10:53 am

Using a MIDI Baudrate on RPi 3

Mon May 15, 2017 2:25 pm

Hello guys,

I'm totally new to the world of Raspberry and Linux, but I totally love it :-)
Aftter the first simple tests with flashing LEDs and Co. I want to build my first project.

At this point I decided to build a MIDI Controller with TouchScreen on my own. Only MIDI out....it's just testing first :-)
But for now I stuck on receiving the data on my PC or with a MIDI Instrumet.

I use RPi3 with an actual Raspbian OS and I mapped my UART back on /dev/ttyAMA0. So in different forum posts and tutorials I found out, that It's not possible to set the MIDI Baud of 31250 per default.
I have to use the UART Clock.....so I did the following in /boot/config.txt:

Code: Select all

init_uart_clock=39062500
init_uart_baud=38400
dtparam=uart0_clkrate=48000000
After rebooting and checking with the following:

Code: Select all

 vcgencmd measure_clock uart
I'll get the result:

Code: Select all

frequency(22)=39063000
Till here it looks good. I build my breadboard after this tutorial, I only need MIDI out:
http://m0xpd.blogspot.de/2013/01/midi-c ... n-rpi.html

And from there I copied a little bit code, my testing script:

Code: Select all

#!/usr/bin/env python
# m0xpd
# shack.nasties 'at Gee Male dot com'
import serial  				# Get the Serial library
ser=serial.Serial('/dev/ttyAMA0',38400)		# Set up the UART  
channel=int(raw_input("Enter MIDI Channel :"))	# input the desired MIDI Channel
command=0xB0 + channel -1			# Build the command byte
controller=int(raw_input("Enter Controller :"))	# input the controller number

while True:					# set up a loop...
 value=int(raw_input("Enter Value :"))		# Get the controller value
 accept=raw_input("Sending " + hex(command) + " " + hex(controller) + " " + hex(value) + "   OK ?  ('y' to send) ")
 if accept=='y':				# Happy with this ?
	message=chr(command) + chr(controller) + chr(value)
	ser.write(message)			# then transmit the message
So with this little script I can send a basic MIDI message. With a Hantek USB Oscilloscope I get a result on my Pin TxD, I get the result I want.

But I don't receive any data - like I said above.

When I run my script from above and after that showing the RPI vcgencmd usage with:

Code: Select all

 vcgencmd measure_clock uart
It prompts:

Code: Select all

frequency(22)=0
So maybe there a different bugs/errors....but I'm tooooooooo noobish to find a solution. From my point of view:

1. Maybe the UART Clock settings do not work as expected?!
2. The Pythoncode is wrong or causes setting UART frequency to 0??

I hope anyone of you can help me in this problem. I searched the web for over a week now and finding out many many things on my own, but here there are missing basics. I don't know how to get any further. Any help would be appreciated.

Thank you, Kiki

SiriusHardware
Posts: 480
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Using a MIDI Baudrate on RPi 3

Mon May 15, 2017 4:18 pm

I think the information you have applied worked up to Raspberry Pi 2, but there are complications when it comes to the Raspberry Pi 3.

One is that (I think) the onboard serial port was reallocated to use by Bluetooth and has to be reallocated away from that purpose so that you can use it. On previous Raspberry Pi versions it was only necessary to stop the system using the serial port as a console so that you could use it for your own purposes, and then, for MIDI specifically, you could use that clock reprogramming dodge to make the serial port run at the right speed.

An additional complication on the Pi 3 is that the baud rate of the serial port can vary when the overall system clock speed varies as a result of 'on demand' clock speed control - this was obviously an undesirable side effect which I am not aware has been resolved yet (apologies if this information is incorrect or just out of date).

With a Pi 3 your easiest way out may be just to use one of the many inexpensive USB to 5-DIN MIDI converter leads - baud rate generation is then handled automatically, and Linux just sees such leads as a MIDI input and output. Hopefully then, you can either find or write a python library to talk to any device which the Linux system classifies as a MIDI device.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1210
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Using a MIDI Baudrate on RPi 3

Mon May 15, 2017 7:18 pm

Using a current Raspbian release, all you should need to do is undo any MIDI-related changes you've made so far (init_uart_clock etc.) and add the following to config.txt:

Code: Select all

enable_uart=1
dtoverlay=pi3-miniuart-bt
dtoverlay=midi-uart0
This combination does three things:

1) Enable the UART. It isn't strictly necessary when combined with pi3-miniuart-bt, but it is better to get into good habits.
2) Use the weaker UART (ttyS0) for Bluetooth, freeing ttyAMA0 for your own purposes.
3) Achieve the same clock foolery as your config.txt settings.

The end result should be that asking for 38400 baud on ttyAMA0 should yield 31250 baud on header pins 8 & 10 (GPIOs 14 & 15).

It may be possible to write an equivalent overlay for ttyS0 - I'd call it "midi-uart1" - using some Device Tree magic called a fixed-factor-clock, but that is a refinement.

By the way, I once used a Korg 05R/W (ca. 1993) that you could drive from a standard UART at 38.4KBaud.

Whykiki
Posts: 3
Joined: Mon May 15, 2017 10:53 am

Re: Using a MIDI Baudrate on RPi 3

Mon May 15, 2017 9:21 pm

Thank you very much!!

I think the baudrate changed now - to see in the image below:

Image

Now I think this problem is solved. As the screen shows my message I send now with the correct rate, I think...
But as I told you, I'm a newbie and maybe there is more than one problem :-(

I don't want to open a new topic, but my question is solved by PhilE - maybe anyone only needs this little piece of help.
I stay in this topic because my problem of receiving messages is still actual.

I couldn't receive any messages - not on a pc and not on my Keyboard. The image shows my message measured directly at the output of the 74HCT Hex Schmitt Inverter and it's what I supposed to get.
I use a 180° 5-pin connector to connect my breadboard with a standard Midi-cable to my PC Midi-Soundcard (TASCAM US-1800) or to a Korg Microkorg XL (both only for testing), but when the Midi-Cable is connected my Oscilloscope doesn't trigger a new measurement...it does nothing.
If I disconnect the MIDI-Cable....I get a signal on the Oscilloscope...

So I think the message now sending is correct and with the correct baud rate, but I made another mistake.

How to find out or debug any further??

I used this scheme:

Image

Sure, I could use a USB-to-MIDI but then I go the easy way without learning anything.
Thats not what I want to do. This "midi-thing" is my first project on how to send data and receive it on other devices.
In a very very very very very far away future I want to control my Kemper Guitar Amp with my own MIDI-Controller....
But I want to learn and understand why.

Thank you very much for your help.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1210
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Using a MIDI Baudrate on RPi 3

Tue May 16, 2017 10:53 am

Why not use your scope to look at the output from a MIDI keyboard when you press a key? Then you can compare the waveforms and look for differences in levels and timing.

Whykiki
Posts: 3
Joined: Mon May 15, 2017 10:53 am

Re: Using a MIDI Baudrate on RPi 3

Tue May 16, 2017 11:44 am

Hahaha thank you. When I was taking a short trip with my dachshund I was thinking about my project and got the same idea: just measure the signals from my Korg :-)
I'll do this later but maybe you have also an idea what causes the trouble measuring with Midi-Cable inserted.

So If I pull off the Midi-Cable (connected to Korg or PC) I get a result.
If I leave the cable connected there is no signal send, but I don't understand why. My Oscillscope stays in "Wait-Mode" and there is no new trigger.
If I disconnect...it triggers....

I would like to understand whats happening there (or possibly not :-) ).
So trying to get the correct Midi-message is also my goal but before achieving this I want to get the basics, so that I can use the knowledge for further projects.

I hope I could correctly describe...I'm not a native speaker. So excuse if some things are not clear...

SiriusHardware
Posts: 480
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Using a MIDI Baudrate on RPi 3

Tue May 23, 2017 10:25 pm

Although the circuit you illustrated does not, most MIDI outputs traditionally use an open-collector transistor or open collector logic gate as the output driver.

That means that unless they are connected to V+ via a resistive load (which will usually be a resistor and optocoupler LED in series inside the device at the other end of the MIDI cable) you won't see any activity coming out of an unconnected MIDI output.

Unfortunately, what you are describing seems to be the exact opposite.

Take a 5-pin 180 degree DIN plug- just the plastic disc part with the 5 pins embedded in it, and connect a resistor of any value between 470R and 4K7 between pins 4 and 5 of the plug (beware the numbering on 5-pin DIN plugs, which goes 1-4-2-5-3, counting around). Plug that into the output of your Korg, and with the scope earth connected to 0V somewhere on the Korg and the scope probe on pin 5 of the DIN connection on the Korg, look for output when you press a key - in fact even when you don't press any keys you'll probably see a regular 'squirt' of data coming out of the Korg. To generate even more data to look at, move the pitch bend wheel (if the Korg has one) back and forth.

Bmillier
Posts: 1
Joined: Wed Jul 26, 2017 9:31 pm

Re: Using a MIDI Baudrate on RPi 3

Wed Jul 26, 2017 9:36 pm

@philE. Thanks a lot for this solution. I had found earlier solutions involving UART master clock changes, which must have worked in the past, but not on my latest Jessie version. The DToverlay does the trick on my Raspi 2.

Return to “Beginners”

Who is online

Users browsing this forum: gkaiseril, klricks, mediastore93 and 33 guests