Page 1 of 1

Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 4:56 pm
by DosEresA
I am trying to use the same callback for events on two different pads. After setting up the pads as inputs, I am enabling events and setting up callbacks like this:

Code: Select all

    palEnablePadEvent(GPIOA, 9, PAL_EVENT_MODE_BOTH_EDGES);
    palEnablePadEvent(GPIOA, 10, PAL_EVENT_MODE_BOTH_EDGES);
    palSetPadCallback(GPIOA, 9, callback, 1);
    palSetPadCallback(GPIOA, 10, callback, 2);


My questions: how to use arguments to a callback function in a way that I can tell in the callback which pad caused the event? (I was hoping that I could pass a literal integer, but C does not seem to allow that even with casts). Am I misunderstanding the concept of arguments to a callback function? Is it wrong to use the same callback for two different events?

Re: Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 5:03 pm
by Giovanni
Hi,

If you just need the pad number then just pass the pad number.

If you need port and pad then you can pass an "ioline_t", it is a combination of port and pad built using PAL_LINE(port, pad). You can use the ioline_t as-is or (recommended) obtain again port and pad using PAL_PORT(line) and PAL_PAD(line).

Note that you need to cast to/from a void*, arguments are all void*.

Giovanni

Re: Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 5:27 pm
by DosEresA
Please ignore, this seems to have an answer already here viewtopic.php?t=5120.

Sorry Giovanni, you are faster than me! Thanks for your reply.

Re: Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 6:15 pm
by DosEresA
I still think there are potential problems passing integer literals... I am testing with GCC on a Linux terminal, but I expect gcc-arm-none-eabi to behave the same. GCC gives segmentation faults whenever I use a literal integer and with reason. The literal will be interpreted as an address.

I guess the only way to pass integers to the callbacks is to create two variables and pass their address cast to (void *) to avoid warnings. And the variables need to be on the heap so that their addresses are valid during the lifetime of the callback function.

Re: Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 6:25 pm
by Giovanni
You need to cast the number to pointer (void *)5 then, in the callback, cast the argument back to an integer (int)arg.

Giovanni

Re: Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 6:35 pm
by DosEresA
If gcc is something to go by, I get a segmentation fault. Does gcc-arm-none-eabi behave differently? Or am I misunderstanding you? Thank you for your patience to look at this.

Code: Select all

#include <stdio.h>
#include <stdint.h>

void test(void *d) {
        printf("D: %d\n", *(uint8_t *)d);
}

int main(void) {
        test((void *)5);
        return 0;
}

% gcc test.c
% ./a.out
zsh: segmentation fault (core dumped)  ./a.out


Re: Understanding callback arguments on palSetPadCallback

Posted: Mon Jul 20, 2020 6:37 pm
by DosEresA
Sorry, ignore. I was derefencing the pointer, that causes the seg fault. For reference, it works like this.

Code: Select all

#include <stdio.h>
#include <stdint.h>

void test(void *d) {
        int pad = (int) d;
        printf("D: %d\n", pad);
}

int main(void) {
        test((void *)5);
        return 0;
}


Thank you Giovanni for your prompt support!