rodizio
Posts: 35
Joined: Sat May 07, 2016 2:40 am

Re: Overlay to remap Pi 3 UART

Fri Jan 27, 2017 12:22 pm

Thanks for the info guys. Edited my initial post regarding hardware/software uart to not confuse others.

Is it maybe possible (with everything being so small and the PCB having many layers I doubt it, but ...) to access these pins somewhere else on the PCB?


Just did some more searching on the topic, this would also work I guess?
https://github.com/themrleon/RpiSoft-UART

dgordon42
Posts: 716
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Overlay to remap Pi 3 UART

Fri Jan 27, 2017 12:44 pm

rodizio wrote:Just did some more searching on the topic, this would also work I guess?
https://github.com/themrleon/RpiSoft-UART
Definitely worth a try, and a lot safer than trying to find GPIO's on a multi-layer PCB without a drawing!

Dave.

User avatar
nsayer
Posts: 50
Joined: Thu Jun 26, 2014 12:43 am
Contact: Website

Re: Overlay to remap Pi 3 UART

Wed Mar 01, 2017 4:30 pm

I just got this working (I think) with up-to-date Jessie. I had bluetooth working and had decided to sacrifice the serial port. After my fiddling, I now have both bluetooth and a serial console. Yay!

What I did was...

Add "dtoverlay=pi3-miniuart-bt" and "force_turbo=1" to config.txt.
Add "console=serial0,115200" to cmdline.txt

/lib/systemd/system/hciuart.service already was configured to use /dev/serial1.
Check out my store! http://store.geppettoelectronics.com/

dgordon42
Posts: 716
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Overlay to remap Pi 3 UART

Wed Mar 01, 2017 5:47 pm

nsayer wrote:... and "force_turbo=1" to config.txt
If you do this, take your Pi's temperature from time to time with:

Code: Select all

vcgencmd measure_temp
If it's running up around 70 degrees C or above, change "force_turbo=1" to:

Code: Select all

core_freq=250
in '/boot/config.txt'

Hope this helps,
Dave.

User avatar
nsayer
Posts: 50
Joined: Thu Jun 26, 2014 12:43 am
Contact: Website

Re: Overlay to remap Pi 3 UART

Tue Mar 28, 2017 6:39 pm

After further research, it appears that

Code: Select all

enable_uart=1
is effective and sufficient.
Check out my store! http://store.geppettoelectronics.com/

Rosario
Posts: 3
Joined: Wed May 10, 2017 8:22 am

Re: Overlay to remap Pi 3 UART

Wed May 10, 2017 8:40 am

Hi! I have the project on Pi2 and Pi3. Previously, I was not required UART in the project, but now needed to adapt the device RS-232 and I have only GPIO19 GPIO13. Is it possible to set the UART on them?

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

Re: Overlay to remap Pi 3 UART

Wed May 10, 2017 9:09 am

No, sorry, not directly. You could add an external UART controlled by a software-driven (bitbashed) I2C port - an SC16IS750, for example - but performance would be limited.

Rosario
Posts: 3
Joined: Wed May 10, 2017 8:22 am

Re: Overlay to remap Pi 3 UART

Wed May 10, 2017 6:17 pm

Thanks for the reply. Tell me please, RPISoft UART can not solve my problem?

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

Re: Overlay to remap Pi 3 UART

Wed May 10, 2017 6:40 pm

It might, if the baud rate is low enough.

Rosario
Posts: 3
Joined: Wed May 10, 2017 8:22 am

Re: Overlay to remap Pi 3 UART

Thu May 11, 2017 12:35 am

Thanks, I'll try it

dumboguy03
Posts: 4
Joined: Sun Oct 29, 2017 4:37 pm

Re: Overlay to remap Pi 3 UART

Sun Oct 29, 2017 4:40 pm

Any tips on doing this for a pi zero w? When I apply the changes to boot/config.txt and the lib file, it doesn't seem to find the other uart as it does on pi3. Any help much appreciated.

This works great on pi3 tho. Thank you.

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

Re: Overlay to remap Pi 3 UART

Mon Oct 30, 2017 10:27 am

What image are you running on the Zero W? With the current Raspbian Stretch image the (now mis-named) pi3-miniuart-bt and pi3-disable-bt should do the same as on the Pi 3, namely:

