2

I'm developing a custom application on the BeagleBone Black that has to use the UART capabilities of the device via the Debian Linux that runs on the BBB, for reading big chunks of data. For reading from the UART I'm opening one of the /dev/ttyO0, /dev/ttyO1, etc. devices with the open() function in nonblocking mode. And then I'm trying to read from this port with the read(2) function:

ssize_t read(int fd, void *buf, size_t count);

I would like to know what is the biggest reasonable number for the parameter count and how is it related to the UART's FIFO buffer?

In the AM335x technical reference manual (TI document spruh73p, page 4328, section 19.3.6) I can see that the HW buffer is 64 bytes long. But, as I suspect by using the read() function my program is not communicating directly with the hardware fifo buffer but it is reading from Linux' serial driver's software buffer (if there is such). Is this true? If yes, what is the size of the software fifo? Could somebody please enlighten this field for me?

Greenberet
  • 490
  • 1
  • 5
  • 18
  • `man read`: *On Linux, read() (and similar system calls) will transfer at most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.)* – Marco Bonelli Oct 31 '19 at 21:31
  • @MarcoBonelli Thanks, I have read this too. But I hardly believe that a serial (UART) fifo buffer in an embedded environment would be 2GB big... – Greenberet Oct 31 '19 at 22:29

1 Answers1

2

And then I'm trying to read from this port with the read(2) function

Rather than the "port", you are actually several layers removed from the hardware device, and reading from the system buffer of the serial terminal.

I would like to know what is the biggest reasonable number for the parameter count and how is it related to the UART's FIFO buffer?

First of all the count must be no larger than the user buffer provided.
For a blocking read(), you could probably make this count as large as any buffer you could allocate.
For a nonblocking read(), a count larger than the terminal receive buffer makes little sense.
Note that count is merely a request, and the read() syscall can return with less than the requested number of bytes.

The sizes of the UART FIFO and serial port driver buffers are irrelevant to any read() requests from userspace.
See Linux serial drivers.

... it is reading from Linux' serial driver's software buffer (if there is such). Is this true?

Almost.
The read() syscall from userspace fetches data from the terminal buffer.
The terminal driver is a higher-level driver than the serial port driver.

The terminal buffer has no direct connection to the UART FIFO.
If DMA is employed, then data is transferred from the UART FIFO to a DMA buffer.
If PIO is employed, then data is transferred from the UART FIFO to a driver buffer.
In either case the serial port driver eventually transfers the data to a tty flip buffer.
In non-interrupt mode, data from the tty flip buffer is transferred to the terminal/line-discipline buffer.
Again refer to Linux serial drivers.

If yes, what is the size of the software fifo?

The terminal receive buffer is typically 4096 bytes, although you could write your own line discipline with a different size, or redefine the macro.
From include/linux/tty.h in Linux kernel source:

#define N_TTY_BUF_SIZE 4096

The terminal driver is common to all architectures supported by Linux, so your BBB should have a terminal buffer of 4096 bytes.

sawdust
  • 16,103
  • 3
  • 40
  • 50
  • I'm really thankful for your answer but my BBB's **/usr/include/linux/tty.h** does not contain such a define.. Moreover, none of the files in this folder contain the string `N_TTY_BUF_SIZE`.. – Greenberet Nov 01 '19 at 13:15
  • The file reference is kernel source, not userspace code. Only the kernel driver needs that macro. In order to use a redefined tty buffer size, you would have to rebuild the kernel. I did not mean to imply that such a change was trivial. I assumed that you understood that this was all part of the kernel. – sawdust Nov 01 '19 at 15:32
  • Oh okay. It's no secret that I'm not used to Linux but I'm starting to get it. So as I understand we can assume quite safely that the serial driver buffer on the BBB' linux is 4k, right? – Greenberet Nov 01 '19 at 15:49