[Tinyos-devel] Re: [Tinyos-help] Question about printf
Philip Levis
pal at cs.stanford.edu
Sat Sep 22 13:02:06 PDT 2007
On Sep 21, 2007, at 8:25 PM, Kevin Klues wrote:
> I don't seen anything in 'man 3 printf' that implies that it has to be
> a blocking operation. In fact the manual shouldn't imply this.
> printf is part of the standard C library, not affiliated with any
> particular operating system.
Right -- the printf semantics have more to do with the associated
file stream (printf is really just a call to fprintf) than with
printf itself. In the case of printf, the file stream is stdout.
>
> To implement printf() functionality for any mcu that has a libc (such
> as msp430-libc or avr-libc) you just have to fill in the body of the
> 'putchar' function declared in "stdio.h". This is exactly what the
> PrintfP component in tos/lib/printf does. No magic..... it just
> collects the bytes coming in via calls to putchar, packs them into
> TInyOS serial messages, and sends them out over the USART.
Right -- so look at how putchar is typically implemented. The example
from avr-gcc's stdio.h goes like this:
static int
uart_putchar(char c, FILE *stream)
{
if (c == '\n')
uart_putchar('\r', stream);
loop_until_bit_is_set(UCSRA, UDRE);
UDR = c;
return 0;
}
int
main(void)
{
init_uart();
stdout = &mystdout;
printf("Hello, world!\n");
return 0;
}
> It should be left up to whatever operating system that implements it
> to define the semantics.
Great: I'd like to define a printf semantics that crashes the computer.
Look -- to reiterate my last mail, I agree that this function is
useful, and it should be available. I'm just saying that, because it
does differ significantly from what one might expect from printf,
maybe it should be named differently. E.g.:
void test() {
uint16_t i;
for (i = 0; i < 20480; i++) {
printf("\n");
}
}
If you can find a reasonable implementation of printf besides yours
that does not print out 20480 newlines, then my concern is totally
unfounded and wrong.
Phil
More information about the Tinyos-devel
mailing list