Could I point a function pointer to a RAM area?

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

Could I point a function pointer to a RAM area?

Jump to solution
1,739 Views
sypii
Contributor I
I am just starting to learn to use CodeWarrior and I encountered a problem. In my program I have to run some subroutines in RAM. The RAM is rare in HCS08 MCUs and I needn't run these subroutines synchronously, so I want to run these different subroutines in the same RAM area one by one.
First I define a buffer area big enough to store the subroutine in RAM as this:
        char buffCode[50];
then I define a function pointer which points to the area buffCode:
        const FunPointer fun=buffCode;
in the definition above, the type FunPointer was defined as this:
        typedef void (*FunPointer)(void);
When I want to run the subroutines, I copy the code from Flash memory to buffCode, and then run it in RAM by
        fun();
But when I compiled this program in CodeWarrior, I got this warning:
C1805: Non standard conversion used
In the description, it says, in ANSI-C it is normally not allowed to cast an object pointer to a function pointer or a function pointer to an object pointer.
Then what can I do if I want to run different subroutines in the same RAM area? Is this warning important? Could the program work properly as I expected if I ignore this warning?
Thanks very much!
Yang
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
720 Views
CompilerGuru
NXP Employee
NXP Employee
No, this particular warning is not important. You can ignore it, switch it off with an option or insert an additional cast to an int to avoid it.

char buf[100];
void fun(void) {
  typedef void(* funType)(void);
  funType d= (funType)(unsigned int)fun;
}

There might be two other potential problems with your code tough,
I can well imagine the problems don't happen for you, so just you know what to look out for.

First, by the HC08 code is not pic (position independent) in general. So when copying code it might not run at the other address. However small routes are likely to be pic, just bigger ones may use a non pic jmp.

Second, in general the compiler can insert runtime routine calls if your C code does expensive operations like a floating point multiplication (well also for simpler ones :smileyhappy:. So if you copy that code to RAM because the flash is not accessible (say because you are just programming it), runtime calls wont work.
But again, for simple C code chances are good that the compiler does not insert any runtime routine calls.

Anyway, there were some threads in this forum about pic code, try to search for them. And there are some technical notes on the subject too (don't know the number by heart :smileyhappy:.

Daniel


View solution in original post

0 Kudos
Reply
2 Replies
721 Views
CompilerGuru
NXP Employee
NXP Employee
No, this particular warning is not important. You can ignore it, switch it off with an option or insert an additional cast to an int to avoid it.

char buf[100];
void fun(void) {
  typedef void(* funType)(void);
  funType d= (funType)(unsigned int)fun;
}

There might be two other potential problems with your code tough,
I can well imagine the problems don't happen for you, so just you know what to look out for.

First, by the HC08 code is not pic (position independent) in general. So when copying code it might not run at the other address. However small routes are likely to be pic, just bigger ones may use a non pic jmp.

Second, in general the compiler can insert runtime routine calls if your C code does expensive operations like a floating point multiplication (well also for simpler ones :smileyhappy:. So if you copy that code to RAM because the flash is not accessible (say because you are just programming it), runtime calls wont work.
But again, for simple C code chances are good that the compiler does not insert any runtime routine calls.

Anyway, there were some threads in this forum about pic code, try to search for them. And there are some technical notes on the subject too (don't know the number by heart :smileyhappy:.

Daniel


0 Kudos
Reply
720 Views
sypii
Contributor I
I really appreciate your help! Thank you very much!
 
Yang.
0 Kudos
Reply