system
November 29, 2012, 2:13pm
1
Sorry guys, probably it's not directly related to Arduino, but to avr-gcc.
I'm trying to write custom printf function.
f = va_arg(argp, double)
returns 0.
Tried both on device(ATMega328P) and Atmel Studio 6 simulator, same result.
Sounds like something to do with linker options?
system
November 29, 2012, 2:35pm
2
I'm trying to write custom printf function...returns 0.
Nothing in that snippet proves that there is any kind of problem.
system
November 29, 2012, 2:38pm
3
I've tried to isolate the problem into separate program, but now Atmel Studio won't let me put breakpoint to see the value of f.
Here is the isolated code:
#include <avr/io.h>
#include <stdlib.h>
#include <stdarg.h>
void debug_print(const char *fmt, ...)
{
const char *p;
va_list argp;
double f;
char *s;
char fmtbuf[80];
va_start(argp, fmt);
for(p = fmt; *p != '\0'; p++)
{
if(*p != '%')
continue;
switch(*++p)
{
case 'f':
f = va_arg(argp, double);
s = dtostrf(f, 10, 6, fmtbuf);
break;
}
}
va_end(argp);
}
int main(void)
{
debug_print("%f", 123.456);
while(1)
{
}
}
Do you know why Atmel Studio declines breakpoint? Device is set correct as in real project file, where breakpoints works just fine.
system
November 29, 2012, 2:49pm
4
In that code, you may be assigning the correct value to f (it looks like you are), but I don't see you actually using that value.
system
November 29, 2012, 2:53pm
5
Turns out to be optimization issue.
When I set compiler optimization to O1, then I got 0. When I set it to None, then I got correct result. Now I will update the code so you can see the usage and optimization will let simulator work.
system
November 29, 2012, 2:57pm
6
With optimization -O1 you will see the problem, debugger said f optimized out and without optimization it works as expected.
So what exactly I have to do in order to avoid f optimizing out?
system
November 29, 2012, 3:17pm
7
So what exactly I have to do in order to avoid f optimizing out?
Use the value. Presumably, you want to print it. So, print it.
system
November 29, 2012, 3:18pm
8
Solved. Debugger is showing cached value (0) but actually it contains correct data.