MIDI baud rate on uart0/ttyAMA0 Raspberry Pi3.

Midi baudrate on the uart0/ttyAMA0 Raspberry Pi3.

In my projects with the Pi2 i use a lot of midi-controller signals, and as you know this is a non-common baud rate of 31250 baud. You might consider a usb midi-converter but i really like to work with the uart.(short lines/solid connection).

With ttyAMA0 on the gpio header of the Pi2 i could reprogram this uart to generate the midi baud rate and work smoothly with the midi applications. This was done by adding init_uart_clock=2441406 and init_uart_baud=38400 in the /boot/config.txt file. At the end of the line  In /boot/cmdline.txt bcm2708.uart_clock=3000000 must be added. After rebooting, the Pi2 generated the physical midi-baud rate on it’s ttyAMA0 under the 38400 commonly used baud rate. No sweat!

So let us go to the new Pi3:

I found out, like a lot of other people, that the ttyAMA0 uart was no longer on the gpio header, but in use by the Bluetooth device. In stead of the ttyAMA0, the software uart ttyS0 has been brought to the gpio header BCM gpio14/15 and is by default the console for logging in by serial connection.

There would not be a problem with this swapping if the uarts were of the same quality, but apparently this is not the case: as i mentioned the ttyS0 is a software uart and it’s clock is directly dependent on the arm_freq, which is far from constant. Furthermore you can’t adjust the divider to get different baud rates than the common ones. That is why you can see a lot of garbage spitting out of this console on your screen if you connect your pi3 by a serial connection and check it with Putty on 115200 baud.

The RaspberryPi team made an effort to help us out in this. By means of an dtoverlay we can switch back the ttyAMA0 to the header BCM gpio14/15. I read there is also a possibility to maintain the Bluetooth device which is working after the overlay with the unstable ttyS0, but i am not interested in that….i got rid of the Bluetooth device and have my ttyAMA0 back on the header and set it to the midi baud rate. This is what i did:

First:

Get the Jessie version of 18 March 2016 or later from the Raspberry site.( https://www.raspberrypi.org/downloads/raspbian/)

Expand Filesystem on your card in Preferences and disable serial. Reboot.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade !!!
sudo rpi-update !!!

Disable bluetooth and put ttyAMA0 back on BCM gpio’s 14&15:

sudo nano /boot/config.txt

Add the following:

dtoverlay=pi3-miniuart-bt
exit en save file

Stop the Bluetooth device from attempting to connect with the uart:

sudo systemctl disable hciuart
sudo nano /lib/systemd/system/hciuart.service
change the first ttyAMA0 in ttyS0

If not already, remove  console=serial0,115200  in/boot/cmdline.txt

sudo nano /boot/cmdline.txt
delete: console=serial0,115200

So now everything is set for swapping the ttyAMA0 back on the gpio header the next reboot of the the Pi, but before doing so you can add the following in  /boot/config.txt file:

sudo nano /boot/config

init_uart_clock=39062500                (or the same as with pi2: 2441406)

init_uart_baud=38400

dtparam=uart0_clkrate=48000000         (or the same as with pi2:3000000)

The uart_clock of the Pi2 is default at 3000000 Hz.

The uart_clock of the Pi3 is default: 48000000Hz

I use the uart_clkrate of 48000000 because that gives a more accurate baud rate, but it works also with the Pi2 setting of 2441406 / 3000000.

Save and exit and reboot.

After rebooting you can check your uart_clock by typing: vcgencmd measure_clock uart. It will prompt: frequency(22)=39063000 (with uart_clkrate=48000000) or 2442000 (uart_clkrate=3000000)

So now you can write sofware (eg Python) to work with midi keyboards/controllers by adding at the top of your program: import serial, ser=serial.Serial(‘/dev/ttyAMA0’, 38400.

When you connect your midi-device to the gpio Rx and Tx, please bear in mind to you use an opto-coupler (standard midi-in) for not to exceed the maximum of 3,3V for a high-level signal on Rx!