Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UART on a Raspberry Pi 5 using GPIO 14/15. #165

Closed
Dygear opened this issue Nov 5, 2024 · 5 comments
Closed

UART on a Raspberry Pi 5 using GPIO 14/15. #165

Dygear opened this issue Nov 5, 2024 · 5 comments
Labels

Comments

@Dygear
Copy link

Dygear commented Nov 5, 2024

After getting some help in the Raspberry Pi forums, on how to configure GPIO 14 & 15 for UART TX / RX, I was able to get the device talking to my Raspberry Pi 5 and confirm that the wiring was correct. When attempting to use rppal to talk to the device, I was unable to send or receive data. Any commands I issued did not produce any results on the device, and I did not receive any feedback from the device either. I read though #127 to see if I had a configuration issue, but it looks like it should just work?

When using minicom -b 57600 -o -D /dev/ttyAMA0 I can see 0x55 (ASCII U) from the device showing that it is talking over that port. It just doesn't look like rppal is picking up that connection. Is there something that I should be doing to configure rrpal to use the ttyAMA0 connection on the Raspberry Pi 5?

My Cargo.toml looks like this:

[package]
name = "rpi-r503"
version = "0.1.0"
edition = "2021"

[dependencies]
rppal = { version = "0.19.0", features = ["hal"] }
r503 = { path = "../r503" }
heapless = "0.8"

The rest of the code can be found on this repo.
https://github.com/Dygear/rpi-r503

The output of gpio_status may be helpful, so here it is:

dygear@MimoFPS-DEV:~/rppal $ cargo run --example gpio_status
    Updating crates.io index
     Locking 21 packages to latest compatible versions
      Adding embedded-hal v0.2.7 (latest: v1.0.0)
      Adding nb v0.1.3 (latest: v1.1.0)
  Downloaded lazy_static v1.5.0
  Downloaded simple-signal v1.1.1
  Downloaded 2 crates (17.2 KB) in 0.43s
   Compiling libc v0.2.161
   Compiling lazy_static v1.5.0
   Compiling rppal v0.19.0 (/home/dygear/rppal)
   Compiling simple-signal v1.1.1
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.76s
     Running `target/debug/examples/gpio_status`
+------+-------+---+---------+---+-------+------+
| GPIO | Mode  | L |   Pin   | L | Mode  | GPIO |
+------+-------+---+----+----+---+-------+------+
|      | 3.3 V |   |  1 |  2 |   | 5 V   |      |
|    2 | NULL  | 0 |  3 |  4 |   | 5 V   |      |
|    3 | NULL  | 0 |  5 |  6 |   | GND   |      |
|    4 | NULL  | 0 |  7 |  8 | 1 | ALT4  |   14 |
|      | GND   |   |  9 | 10 | 1 | ALT4  |   15 |
|   17 | NULL  | 0 | 11 | 12 | 1 | IN    |   18 |
|   27 | NULL  | 0 | 13 | 14 |   | GND   |      |
|   22 | NULL  | 0 | 15 | 16 | 0 | NULL  |   23 |
|      | 3.3 V |   | 17 | 18 | 0 | NULL  |   24 |
|   10 | NULL  | 0 | 19 | 20 |   | GND   |      |
|    9 | NULL  | 0 | 21 | 22 | 0 | NULL  |   25 |
|   11 | NULL  | 0 | 23 | 24 | 0 | NULL  |    8 |
|      | GND   |   | 25 | 26 | 0 | NULL  |    7 |
|    0 | IN    | 1 | 27 | 28 | 1 | IN    |    1 |
|    5 | NULL  | 0 | 29 | 30 |   | GND   |      |
|    6 | NULL  | 0 | 31 | 32 | 0 | NULL  |   12 |
|   13 | NULL  | 0 | 33 | 34 |   | GND   |      |
|   19 | NULL  | 0 | 35 | 36 | 0 | NULL  |   16 |
|   26 | NULL  | 0 | 37 | 38 | 0 | NULL  |   20 |
|      | GND   |   | 39 | 40 | 0 | NULL  |   21 |
+------+-------+---+----+----+---+-------+------+
@Dygear Dygear changed the title UART on a Raspberry Pi 5 UART on a Raspberry Pi 5 using GPIO 14/15. Nov 5, 2024
@golemparts
Copy link
Owner

