printf() fails

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using a lot of printf()s in my code to debug via an UART interface. This works perfectly well for most of the printf()s. But some of them fail to produce any output! These are simple printf()s with only one parameter.

One of them only fill in blanks for as many characters as it should print, another only sends its \n to the terminal. Yet another prints "We have \n" where I put in a printf ("We have link "); in my code.

I include io.h, pgmspace.h and stdio.h in that order. I'm using TeraTerm at 57600baud. I've checked all my cables etc.

Are there any known bugs with printf() in avr-gcc (GCC) 3.4.3? I did use printf_P() for a while. But my code has to be portable to other compilers. So I wanted to go for good old printf() rather than defining macros and stuff.

Hope you can help me out on this one!

Cheers,
Børge

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What does your "putchar" (first(?) argument to fdevopen) function look like?
Any chance of buffer overflow?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you give an example of a simple project where printf fails?

Are you sure that you have enough RAM to hold all the formats, strings, etc. while coexisting with the rest of your data, as well as the stack?

Also, how have you set up your UART driver? Is it polled, or interrupt-driven? If interrupt driven, have you checked for the possibility that you might be overrunning the buffer?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My uart is polled. Here's the code for putchar and getchar.


#ifdef DEBUG

// Sends a single char over the uart
int mcu_uart_putchar(char c) {
   loop_until_bit_is_set(UCSRA,UDRE);
   UDR = c;
   return 0;
}

int mcu_uart_getchar(void) {
   char c;
   loop_until_bit_is_set(UCSRA,RXC);
   c = UDR;
   mcu_uart_putchar (c);
   return c;
}


// Sets up the uart for 57600 baud, 8 data, 
// 1 stop with a 3.686MHz clock
void mcu_uart_init(void) {
   UBRRH = 0x00;
   UBRRL = 0x03;
   UCSRB = (1<<RXEN)|(1<<TXEN);
   UCSRC = (1<<URSEL)|(3<<UCSZ0);
   fdevopen(mcu_uart_putchar, mcu_uart_getchar, 0);
}

#endif

My project is quite big, occupying 5 .c files and 5 .h files. It might well be the case that I'm filling up the RAM. I'm using a mega16 in this project.

Thanks,

Børge