You are here

Arrays of function pointers?? | Cypress Semiconductor

Arrays of function pointers??

Summary: 7 Replies, Latest post by Bob Marlowe on 08 Jan 2014 03:12 AM PST
Verified Answers: 0
Last post
Log in to post new comments.
ChrisJ's picture
User
12 posts

Hi all,

Just a quick check - has anybody used function pointers and arrays thereof with PSoC 1?  I'm using a '437 and cannot for the life of me get an array of function pointers to work using standard C syntax etc.  Neither I nor a coworker can tell any difference between mine and numerous tutorials on the web (for standard C, anyhow).  The same coworker has successfully used an array of function pointers on a PSoC1, though with an older version of Designer (4.something, I think) and the compiler, for a 794.

I might post my project, but honestly I am in project mode and got it working using a large switch/case statement, so I might not bother right now.  Just wondered if anybody had any experience or prior problems.

Also, while I'm thinking of it - something we found that seemed strange - does the generated .lst file always contain a copy of ALL compiled/assembled code?  Because I looked at my call of a function via said array of function pointers in the list file - it did some register/memory shifting and then (I believe - going from memory here) an ljmp to a "__plcall" ("pointer long call", presumably) function, at a specified address of course.  However, my listing stopped just before that address and no trace of __plcall could be found except in the map file (specifying it at that phantom address).  Didn't dig into the raw hex file to look for it though.  Couldn't find __plcall on the Cypress web site anywhere either.

The kicker was that we moved a (very simple) ISR from the bottom of main.c to earlier in the file, and __plcall() appeared in the list.  But I'm nowhere near RAM or flash capacity (both in the 30 percents).  Weird...at least to this PSoC newb.

Has anyone seen either of these things before?

Best regards,
Chris

user_1377889's picture
User
10803 posts

Using arrays of function (pointers) are a bit uncommon in my projects but I did use them and I didn't have had problems in the PSoC1 world, but in PSoC3 only. Care should be taken to define the "const" attributes clearly.

I would suggest you to create a sample project, LEDs are a willing candidate using something like Turn_Led_On[2](); giving a quick result on a PSoC1 development kit.

 

Bob

cypro's picture
User
25 posts

I wrote an App Note quite a few years ago for PSoC1- AN2135- Calling Functions Using Vector Tables.  The syntax has changed with different compilers, but it's a start.  I think you will be able to figure it out, even if your compiler doesn't like the exact syntax used in the note.

I've attached it.

 

Bill

ChrisJ's picture
User
12 posts

Hey, thanks for the replies and the "pointers" (har har).  I've had a chance to look over this...I think I understand what you both were saying about const etc. now.  The app note is very helpful - do you have the code example that went with it?  I was unable to find it on the web.

So I have changed my code to be like in the app note, and got it to compile (had to figure out I had to make my array global instead of local).  However, it's still not working.  I've looked through the compiler documentation and that's been helpful too.  However, I also found this blurb in there:

Indirect function calls to fastcall/fastcall16 functions are not supported.
For example:
typedef void (* const PassData)(char i);
static PassData DataRedirect=&PWM8_WritePeriod;//WritePeriod is fastcall16
DataRedirect(128);

Now, I'm trying to control "LED" outputs using my CapSense inputs, so it's User Module functions that I'm trying to call, and of course they're all fastcalls.  So it sounds to me like I can't do this anyway?  Is that correct?  If so, disappointing, but I suppose the switch statement method isn't too bad.

Thanks again,
Chris

user_1377889's picture
User
10803 posts

Hope this will work, yet it is untested.

 

Bob

ChrisJ's picture
User
12 posts

Bob - thanks for that!  I will try to give it a test in the next couple of days and I'll let you know how it goes.

Happy New Year to all!

ChrisJ's picture
User
12 posts

OK, I've tried Bob's example.  Since I'm using a different chip and hw pinout, I just made a new project for the 437 (with 4 LED modules) and pasted the main.c into it.  (Was there anything else important that I should copy over?)

 

At first I thought it wasn't working, but then I added some delays to slow it down enough for my eyes :).  Works a treat, actually.

Here's my code with delays for a neat little "ladder" effect with 4 sequential LEDs:

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules

#define forever 1

typedef void (* LEDFunc)(void);

const LEDFunc MyLEDsOn[4]    = {LED_1_On,  LED_2_On,  LED_3_On,  LED_4_On};
const LEDFunc MyLEDsOff[4]    = {LED_1_Off, LED_2_Off, LED_3_Off, LED_4_Off};

void main(void)
{
    unsigned char ii;
    unsigned int j=0, k=0, l=0;
   
    while (forever)
    {
        for(ii = 0; ii < 4; ii++)
        {
            MyLEDsOn[ii]();
            for (k = 0; k < 42345; k++)
            {
                ;
            }
        }
       
        for (j = 0; j < 42345; j++)
        {
            ;  //delay
        }
        for(ii = 0; ii < 4; ii++) MyLEDsOff[ii]();
       
        for (l = 0; l < 42345; l++)
        {
            ;
        }
    }
}
 

 

...Now to figure out what's different in my project's code!

 

Thanks again!

user_1377889's picture
User
10803 posts

An additional secret hint: When you add an LCD-Module to your project and have a look at the generated sources you will find some delay-functions which you can cut & paste into your project.

 

Bob

Log in to post new comments.