Raspberry Pi UART0 Configuration

From Wiki
Jump to navigationJump to search

Overview

Configure Raspberry Pi 3 Model B UART0 to enable RTS0 to GPIO17

For my application, I needed RTS0 available to drive the transmit enable pin on an RS-485 adapter. libmodbus does this by switching the RTS line state when it wants to transmit.

If CTS0 is also needed, the file below can be modified by changing the '17 32 33' to '16 17 32 33', and the '0 0 2' to '0 0 0 2'.

Create The Overlay

Copy the following text into a file named 'uart0-full-overlay.dts'

/dts-v1/;
/plugin/;

/{
  compatible = "brcm,bcm2708";

  fragment@0 {
    target = <&uart0>;
    __overlay__ {
      pinctrl-names = "default";
      pinctrl-0 = <&uart0_pins>;
      status = "okay";
    };
  };

  fragment@1 {
    target = <&gpio>;
    __overlay__ {
      uart0_pins: uart0_pins {
        brcm,pins = <17 32 33>; /* UART0: RTS0 TX0 RX0 */
        brcm,function = <7>;    /* ALT3 */
        brcm,pull = <0 0 2>;
      };
    };
  };

  fragment@2 {
    target-path = "/aliases";
    __overlay__ {
      serial0 = "/soc/uart@7e201000";
    };
  };
};

Compile the overlay:

dtc -@ -I dts -O dtb -o uart0-full.dtbo uart0-full-overlay.dts

Copy the output file 'uart0-full.dtbo' to the /boot/overlay directory:

sudo cp uart0-full.dtbo /boot/overlays/uart0-full.dtbo

Edit /boot/cmdline.txt. Find the console entry that refers to the serial0 device, and remove it, including the baud rate setting. It will look something like

console=serial0,115200

Option 1: Disable Bluetooth Hardware

Update the /boot/config.txt file by adding these two lines to the end:

dtoverlay=pi3-disable-bt
dtoverlay=uart0-full

Disable the Bluetooth hardware:

sudo systemctl disable hciuart

Option 2: Move Bluetooth Hardware To The Mini-UART

Update the /boot/config.txt file by adding these two lines to the end:

dtoverlay=pi3-miniuart-bt
dtoverlay=uart0-full

It is also necessary to edit /lib/systemd/system/hciuart.service and replace ttyAMA0 with ttyS0, unless you have a system with udev rules that creates /dev/serial0 and /dev/serial1.

Verification

Run the following command to check the settings of the I/O pins

for i in 16 17 32 33; do sudo raspi-gpio get $i; done

If raspi-gpio is not installed, use the following command to install it:

sudo apt-get -q -q update && sudo apt-get install -y raspi-gpio

The output should look like the following (16 will show as CTS0 if it was enabled)

GPIO 16: level=0 fsel=0 func=INPUT
GPIO 17: level=1 fsel=7 alt=3 func=RTS0
GPIO 32: level=1 fsel=7 alt=3 func=TXD0
GPIO 33: level=1 fsel=7 alt=3 func=RXD0

References