We have moved to a new Sailfish OS Forum. Please start new discussions there.
2

Handling interrupt pin

asked 2015-09-14 15:24:09 +0300

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

updated 2015-09-15 08:01:06 +0300

I am developing simple board that could be integrated with Jolla and I need a way to notify Jolla that my board is ready to send data through I2C (every random interval).

I am ultra beginner with Linux, interrupts or polling and documentation is a bit enigmatic (3.2.3 INT LINE HANDLING) and I believe this is the last piece of my puzzles. I assume, that the code could possibly look like:

void interrupt_handler() {

    // Horray, my board has new data for me!
}

int main() {

    set_interrupt_listener( 67, interrupt_handler ) // GPIO 67

    while(1);
}

`

edit retag flag offensive close delete

1 Answer

Sort by » oldest newest most voted
1

answered 2015-09-14 15:47:07 +0300

pichlo gravatar image

Disclaimer: I have yet to start hardware development on Jolla. I meant to for ages but real life always gets in the way. So I cannot confirm or deny your syntax. Here is just a general guidance on implementing interrupt-driven software.

You do not usually perform a lot of operations in an interrupt handler routine. Interrupts are meant to return quickly. If you have a lot of things to do in response to the interrupt, make your main program wait for a signal and have the interrupt handler do nothing more than send the signal.

The simplest way to implement could be something like:

static volatile int interrupt_received = 0;

void interrupt_handler() {
    // Horray, my board has new data for me!
    interrupt_received = 1;
}

int main() {

    set_interrupt_listener( 67, interrupt_handler ) // GPIO 67

    while(1) {
        while (!interrupt_received)
            ;    // spin until the flag has been raised

        // Do your action here

        interrupt_received = 0;
    }
}

This is of course a very high level and rough outline. In real code, you would have separate threads and semaphoers instead of spin locks in the main routine. Please look up those things, there is not much space here for a full introduction.

edit flag offensive delete publish link more

Comments

@pichlo Thank you for this clarification, but how could you refer to situation when we have UI application (Qt in this case) and while instruction is blocking. Should we have a separate thread for handling interruptions? Or rather use open("/sys/class/gpio/gpio67/value", O_RDONLY | O_NONBLOCK); or even better O_ASYNC?

Mandrako ( 2015-09-14 18:53:07 +0300 )edit

That's why I said this is only a crude high-level overview.

There are many ways to skin that cat. You can spawn a new thread to stop blocking the main routine and make that thread wait on a semaphore. The interrupt handler would simply raise that semaphore and off you go. A thread waiting on a semaphore does not eat CPU cycles. Or you could make the handler start a new thread, assuming it does not take too long.

Qt does a lot of that for you. Like many other frameworks, it adopts the "do not call me, I will call you" paradigm. Make your interrupt handler emit a signal and write a slot for that signal.

pichlo ( 2015-09-14 19:36:51 +0300 )edit
Login/Signup to Answer

Question tools

Follow
2 followers

Stats

Asked: 2015-09-14 15:24:09 +0300

Seen: 166 times

Last updated: Sep 14 '15