This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Nonblocking UART driver using _write_r - error if no data was written?
- From: MikuslawProxy <mikuslawproxy at gmail dot com>
- To: newlib at sourceware dot org
- Date: Wed, 26 Feb 2014 03:18:21 +0100
- Subject: Nonblocking UART driver using _write_r - error if no data was written?
- Authentication-results: sourceware.org; auth=none
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