Example of stdin retargeting

Asked by Jonathan Farmer

I'm having trouble with retargeting stdin to a UART. The stdout retargeting is working properly, I have an implementation of _write() the pushes all bytes to the UART, however I cannot seem to get _read() to function properly.

Is there a working example of a _read() implementation somewhere? What are the details of the _read() interface? What length value must be returned? Is it required to terminate with newline? Carriage return? Null terminator?

Thanks,
JF

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Joey Ye (jinyun-ye) said :
#1

Jonathan,

Here is my example, hope it helpful:

int _read (int fd, char *ptr, int len)
{
  int i;
  for (i=0; i < len; i++)
  {
    char ch = getkey();
        sendchar (ch);
        ptr[i] = ch;
        if (ch == '\r') break;
  }
  return i;
}

Revision history for this message
Jonathan Farmer (jrfarmer-com) said :
#2

Joey,

I implemented my _read() very similar to the example you provided. In my application, I do the following:

char myString[32];
gets(myString);
printf(myString);

At the console, if I enter 'jon' and hit the return string, then _read() returns 3 (i == 3 on exit of this function). Is this correct? It excludes the carriage return character. Should the return value be 4?

Also, I am finding that gets() function call never returns. Somehow _read() is being called repeatedly. Why does this happen?

By the way, here is the toolchain version I'm using, in case that makes a difference:

arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.7.3 20121207 (release) [ARM/embedded-4_7-branch revision 194305]
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Thanks,
JF

Revision history for this message
Jonathan Farmer (jrfarmer-com) said :
#3

As I stated in my previous response, _read() is called repeatedly. If I return 0, from _read(), the application call to gets() will finally return.

Revision history for this message
Jonathan Farmer (jrfarmer-com) said :
#4

After returning 0 from _read(), subsequent calls to gets() are not successful:
gets() returns immediately (does not filter down to _read()), and the return value from gets() is NULL.

So again, I wonder if there is a working example of _read()? What are the expected return values from the _read() function? How should this function behave for successful retargeting of stdin?

Thanks,
JF

Revision history for this message
Joey Ye (jinyun-ye) said :
#5

Suggest debug your gets implementation. This doesn't seem to be a tools or
library issue to me.
On Nov 5, 2013 11:51 PM, "Jonathan Farmer" <
<email address hidden>> wrote:

> Question #238665 on GCC ARM Embedded changed:
> https://answers.launchpad.net/gcc-arm-embedded/+question/238665
>
> Jonathan Farmer gave more information on the question:
> As I stated in my previous response, _read() is called repeatedly. If I
> return 0, from _read(), the application call to gets() will finally
> return.
>
> --
> You received this question notification because you are an answer
> contact for GCC ARM Embedded.
>

Revision history for this message
Jonathan Farmer (jrfarmer-com) said :
#6

'gets()' is not my implementation. This function is coming from stdio library, as provided by this toolchain. I have similar problems when using scanf().

So I still don't understand this stdin retargetting. What is needed to successfully implement _read()? How should this interface behave?

Revision history for this message
Joey Ye (jinyun-ye) said :
#7

gets won't read from UART unless you have you UART operation implemented in _read. So don't call gets. Instead, implement your getkey() with UART IO operations.

Revision history for this message
strange-corner (strange-corner) said :
#8

I had the same behaviour. The problem was that I implemented _read blocking (waiting for input). When implemented with immediate return, it worked fine.

Can you help with this problem?

Provide an answer of your own, or ask Jonathan Farmer for more information if necessary.

To post a message you must log in.