printf() problem used in the mc9s12dj64 in the CW 5.7.0

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

printf() problem used in the mc9s12dj64 in the CW 5.7.0

Jump to solution
2,804 Views
zhangtracker
Contributor I
I define the start_up_server function as follow.
 
unsigned short start_up_server (void * p_device)
{
...
reg_value = 0x55;
printf("**reg_value = 0x%02x\n",reg_value);
...
}
 
Call the start_up_server in the main function.  The printf function can't work normally.
output:
**reg_value = 0x30f9
note: 30f9 is the address of the p_device.
 
why?
Labels (1)
Tags (1)
0 Kudos
1 Solution
680 Views
CrasyCat
Specialist III
Hello
 
Do you have included stdio.h on top of the source file where you are using printf?
Where is reg_value allocated (at which address)?
Can you try
printf("**reg_value = 0x%02x\n",(unsigned int) reg_value); instead?
CrasyCat

View solution in original post

0 Kudos
8 Replies
680 Views
zhangtracker
Contributor I
I didn't include stdio.h.
After I include stdio.h, printf funtion works normally.
 
Why?

 
Thanks  Lundin and CrasyCat.
0 Kudos
680 Views
CrasyCat
Specialist III
Hello
 
  printf is a function with an open parameter list. Parameter passing scheme is different for such  
   functions, so the compiler needs to know it should use a special parameter passing scheme.
 
CrasyCat
0 Kudos
680 Views
zhangtracker
Contributor I
Thank you for your reply.
I have try to define "reg_value" variable to volatile. But the output of printf function doesn't change.
 
You said:
>I have experienced strange behavior like this before because the optimizer wasn't behaving.
 
What dose  the optimizer need to be set?  I use none optimization. My complie command  line arguments is "-CPUHCS12 -Mb".
 
 
0 Kudos
680 Views
Lundin
Senior Contributor IV
That is quite a complicated topic. Some optimization is enabled by default, some is not. The Freescalers are better suitable for answering this question than me, though I would at least recommend -Onf which turns off "Create sub-functions with common code", since that one usually creates problems and does more harm than good.
0 Kudos
681 Views
CrasyCat
Specialist III
Hello
 
Do you have included stdio.h on top of the source file where you are using printf?
Where is reg_value allocated (at which address)?
Can you try
printf("**reg_value = 0x%02x\n",(unsigned int) reg_value); instead?
CrasyCat
0 Kudos
680 Views
zhangtracker
Contributor I
reg_value is a variable not a pointer. It is defined as follow.
 
unsigned char reg_value;
 
And  I use the termio.c to make the output to UART0.
0 Kudos
680 Views
Lundin
Senior Contributor IV
Then indeed there is something wrong with the printf function, or perhaps with the optimizer settings. Have you tried looking at the disassembly or single-stepping through it? I have experienced strange behavior like this before because the optimizer wasn't behaving. You could try to set all variables to volatile and see if it works.
0 Kudos
680 Views
Lundin
Senior Contributor IV
What type is reg_value? If it is an allocated variable, did you try typecasting it to unsigned short? If it is a pointer, perhaps you forget the '*' operator?
0 Kudos