puts() not working, printf() is okay

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

I am trying to set up some USART redirection (no ASF). I have created a _write() function that works fine with printf(). However, for some reason it doesn't work with puts().

 

int _write(int file, char *data, int len)
{
	for (int i = 0; i < len; i++)
	{
		while (!STDIO_SERCOM->USART.INTFLAG.bit.DRE);
		STDIO_SERCOM->USART.DATA.reg = *data++;
	}
	return len;
}

void func(void)
{
	setvbuf(stdout, NULL, _IONBF, 0);
	setbuf(stdout, NULL);
	printf("abcdef\r\n");
	puts("why doesn't this work");
}

 

All I see on the output is "abcdef\r\n". The "why doesn't this work" string is not output. I am using nosys.specs. MCU is a SAM D21, I am using Atmel Studio 7 and the supplied GCC compiler.

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

You need a single character output function not a char* function. 

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

david.prentice wrote:

You need a single character output function not a char* function. 

 

So what do I override, _putc()? I tried that, and _fputc(), but it did not help.

 

All the examples I found (e.g. https://www.mikrocontroller.net/...) only implement _write() or _write_r().

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

mojo-chan wrote:
All I see on the output is "abcdef\r\n". The "why doesn't this work" string is not output

Does it make any difference if you add \r\n at the end of the puts() string ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

mojo-chan wrote:
All I see on the output is "abcdef\r\n". The "why doesn't this work" string is not output

Does it make any difference if you add \r\n at the end of the puts() string ?

 

I should have mentioned that I tried that, because I had the same thought. It doesn't work unfortunately.

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

Oh well.

 

Is your _write() function actually getting called for the puts() case?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Oh well.

 

Is your _write() function actually getting called for the puts() case?

 

It isn't being called. Neither are _putc(), _fputc() or _write_r().

 

Difficult to debug because it's a library.