PROB: Partly string output with printf_P and PROGMEM

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

Can someone help me with the following problem:

String declared:

const char date_txt[] PROGMEM = __DATE__" / "__TIME__"\0";

List file content:

  84               		.section	.progmem.data,"a",@progbits
  87               	date_txt:
  88 0000 4E6F 7620 		.string	"Nov  7 2004 / 16:51:44"
  88      2037 2032 
  88      3030 3420 
  88      2F20 3136 
  88      3A35 313A 
  89 0017 00        		.string	""

Code to output this string:

fdevopen(usart_putc, usart_getc, 0);  // open USART file stream
...
printf_P("%s",date_txt);

Terminal output:

/ 16:52:40

I think printf_P should send the whole string from program memory, not just a part of it.
Does somebody knows where the problem is?

I'm using the latest version of WinAVR and the minimalistic printf version.

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

I have used the attached file for testing, with strange results...

The terminal output for test.c is:

WWW

I have no idea where my problem is... :?

Attachment(s): 

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

I don't know the answer to your original question, but I do have a Typo
Alert: Your "infinite" for statement doesn't have a semicolon, so main()
is returning. This might help explain the repetition of the 'W', but not
much else.

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

Thanks, fixed the typo in my test file, but same result for terminal output "WWW".

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

I haven't used printf_P, but from the manual it appears it expects the
format string (and nothing else?) to be in flash. In your case, the format
string is in .data. My guess is that the SRAM offset of the "%s" string
just happens to match an offset 12 into the target string in Flash; in
other words, printf_P is processing (part of) your target string when it
thinks it's processing the format string.

Having no experience, I don't know how to feed PROGMEM target strings
to printfX. A quick fix, though, would be pass your target strings as
format strings (watch out for stray '%'s), or just use puts_P().

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

In the function
int printf_P( const char *format [, argument]... );
The parameter format string should reside in program memory, but not argument.
For example, you may:
static char code_txt[] PROGMEM = "AVR J1850 VPW\n";
printf_P(code_txt);

Then it will work.

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

Quote:
...but from the manual it appears it expects the
format string (and nothing else?) to be in flash.

Doooh! That's it! :)

	printf_P(code_txt);
	printf_P(date_txt);
	printf_P(version_txt);

This works as expected. Thank's for your help.

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

Hi!

I had a similar problem some time ago and therefore I implemented the format parameter
%S (note the big S) into the vfprintf() function to handle argument strings from program memory.
see this thread:
https://www.avrfreaks.net/phpBB2/viewtopic.php?t=19899&highlight=

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

Well, %S is OK by the standard (only lower-case letters are reserved
for future library extensions).

Could I persuade you to submit this as an official patch on the
avr-libc project page?

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Thanks, I've already seen the automated message generated by this.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.