GPIOD reading an input in the event callback

I created a thread and used gpiod_ctxless_event_monitor to wait for an event on one of 6 inputs.
When I get an event, like a falling edge on line 1, i’d like to read the other inputs as well.
I tried gpiod_ctxless_get_value, but it comes back as a -1. I think this is happening because the event_monitor has the chip open already.

Any other way to read an input value while in a call back?

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Based on the context provided, the issue you’re encountering is due to the gpiod_ctxless_event_monitor function holding the GPIO chip open, which prevents simultaneous access to read other GPIO inputs using gpiod_ctxless_get_value. Here are some alternative approaches you can consider:

  1. Separate Context: Use a separate context or handle for reading GPIO values. This might involve temporarily closing the event monitor or using a different method to access the GPIO states without conflict.

  2. Post-Event Handling: Handle the reading of other inputs after the event has been processed. This ensures that the event monitor is not holding the chip open during the read operation.

  3. Check Documentation: Review the documentation for gpiod to see if there are specific guidelines or functions that allow reading values while an event monitor is active.

These strategies aim to manage the GPIO chip’s state more effectively or restructure your code to ensure that the GPIO chip can be accessed without conflicts during event handling.

Please, also check if these links can help you solve your problem:

GPIOs in Colibri iMX6.

Yes, the gpiod_ctxless_get_value() attempts to request the GPIO line for input. However, during the execution in the event callback , the line is already requested and actively monitored for events. Requesting the same line again without releasing it first leads to a conflict, resulting in an error.
Within the event callback , you can directly call gpiod_line_get_value() on the line object associated with the event. This function retrieves the current value of the line without attempting to request it again.

void event_callback(struct gpiod_edge_event *event, void *data)
{
int value = gpiod_line_get_value(event->line);
if (value == -1) {
printf(“Failed to read GPIO value”);
} else {
printf(“GPIO value: %d\n”, value);
}
}

Just tried this out and it can’t be done. the call back has to be in the form
cb(int eventType, unsigned int offset, const struct timespec *tStamp, void *userdata)

Also, in the gpiod.h there is no reference to struct gpiod_edge_event.

Can you link me to some documents you found this in?

Apologies for the confusion; I was writing from memory.
A good set of libgpiod examples is available here.
In your case, you could create a thread with a callback function to wait for an event using gpiod_line_event_wait_bulk(), then read the lines value. Afterward, you can invoke the callback function, providing the line state. (I noticed that @Edward already suggested this solution to you a few years ago)

Or you can use 6 separate gpiod_ctxless_event_monitor().