Could I point a function pointer to a RAM area?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Could I point a function pointer to a RAM area?

ソリューションへジャンプ
1,740件の閲覧回数
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
ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
721件の閲覧回数
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 件の賞賛
返信
2 返答(返信)
722件の閲覧回数
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 件の賞賛
返信
721件の閲覧回数
sypii
Contributor I
I really appreciate your help! Thank you very much!
 
Yang.
0 件の賞賛
返信