Skip to content

Day29 - UART and TTY Subsystem

🎯 Objective

  • Understand how UART is exposed as TTY in Linux
  • Learn how to configure serial port using termios / stty
  • Create a clean UART device (ttyAMA2)
  • Perform loopback test
  • Understand TTY behavior differences

🧠 Key Concepts

UART vs TTY

  • UART = hardware interface (TX/RX)
  • TTY = Linux abstraction layer
User App
  → /dev/ttyXXX
  → TTY core
  → line discipline
  → serial driver
  → UART hardware

🧪 Lab 1 - Enable Additional UART

Edit config:

sudo nano /boot/firmware/config.txt

Add:

dtoverlay=uart2

Reboot:

sudo reboot

Verify:

ls -l /dev/ttyAMA*
dmesg | grep -i ttyAMA

Expected:

  • /dev/ttyAMA10 → console
  • /dev/ttyAMA2 → new UART

🧪 Lab 2 - Verify Pinmux

pinctrl get 4
pinctrl get 5

Expected:

  • GPIO4 = TXD2
  • GPIO5 = RXD2

🧪 Lab 3 - Loopback Wiring

Connect:

  • Pin 7 (GPIO4 / TXD2)
  • Pin 29 (GPIO5 / RXD2)

🧪 Lab 4 - Configure TTY

stty -F /dev/ttyAMA2 115200 cs8 -cstopb -parenb -icanon -echo
stty -F /dev/ttyAMA2 -a

🧪 Lab 5 - Shell Loopback Test

Terminal A:

dd if=/dev/ttyAMA2 bs=1 count=16 status=none | hexdump -C

Terminal B:

printf 'ABC' > /dev/ttyAMA2

🧪 Lab 6 - Python Loopback Test

python3 uart_loopback_test.py /dev/ttyAMA2

Expected:

TX (...)
RX (...)
PASS

⚠️ Observations

  • cat is not reliable for UART debugging
  • printf + cat may fail due to timing issues
  • program-based testing is more stable

🧠 Conclusion

  • UART is accessed via /dev/ttyXXX
  • termios controls TTY behavior
  • clean UART must not be used by console
  • loopback verifies full system path