Hi @Dygear. I'm not seeing any obvious problems in your code. There are a few things you could try to debug the issue:

  • new will use /dev/serial0 by default. Try calling with_path instead and specify /dev/ttyAMA0.
  • By default read (and write) are configured as non-blocking. Try set_read_mode(1, Duration::default()) to wait for at least 1 byte in the read buffer before returning.
  • Data sent using write is first copied to an internal output buffer and hasn't all been sent to the device yet when it returns. You can call output_len to make sure the output buffer has been emptied. Similarly, you can use input_len to check if any data is available in the input buffer.
  • Check the return value for read and write to see how many bytes were actually copied to/read from the buffers, if any.

@golemparts golemparts added the uart label Nov 5, 2024
@golemparts
Copy link
Owner

golemparts commented Nov 5, 2024

I forgot to mention drain, which will block until all data in the output buffer has been transmitted, and might be useful in this situation as well.

@Dygear
Copy link
Author

Dygear commented Nov 5, 2024

Updated the uart section of the code to now read as follows:

    let mut uart = match Uart::with_path("/dev/ttyAMA0", 57600, Parity::None, 8, 1) {
        Ok(uart) => uart,
        Err(e) => panic!("UART Setup Error: {e}")
    };

I'm now able to talk to the device. Thanks for the help and putting me on the correct path.


For what it's worth, here's the output of ls -lha for the /dev/ directory.

Looks like /dev/serial0 points to /dev/ttyAMA10. But as you said I wanted /dev/ttyAMA0.

