Understanding callback arguments on palSetPadCallback

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
DosEresA
Posts: 7
Joined: Thu Jul 09, 2020 9:16 pm
Has thanked: 2 times

Understanding callback arguments on palSetPadCallback

Postby DosEresA » Mon Jul 20, 2020 4:56 pm

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?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Understanding callback arguments on palSetPadCallback

Postby Giovanni » Mon Jul 20, 2020 5:03 pm

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

DosEresA
Posts: 7
Joined: Thu Jul 09, 2020 9:16 pm
Has thanked: 2 times

Re: Understanding callback arguments on palSetPadCallback

Postby DosEresA » Mon Jul 20, 2020 5:27 pm

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.

DosEresA
Posts: 7
Joined: Thu Jul 09, 2020 9:16 pm
Has thanked: 2 times

Re: Understanding callback arguments on palSetPadCallback

Postby DosEresA » Mon Jul 20, 2020 6:15 pm

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.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Understanding callback arguments on palSetPadCallback

Postby Giovanni » Mon Jul 20, 2020 6:25 pm

You need to cast the number to pointer (void *)5 then, in the callback, cast the argument back to an integer (int)arg.

Giovanni

DosEresA
Posts: 7
Joined: Thu Jul 09, 2020 9:16 pm
Has thanked: 2 times

Re: Understanding callback arguments on palSetPadCallback

Postby DosEresA » Mon Jul 20, 2020 6:35 pm

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


DosEresA
Posts: 7
Joined: Thu Jul 09, 2020 9:16 pm
Has thanked: 2 times

Re: Understanding callback arguments on palSetPadCallback

Postby DosEresA » Mon Jul 20, 2020 6:37 pm

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!


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 17 guests