* pi3-miniuart-bt - use the inferior mini-UART (ttyS0, UART1) for Bluetooth and leave ttyAMA0/UART0 for console or other uses.
* pi3-disable-bt - disable the mini-UART altogether, and leave ttyAMA0/UART0 for console or other uses.

You should not need to change any other files.

If your experience or use case doesn't match these descriptions then please give more details.

Phil

dumboguy03
Posts: 4
Joined: Sun Oct 29, 2017 4:37 pm

Re: Overlay to remap Pi 3 UART

Tue Oct 31, 2017 3:30 am

Hey Phil - Thanks for the response. For clarification, i'm looking to use bluetooth over the inferior uart as I'm using the stable uart for a GSM modem.

On the pi3 - I ran `hcitool dev` and got an address for the stable uart.
I added the following to /boot/config.txt :
dtoverlay=pi3-miniuart-bt
core-freq=250
and this to /lib/systemd/system/hciuart.service :
ExecStart=/usr/bin/hciattach /dev/serial1 bcm43xx 460800 noflow -

After rebooting, `hcitool dev` renders an address for the inferior uart.

This was NOT the case on the pi0w. Since your post, I've tried all combinations (one, the other, and both) you recommended and am unable to render an address for the inferior uart. Any other ideas you may have would be most useful. Thanks again!

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

Re: Overlay to remap Pi 3 UART

Tue Oct 31, 2017 4:02 pm

On a current Stretch image you don't need to edit the hciattach service - it calls btuart which recognises the different devices. Also, for the sake of anyone else reading this thread, "cpu-freq=250" should read:

Code: Select all

cpu_freq=250
Having said that, I currently can't get Bluetooth working over the mini-UART on the Zero W. As you say, Pi3 is happy. Manually going through the sequence to reset the modem and then sending it an initial command gets no response.

Ah, I see. Unlike the 3B, the Zero W has the BT modem flow control pins hooked up to GPIOs 30 and 31 - 30 is CTS for the Pi and RTS for the BT modem, while 31 is RTS for the Pi and CTS for the BT modem. In order for the modem to be able to send characters the CTS line (which is active low) must be low. This is hard-wired on a Pi3, but on a Zero W pin 31 must be configured correctly.

After booting with the pi3-miniuart-bt overlay, the Zero W comes up with pins 30 and 31 with inputs naturally pulling low. However, it seems that the external pull-up is stronger than the internal pull-down, so the modem can never respond. The pi3-miniuart-bt overlays needs to configure pins 30 and 31 for mini-UART flow control (CTS1 and RTS1). This is harmless on a 3B, but vital on the ZeroW.

Try this updated pi3-miniuart-bt overlay. After downloading, copy it to /boot/overlays:

Code: Select all

sudo cp pi3-miniuart-bt.dtbo /boot/overlays
and reboot.

dumboguy03
Posts: 4
Joined: Sun Oct 29, 2017 4:37 pm

Re: Overlay to remap Pi 3 UART

Fri Nov 03, 2017 1:29 am

Hey Phil - No dice. I switched out the pi3-miniuart-bt.dtbo 's and checked hcitool dev to no avail. Is there a better way to check if the switch is occurring? Let me know if you have any other ideas, and thanks again for the clarification thus far!

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

Re: Overlay to remap Pi 3 UART

Fri Nov 03, 2017 11:02 am

I think the new overlay is correct, but there is some additional issue - timing perhaps - at start of day. I am investigating, but it isn't the top priority.

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

Re: Overlay to remap Pi 3 UART

Fri Nov 03, 2017 12:53 pm

I can't get flow control on the mini-UART to start up reliably. As a workaround there is a new version of the overlay here (same URL) that drives the modem's CTS line low while keeping flow control disabled on the BCM2835. With this version of the overlay Bluetooth starts reliably for me on the Zero W.

Let me know how you get on, and if it works I'll publish the update.

dumboguy03
Posts: 4
Joined: Sun Oct 29, 2017 4:37 pm

Re: Overlay to remap Pi 3 UART

Sat Nov 04, 2017 10:41 pm

