CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Function pointer as a parameter
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

Re: Passing address of function (function pointer)as a param
PostPosted: Thu Jun 27, 2019 3:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 27, 2019 5:48 pm     Reply with quote

I'm a user.
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Jul 01, 2019 4:36 pm     Reply with quote

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 Smile but I thought I'd post it here with that error in case anyone else stumbled upon it.

Smile
_________________
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

View user's profile Send private message

PostPosted: Mon Jul 01, 2019 4:54 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 02, 2019 6:40 am     Reply with quote

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 Smile
_________________
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: 1910
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Jul 02, 2019 7:59 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 02, 2019 9:41 am     Reply with quote

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 ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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