This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Nonblocking UART driver using _write_r - error if no data was written?


Hi Freddie,

I was wandering if I should ask on you website first, but I see that
it came to the same thing.

As for the clue of discussion: I use open(...) without flags to open
the device. I do it using the _open_r stub as it was described on
different websites - _open_r redirects the open call to specific
device open() call. I'm under the impression that if I use it this
way, the O_NONBLOCK flag would have to be interpreted by my driver,
not the newlib, so if I'm running always in nonblocking mode it will
not change anything.
The fopen call could change something, but in newlib code in version
2.1.0 only references I see to NONBLOCK are in mq_open code etc.
Nothing that seems to touch normal stdin/stdout.

I think that a question I start to ask myself is if newlib is even
capable of flushing to nonblocking device. Also, what is the rationale
behind doing (fflush.c:193):

t = fp->_write (ptr, fp->_cookie, (char *) p, n);
      if (t <= 0) //should return of 0 mean an error?
{
          fp->_flags |= __SERR;
          return EOF;
}

instead of:

t = fp->_write (ptr, fp->_cookie, (char *) p, n);
      if (t < 0)
{
          fp->_flags |= __SERR;
          return EOF;
}

Cheers,
Jerzy

On Wed, Feb 26, 2014 at 3:18 AM, MikuslawProxy <mikuslawproxy@gmail.com> wrote:
> Hi,
>
> I'm new to the group, so I would like to say hi to everybody and start
> with the questions (sorry... :) ).
>
> I'm trying to use the _write_r stub to push data through the UART
> driver to the console. The CPU is STM32 F103 and I wrote the driver to
> use the interrupts with driver own cyclic buffer. The driver works ok
> (I tested it without using _write_r stubs and stdio and it was ok).
>
> When I try to write more data than fits in my cyclic buffer it cuts
> the string in half.
>
> I try to printf/fflush:
> iprintf("abcdefghijklmnoprstuwxyzabcdefghijklmnoprstuwxyzabcdefghijklmnoprstuwxyzabcdefghijklmnoprstuwxyzAAAA");
> fflush(stdout);
>
> I get on console with 32 byte cyclic buffer (not deterministic):
> abcdefghijklmnoprstuwxyzabcd
>
> The issue I have is that when my cyclic buffer fills up, I return zero
> bytes written from the _write_r stub. This doesn't fit well with
> newlib's fflush, specifically fflush.c:193
>
>   while (n > 0)
>     {
>       t = fp->_write (ptr, fp->_cookie, (char *) p, n);
>       if (t <= 0)
> {
>           fp->_flags |= __SERR;
>           return EOF;
> }
>       p += t;
>       n -= t;
>     }
>
> when _write returns 0 it is treated as an error.
>
> http://linux.die.net/man/2/write says that returning zero is valid case:
>
> "On success, the number of bytes written is returned (zero indicates
> nothing was written). On error, -1 is returned, and errno is set
> appropriately.
>
> If count is zero and fd refers to a regular file, then write() may
> return a failure status if one of the errors below is detected. If no
> errors are detected, 0 will be returned without causing any other
> effect. If count is zero and fd refers to a file other than a regular
> file, the results are not specified."
>
> ,where count refers to input parameter not return value, so I think my
> point is still valid.
>
> Am I doing something wrong? When creating buffers internally in
> iprintf, newlib calls my istty stub and gets correct value. Any other
> things I should check?
>
> My send method (interrupt will push data from cyclic buf to peripheral):
>
> ssize_t DevUsart::com_write_r(struct _reent *r, int file, const void
> *buf, size_t nbyte)
> {
> uint32_t dataSent = 0;
>
> if(mIsOpened == false)
> {
> r->_errno = EBADF;
> return -1;//EBADF;
> }
>
> //Add data to the buffer
> dataSent = mTxBuffer.addToCyclicBuffer(static_cast<unsigned
> char*>(const_cast<void *>(buf)), static_cast<uint32_t>(nbyte));
>
> USART_ITConfig(mUsartBase, USART_IT_TXE, ENABLE);
>
> return dataSent;
> }
>
> Any help would be great!
>
> Cheers,
> Jerzy


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]