UART driver: read function

Daniel Krüger daniel.krueger at systec-electronic.com
Fri Mar 20 10:03:09 UTC 2015


Am 20.03.2015 um 10:02 schrieb Sebastian Huber:
> On 20/03/15 09:42, Daniel Krüger wrote:
>> Am 06.03.2015 um 11:20 schrieb Sebastian Huber:
>>>
>>> I would not use the C stdio for this and instead directly use the POSIX
>>> read/write. You have to set up the right Termios settings.  In case you
>>> use RTEMS 4.11 I would use the new Termios device interface (see
>>> rtems_termios_device_install()).
>>
>> The code is intended to be platform independent, so I don't want to
>> change it if not really necessary.
>
> will this work on non-POSIX targets at all?

Currently, we use this code under Linux.

This code switches stdin into non-blocking mode via the following sequence:

     tcgetattr(STDIN_FILENO, &oldt);
     oldf = fcntl(STDIN_FILENO, F_GETFL, 0);

     newt = oldt;
     newt.c_lflag &= ~(ICANON | ECHO);
     newf = oldf | O_NONBLOCK;

     tcsetattr(STDIN_FILENO, TCSANOW, &newt);
     fcntl(STDIN_FILENO, F_SETFL, newf);

Then it tries to read from the input via getchar().
I must admit this code is a little bit tricky, because it uses ungetc() 
for the implementation of a Windows kbhit() equivalent.

>> The interesting thing is, that newlib/libc/stdio/refill.c contains
>> some "#ifndef __CYGWIN__" which seem to fix the problem under Cygwin.
>
> This Cygwin approach seems to be quite a hack. The current FreeBSD
> variant of refill.c uses the default. How does this work on Linux?

I didn't checked the glibc code, but non-blocking getchar() works as 
expected.

Best regards,
   Daniel Krüger

-- 
SYS TEC electronic GmbH
Am Windrad 2
08468 Heinsdorfergrund

Telefon : +49 (0) 3765 38600-0
Fax     : +49 (0) 3765 38600-4100
Email   : daniel.krueger at systec-electronic.com
Website : http://www.systec-electronic.com

Managing Director   : Dipl.-Phys. Siegmar Schmidt
Commercial registry : Amtsgericht Chemnitz, HRB 28082


More information about the devel mailing list