Still no devices in hcitool dev after updating the overlay. I just did a fresh raspbian install to make sure nothing I did previously was interfering. Can't figure out what's different seeing as it works on your machine. Any other ideas?

CRImier
Posts: 34
Joined: Sat Apr 05, 2014 4:34 pm

Re: Overlay to remap Pi 3 UART

Mon Nov 06, 2017 12:10 pm

Can't remap Bluetooth on Pi Zero W, either. Will try the proposed overlays and see how it goes.
hackaday.io/CRImier
Current project - ZeroPhone

bit_pusher
Posts: 2
Joined: Thu May 14, 2015 8:52 pm

Re: Overlay to remap Pi 3 UART

Fri Nov 24, 2017 4:41 pm

I have read most (not yet all) of the posts, and it appears what to in order to have the mini Uart used for BT and the PL011 Uart available for using on hats is (for Jessie, Nov., 2017) to add the following to /boot/config.txt
enable_uart=1
dtoverlay=pi3-miniuart-bt
core_freq=250
What baud rate would this result in for the BT?
I'm guessing enable_uart applies to the mini Uart (couldn't find this). Also, it appears that in /lib/systemd/system/hciuart.service, we need to have either:
After=dev-ttyS0.device or After=dev-serial1.device and that either works. My question is regarding the line:
ConditionPathIsDirectory=/proc/device-tree/soc/gpio@7e201000/bt_pins
should this be changed to:
ConditionPathIsDirectory=/proc/device-tree/soc/gpio@7e215040/bt_pins
as I believe 7e201000 is the address of serial0 (which is the PL011) and 7e215040 is the address of serial1 (which is set by udev to the mini Uart)?
It appears to me also that: https://www.raspberrypi.org/documentati ... on/uart.md is not current, going through all the posts, it is difficult to understand where we are at now, and how to determine (and set) the required baud rates
Thank you for any help and/or clarifications.

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

Re: Overlay to remap Pi 3 UART

Fri Nov 24, 2017 8:24 pm

Of your three config.txt lines only one is actually necessary:

Code: Select all

dtoverlay=pi3-miniuart-bt
"core_freq=250" is a side-effect of "enable_uart=1" if the UARTs haven't been swapped on a BT Pi (Pi3 and ZeroW), and if the UARTs have been swapped on a BT Pi then "enable_uart=1" is the default.

The mini-UART has smaller FIFOs and is more likely to overflow, so the baud rate is reduce to 460800.

In hciuart.service, "After=dev-serial1.device" is the better choice because it automatically points to the correct UART whether they are swapped or not.

That was a good guess regarding the ConditionPathIsDirectrory setting, but the line is actually:

Code: Select all

ConditionPathIsDirectory=/proc/device-tree/soc/gpio@7e200000/bt_pins
where 7e200000 is the address of the GPIO controller. The bt_pins node is always created under there, regardless of which UART makes use of them, so the value is always the same.

Baud rates can be set and read using the stty command:

Code: Select all

pi@raspberrypi:~$ stty -F /dev/serial0 -a
speed 115200 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff
-iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc
pi@raspberrypi:~$ stty -F /dev/serial0 460800

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

Re: Overlay to remap Pi 3 UART

Fri Nov 24, 2017 10:23 pm

Actually, no, let's leave the "core_freq=250" in there because although you are using the mini-UART the firmware doesn't know it.

bit_pusher
Posts: 2
Joined: Thu May 14, 2015 8:52 pm

Re: Overlay to remap Pi 3 UART

Fri Nov 24, 2017 11:27 pm

Wow, PhilE, what an excellent answer. Thank you.

Petra-Kathi
Posts: 1
Joined: Sun Nov 26, 2017 10:41 am

Re: Overlay to remap Pi 3 UART

Sun Nov 26, 2017 11:50 am

Hello @all,

I have read many of the posts in this thread, and some other discussions on Midi UART usage on the Raspi 3 at other places as well, but I am still quite confused what to try and what not, as many of the posts relate to older hardware and software configurations. :cry:

