|
|
View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 537 Location: Des Moines, Iowa, USA
|
Re: Passing address of function (function pointer)as a param |
Posted: Thu Jun 27, 2019 3:17 pm |
|
|
PCM programmer wrote: | I was able to make it work by casting the function pointer address
to an int16 in three places. I tested this with CCS vs. 5.085.
The program shown below displays the following result in the MPLAB
vs. 8.92 simulator output window:
|
Having a simulator would be useful. I don't like lugging my work equipment home for testing ;-) It's connected to a rather large piece of lumber.
Thanks for the int16 casting solution. I will have to look into that and see why it works.
I used the &add and &sub just for "clean code" -- i.e., "address of function()".
As to setting the pointer directly, in my production code, the pointer variable is being isolated. It's a call back so one piece of code can have a routine called by another piece of code, without either having any direct ties to each other.
Learning PIC has been fun. I am trying to keep my code as cross-platform as I can, compiling under GCC with fake routines that simulate any native PIC stuff I do). I had coded this under GCC (as a Windows command line test program) and got snagged when it wouldn't compile for PIC. Fortunately, solutions were found here. Big fan of the forum lately.
Thanks again for your response. Are you just a helpful user, or are you involved with CCS? _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 27, 2019 5:48 pm |
|
|
I'm a user. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 537 Location: Des Moines, Iowa, USA
|
|
Posted: Mon Jul 01, 2019 4:36 pm |
|
|
This may just be me hitting another compiler bug, but I thought I'd ask just in case.
I have a working Function Pointer example I wrote... Here's a quick example. Works fine. No issues.
Code: |
int add( int x, int y )
{
return (x + y);
}
void setFunctionPtr( void *ptr )
{
functionPtr = ptr;
}
void main()
{
void *ptr;
ptr = add;
setFunctionPtr( ptr );
result = (*functionPtr)( 1, 2 );
while(true)
{
}
}
|
But the same code (different names) fails when placed in our actual project:
Code: |
// Register our callback function so we can be notified
void *FunctionPtr;
FunctionPtr = &CallbackFunction;
|
And the compiler hates this:
Quote: | *** Error 44 "LogHandler.c" Line 161(1,1): Internal Error - Contact CCS LABEL SCR=11601
|
I'll be contacting CCS, per the message but I thought I'd post it here with that error in case anyone else stumbled upon it.
_________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 01, 2019 4:54 pm |
|
|
The code you posted doesn't compile. I dropped it into an MPLAB project
and got these errors:
Quote: |
*** Error 12 "PCH_Test.c" Line 13(4,15): Undefined identifier functionPtr
*** Error 12 "PCH_Test.c" Line 23(4,10): Undefined identifier result
2 Errors, 0 Warnings.
Build Failed.
|
|
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 537 Location: Des Moines, Iowa, USA
|
|
Posted: Tue Jul 02, 2019 6:40 am |
|
|
PCM programmer wrote: | The code you posted doesn't compile. I dropped it into an MPLAB project
and got these errors:
Quote: |
*** Error 12 "PCH_Test.c" Line 13(4,15): Undefined identifier functionPtr
*** Error 12 "PCH_Test.c" Line 23(4,10): Undefined identifier result
2 Errors, 0 Warnings.
Build Failed.
|
|
Correct. Those were just code snippets. It's just one of the weird compiler quirks. As I stated, this code works fine in another C file. And, even weirder, works fine earlier in the *same* file. As I've learned in these forums, there are just quirks (or bugs as most call them).
I commented out one line, and it built fine. This morning, I uncommented that line and built, and it's now giving me a completely different error ;-)
As a former manager taught me, "sometimes you just have to wave the rubber chicken." It's like playing Whac-a-Mole(tm) with this thing _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1912 Location: Norman, OK
|
|
Posted: Tue Jul 02, 2019 7:59 am |
|
|
Something I learned more than 40 years ago is that every compiler has its
issues. I currently program in 6 different "languages" and each one has it's
quirks. When I run into issues with CCS C it is usually syntactical on my
part, not necessarily a compiler "bug" per se. I also program commercially in
in GNU C and C++ and can cite numerous examples of that. One thing I have
to remember is this in NOT an ANSI C compiler, it is an embedded compiler
with special extensions that are a world away from x86. I have learned how
to avoid many of the CCS quirks over the years and make frequent backups
that I sometimes roll back to when it gets "weird". Interestingly the most
common time I run into issues with CCS is when I build state machines for
real time interrupt driven processing. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 537 Location: Des Moines, Iowa, USA
|
|
Posted: Tue Jul 02, 2019 9:41 am |
|
|
dyeatman wrote: | Something I learned more than 40 years ago is that every compiler has its
issues. I currently program in 6 different "languages" and each one has it's
quirks. When I run into issues with CCS C it is usually syntactical on my
part, not necessarily a compiler "bug" per se. I also program commercially in
in GNU C and C++ and can cite numerous examples of that. One thing I have
to remember is this in NOT an ANSI C compiler, it is an embedded compiler
with special extensions that are a world away from x86. I have learned how
to avoid many of the CCS quirks over the years and make frequent backups
that I sometimes roll back to when it gets "weird". Interestingly the most
common time I run into issues with CCS is when I build state machines for
real time interrupt driven processing. |
I agree with you 100%. I have used a laundry list of embedded tools over my career as well.
The thing that go me here was code placed in one function compiled and built fine, but moved to another function in the same file gave an error. Stuff like that (and how I moved an #if 0 above/below a comment which prevented the debugger from working) are just "rubber chicken" moments for me. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|