4

I am running two different Raspberry PI and they are running Raspbian Wheezy and Jessie. On Wheezy, I simply commented out a line T0:23: respawn:/sbin/getty -L ttyAMA0 115200 Vt00 in /etc/inittab file. That worked and my program was able to get access to ttyAMA0. However, when I try to do the same on Jessie, I can't even find the file /etc/inittab. Then, I learned it is completely based on Debian. So, ttyAMA0 is treated like a service.

I searched online for solution about this and found a terminal command to stop or disable getty ttyAMA0 service as follow. So, my program could get access to ttyAMA0.

sudo systemctl stop serial-getty@ttyAMA0.service

or

sudo systemctl disable serial-getty@ttyAMA0.service

These commands did not help or make any changes. My program complains it can't connect. How do I make my program get access to ttyAMA0 on Jessie?

ThN
  • 1,042
  • 6
  • 19
  • 33
  • Apparently proper way to disable the service is sudo systemctl mask serial-getty@ttyAMA0.service (see http://stackoverflow.com/questions/21596384/cannot-disable-systemd-serial-getty-service ). But I tried this, and commenting out ttyAMA0 from /boot/cmdline.txt and disabling serial terminal via raspi-config - and still no luck. I see though that my pins are not in ALT0 state - https://raspberrypi.stackexchange.com/questions/7730/how-to-configure-serial-port-settings-of-dev-ttyama0 I'm still investigating why is that and how to fix that. – mvmn May 24 '16 at 02:27
  • Just did "gpio mode 15 ALT0" and "gpio mode 16 ALT0" - and my ttyAMA0 communication started to work. Guess I could also do it programmatically with wiring-pi instead of using gpio CLI utility, but at least I can confirm it works now after all the steps. – mvmn May 24 '16 at 02:45

1 Answers1

16
  1. Ensure terminal over serial is disabled in raspi-config

Run

sudo raspi-config

and in "Advanced" choose "Serial" (Enable/Disable shell and kernel messages on the serial connection) and disable it.

Steps 2 and 3 should not be necessary if you do this step first, but in case it didn't work - check them also.

  1. Ensure /boot/cmdline.txt has no ttyAMA0

The previous step should have already done that, but just in case do

cat /boot/cmdline.txt

and ensure its like this:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

and not like this:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

See also: http://elinux.org/RPi_Serial_Connection#S.2FW:_Preventing_Linux_from_using_the_serial_port

  1. Disable serial-getty properly if it's still running

If ps aux | grep tty still shows getty using ttyAMA0 - disable it, but don't do

sudo systemctl disable serial-getty@ttyAMA0.service

As explained here, the service may still run. Use this instead:

sudo systemctl mask serial-getty@ttyAMA0.service

  1. Ensure UART pins (15 and 16 wPi) are in state ALT0

Use command-line gpio util to check state of all pins

gpio readall

It should produce result like this

 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 |   IN | 0 | 51 || 52 | 0 | IN   | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 |   IN | 0 | 53 || 54 | 0 | IN   | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+

Note that Tx and Rx pins are in state ALT0.

  1. If in your case Tx and Rx pins are not in state ALT0, you can use same utility to manually change their state to ALT0. This should fix your problem until next reboot.

gpio mode 15 ALT0; gpio mode 16 ALT0

UPDATE:

  1. Enable uart in /boot/config.txt

In order to avoid manual step 5 and some issues that might occur with port ttyAMA0 not being found (/dev/ttyAMA0 being absent) you can set enable_uart=1 in /boot/config.txt, which seems to bring back /dev/ttyAMA0 and ports 15+16 are now in ALT0 state after reboot.

mvmn
  • 276
  • 2
  • 5
  • 4
    Step 6 solved my problem. After rebooting I ran the `gpio readall` command and now pins 8 and 10 are in ALT5 (not ALT0) mode, previously they were in IN mode. I also noticed that now there is a ttyS0 device that was not there before. I tried to use this device instead of ttyAMA0 and it works perfectly. – Bruno Ferreira Jul 19 '16 at 16:12
  • 1
    After going through your setup steps, now it works like a charm. – ThN Jul 27 '16 at 15:00