dygear@MimoFPS-DEV:~ $ ls -lha /dev/
total 4.0K
drwxr-xr-x 16 root root        4.1K Nov  5 09:40 .
drwxr-xr-x 18 root root        4.0K Oct 22 09:13 ..
crw-r--r--  1 root root     10, 235 Nov  5 09:40 autofs
drwxr-xr-x  2 root root         580 Nov  5 09:40 block
crw-------  1 root root     10, 234 Nov  5 09:40 btrfs-control
drwxr-xr-x  3 root root          60 Nov  5 09:40 bus
crw-------  1 root root     10, 126 Nov  5 09:40 cachefiles
crw-rw----  1 root video   511,   0 Nov  5 09:40 cec0
crw-rw----  1 root video   511,   1 Nov  5 09:40 cec1
drwxr-xr-x  2 root root        3.4K Nov  5 09:40 char
crw--w----  1 root tty       5,   1 Nov  5 09:40 console
crw-------  1 root root     10, 124 Nov  5 09:40 cpu_dma_latency
crw-------  1 root root     10, 203 Nov  5 09:40 cuse
drwxr-xr-x  8 root root         160 Nov  5 09:40 disk
drwxr-xr-x  2 root root         100 Nov  5 09:40 dma_heap
drwxr-xr-x  3 root root         120 Nov  5 09:40 dri
lrwxrwxrwx  1 root root          13 Nov  5 09:40 fd -> /proc/self/fd
crw-rw-rw-  1 root root      1,   7 Nov  5 09:40 full
crw-rw-rw-  1 root root     10, 229 Nov  5 09:40 fuse
crw-rw----  1 root gpio    254,   0 Nov  5 09:40 gpiochip0
crw-rw----  1 root gpio    254,  10 Nov  5 09:40 gpiochip10
crw-rw----  1 root gpio    254,  11 Nov  5 09:40 gpiochip11
crw-rw----  1 root gpio    254,  12 Nov  5 09:40 gpiochip12
crw-rw----  1 root gpio    254,  13 Nov  5 09:40 gpiochip13
lrwxrwxrwx  1 root root           9 Nov  5 09:40 gpiochip4 -> gpiochip0
crw-rw----  1 root gpio    235,   0 Nov  5 09:40 gpiomem0
crw-rw----  1 root gpio    239,   0 Nov  5 09:40 gpiomem1
crw-rw----  1 root gpio    238,   0 Nov  5 09:40 gpiomem2
crw-rw----  1 root gpio    237,   0 Nov  5 09:40 gpiomem3
crw-rw----  1 root gpio    236,   0 Nov  5 09:40 gpiomem4
crw-------  1 root root     10, 183 Nov  5 09:40 hwrng
lrwxrwxrwx  1 root root          12 Nov  5 09:40 initctl -> /run/initctl
drwxr-xr-x  3 root root         180 Nov  5 09:40 input
crw-r--r--  1 root root      1,  11 Nov  5 09:40 kmsg
crw-rw----  1 root kvm      10, 232 Nov  5 09:40 kvm
lrwxrwxrwx  1 root root          28 Nov  5 09:40 log -> /run/systemd/journal/dev-log
brw-rw----  1 root disk      7,   0 Nov  5 09:40 loop0
brw-rw----  1 root disk      7,   1 Nov  5 09:40 loop1
brw-rw----  1 root disk      7,   2 Nov  5 09:40 loop2
brw-rw----  1 root disk      7,   3 Nov  5 09:40 loop3
brw-rw----  1 root disk      7,   4 Nov  5 09:40 loop4
brw-rw----  1 root disk      7,   5 Nov  5 09:40 loop5
brw-rw----  1 root disk      7,   6 Nov  5 09:40 loop6
brw-rw----  1 root disk      7,   7 Nov  5 09:40 loop7
crw-rw----  1 root disk     10, 237 Nov  5 09:40 loop-control
drwxr-xr-x  2 root root          60 Nov  5 09:40 mapper
crw-rw----  1 root video   234,   0 Nov  5 09:40 media0
crw-rw----  1 root video   234,   1 Nov  5 09:40 media1
crw-rw----  1 root video   234,   2 Nov  5 09:40 media2
crw-r-----  1 root kmem      1,   1 Nov  5 09:40 mem
brw-rw----  1 root disk    179,   0 Nov  5 09:40 mmcblk0
brw-rw----  1 root disk    179,   1 Nov  5 09:40 mmcblk0p1
brw-rw----  1 root disk    179,   2 Nov  5 09:40 mmcblk0p2
drwxrwxrwt  2 root root          40 Dec 31  1969 mqueue
drwxr-xr-x  2 root root          60 Nov  5 09:40 net
crw-rw-rw-  1 root root      1,   3 Nov  5 09:40 null
crw-r-----  1 root kmem      1,   4 Nov  5 09:40 port
crw-------  1 root root    108,   0 Nov  5 09:40 ppp
crw-------  1 root root    250,   0 Nov  5 09:40 pps0
crw-rw-rw-  1 root tty       5,   2 Nov  5 16:28 ptmx
crw-------  1 root root    249,   0 Nov  5 09:40 ptp0
drwxr-xr-x  2 root root           0 Nov  5 09:40 pts
brw-rw----  1 root disk      1,   0 Nov  5 09:40 ram0
brw-rw----  1 root disk      1,   1 Nov  5 09:40 ram1
brw-rw----  1 root disk      1,  10 Nov  5 09:40 ram10
brw-rw----  1 root disk      1,  11 Nov  5 09:40 ram11
brw-rw----  1 root disk      1,  12 Nov  5 09:40 ram12
brw-rw----  1 root disk      1,  13 Nov  5 09:40 ram13
brw-rw----  1 root disk      1,  14 Nov  5 09:40 ram14
brw-rw----  1 root disk      1,  15 Nov  5 09:40 ram15
brw-rw----  1 root disk      1,   2 Nov  5 09:40 ram2
brw-rw----  1 root disk      1,   3 Nov  5 09:40 ram3
brw-rw----  1 root disk      1,   4 Nov  5 09:40 ram4
brw-rw----  1 root disk      1,   5 Nov  5 09:40 ram5
brw-rw----  1 root disk      1,   6 Nov  5 09:40 ram6
brw-rw----  1 root disk      1,   7 Nov  5 09:40 ram7
brw-rw----  1 root disk      1,   8 Nov  5 09:40 ram8
brw-rw----  1 root disk      1,   9 Nov  5 09:40 ram9
crw-rw-rw-  1 root root      1,   8 Nov  5 09:40 random
crw-rw-r--  1 root netdev   10, 242 Nov  5 09:40 rfkill
lrwxrwxrwx  1 root root           4 Nov  5 09:40 rtc -> rtc0
crw-------  1 root root    252,   0 Nov  5 09:40 rtc0
lrwxrwxrwx  1 root root           8 Nov  5 09:40 serial0 -> ttyAMA10
drwxrwxrwt  2 root root          40 Nov  5 09:40 shm
drwxr-xr-x  3 root root         180 Nov  5 09:40 snd
crw-rw----  1 root spi     153,   0 Nov  5 09:40 spidev10.0
lrwxrwxrwx  1 root root          15 Nov  5 09:40 stderr -> /proc/self/fd/2
lrwxrwxrwx  1 root root          15 Nov  5 09:40 stdin -> /proc/self/fd/0
lrwxrwxrwx  1 root root          15 Nov  5 09:40 stdout -> /proc/self/fd/1
crw-rw-rw-  1 root tty       5,   0 Nov  5 15:00 tty
crw--w----  1 root tty       4,   0 Nov  5 09:40 tty0
crw--w----  1 root tty       4,   1 Nov  5 09:40 tty1
crw--w----  1 root tty       4,  10 Nov  5 09:40 tty10
crw--w----  1 root tty       4,  11 Nov  5 09:40 tty11
crw--w----  1 root tty       4,  12 Nov  5 09:40 tty12
crw--w----  1 root tty       4,  13 Nov  5 09:40 tty13
crw--w----  1 root tty       4,  14 Nov  5 09:40 tty14
crw--w----  1 root tty       4,  15 Nov  5 09:40 tty15
crw--w----  1 root tty       4,  16 Nov  5 09:40 tty16
crw--w----  1 root tty       4,  17 Nov  5 09:40 tty17
crw--w----  1 root tty       4,  18 Nov  5 09:40 tty18
crw--w----  1 root tty       4,  19 Nov  5 09:40 tty19
crw--w----  1 root tty       4,   2 Nov  5 09:40 tty2
crw--w----  1 root tty       4,  20 Nov  5 09:40 tty20
crw--w----  1 root tty       4,  21 Nov  5 09:40 tty21
crw--w----  1 root tty       4,  22 Nov  5 09:40 tty22
crw--w----  1 root tty       4,  23 Nov  5 09:40 tty23
crw--w----  1 root tty       4,  24 Nov  5 09:40 tty24
crw--w----  1 root tty       4,  25 Nov  5 09:40 tty25
crw--w----  1 root tty       4,  26 Nov  5 09:40 tty26
crw--w----  1 root tty       4,  27 Nov  5 09:40 tty27
crw--w----  1 root tty       4,  28 Nov  5 09:40 tty28
crw--w----  1 root tty       4,  29 Nov  5 09:40 tty29
crw--w----  1 root tty       4,   3 Nov  5 09:40 tty3
crw--w----  1 root tty       4,  30 Nov  5 09:40 tty30
crw--w----  1 root tty       4,  31 Nov  5 09:40 tty31
crw--w----  1 root tty       4,  32 Nov  5 09:40 tty32
crw--w----  1 root tty       4,  33 Nov  5 09:40 tty33
crw--w----  1 root tty       4,  34 Nov  5 09:40 tty34
crw--w----  1 root tty       4,  35 Nov  5 09:40 tty35
crw--w----  1 root tty       4,  36 Nov  5 09:40 tty36
crw--w----  1 root tty       4,  37 Nov  5 09:40 tty37
crw--w----  1 root tty       4,  38 Nov  5 09:40 tty38
crw--w----  1 root tty       4,  39 Nov  5 09:40 tty39
crw--w----  1 root tty       4,   4 Nov  5 09:40 tty4
crw--w----  1 root tty       4,  40 Nov  5 09:40 tty40
crw--w----  1 root tty       4,  41 Nov  5 09:40 tty41
crw--w----  1 root tty       4,  42 Nov  5 09:40 tty42
crw--w----  1 root tty       4,  43 Nov  5 09:40 tty43
crw--w----  1 root tty       4,  44 Nov  5 09:40 tty44
crw--w----  1 root tty       4,  45 Nov  5 09:40 tty45
crw--w----  1 root tty       4,  46 Nov  5 09:40 tty46
crw--w----  1 root tty       4,  47 Nov  5 09:40 tty47
crw--w----  1 root tty       4,  48 Nov  5 09:40 tty48
crw--w----  1 root tty       4,  49 Nov  5 09:40 tty49
crw--w----  1 root tty       4,   5 Nov  5 09:40 tty5
crw--w----  1 root tty       4,  50 Nov  5 09:40 tty50
crw--w----  1 root tty       4,  51 Nov  5 09:40 tty51
crw--w----  1 root tty       4,  52 Nov  5 09:40 tty52
crw--w----  1 root tty       4,  53 Nov  5 09:40 tty53
crw--w----  1 root tty       4,  54 Nov  5 09:40 tty54
crw--w----  1 root tty       4,  55 Nov  5 09:40 tty55
crw--w----  1 root tty       4,  56 Nov  5 09:40 tty56
crw--w----  1 root tty       4,  57 Nov  5 09:40 tty57
crw--w----  1 root tty       4,  58 Nov  5 09:40 tty58
crw--w----  1 root tty       4,  59 Nov  5 09:40 tty59
crw--w----  1 root tty       4,   6 Nov  5 09:40 tty6
crw--w----  1 root tty       4,  60 Nov  5 09:40 tty60
crw--w----  1 root tty       4,  61 Nov  5 09:40 tty61
crw--w----  1 root tty       4,  62 Nov  5 09:40 tty62
crw--w----  1 root tty       4,  63 Nov  5 09:40 tty63
crw--w----  1 root tty       4,   7 Nov  5 09:40 tty7
crw--w----  1 root tty       4,   8 Nov  5 09:40 tty8
crw--w----  1 root tty       4,   9 Nov  5 09:40 tty9
crw-rw----  1 root dialout 204,  64 Nov  5 16:26 ttyAMA0
crw-rw----  1 root dialout 204,  74 Nov  5 15:18 ttyAMA10
crw-------  1 root root      5,   3 Nov  5 09:40 ttyprintk
crw-------  1 root root     10, 239 Nov  5 09:40 uhid
crw-------  1 root root     10, 223 Nov  5 09:40 uinput
crw-rw-rw-  1 root root      1,   9 Nov  5 09:40 urandom
drwxr-xr-x  3 root root          60 Nov  5 09:40 v4l
crw-rw----  1 root video    10, 125 Nov  5 09:40 vcio
crw-------  1 root root    246,   0 Nov  5 09:40 vc-mem
crw-rw----  1 root tty       7,   0 Nov  5 09:40 vcs
crw-rw----  1 root tty       7,   1 Nov  5 09:40 vcs1
crw-rw----  1 root tty       7,   2 Nov  5 09:40 vcs2
crw-rw----  1 root tty       7,   3 Nov  5 09:40 vcs3
crw-rw----  1 root tty       7,   4 Nov  5 09:40 vcs4
crw-rw----  1 root tty       7,   5 Nov  5 09:40 vcs5
crw-rw----  1 root tty       7,   6 Nov  5 09:40 vcs6
crw-rw----  1 root tty       7, 128 Nov  5 09:40 vcsa
crw-rw----  1 root tty       7, 129 Nov  5 09:40 vcsa1
crw-rw----  1 root tty       7, 130 Nov  5 09:40 vcsa2
crw-rw----  1 root tty       7, 131 Nov  5 09:40 vcsa3
crw-rw----  1 root tty       7, 132 Nov  5 09:40 vcsa4
crw-rw----  1 root tty       7, 133 Nov  5 09:40 vcsa5
crw-rw----  1 root tty       7, 134 Nov  5 09:40 vcsa6
crw-rw----  1 root tty       7,  64 Nov  5 09:40 vcsu
crw-rw----  1 root tty       7,  65 Nov  5 09:40 vcsu1
crw-rw----  1 root tty       7,  66 Nov  5 09:40 vcsu2
crw-rw----  1 root tty       7,  67 Nov  5 09:40 vcsu3
crw-rw----  1 root tty       7,  68 Nov  5 09:40 vcsu4
crw-rw----  1 root tty       7,  69 Nov  5 09:40 vcsu5
crw-rw----  1 root tty       7,  70 Nov  5 09:40 vcsu6
crw-------  1 root root     10, 127 Nov  5 09:40 vga_arbiter
crw-------  1 root root     10, 137 Nov  5 09:40 vhci
crw-rw----  1 root kvm      10, 238 Nov  5 09:40 vhost-net
crw-rw----  1 root kvm      10, 241 Nov  5 09:40 vhost-vsock
crw-rw----  1 root video    81,  16 Nov  5 09:40 video19
crw-rw----  1 root video    81,   0 Nov  5 09:40 video20
crw-rw----  1 root video    81,   1 Nov  5 09:40 video21
crw-rw----  1 root video    81,   2 Nov  5 09:40 video22
crw-rw----  1 root video    81,   3 Nov  5 09:40 video23
crw-rw----  1 root video    81,   4 Nov  5 09:40 video24
crw-rw----  1 root video    81,   5 Nov  5 09:40 video25
crw-rw----  1 root video    81,   6 Nov  5 09:40 video26
crw-rw----  1 root video    81,   7 Nov  5 09:40 video27
crw-rw----  1 root video    81,   8 Nov  5 09:40 video28
crw-rw----  1 root video    81,   9 Nov  5 09:40 video29
crw-rw----  1 root video    81,  10 Nov  5 09:40 video30
crw-rw----  1 root video    81,  11 Nov  5 09:40 video31
crw-rw----  1 root video    81,  12 Nov  5 09:40 video32
crw-rw----  1 root video    81,  13 Nov  5 09:40 video33
crw-rw----  1 root video    81,  14 Nov  5 09:40 video34
crw-rw----  1 root video    81,  15 Nov  5 09:40 video35
crw-------  1 root root     10, 130 Nov  5 09:40 watchdog
crw-------  1 root root    248,   0 Nov  5 09:40 watchdog0
crw-rw-rw-  1 root root      1,   5 Nov  5 09:40 zero

