Announcement

Collapse
No announcement yet.

Configuring The GPIO Serial Port On Raspbian Jessie on an RPi2/3

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Configuring The GPIO Serial Port On Raspbian Jessie on an RPi2/3

    Ran in to an issue relating to using ZWave me GPIO card on RPi Jessie versus Wheezy on an RPi2. I am currently utilizing the old Zee-2 with GPIO ZWave card and RTC as a slave Z-Wave controller to a new HS3 lite box.

    Default Jessie configuration is for the Bluetooth on the RPi3. On an RPi2 Jessie defaults to a bluetooth configuration and takes the GPIO ports. You have to reconfigure Jessie GPIO ports for the GPIO ZWave card. Not a big deal but a bit confusing.

    Note the following is a clip and paste from this article.

    Configuring The GPIO Serial Port On Raspbian Jessie Including Pi 3
    By Jon Watkins on May 29, 2016 in Raspberry Pi

    This used to be relatively straightforward, but with move from Raspbian Wheezy to Raspbian Jessie, things changed. Add to this, the new Raspberry Pi 3 with new hardware and the whole thing became a bit of a Dog’s Breakfast and issues with the Bluetooth to boot.

    It suddenly got very confusing.

    Following lots of wasted time, I’ve noted down what I think I know so far in this post. With luck you can have your cake and eat it: use the serial port on a Raspberry Pi 3 and use the Bluetooth AND have the same code work on other Raspberry Pi’s (non RPi3).

    NOTE: You will need the latest firmware May 2016 (2016-05-10) or later for this to work. Should this change again, I’ll update this post.

    History

    Before I dive into the configuration, it’s worth taking a moment for a little history and orientation about the serial port on the Raspberry Pi.

    If you’re a bit old school like me, you’d be expecting to find something called COM1 or similar on a header. In Raspberry Pi / Linux land this COM1 equivalent is found on pins 14 and 15 of the GPIO header and is called /dev/ttyAMA0 (obvious, right?).

    Also in Raspberry Pi land, you can use the serial port as a terminal to log in, which is useful if you don’t have a network connection to hand. You can connect to another computer via their serial ports and run a terminal emulator on the other computer and you’ll get a login prompt.

    By default the Raspberry Pi uses the serial port for this “console” login and via a software service called “getty”.

    Using the serial port with other hardware

    So that’s the ‘normal” configuration of the serial port, but serial ports are very useful things. What if we want to use the serial port to get data from a GPS card or program an arduino? In this case we need to disable the console login so that we alone get control of the port. Easy right? Yes and no. There is a big elephant in the room and he’s called Raspberry Pi 3.

    Before we can describe using the serial port, we have to talk about Raspberry Pi 3, which throws a great big spanner in the works as far as serial ports are concerned.

    Raspberry Pi 3

    Raspberry Pi 3’s are great little beasts, and add Bluetooth, yay! However, in order to use the Bluetooth correctly the /dev/ttyAMA0 has been “stolen” from the GPIO header and an inferior second one has been substituted in it’s place. No-one will ever know! Unfortunately /dev/ttyAMA0 was a hardware serial port (uart) and high performance (hence it was nabbed for the Bluetooth) and the second port is partly software and a bit flaky. Many people’s applications got broken.

    The second serial port you will see referred to as the “mini uart” and lives at /dev/ttyS0. It also calculates it’s bit timing’s from the CPU cores frequency and if the CPU is under heavy load it can corrupt the serial communications. Not good.

    In order to work around this, many people “fix” the CPU core frequency so that the serial port is stable. This comes at a slight loss in performance (though normally not noticeable). I’ll describe how you do this in the next section.

    By the way, it’s not all bad for the change of serial port on the Raspberry Pi 3. The Arduino IDE expects the serial communications to be on /dev/ttyS0 so you have no work to do to map the serial ports across. Yay!

    To summarize the ports on a Raspberry Pi 3 and be crystal clear:

    /dev/ttyAMA0 -> Bluetooth
    /dev/ttyS0 -> GPIO serial port.

    If you stick with these as is, your Bluetooth will work as nature intended AND you can use a serial port over the GPIO (there is a way of swapping the serial ports around if you don’t want to use the Bluetooth and I’ll cover that at the end of this post).

    Enabling

    There is yet another wrinkle in that in the latest Jessie releases (as of May 2016) the GPIO serial port is disabled by default. In order to enable it, edit config.txt:

    $ sudo nano /boot/config.txt

    and add the line (at the bottom):
    enable_uart=1

    As of May 2016 this will also lock the cpu core frequency for you so there’s nothing else you need to do (If you aren’t convinced and you really like to belt and braces it the command is: core_freq=250 which you add to the same file aswell).

    Reboot for the changes to take effect.

    This should get you good serial communications for most uses.

    Serial Aliases

    On the Raspberry Pi 3 the second serial port is called /dev/ttyS0 and is by default mapped to the GPIO pins 14 and 15. So immediately, if you have code that references /dev/ttyAMA0 you’re going to have problems and things aren’t going to work.

    You could go through your code and replace ttyAMA0 with ttyS0 and that should work. However, if you find yourself use the same SD card on a Raspberry Pi other than a rpi3 your code won’t work again.

    In order to try and get around this the Foundation have introduced a serial port alias (as of May 2016 – 2016-05-10). Thus you have serial ports: serial0 and serial1 (rpi3). The Raspberry Pi kernel sorts out where these point to depending on which Raspberry Pi you are on. Thus on a Raspberry Pi 3 serial0 will point to GPIO pins 14 and 15 and use the “mini-uart” aka /dev/ttyS0. On other Raspberry Pi’s it will point to the hardware UART and /dev/ttyAMA0.

    To find out where it is pointing you can use the command:

    ls -l /dev


    Default RPi3 alias

    Default RPi2 alias

    Disabling the Console

    If you are using the serial port for anything other than the console you need to disable it. This will be slightly different depending on whether you are running a Raspberry Pi 3 or not.
    For non Raspberry Pi 3 machines, remember it’s /dev/ttyAMA0 that is linked to the getty (console) service.

    So you need to perform this command from a terminal window:

    $ sudo systemctl stop serial-getty@ttyAMA0.service
    $ sudo systemctl disable serial-getty@ttyAMA0.service

    The “disable” will stop it loading in the future.

    For Raspberry Pi 3’s the command is similar but referencing /dev/ttyS0:

    $ sudo systemctl stop serial-getty@ttyS0.service
    $ sudo systemctl disable serial-getty@ttyS0.service

    You also need to remove the console from the cmdline.txt. If you edit this with:

    sudo nano /boot/cmdline.txt

    you will see something like:

    dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes root wait

    remove the line: console=serial0,115200 and save and reboot for changes to take effect.

    Swapping the Serial Ports on Raspberry Pi 3

    What if you don’t want to use the Bluetooth and you want that high performance /dev/ttyAMA0 back on the GPIO?
    Well you can do this and the way you do this is via a device overlay called “pi3-miniuart-bt” i.e. use the mini-uart (/dev/ttyS0) for Bluetooth (you may get some loss of performance on your Bluetooth though).

    You can also just disable the Bluetooth all together by using another overlay “pi3-disable-bt”. In both cases if you can find out more of what they do here: /boot/overlays/README

    To use add the following line to the /boot/config.txt

    $ sudo nano /boot/config.txt

    and add:

    dtoverlay=pi3-miniuart-bt

    Save and reboot for changes to take effect.

    You can check that it has worked by:

    $ ls -l /dev

    and you’ll see something like this:


    Swapped Raspberry PI 3 serial port aliases
    Last edited by Pete; August 19, 2016, 03:55 PM.
    - Pete

    Auto mator
    Homeseer 3 Pro - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU 16Gb
    Homeseer Zee2 (Lite) - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro
    HS4 Lite - Ubuntu 22.04 / Lenovo Tiny M900 / 32Gb Ram

    HS4 Pro - V4.1.18.1 - Ubuntu 22.04 / Lenova Tiny M900 / 32Gb Ram
    HSTouch on Intel tabletop tablets (Jogglers) - Asus AIO - Windows 11

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation-Tasmota-Espurna. OmniPro 2, Russound zoned audio, Alexa, Cheaper RFID, W800 and Home Assistant
Working...
X