My situation: I need both BT and Midi over UART, as I interface with a Yamaha BTLE UD-BT01 module that sends wind controller messages, and want to hand over the received messages to a synthesizer with standard Midi-in, preferrably attached directly to the Raspi UART port. Presently I take the longer way and forward the received messages over a USB-to-Midi converter, see https://www.musiker-board.de/threads/ew ... ne.647564/ (sorry, German only, but the images should speak for themselves). I'd love to simplify the whole setup as far as possible since every component draws on the power bank and may add to the latency whose minimization is crucial for live music playing (see https://www.musiker-board.de/threads/ew ... st-8040531 for a practical timing check of the present system), and I'd love to get rid of unnecessary cables and outstanding plugs.

From the foregoing messages I conclude that as of today I'd have to trade a worse BTLE performance against a stable UART midi setup. Is this correct?

As the UART tinkering is somewhat deep-rooted in the OS setup, IMHO: Is there any difference in using Jessie (presently operative on my actual equipment -- at least I think so) or Stretch?

Kind regards,
Petra

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

Re: Overlay to remap Pi 3 UART

Mon Nov 27, 2017 3:22 pm

MIDI uses a nice low baud rate (31250), so even without flow control I don't see any reason why ttyS0 wouldn't be suitable in principle.

You will need to go through the following setup to use ttyS0 for MIDI:
i. Enable the serial port by setting "enable_uart=1" in config.txt.
ii. Remove any "dtoverlay=pi3-miniuart-bt" or "dtoverlay=pi3-miniuart-bt" from config.txt.
iii. Also make sure that ttyS0 is not being used by removing any "console=ttyS0" or "console=serial0" (or replacing with "xconsole=...").

The old method for selecting the custom baudrate (init_uart_clock in config.txt) only worked for ttyAMA0, but there are two new ways that should work with ttyS0:

1. There are two overlays - midi-uart0 and midi-uart1 - that play with the clock rates of the two UARTs such that asking for 38400 should result in an actual clock of 31250.
For ttyAMA0: dtoverlay=midi-uart0
For ttyS0: dtoverlay=midi-uart1

2. The termios2 group of IOCTLs allows arbitrary baud rates to be specified if the BOTHER flag is set. I've seen a link to a suitable utility floating around but my search-fu is weak today. If somebody reading can find a good existing utility then please link to it, but until then here is my own version.

Code: Select all

/*
Copyright (c) 2017 Raspberry Pi (Trading) Ltd.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the copyright holder nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <asm-generic/termbits.h>

static void print_val(const char *name, int before, int after)
{
    printf((before != after) ? "%s: %d->%d\n" : "%s: %d\n",
	   name, before, after);
}

int main(int argc, char* argv[]) 
{
    struct termios2 tio;
    int fd, ispeed, ospeed;

    if (argc < 2 || argc > 3)
    {
	printf("Usage: setbaud <dev> [<baud>]\n");
        return -1;
    }

    fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
	perror("* open failed");
	return -1;
    }

    if (ioctl(fd, TCGETS2, &tio) < 0)
    {
	perror("* ioctl(TCGETS2) failed");
	return -1;
    }

    ispeed = tio.c_ispeed;
    ospeed = tio.c_ospeed;

    if (argc == 3)
    {
	int speed = atoi(argv[2]);
	tio.c_cflag &= ~CBAUD;
	tio.c_cflag |= BOTHER;
	tio.c_ispeed = speed;
	tio.c_ospeed = speed;
	if (ioctl(fd, TCSETS2, &tio))
	{
	    perror("* ioctl(TCSETS2) failed");
	    return -1;
	}

	ioctl(fd, TCGETS2, &tio);
    }

    if (ispeed == ospeed)
    {
	print_val("speed", ispeed, tio.c_ispeed);
    }
    else
    {
	print_val("ispeed", ispeed, tio.c_ispeed);
	print_val("ospeed", ospeed, tio.c_ospeed);
    }

    return 0;
}
Call the file setbaud.c and compile it with:

Code: Select all

gcc -O2 -o setbaud setbaud.c; strip setbaud
This will produce a tiny executable (less than 4KB), which you use like this:

Code: Select all

$ setbaud /dev/ttyS0
speed: 9600
$ setbaud /dev/ttyS0 31250
speed: 9600->31250
Note: You will need to specify the path to the executable (such as "./setbaud") unless you copy the utility to somewhere on your search PATH.

Return to “Device Tree”

Who is online

Users browsing this forum: No registered users and 2 guests