Did you ever get a Raspberry Pi 5 to use for testing?

@Dygear Dygear closed this as completed Nov 5, 2024
@golemparts
Copy link
Owner

Glad to hear that fixed the issue!

Looks like /dev/serial0 points to /dev/ttyAMA10. But as you said I wanted /dev/ttyAMA0.

Hmm interesting. Thanks for pointing that out. I haven't checked for UART issues on the Pi 5. I may have to change the default device depending on which Pi is used. Using /dev/serial0 seemed like a safe choice, but it seems to be pointing to the wrong device on the Pi 5. At least there's a workaround.

Did you ever get a Raspberry Pi 5 to use for testing?

I did. I'll have to look into what default options make sense for the Pi 5. Based on your forum post I should also update the documentation on how to configure UART since that seems to have changed.

@Dygear
Copy link
Author

Dygear commented Nov 5, 2024

The Raspberry Pi side of documentation wasn't that helpful. It was only in the forums that I found out about the dtoverlay=uart0-pi5 option to enable UART on GPIO 14/15. I think that the 3 pin UART connection is still active, and you shouldn't need to disable the Bluetooth connection. This is the only thing I have in my [all] section of the /boot/firmware/config.txt file at this point.

[all]
# Enable UART0 on GPIO 14/15.
dtoverlay=uart0-pi5

I did disable bluetooth services (with sudo systemctl disable hciuart), but I don't actually think that is needed. The raspberry pi 5 has like an extra 4 UART connections, so one is given to bluetooth and doesn't conflict with the GPIO headers being used anymore like they did on the Pi 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants