c - fprintf() on avr-libc instantly crashes -


I am writing a program to run on an ATmega 328P - bare avr-libc Instead of Arduino though, it uses the Arduino bootloader on serial (though I do not think it affects the following question).

// if in the current LFS CRLF (C == '\ n') uart_putc ('\ r'); While (! (UCSR0A & amp; _BV (UDRE0)); UDR 0 = C; } Fixed int _putc (char c, FILE * _) {uart_putc (c); Return 0; } ... fdev_setup_stream (stdout, & amp; _putc, NULL, _FDEV_SETUP_WRITE);

If I now write my program using only fputc and fputs then all works fine i snprintf I can call () to enter a char buffer [16] and then in fputs () formatted stranders; All of these things work fine.

  fputs ("hello, world \ n", stdout); / * Works fine * / four buffer [16]; Snprintf (buffer, shape buffer, "hello,% s \ n", "world"); Breadth (buffer, standout); / * Also works fine * /   

However, at this time I have to crash a real fprintf () to stdout I try and reboot the atmega:

  fprintf (stdout, "hello,% s \ n", "world");   

Even before the initial h output, it immediately crashes.

Is there any such thing that might be missing, snprintf () and fputs () can work fine < / Div>

Div class = "post-text" itemprop = "text">

You can update the assignment of stdout For using fdev_setup_stream () . Explains clearly:

Note: No work will be done in standard currents fdev_setup_stream () If standard currents are to be used, then this user needs to be allocated.

Indicates only one document. The code looks like yours except for two things:

  1. A custom stream macro FDEV_SETUP_STREAM () : Fixed FILE Mystdout = FDEV_SETUP_STREAM (uart_putchar, NULL, _FDEV_SETUP_WRITE);

  2. stdout has been started in this custom stream:

      stdout = & amp; Mystdout;     

    I think your code is using an unrestricted stdout stream "Introduction to standard IO features" (www .nongnu.org / avr-libc / user-manual / group__avr__stdio.html # details) says:

    Standard studs stdin, stdout, and CDR are provided, but unlike the C standard , Since avr-libc has no information about the applicable device, so these streams are not already pre-initiated on the application startup.

    All function calls using stdout are undefined where stdout relies on exactly what is pointing to it, Some calls may still work.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -